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 (os::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
,"DS_CONFIG_FILE") !=NULL
)
74 { strcpy(cVal
.dsConfigFile
,value
);}
75 else if (os::strcasestr(key
, "TABLE_CONFIG_FILE") != NULL
)
76 { strcpy(cVal
.tableConfigFile
, value
); }
77 else if (os::strcasestr(key
, "STDERR_FILE") != NULL
)
78 { strcpy(cVal
.stderrFile
, value
); }
79 else if (os::strcasestr(key
, "CONFL_RESOL_FILE") != NULL
)
80 { strcpy(cVal
.conflResoFile
, value
); }
81 else if (os::strcasestr(key
, "CACHE_TABLE") != NULL
)
82 { cVal
.isCache
= os::atobool(value
); }
83 else if (os::strcasestr(key
, "CACHE_MODE") != NULL
)
85 if (strcmp(value
, "SYNC") == 0)
86 { cVal
.mode
= SYNC_MODE
; }
87 else if (strcmp(value
, "ASYNC") == 0)
88 { cVal
.mode
= ASYNC_MODE
; }
89 else cVal
.mode
= UNKNOWN
;
91 else if(os::strcasestr(key
,"SITE_ID")!=NULL
)
92 { cVal
.siteID
= atoi(value
);}
93 else if (os::strcasestr(key
, "DURABILITY") != NULL
)
94 { cVal
.isDurable
= os::atobool(value
); }
95 else if (os::strcasestr(key
, "DURABLE_MODE") != NULL
)
96 { cVal
.durableMode
= ::atoi(value
); }
97 else if (os::strcasestr(key
, "CSQL_SQL_SERVER") != NULL
)
98 { cVal
.isCsqlSqlServer
= os::atobool(value
); }
99 else if (os::strcasestr(key
, "PORT") != NULL
)
100 { cVal
.port
= atoi(value
); }
101 else if (os::strcasestr(key
, "MAX_QUEUE_LOGS") != NULL
)
102 { cVal
.maxQueueLogs
= atol(value
); }
103 else if (os::strcasestr(key
, "MSG_KEY") != NULL
)
104 { cVal
.msgKey
= atoi(value
); }
105 else if (os::strcasestr(key
, "ASYNC_MSGMAX") != NULL
)
106 { cVal
.asyncMsgMax
= atoi(value
); }
107 else if (os::strcasestr(key
, "ID_SHM_KEY") != NULL
)
108 { cVal
.shmKeyForId
= atoi(value
); }
109 else if (os::strcasestr(key
, "NETWORK_RESPONSE_TIMEOUT") != NULL
)
110 { cVal
.nwResponseTimeout
= atoi(value
); }
111 else if (os::strcasestr(key
, "NETWORK_CONNECT_TIMEOUT") != NULL
)
112 { cVal
.nwConnectTimeout
= atoi(value
); }
113 else if (os::strcasestr(key
, "ENABLE_BIDIRECTIONAL_CACHE") != NULL
)
114 { cVal
.isTwoWay
= os::atobool(value
); }
115 else if (os::strcasestr(key
, "CACHE_RECEIVER_WAIT_SECS") != NULL
)
116 { cVal
.cacheWaitSecs
= atoi(value
); }
117 else if (os::strcasestr(key
, "STMT_CACHE_SIZE") != NULL
)
118 { cVal
.stmtCacheSize
= atoi(value
); }
119 else if (os::strcasestr(key
, "STMT_CACHE_NOPARAM") != NULL
)
120 { cVal
.isCacheNoParam
= os::atobool(value
); }
121 else if (os::strcasestr(key
, "MONITOR_SERVERS") != NULL
)
122 { cVal
.isMonitor
= os::atobool(value
); }
123 else if (os::strcasestr(key
, "CHECKPOINT_SECS") != NULL
)
124 { cVal
.chkptSecs
= ::atoi(value
); }
125 else if (os::strcasestr(key
, "CHECKPOINT_LOG_SIZE") != NULL
)
126 { cVal
.chkptRedoLimit
= ::atoi(value
); }
133 int Config::validateValues()
135 if (cVal
.pageSize
< 8192 || cVal
.pageSize
> 1024 * 1024 * 10 )
137 printError(ErrBadArg
, "PAGE_SIZE should be >= 8192 and <= 10 MB");
140 if (cVal
.pageSize
% 1024 !=0 )
142 printError(ErrBadArg
, "PAGE_SIZE should be multiples of 1024");
145 if (cVal
.maxProcs
< 10 || cVal
.maxProcs
> 8192)
147 printError(ErrBadArg
, "MAX_PROCS should be >= 10 and <= 8192");
150 if (cVal
.maxSysSize
< 1024 * 1024 || cVal
.maxSysSize
> 1024 *1024 *1024)
152 printError(ErrBadArg
, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
155 if (cVal
.maxSysSize
% 8192 !=0 )
157 printError(ErrBadArg
, "MAX_SYS_DB_SIZE should be multiples of 8192");
162 if (cVal
.maxDbSize
< 1024 * 1024 || cVal
.maxDbSize
> ( 100*1024*1024*1024L))
164 printError(ErrBadArg
, "MAX_DB_SIZE should be >= 1 MB and <= 100 GB");
168 unsigned long maxVal
= 2*1024*1024*1024L;
169 if (cVal
.maxDbSize
< 1024 * 1024 || ((unsigned long)cVal
.maxDbSize
) > maxVal
)
171 printError(ErrBadArg
, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
175 if (cVal
.maxDbSize
% 8192 !=0)
177 printError(ErrBadArg
, "MAX_DB_SIZE should be multiples of 8192");
181 if (cVal
.sysDbKey
< 10 || cVal
.sysDbKey
> 8192)
183 printError(ErrBadArg
, "SYS_DB_KEY should be >= 10 and <= 8192");
186 if (cVal
.userDbKey
< 10 || cVal
.userDbKey
> 8192)
188 printError(ErrBadArg
, "USER_DB_KEY should be >= 10 and <= 8192");
191 if ( cVal
.sysDbKey
== cVal
.userDbKey
)
193 printError(ErrBadArg
, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal
.userDbKey
);
196 if (0 == strcmp(cVal
.logFile
,""))
198 //TODO::check whether file exists
199 printError(ErrBadArg
, "LOG_FILE is set to NULL");
202 if (0 == strcmp(cVal
.dbFile
,""))
204 printError(ErrBadArg
, "LOG_FILE is set to NULL");
207 #if (!defined __sparcv9)
208 if (cVal
.mapAddr
< 400000000 || cVal
.mapAddr
> 2000000000)
210 printError(ErrBadArg
, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
214 if (cVal
.mutexSecs
< 0 || cVal
.mutexSecs
> 360)
216 printError(ErrBadArg
, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
219 if (cVal
.mutexUSecs
< 0 || cVal
.mutexUSecs
> 1000000)
221 printError(ErrBadArg
, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
224 if (cVal
.mutexRetries
< 0 || cVal
.mutexRetries
> 100)
226 printError(ErrBadArg
, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
229 if (cVal
.lockSecs
< 0 || cVal
.lockSecs
> 360)
231 printError(ErrBadArg
, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
234 if (cVal
.lockUSecs
< 0 || cVal
.lockUSecs
> 1000000)
236 printError(ErrBadArg
, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
239 if (cVal
.lockRetries
< 0 || cVal
.lockRetries
> 100)
241 printError(ErrBadArg
, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
244 if (cVal
.logLevel
< 0 || cVal
.logLevel
> 3)
246 printError(ErrBadArg
, "LOG_LEVEL should be >= 0 and <= 3");
250 if (0 == strcmp(cVal
.dsn
,""))
252 printError(ErrBadArg
, "DSN is set to NULL");
257 if (0 == strcmp(cVal
.tableConfigFile
,""))
259 //TODO::check whether file exists
260 printError(ErrBadArg
, "TABLE_CONFIG_FILE is set to NULL");
263 /*FILE *fp = fopen(cVal.tableConfigFile,"r");
265 printError(ErrSysInit, "Invalid path/filename for TABLE_CONFIG_FILE.\n");
270 char hostname[IDENTIFIER_LENGTH];
274 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
279 if (cVal
.nwResponseTimeout
<0 || cVal
.nwResponseTimeout
> 60)
281 printError(ErrBadArg
, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
284 if (cVal
.nwConnectTimeout
<0 || cVal
.nwConnectTimeout
> 60)
286 printError(ErrBadArg
, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
289 if (cVal
.cacheWaitSecs
<1)
291 printError(ErrBadArg
, "CACHE_RECEIVER_WAIT_SECS should be >1");
294 if (cVal
.port
<= 1024)
296 printError(ErrBadArg
, "Invalid Port Number");
299 if (cVal
.durableMode
< 1 || cVal
.durableMode
>4)
301 if (!cVal
.isDurable
) {
302 printError(ErrBadArg
, "Durability is not enabled but mode is set");
304 printError(ErrBadArg
, "Durable Mode should be 1 to 4");
308 if (cVal
.stmtCacheSize
< 0 || cVal
.stmtCacheSize
> 1024)
310 printError(ErrBadArg
, "STMT_CACHE_SIZE should be >=0 and <1024");
313 if (cVal
.chkptSecs
< 1 || cVal
.chkptSecs
> 1000000)
315 printError(ErrBadArg
, "CHECKPOINT_SECS should be >=1 and <1000000");
318 if (cVal
.chkptRedoLimit
< 1024 * 1024 || cVal
.stmtCacheSize
> 1024 * 1024 * 1024)
320 printError(ErrBadArg
, "CHECKPOINT_LOG_SIZE should be >=1MB and <1 GB");
326 int Config::readAllValues(char *fileName
)
328 if (isLoaded
) return 0;
330 if (fileName
== NULL
|| 0 == strcmp(fileName
, "")) fileName
= DEFAULT_CONFIG_FILE
;
331 fp
= fopen(fileName
,"r");
333 printError(ErrSysInit
, "Invalid path/filename in CSQL_CONFIG_FILE.");
343 memset(buffer
, 0, 1024);
344 //int ret = fscanf(fp,"%s\r",buffer);
345 int ret
= readLine(fp
, buffer
);
346 if (ret
== EOF
) break;
347 bool isComment
= false;
349 for (int i
= 0; i
<1024; i
++)
351 if (buffer
[i
] == '=' ) posEqual
=i
;
352 else if (buffer
[i
] == '#' ) { isComment
= true; break; }
353 else if (buffer
[i
] == '\n') { break; }
354 else if (buffer
[i
] == '\0') { break; }
356 if (isComment
) continue;
357 if (!posEqual
) continue;
358 strncpy(key
, buffer
, posEqual
);
359 key
[posEqual
] = '\0';
361 strcpy(value
, &buffer
[posEqual
]);
362 storeKeyVal(key
, value
);
365 if (validateValues())
369 cVal
.noOfProcessors
= os::getNoOfProcessors();
373 void Config::logConfig()
375 logFinest(Conf::logger
, "Config: LOG_FILE %s", getLogFile());
376 logFinest(Conf::logger
, "Config: LOG_LEVEL %d", getLogLevel());
377 logFinest(Conf::logger
, "Config: DURABILITY %d", useDurability());
378 logFinest(Conf::logger
, "Config: MMAP %d", useMmap());
379 logFinest(Conf::logger
, "Config: DURABLE_MODE %d", getDurableMode());
380 logFinest(Conf::logger
, "Config: MUTEX_TIMEOUT_SECS %d", getMutexSecs());
381 logFinest(Conf::logger
, "Config: MUTEX_TIMEOUT_USECS %d", getMutexUSecs());
382 logFinest(Conf::logger
, "Config: MUTEX_TIMEOUT_RETRIES %d", getMutexRetries());
383 logFinest(Conf::logger
, "Config: LOCK_TIMEOUT_SECS %d", getLockSecs());
384 logFinest(Conf::logger
, "Config: LOCK_TIMEOUT_USECS %d", getLockUSecs());
385 logFinest(Conf::logger
, "Config: LOCK_TIMEOUT_RETRIES %d", getLockRetries());
389 printf("ConfigValues\n");
390 printf(" getPageSize %d\n", getPageSize());
391 printf(" getMaxProcs %d\n", getMaxProcs());
392 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
393 printf(" getMaxDbSize %ld\n", getMaxDbSize());
394 printf(" getSysDbKey %d\n", getSysDbKey());
395 printf(" getUserDbKey %d\n", getUserDbKey());
396 printf(" getLogFile %s\n", getLogFile());
397 printf(" getLogLevel %d\n", getLogLevel());
398 printf(" getDatabaseFile %s\n", getDbFile());
399 printf(" getMapAddress %ld\n", getMapAddress());
400 printf(" getMutexSecs %d\n", getMutexSecs());
401 printf(" getMutexUSecs %d\n", getMutexUSecs());
402 printf(" getMutexRetries %d\n", getMutexRetries());
403 printf(" getLockSecs %d\n", getLockSecs());
404 printf(" getLockUSecs %d\n", getLockUSecs());
405 printf(" getLockRetries %d\n", getLockRetries());
406 printf(" useCache %d\n", useCache());
407 printf(" getDSN %s\n", getDSN());
408 printf(" getDsConfigFile %s\n",getDsConfigFile());
409 printf(" getTableConfigFile %s\n", getTableConfigFile());
410 printf(" isTwoWayCache %d\n", useTwoWayCache());
411 printf(" getCacheMode %d\n", getCacheMode());
412 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
413 printf(" useCsqlSqlServer %d\n", useCsqlSqlServer());
414 printf(" getPort %d\n", getPort());
415 printf(" isDurable %d\n", useDurability());
416 printf(" getSiteID %d\n", getSiteID());
417 printf(" getMsgKey %d\n", getMsgKey());
418 printf(" getShmIDKey %d\n", getShmIDKey());