Adding new tool cacheverify to the build system
[csql.git] / src / server / Config.cxx
blob4d9a193a0800cb236e9053ea5a7e580787ee1cd7
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<Config.h>
17 #include<ErrorType.h>
18 #include<Debug.h>
20 Config Conf::config;
22 int Config::readLine(FILE *fp, char * buffer)
24 char c =0;
25 int count =0;
26 while (true)
28 c = fgetc(fp);
29 if (c == '\n') break;
30 if (c == EOF) return EOF;
31 buffer[count++] = c;
33 return count;
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, "MAP_ADDRESS") != NULL)
52 { cVal.mapAddr = atol(value); }
53 else if (strcasestr(key, "MUTEX_TIMEOUT_SECS") != NULL)
54 { cVal.mutexSecs = atoi(value); }
55 else if (strcasestr(key, "MUTEX_TIMEOUT_USECS") != NULL)
56 { cVal.mutexUSecs = atoi(value); }
57 else if (strcasestr(key, "MUTEX_TIMEOUT_RETRIES") != NULL)
58 { cVal.mutexRetries = atoi(value); }
59 else if (strcasestr(key, "LOCK_TIMEOUT_SECS") != NULL)
60 { cVal.lockSecs = atoi(value); }
61 else if (strcasestr(key, "LOCK_TIMEOUT_USECS") != NULL)
62 { cVal.lockUSecs = atoi(value); }
63 else if (strcasestr(key, "LOCK_TIMEOUT_RETRIES") != NULL)
64 { cVal.lockRetries = atoi(value); }
65 else if (strcasestr(key, "DSN") != NULL)
66 { strcpy(cVal.dsn , value); }
67 else if (strcasestr(key, "TABLE_CONFIG_FILE") != NULL)
68 { strcpy(cVal.tableConfigFile , value); }
69 else if (strcasestr(key, "CACHE_TABLE") != NULL)
70 { cVal.isCache = os::atobool(value); }
71 else if (strcasestr(key, "REPLICATION") != NULL)
72 { cVal.isReplication = os::atobool(value); }
73 else if (strcasestr(key, "NETWORK_CONFIG_FILE") != NULL)
74 { strcpy(cVal.replConfigFile , value); }
75 else if (strcasestr(key, "MAX_LOG_STORE_SIZE") != NULL)
76 { cVal.logStoreSize = atol(value); }
77 else if (strcasestr(key, "MY_NETWORK_ID") != NULL)
78 { cVal.networkID = atoi(value); }
79 else if (strcasestr(key, "CACHE_NETWORK_ID") != NULL)
80 { cVal.cacheNetworkID = atoi(value); }
81 else if (strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
82 { cVal.nwResponseTimeout = atoi(value); }
83 else if (strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
84 { cVal.nwConnectTimeout = atoi(value); }
85 else return 1;
86 return 0;
88 int Config::validateValues()
90 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
92 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
93 return 1;
95 if (cVal.pageSize % 1024 !=0 )
97 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
98 return 1;
100 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
102 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
103 return 1;
105 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
107 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
108 return 1;
110 if (cVal.maxSysSize % 8192 !=0 )
112 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
113 return 1;
116 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > (1024*1024*1024))
118 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
119 return 1;
121 if (cVal.maxDbSize % 8192 !=0)
123 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
124 return 1;
127 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
129 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
130 return 1;
132 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
134 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
135 return 1;
137 if ( cVal.sysDbKey == cVal.userDbKey)
139 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
140 return 1;
142 if (0 == strcmp(cVal.logFile,""))
144 //TODO::check whether file exists
145 printError(ErrBadArg, "LOG_FILE is set to NULL");
146 return 1;
148 //TODO::Check for the existence of the log file
149 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
151 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
152 return 1;
154 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
156 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
157 return 1;
159 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
161 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
162 return 1;
164 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
166 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
167 return 1;
169 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
171 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
172 return 1;
174 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
176 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
177 return 1;
179 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
181 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
182 return 1;
184 if (cVal.isCache && cVal.isReplication) {
185 printError(ErrBadArg, "Either caching or replication option should be set."
186 " Both options are not supported together");
187 return 1;
189 if (cVal.isCache) {
190 if (0 == strcmp(cVal.dsn,""))
192 printError(ErrBadArg, "DSN is set to NULL");
193 return 1;
196 if (cVal.isReplication || cVal.isCache) {
197 if (0 == strcmp(cVal.replConfigFile,""))
199 //TODO::check whether file exists
200 printError(ErrBadArg, "NETWORK_CONFIG_FILE is set to NULL");
201 return 1;
203 if (0 == strcmp(cVal.tableConfigFile,""))
205 //TODO::check whether file exists
206 printError(ErrBadArg, "TABLE_CONFIG_FILE is set to NULL");
207 return 1;
209 /*FILE *fp = fopen(cVal.replConfigFile,"r");
210 if( fp == NULL ) {
211 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
212 return 1;
214 int count =0;
215 int nwid, port;
216 char hostname[IDENTIFIER_LENGTH];
217 char nwmode;
219 while(!feof(fp)) {
220 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
221 count++;
223 if (count >2) {
224 printError(ErrSysInit, "NETWORK_CONFIG_FILE has more than 2 entries\n");
225 return 1;
229 /*if (cVal.isCache)
232 if (cVal.cacheNetworkID == -1)
234 printError(ErrBadArg, "CACHE_NETWORK_ID should not be -1");
235 return 1;
236 }else {
237 FILE *fp;
238 int nwid;
239 char hostname[IDENTIFIER_LENGTH];
240 char nwmode;
241 int port;
242 fp = fopen(Conf::config.getReplConfigFile(),"r");
243 if( fp == NULL ) {
244 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
245 return 1;
247 bool found = false;
248 while(!feof(fp)) {
249 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
250 if (cVal.cacheNetworkID == nwid) found = true;
252 if (!found) return 1;
255 if (cVal.logStoreSize < 1024 * 1024 || cVal.logStoreSize > 1024 *1024 *1024)
257 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
258 return 1;
260 if (cVal.logStoreSize % 8192 !=0)
262 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be multiples of 8192");
263 return 1;
265 if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
267 printError(ErrBadArg, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
268 return 1;
270 if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
272 printError(ErrBadArg, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
273 return 1;
275 return 0;
278 int Config::readAllValues(char *fileName)
280 FILE *fp;
282 fp = fopen(fileName,"r");
283 if( fp == NULL ) {
284 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
285 return 1;
288 int hasData = 1;
289 char buffer[1024];
290 char key[1024];
291 char value[1024];
292 while (hasData)
294 memset(buffer, 0, 1024);
295 //int ret = fscanf(fp,"%s\r",buffer);
296 int ret = readLine(fp, buffer);
297 if (ret == EOF) break;
298 bool isComment= false;
299 int posEqual =0;
300 for (int i = 0; i <1024; i++)
302 if (buffer[i] == '=' ) posEqual=i;
303 else if (buffer[i] == '#' ) { isComment = true; break; }
304 else if (buffer[i] == '\n') { break; }
305 else if (buffer[i] == '\0') { break; }
307 if (isComment) continue;
308 if (!posEqual) continue;
309 strncpy(key, buffer, posEqual);
310 key[posEqual] = '\0';
311 posEqual++;
312 strcpy(value, &buffer[posEqual]);
313 storeKeyVal(key, value);
315 fclose(fp);
316 if (validateValues())
318 return 1;
321 return 0;
323 void Config::print()
325 printf("ConfigValues\n");
326 printf(" getPageSize %d\n", getPageSize());
327 printf(" getMaxProcs %d\n", getMaxProcs());
328 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
329 printf(" getMaxDbSize %ld\n", getMaxDbSize());
330 printf(" getSysDbKey %d\n", getSysDbKey());
331 printf(" getUserDbKey %d\n", getUserDbKey());
332 printf(" getLogFile %s\n", getLogFile());
333 printf(" getMapAddress %ld\n", getMapAddress());
334 printf(" getMutexSecs %d\n", getMutexSecs());
335 printf(" getMutexUSecs %d\n", getMutexUSecs());
336 printf(" getMutexRetries %d\n", getMutexRetries());
337 printf(" getLockSecs %d\n", getLockSecs());
338 printf(" getLockUSecs %d\n", getLockUSecs());
339 printf(" getLockRetries %d\n", getLockRetries());
340 printf(" useCache %d\n", useCache());
341 printf(" getDSN %s\n", getDSN());
342 printf(" getTableConfigFile %s\n", getTableConfigFile());
343 //printf(" useReplication %d\n", useReplication());
344 //printf(" getReplConfigFile %s\n", getReplConfigFile());
345 //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
346 //printf(" getNetworkID %d\n", getNetworkID());
347 //printf(" getCacheNetworkID %d\n", getCacheNetworkID());