Fixing bugs in aggregate and join
[csql.git] / src / storage / Config.cxx
blob06a2f4a1c55a18be2cfa0485743edde4af330bd8
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, "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);}
76 else if (strcasestr(key, "REPLICATION") != NULL)
77 { cVal.isReplication = os::atobool(value); }
78 else if (strcasestr(key, "CSQL_SQL_SERVER") != NULL)
79 { cVal.isCsqlSqlServer = os::atobool(value); }
80 else if (strcasestr(key, "PORT") != NULL)
81 { cVal.port = atoi(value); }
82 else if (strcasestr(key, "NETWORK_CONFIG_FILE") != NULL)
83 { strcpy(cVal.replConfigFile , value); }
84 else if (strcasestr(key, "MAX_LOG_STORE_SIZE") != NULL)
85 { cVal.logStoreSize = atol(value); }
86 else if (strcasestr(key, "MY_NETWORK_ID") != NULL)
87 { cVal.networkID = atoi(value); }
88 else if (strcasestr(key, "CACHE_NETWORK_ID") != NULL)
89 { cVal.cacheNetworkID = atoi(value); }
90 else if (strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
91 { cVal.nwResponseTimeout = atoi(value); }
92 else if (strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
93 { cVal.nwConnectTimeout = atoi(value); }
94 else if (strcasestr(key, "ENABLE_BIDIRECTIONAL_CACHE") != NULL)
95 { cVal.isTwoWay = os::atobool(value); }
96 else if (strcasestr(key, "CACHE_RECEIVER_WAIT_SECS") != NULL)
97 { cVal.cacheWaitSecs = atoi(value); }
98 else return 1;
99 return 0;
101 int Config::validateValues()
103 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
105 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
106 return 1;
108 if (cVal.pageSize % 1024 !=0 )
110 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
111 return 1;
113 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
115 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
116 return 1;
118 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
120 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
121 return 1;
123 if (cVal.maxSysSize % 8192 !=0 )
125 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
126 return 1;
129 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > (1024*1024*1024))
131 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
132 return 1;
134 if (cVal.maxDbSize % 8192 !=0)
136 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
137 return 1;
140 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
142 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
143 return 1;
145 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
147 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
148 return 1;
150 if ( cVal.sysDbKey == cVal.userDbKey)
152 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
153 return 1;
155 if (0 == strcmp(cVal.logFile,""))
157 //TODO::check whether file exists
158 printError(ErrBadArg, "LOG_FILE is set to NULL");
159 return 1;
161 if (0 == strcmp(cVal.dbFile,""))
163 printError(ErrBadArg, "LOG_FILE is set to NULL");
164 return 1;
166 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
168 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
169 return 1;
171 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
173 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
174 return 1;
176 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
178 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
179 return 1;
181 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
183 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
184 return 1;
186 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
188 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
189 return 1;
191 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
193 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
194 return 1;
196 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
198 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
199 return 1;
201 if (cVal.isCache && cVal.isReplication) {
202 printError(ErrBadArg, "Either caching or replication option should be set."
203 " Both options are not supported together");
204 return 1;
206 if (cVal.isCache) {
207 if (0 == strcmp(cVal.dsn,""))
209 printError(ErrBadArg, "DSN is set to NULL");
210 return 1;
213 if (cVal.isReplication || cVal.isCache) {
214 if (0 == strcmp(cVal.replConfigFile,""))
216 //TODO::check whether file exists
217 printError(ErrBadArg, "NETWORK_CONFIG_FILE is set to NULL");
218 return 1;
220 if (0 == strcmp(cVal.tableConfigFile,""))
222 //TODO::check whether file exists
223 printError(ErrBadArg, "TABLE_CONFIG_FILE is set to NULL");
224 return 1;
226 /*FILE *fp = fopen(cVal.replConfigFile,"r");
227 if( fp == NULL ) {
228 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
229 return 1;
231 int count =0;
232 int nwid, port;
233 char hostname[IDENTIFIER_LENGTH];
234 char nwmode;
236 while(!feof(fp)) {
237 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
238 count++;
240 if (count >2) {
241 printError(ErrSysInit, "NETWORK_CONFIG_FILE has more than 2 entries\n");
242 return 1;
246 /*if (cVal.isCache)
249 if (cVal.cacheNetworkID == -1)
251 printError(ErrBadArg, "CACHE_NETWORK_ID should not be -1");
252 return 1;
253 }else {
254 FILE *fp;
255 int nwid;
256 char hostname[IDENTIFIER_LENGTH];
257 char nwmode;
258 int port;
259 fp = fopen(Conf::config.getReplConfigFile(),"r");
260 if( fp == NULL ) {
261 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
262 return 1;
264 bool found = false;
265 while(!feof(fp)) {
266 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
267 if (cVal.cacheNetworkID == nwid) found = true;
269 if (!found) return 1;
272 if (cVal.logStoreSize < 1024 * 1024 || cVal.logStoreSize > 1024 *1024 *1024)
274 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
275 return 1;
277 if (cVal.logStoreSize % 8192 !=0)
279 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be multiples of 8192");
280 return 1;
282 if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
284 printError(ErrBadArg, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
285 return 1;
287 if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
289 printError(ErrBadArg, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
290 return 1;
292 if (cVal.cacheWaitSecs <1)
294 printError(ErrBadArg, "CACHE_RECEIVER_WAIT_SECS should be >1");
295 return 1;
297 if (cVal.port <= 1024)
299 printError(ErrBadArg, "Invalid Port Number");
300 return 1;
302 return 0;
305 int Config::readAllValues(char *fileName)
307 FILE *fp;
309 fp = fopen(fileName,"r");
310 if( fp == NULL ) {
311 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
312 return 1;
315 int hasData = 1;
316 char buffer[1024];
317 char key[1024];
318 char value[1024];
319 while (hasData)
321 memset(buffer, 0, 1024);
322 //int ret = fscanf(fp,"%s\r",buffer);
323 int ret = readLine(fp, buffer);
324 if (ret == EOF) break;
325 bool isComment= false;
326 int posEqual =0;
327 for (int i = 0; i <1024; i++)
329 if (buffer[i] == '=' ) posEqual=i;
330 else if (buffer[i] == '#' ) { isComment = true; break; }
331 else if (buffer[i] == '\n') { break; }
332 else if (buffer[i] == '\0') { break; }
334 if (isComment) continue;
335 if (!posEqual) continue;
336 strncpy(key, buffer, posEqual);
337 key[posEqual] = '\0';
338 posEqual++;
339 strcpy(value, &buffer[posEqual]);
340 storeKeyVal(key, value);
342 fclose(fp);
343 if (validateValues())
345 return 1;
348 return 0;
350 void Config::print()
352 printf("ConfigValues\n");
353 printf(" getPageSize %d\n", getPageSize());
354 printf(" getMaxProcs %d\n", getMaxProcs());
355 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
356 printf(" getMaxDbSize %ld\n", getMaxDbSize());
357 printf(" getSysDbKey %d\n", getSysDbKey());
358 printf(" getUserDbKey %d\n", getUserDbKey());
359 printf(" getLogFile %s\n", getLogFile());
360 printf(" getDatabaseFile %s\n", getDbFile());
361 printf(" getMapAddress %ld\n", getMapAddress());
362 printf(" getMutexSecs %d\n", getMutexSecs());
363 printf(" getMutexUSecs %d\n", getMutexUSecs());
364 printf(" getMutexRetries %d\n", getMutexRetries());
365 printf(" getLockSecs %d\n", getLockSecs());
366 printf(" getLockUSecs %d\n", getLockUSecs());
367 printf(" getLockRetries %d\n", getLockRetries());
368 printf(" useCache %d\n", useCache());
369 printf(" getDSN %s\n", getDSN());
370 printf(" getTableConfigFile %s\n", getTableConfigFile());
371 printf(" isTwoWayCache %d\n", useTwoWayCache());
372 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
373 printf(" useCsqlSqlServer %d\n", useCsqlSqlServer());
374 printf(" getPort %d\n", getPort());
375 //printf(" useReplication %d\n", useReplication());
376 //printf(" getReplConfigFile %s\n", getReplConfigFile());
377 //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
378 //printf(" getNetworkID %d\n", getNetworkID());
379 //printf(" getCacheNetworkID %d\n", getCacheNetworkID());