code reorg and moving files to csql base directory
[csql.git] / src / base / Config.cxx
blob4317243d4af98a3ada35c0922381c5f0456388fe
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 (os::strcasestr(key, "PAGE_SIZE") != NULL )
38 { cVal.pageSize = atoi(value); }
39 else if (os::strcasestr(key, "MAX_PROCS") != NULL)
40 { cVal.maxProcs = atoi(value); }
41 else if (os::strcasestr(key, "MAX_SYS_DB_SIZE") != NULL)
42 { cVal.maxSysSize = atol(value); }
43 else if (os::strcasestr(key, "MAX_DB_SIZE") != NULL)
44 { cVal.maxDbSize = atol(value); }
45 else if (os::strcasestr(key, "MMAP") != NULL)
46 { cVal.mmap = os::atobool(value); }
47 else if (os::strcasestr(key, "SYS_DB_KEY") != NULL)
48 { cVal.sysDbKey = atoi(value); }
49 else if (os::strcasestr(key, "USER_DB_KEY") != NULL)
50 { cVal.userDbKey = atoi(value); }
51 else if (os::strcasestr(key, "LOG_FILE") != NULL)
52 { strcpy(cVal.logFile , value); }
53 else if (os::strcasestr(key, "DATABASE_FILE") != NULL)
54 { strcpy(cVal.dbFile , value); }
55 else if (os::strcasestr(key, "MAP_ADDRESS") != NULL)
56 { cVal.mapAddr = atol(value); }
57 else if (os::strcasestr(key, "MUTEX_TIMEOUT_SECS") != NULL)
58 { cVal.mutexSecs = atoi(value); }
59 else if (os::strcasestr(key, "MUTEX_TIMEOUT_USECS") != NULL)
60 { cVal.mutexUSecs = atoi(value); }
61 else if (os::strcasestr(key, "MUTEX_TIMEOUT_RETRIES") != NULL)
62 { cVal.mutexRetries = atoi(value); }
63 else if (os::strcasestr(key, "LOCK_TIMEOUT_SECS") != NULL)
64 { cVal.lockSecs = atoi(value); }
65 else if (os::strcasestr(key, "LOCK_TIMEOUT_USECS") != NULL)
66 { cVal.lockUSecs = atoi(value); }
67 else if (os::strcasestr(key, "LOCK_TIMEOUT_RETRIES") != NULL)
68 { cVal.lockRetries = atoi(value); }
69 else if (os::strcasestr(key, "LOG_LEVEL") != NULL)
70 { cVal.logLevel = atoi(value); }
71 else if (os::strcasestr(key, "DSN") != NULL)
72 { strcpy(cVal.dsn , value); }
73 else if (os::strcasestr(key, "NO_OF_ROWS_TO_FETCH") != NULL)
74 { cVal.nRowsToFetch = atoi(value); }
75 else if (os::strcasestr(key,"DS_CONFIG_FILE") !=NULL)
76 { strcpy(cVal.dsConfigFile,value);}
77 else if (os::strcasestr(key, "TABLE_CONFIG_FILE") != NULL)
78 { strcpy(cVal.tableConfigFile , value); }
79 else if (os::strcasestr(key, "STDERR_FILE") != NULL)
80 { strcpy(cVal.stderrFile, value); }
81 else if (os::strcasestr(key, "CONFL_RESOL_FILE") != NULL)
82 { strcpy(cVal.conflResoFile , value); }
83 else if (os::strcasestr(key, "CACHE_TABLE") != NULL)
84 { cVal.isCache = os::atobool(value); }
85 else if (os::strcasestr(key, "CACHE_MODE") != NULL)
87 if (strcmp(value, "SYNC") == 0)
88 { cVal.mode = SYNC_MODE; }
89 else if (strcmp(value, "ASYNC") == 0)
90 { cVal.mode = ASYNC_MODE; }
91 else if (strcmp(value, "OFFLINE") == 0)
92 { cVal.mode = OFFLINE_MODE; }
93 else cVal.mode = UNKNOWN;
95 else if (os::strcasestr(key, "OFFLINE_FILE_SIZE") != NULL)
96 { cVal.offlineLogFileSize = atol(value); }
97 else if(os::strcasestr(key,"SITE_ID")!=NULL)
98 { cVal.siteID = atoi(value);}
99 else if (os::strcasestr(key, "DURABILITY") != NULL)
100 { cVal.isDurable = os::atobool(value); }
101 else if (os::strcasestr(key, "DURABLE_MODE") != NULL)
102 { cVal.durableMode = ::atoi(value); }
103 else if (os::strcasestr(key, "CSQL_SQL_SERVER") != NULL)
104 { cVal.isCsqlSqlServer = os::atobool(value); }
105 else if (os::strcasestr(key, "PORT") != NULL)
106 { cVal.port = atoi(value); }
107 else if (os::strcasestr(key, "MAX_QUEUE_LOGS") != NULL)
108 { cVal.maxQueueLogs = atol(value); }
109 else if (os::strcasestr(key, "MSG_KEY") != NULL)
110 { cVal.msgKey = atoi(value); }
111 else if (os::strcasestr(key, "ASYNC_MSGMAX") != NULL)
112 { cVal.asyncMsgMax = atoi(value); }
113 else if (os::strcasestr(key, "ID_SHM_KEY") != NULL)
114 { cVal.shmKeyForId = atoi(value); }
115 else if (os::strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
116 { cVal.nwResponseTimeout = atoi(value); }
117 else if (os::strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
118 { cVal.nwConnectTimeout = atoi(value); }
119 else if (os::strcasestr(key, "ENABLE_BIDIRECTIONAL_CACHE") != NULL)
120 { cVal.isTwoWay = os::atobool(value); }
121 else if (os::strcasestr(key, "CACHE_RECEIVER_WAIT_SECS") != NULL)
122 { cVal.cacheWaitSecs = atoi(value); }
123 else if (os::strcasestr(key, "STMT_CACHE_SIZE") != NULL)
124 { cVal.stmtCacheSize = atoi(value); }
125 else if (os::strcasestr(key, "STMT_CACHE_NOPARAM") != NULL)
126 { cVal.isCacheNoParam = os::atobool(value); }
127 else if (os::strcasestr(key, "MONITOR_SERVERS") != NULL)
128 { cVal.isMonitor = os::atobool(value); }
129 else if (os::strcasestr(key, "CHECKPOINT_SECS") != NULL)
130 { cVal.chkptSecs = ::atoi(value); }
131 else if (os::strcasestr(key, "CHECKPOINT_LOG_SIZE") != NULL)
132 { cVal.chkptRedoLimit = ::atoi(value); }
135 else return 1;
136 return 0;
138 int Config::validateValues()
140 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
142 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
143 return 1;
145 if (cVal.pageSize % 1024 !=0 )
147 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
148 return 1;
150 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
152 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
153 return 1;
155 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
157 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
158 return 1;
160 if (cVal.maxSysSize % 8192 !=0 )
162 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
163 return 1;
166 #ifdef x86_64
167 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > ( 100*1024*1024*1024L))
169 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 100 GB");
170 return 1;
172 #else
173 unsigned long maxVal = 2*1024*1024*1024L;
174 if (cVal.maxDbSize < 1024 * 1024 || ((unsigned long)cVal.maxDbSize) > maxVal)
176 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
177 return 1;
179 #endif
180 if (cVal.maxDbSize % 8192 !=0)
182 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
183 return 1;
186 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
188 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
189 return 1;
191 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
193 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
194 return 1;
196 if ( cVal.sysDbKey == cVal.userDbKey)
198 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
199 return 1;
201 if (0 == strcmp(cVal.logFile,""))
203 //TODO::check whether file exists
204 printError(ErrBadArg, "LOG_FILE is set to NULL");
205 return 1;
207 if (0 == strcmp(cVal.dbFile,""))
209 printError(ErrBadArg, "LOG_FILE is set to NULL");
210 return 1;
212 #if (!defined __sparcv9)
213 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
215 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
216 return 1;
218 #endif
219 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
221 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
222 return 1;
224 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
226 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
227 return 1;
229 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
231 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
232 return 1;
234 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
236 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
237 return 1;
239 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
241 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
242 return 1;
244 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
246 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
247 return 1;
249 if (cVal.logLevel < 0 || cVal.logLevel > 3)
251 printError(ErrBadArg, "LOG_LEVEL should be >= 0 and <= 3");
252 return 1;
254 if (cVal.isCache) {
255 if (0 == strcmp(cVal.dsn,""))
257 printError(ErrBadArg, "DSN is set to NULL");
258 return 1;
261 if (cVal.isCache) {
262 if (0 == strcmp(cVal.tableConfigFile,""))
264 //TODO::check whether file exists
265 printError(ErrBadArg, "TABLE_CONFIG_FILE is set to NULL");
266 return 1;
269 if (cVal.isCache && (cVal.nRowsToFetch < 1 || cVal.nRowsToFetch > 1000)) {
270 printError(ErrBadArg, "Resultset cache is not in range [1 - 1000].");
271 return 1;
273 if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
275 printError(ErrBadArg, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
276 return 1;
278 if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
280 printError(ErrBadArg, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
281 return 1;
283 if (cVal.cacheWaitSecs <1)
285 printError(ErrBadArg, "CACHE_RECEIVER_WAIT_SECS should be >1");
286 return 1;
288 if (cVal.port <= 1024)
290 printError(ErrBadArg, "Invalid Port Number");
291 return 1;
293 if (cVal.durableMode < 1 || cVal.durableMode >4)
295 if (!cVal.isDurable) {
296 printError(ErrBadArg, "Durability is not enabled but mode is set");
297 } else {
298 printError(ErrBadArg, "Durable Mode should be 1 to 4");
300 return 1;
302 if (cVal.stmtCacheSize < 0 || cVal.stmtCacheSize > 1024)
304 printError(ErrBadArg, "STMT_CACHE_SIZE should be >=0 and <1024");
305 return 1;
307 if (cVal.chkptSecs < 1 || cVal.chkptSecs > 1000000)
309 printError(ErrBadArg, "CHECKPOINT_SECS should be >=1 and <1000000");
310 return 1;
312 if (cVal.chkptRedoLimit < 1024 * 1024 || cVal.stmtCacheSize > 1024 * 1024 * 1024)
314 printError(ErrBadArg, "CHECKPOINT_LOG_SIZE should be >=1MB and <1 GB");
315 return 1;
317 return 0;
320 int Config::readAllValues(char *fileName)
322 if (isLoaded) return 0;
323 FILE *fp;
324 if (fileName == NULL || 0 == strcmp(fileName, "")) fileName = DEFAULT_CONFIG_FILE;
325 fp = fopen(fileName,"r");
326 if( fp == NULL ) {
327 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
328 return 1;
331 int hasData = 1;
332 char buffer[1024];
333 char key[1024];
334 char value[1024];
335 while (hasData)
337 memset(buffer, 0, 1024);
338 //int ret = fscanf(fp,"%s\r",buffer);
339 int ret = readLine(fp, buffer);
340 if (ret == EOF) break;
341 bool isComment= false;
342 int posEqual =0;
343 for (int i = 0; i <1024; i++)
345 if (buffer[i] == '=' ) posEqual=i;
346 else if (buffer[i] == '#' ) { isComment = true; break; }
347 else if (buffer[i] == '\n') { break; }
348 else if (buffer[i] == '\0') { break; }
350 if (isComment) continue;
351 if (!posEqual) continue;
352 strncpy(key, buffer, posEqual);
353 key[posEqual] = '\0';
354 posEqual++;
355 strcpy(value, &buffer[posEqual]);
356 storeKeyVal(key, value);
358 fclose(fp);
359 if (validateValues())
361 return 1;
363 cVal.noOfProcessors = os::getNoOfProcessors();
364 isLoaded = true;
365 return 0;
367 void Config::logConfig()
369 logFinest(Conf::logger, "Config: LOG_FILE %s", getLogFile());
370 logFinest(Conf::logger, "Config: LOG_LEVEL %d", getLogLevel());
371 logFinest(Conf::logger, "Config: DURABILITY %d", useDurability());
372 logFinest(Conf::logger, "Config: MMAP %d", useMmap());
373 logFinest(Conf::logger, "Config: DURABLE_MODE %d", getDurableMode());
374 logFinest(Conf::logger, "Config: MUTEX_TIMEOUT_SECS %d", getMutexSecs());
375 logFinest(Conf::logger, "Config: MUTEX_TIMEOUT_USECS %d", getMutexUSecs());
376 logFinest(Conf::logger, "Config: MUTEX_TIMEOUT_RETRIES %d", getMutexRetries());
377 logFinest(Conf::logger, "Config: LOCK_TIMEOUT_SECS %d", getLockSecs());
378 logFinest(Conf::logger, "Config: LOCK_TIMEOUT_USECS %d", getLockUSecs());
379 logFinest(Conf::logger, "Config: LOCK_TIMEOUT_RETRIES %d", getLockRetries());
381 void Config::print()
383 printf("ConfigValues\n");
384 printf(" getPageSize %d\n", getPageSize());
385 printf(" getMaxProcs %d\n", getMaxProcs());
386 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
387 printf(" getMaxDbSize %ld\n", getMaxDbSize());
388 printf(" getSysDbKey %d\n", getSysDbKey());
389 printf(" getUserDbKey %d\n", getUserDbKey());
390 printf(" getLogFile %s\n", getLogFile());
391 printf(" getLogLevel %d\n", getLogLevel());
392 printf(" getDatabaseFile %s\n", getDbFile());
393 printf(" getMapAddress %ld\n", getMapAddress());
394 printf(" getMutexSecs %d\n", getMutexSecs());
395 printf(" getMutexUSecs %d\n", getMutexUSecs());
396 printf(" getMutexRetries %d\n", getMutexRetries());
397 printf(" getLockSecs %d\n", getLockSecs());
398 printf(" getLockUSecs %d\n", getLockUSecs());
399 printf(" getLockRetries %d\n", getLockRetries());
400 printf(" useCache %d\n", useCache());
401 printf(" getDSN %s\n", getDSN());
402 printf(" getNoOfRowsToFetchFromTDB %d\n", getNoOfRowsToFetchFromTDB());
403 printf(" getDsConfigFile %s\n",getDsConfigFile());
404 printf(" getTableConfigFile %s\n", getTableConfigFile());
405 printf(" isTwoWayCache %d\n", useTwoWayCache());
406 printf(" getCacheMode %d\n", getCacheMode());
407 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
408 printf(" useCsqlSqlServer %d\n", useCsqlSqlServer());
409 printf(" getPort %d\n", getPort());
410 printf(" isDurable %d\n", useDurability());
411 printf(" getSiteID %d\n", getSiteID());
412 printf(" getMsgKey %d\n", getMsgKey());
413 printf(" getShmIDKey %d\n", getShmIDKey());