1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
22 int Config::readLine(FILE *fp
, char * buffer
)
30 if (c
== EOF
) return EOF
;
35 int Config::storeKeyVal(char *key
, char *value
)
37 if (strcasestr(key
, "PAGE_SIZE") != NULL
)
38 { cVal
.pageSize
= atoi(value
); }
39 else if (strcasestr(key
, "MAX_PROCS") != NULL
)
40 { cVal
.maxProcs
= atoi(value
); }
41 else if (strcasestr(key
, "MAX_SYS_DB_SIZE") != NULL
)
42 { cVal
.maxSysSize
= atol(value
); }
43 else if (strcasestr(key
, "MAX_DB_SIZE") != NULL
)
44 { cVal
.maxDbSize
= atol(value
); }
45 else if (strcasestr(key
, "SYS_DB_KEY") != NULL
)
46 { cVal
.sysDbKey
= atoi(value
); }
47 else if (strcasestr(key
, "USER_DB_KEY") != NULL
)
48 { cVal
.userDbKey
= atoi(value
); }
49 else if (strcasestr(key
, "LOG_FILE") != NULL
)
50 { strcpy(cVal
.logFile
, value
); }
51 else if (strcasestr(key
, "DATABASE_FILE") != NULL
)
52 { strcpy(cVal
.dbFile
, value
); }
53 else if (strcasestr(key
, "MAP_ADDRESS") != NULL
)
54 { cVal
.mapAddr
= atol(value
); }
55 else if (strcasestr(key
, "MUTEX_TIMEOUT_SECS") != NULL
)
56 { cVal
.mutexSecs
= atoi(value
); }
57 else if (strcasestr(key
, "MUTEX_TIMEOUT_USECS") != NULL
)
58 { cVal
.mutexUSecs
= atoi(value
); }
59 else if (strcasestr(key
, "MUTEX_TIMEOUT_RETRIES") != NULL
)
60 { cVal
.mutexRetries
= atoi(value
); }
61 else if (strcasestr(key
, "LOCK_TIMEOUT_SECS") != NULL
)
62 { cVal
.lockSecs
= atoi(value
); }
63 else if (strcasestr(key
, "LOCK_TIMEOUT_USECS") != NULL
)
64 { cVal
.lockUSecs
= atoi(value
); }
65 else if (strcasestr(key
, "LOCK_TIMEOUT_RETRIES") != NULL
)
66 { cVal
.lockRetries
= atoi(value
); }
67 else if (strcasestr(key
, "DSN") != NULL
)
68 { strcpy(cVal
.dsn
, value
); }
69 else if (strcasestr(key
, "TABLE_CONFIG_FILE") != NULL
)
70 { strcpy(cVal
.tableConfigFile
, value
); }
71 else if (strcasestr(key
, "CACHE_TABLE") != NULL
)
72 { cVal
.isCache
= os::atobool(value
); }
73 else if(strcasestr(key
,"CACHE_ID")!=NULL
)
74 { cVal
.cacheId
= atoi(value
);}
75 else if (strcasestr(key
, "REPLICATION") != NULL
)
76 { cVal
.isReplication
= os::atobool(value
); }
77 else if (strcasestr(key
, "NETWORK_CONFIG_FILE") != NULL
)
78 { strcpy(cVal
.replConfigFile
, value
); }
79 else if (strcasestr(key
, "MAX_LOG_STORE_SIZE") != NULL
)
80 { cVal
.logStoreSize
= atol(value
); }
81 else if (strcasestr(key
, "MY_NETWORK_ID") != NULL
)
82 { cVal
.networkID
= atoi(value
); }
83 else if (strcasestr(key
, "CACHE_NETWORK_ID") != NULL
)
84 { cVal
.cacheNetworkID
= atoi(value
); }
85 else if (strcasestr(key
, "NETWORK_RESPONSE_TIMEOUT") != NULL
)
86 { cVal
.nwResponseTimeout
= atoi(value
); }
87 else if (strcasestr(key
, "NETWORK_CONNECT_TIMEOUT") != NULL
)
88 { cVal
.nwConnectTimeout
= atoi(value
); }
89 else if (strcasestr(key
, "ENABLE_BIDIRECTIONAL_CACHE") != NULL
)
90 { cVal
.isTwoWay
= os::atobool(value
); }
91 else if (strcasestr(key
, "CACHE_RECEIVER_WAIT_SECS") != NULL
)
92 { cVal
.cacheWaitSecs
= atoi(value
); }
96 int Config::validateValues()
98 if (cVal
.pageSize
< 8192 || cVal
.pageSize
> 1024 * 1024 * 10 )
100 printError(ErrBadArg
, "PAGE_SIZE should be >= 8192 and <= 10 MB");
103 if (cVal
.pageSize
% 1024 !=0 )
105 printError(ErrBadArg
, "PAGE_SIZE should be multiples of 1024");
108 if (cVal
.maxProcs
< 10 || cVal
.maxProcs
> 8192)
110 printError(ErrBadArg
, "MAX_PROCS should be >= 10 and <= 8192");
113 if (cVal
.maxSysSize
< 1024 * 1024 || cVal
.maxSysSize
> 1024 *1024 *1024)
115 printError(ErrBadArg
, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
118 if (cVal
.maxSysSize
% 8192 !=0 )
120 printError(ErrBadArg
, "MAX_SYS_DB_SIZE should be multiples of 8192");
124 if (cVal
.maxDbSize
< 1024 * 1024 || cVal
.maxDbSize
> (1024*1024*1024))
126 printError(ErrBadArg
, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
129 if (cVal
.maxDbSize
% 8192 !=0)
131 printError(ErrBadArg
, "MAX_DB_SIZE should be multiples of 8192");
135 if (cVal
.sysDbKey
< 10 || cVal
.sysDbKey
> 8192)
137 printError(ErrBadArg
, "SYS_DB_KEY should be >= 10 and <= 8192");
140 if (cVal
.userDbKey
< 10 || cVal
.userDbKey
> 8192)
142 printError(ErrBadArg
, "USER_DB_KEY should be >= 10 and <= 8192");
145 if ( cVal
.sysDbKey
== cVal
.userDbKey
)
147 printError(ErrBadArg
, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal
.userDbKey
);
150 if (0 == strcmp(cVal
.logFile
,""))
152 //TODO::check whether file exists
153 printError(ErrBadArg
, "LOG_FILE is set to NULL");
156 if (0 == strcmp(cVal
.dbFile
,""))
158 printError(ErrBadArg
, "LOG_FILE is set to NULL");
161 if (cVal
.mapAddr
< 400000000 || cVal
.mapAddr
> 2000000000)
163 printError(ErrBadArg
, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
166 if (cVal
.mutexSecs
< 0 || cVal
.mutexSecs
> 360)
168 printError(ErrBadArg
, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
171 if (cVal
.mutexUSecs
< 0 || cVal
.mutexUSecs
> 1000000)
173 printError(ErrBadArg
, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
176 if (cVal
.mutexRetries
< 0 || cVal
.mutexRetries
> 100)
178 printError(ErrBadArg
, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
181 if (cVal
.lockSecs
< 0 || cVal
.lockSecs
> 360)
183 printError(ErrBadArg
, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
186 if (cVal
.lockUSecs
< 0 || cVal
.lockUSecs
> 1000000)
188 printError(ErrBadArg
, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
191 if (cVal
.lockRetries
< 0 || cVal
.lockRetries
> 100)
193 printError(ErrBadArg
, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
196 if (cVal
.isCache
&& cVal
.isReplication
) {
197 printError(ErrBadArg
, "Either caching or replication option should be set."
198 " Both options are not supported together");
202 if (0 == strcmp(cVal
.dsn
,""))
204 printError(ErrBadArg
, "DSN is set to NULL");
208 if (cVal
.isReplication
|| cVal
.isCache
) {
209 if (0 == strcmp(cVal
.replConfigFile
,""))
211 //TODO::check whether file exists
212 printError(ErrBadArg
, "NETWORK_CONFIG_FILE is set to NULL");
215 if (0 == strcmp(cVal
.tableConfigFile
,""))
217 //TODO::check whether file exists
218 printError(ErrBadArg
, "TABLE_CONFIG_FILE is set to NULL");
221 /*FILE *fp = fopen(cVal.replConfigFile,"r");
223 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
228 char hostname[IDENTIFIER_LENGTH];
232 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
236 printError(ErrSysInit, "NETWORK_CONFIG_FILE has more than 2 entries\n");
244 if (cVal.cacheNetworkID == -1)
246 printError(ErrBadArg, "CACHE_NETWORK_ID should not be -1");
251 char hostname[IDENTIFIER_LENGTH];
254 fp = fopen(Conf::config.getReplConfigFile(),"r");
256 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
261 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
262 if (cVal.cacheNetworkID == nwid) found = true;
264 if (!found) return 1;
267 if (cVal
.logStoreSize
< 1024 * 1024 || cVal
.logStoreSize
> 1024 *1024 *1024)
269 printError(ErrBadArg
, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
272 if (cVal
.logStoreSize
% 8192 !=0)
274 printError(ErrBadArg
, "MAX_LOG_STORE_SIZE should be multiples of 8192");
277 if (cVal
.nwResponseTimeout
<0 || cVal
.nwResponseTimeout
> 60)
279 printError(ErrBadArg
, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
282 if (cVal
.nwConnectTimeout
<0 || cVal
.nwConnectTimeout
> 60)
284 printError(ErrBadArg
, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
287 if (cVal
.cacheWaitSecs
<1)
289 printError(ErrBadArg
, "CACHE_RECEIVER_WAIT_SECS should be >1");
295 int Config::readAllValues(char *fileName
)
299 fp
= fopen(fileName
,"r");
301 printError(ErrSysInit
, "Invalid path/filename in CSQL_CONFIG_FILE.");
311 memset(buffer
, 0, 1024);
312 //int ret = fscanf(fp,"%s\r",buffer);
313 int ret
= readLine(fp
, buffer
);
314 if (ret
== EOF
) break;
315 bool isComment
= false;
317 for (int i
= 0; i
<1024; i
++)
319 if (buffer
[i
] == '=' ) posEqual
=i
;
320 else if (buffer
[i
] == '#' ) { isComment
= true; break; }
321 else if (buffer
[i
] == '\n') { break; }
322 else if (buffer
[i
] == '\0') { break; }
324 if (isComment
) continue;
325 if (!posEqual
) continue;
326 strncpy(key
, buffer
, posEqual
);
327 key
[posEqual
] = '\0';
329 strcpy(value
, &buffer
[posEqual
]);
330 storeKeyVal(key
, value
);
333 if (validateValues())
342 printf("ConfigValues\n");
343 printf(" getPageSize %d\n", getPageSize());
344 printf(" getMaxProcs %d\n", getMaxProcs());
345 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
346 printf(" getMaxDbSize %ld\n", getMaxDbSize());
347 printf(" getSysDbKey %d\n", getSysDbKey());
348 printf(" getUserDbKey %d\n", getUserDbKey());
349 printf(" getLogFile %s\n", getLogFile());
350 printf(" getDatabaseFile %s\n", getDbFile());
351 printf(" getMapAddress %ld\n", getMapAddress());
352 printf(" getMutexSecs %d\n", getMutexSecs());
353 printf(" getMutexUSecs %d\n", getMutexUSecs());
354 printf(" getMutexRetries %d\n", getMutexRetries());
355 printf(" getLockSecs %d\n", getLockSecs());
356 printf(" getLockUSecs %d\n", getLockUSecs());
357 printf(" getLockRetries %d\n", getLockRetries());
358 printf(" useCache %d\n", useCache());
359 printf(" getDSN %s\n", getDSN());
360 printf(" getTableConfigFile %s\n", getTableConfigFile());
361 printf(" isTwoWayCache %d\n", useTwoWayCache());
362 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
363 //printf(" useReplication %d\n", useReplication());
364 //printf(" getReplConfigFile %s\n", getReplConfigFile());
365 //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
366 //printf(" getNetworkID %d\n", getNetworkID());
367 //printf(" getCacheNetworkID %d\n", getCacheNetworkID());