Enterprise to opensource. 40 files including Makefil.am and .in from storage, sqllog...
[csql.git] / src / storage / Config.cxx
blob7ca4d32b6a07cdb5051c8e7aa88c2fd88fe9e762
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);}
75 else if (strcasestr(key, "DURABILITY") != NULL)
76 { cVal.isDurable = os::atobool(value); }
77 else if (strcasestr(key, "CSQL_SQL_SERVER") != NULL)
78 { cVal.isCsqlSqlServer = os::atobool(value); }
79 else if (strcasestr(key, "PORT") != NULL)
80 { cVal.port = atoi(value); }
81 else if (strcasestr(key, "MAX_LOG_STORE_SIZE") != NULL)
82 { cVal.logStoreSize = atol(value); }
83 else if (strcasestr(key, "MY_NETWORK_ID") != NULL)
84 { cVal.networkID = atoi(value); }
85 else if (strcasestr(key, "ID_SHM_KEY") != NULL)
86 { cVal.shmKeyForId = atoi(value); }
87 else if (strcasestr(key, "CACHE_NETWORK_ID") != NULL)
88 { cVal.cacheNetworkID = atoi(value); }
89 else if (strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
90 { cVal.nwResponseTimeout = atoi(value); }
91 else if (strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
92 { cVal.nwConnectTimeout = atoi(value); }
93 else if (strcasestr(key, "ENABLE_BIDIRECTIONAL_CACHE") != NULL)
94 { cVal.isTwoWay = os::atobool(value); }
95 else if (strcasestr(key, "CACHE_RECEIVER_WAIT_SECS") != NULL)
96 { cVal.cacheWaitSecs = atoi(value); }
97 else return 1;
98 return 0;
100 int Config::validateValues()
102 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
104 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
105 return 1;
107 if (cVal.pageSize % 1024 !=0 )
109 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
110 return 1;
112 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
114 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
115 return 1;
117 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
119 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
120 return 1;
122 if (cVal.maxSysSize % 8192 !=0 )
124 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
125 return 1;
128 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > (1024*1024*1024))
130 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
131 return 1;
133 if (cVal.maxDbSize % 8192 !=0)
135 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
136 return 1;
139 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
141 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
142 return 1;
144 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
146 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
147 return 1;
149 if ( cVal.sysDbKey == cVal.userDbKey)
151 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
152 return 1;
154 if (0 == strcmp(cVal.logFile,""))
156 //TODO::check whether file exists
157 printError(ErrBadArg, "LOG_FILE is set to NULL");
158 return 1;
160 if (0 == strcmp(cVal.dbFile,""))
162 printError(ErrBadArg, "LOG_FILE is set to NULL");
163 return 1;
165 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
167 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
168 return 1;
170 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
172 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
173 return 1;
175 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
177 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
178 return 1;
180 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
182 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
183 return 1;
185 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
187 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
188 return 1;
190 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
192 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
193 return 1;
195 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
197 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
198 return 1;
200 if (cVal.isCache) {
201 if (0 == strcmp(cVal.dsn,""))
203 printError(ErrBadArg, "DSN is set to NULL");
204 return 1;
207 /*if (cVal.isCache)
210 if (cVal.cacheNetworkID == -1)
212 printError(ErrBadArg, "CACHE_NETWORK_ID should not be -1");
213 return 1;
214 }else {
215 FILE *fp;
216 int nwid;
217 char hostname[IDENTIFIER_LENGTH];
218 char nwmode;
219 int port;
220 fp = fopen(Conf::config.getReplConfigFile(),"r");
221 if( fp == NULL ) {
222 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
223 return 1;
225 bool found = false;
226 while(!feof(fp)) {
227 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
228 if (cVal.cacheNetworkID == nwid) found = true;
230 if (!found) return 1;
233 if (cVal.logStoreSize < 1024 * 1024 || cVal.logStoreSize > 1024 *1024 *1024)
235 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
236 return 1;
238 if (cVal.logStoreSize % 8192 !=0)
240 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be multiples of 8192");
241 return 1;
243 if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
245 printError(ErrBadArg, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
246 return 1;
248 if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
250 printError(ErrBadArg, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
251 return 1;
253 if (cVal.cacheWaitSecs <1)
255 printError(ErrBadArg, "CACHE_RECEIVER_WAIT_SECS should be >1");
256 return 1;
258 if (cVal.port <= 1024)
260 printError(ErrBadArg, "Invalid Port Number");
261 return 1;
263 return 0;
266 int Config::readAllValues(char *fileName)
268 FILE *fp;
270 fp = fopen(fileName,"r");
271 if( fp == NULL ) {
272 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
273 return 1;
276 int hasData = 1;
277 char buffer[1024];
278 char key[1024];
279 char value[1024];
280 while (hasData)
282 memset(buffer, 0, 1024);
283 //int ret = fscanf(fp,"%s\r",buffer);
284 int ret = readLine(fp, buffer);
285 if (ret == EOF) break;
286 bool isComment= false;
287 int posEqual =0;
288 for (int i = 0; i <1024; i++)
290 if (buffer[i] == '=' ) posEqual=i;
291 else if (buffer[i] == '#' ) { isComment = true; break; }
292 else if (buffer[i] == '\n') { break; }
293 else if (buffer[i] == '\0') { break; }
295 if (isComment) continue;
296 if (!posEqual) continue;
297 strncpy(key, buffer, posEqual);
298 key[posEqual] = '\0';
299 posEqual++;
300 strcpy(value, &buffer[posEqual]);
301 storeKeyVal(key, value);
303 fclose(fp);
304 if (validateValues())
306 return 1;
309 return 0;
311 void Config::print()
313 printf("ConfigValues\n");
314 printf(" getPageSize %d\n", getPageSize());
315 printf(" getMaxProcs %d\n", getMaxProcs());
316 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
317 printf(" getMaxDbSize %ld\n", getMaxDbSize());
318 printf(" getSysDbKey %d\n", getSysDbKey());
319 printf(" getUserDbKey %d\n", getUserDbKey());
320 printf(" getLogFile %s\n", getLogFile());
321 printf(" getDatabaseFile %s\n", getDbFile());
322 printf(" getMapAddress %ld\n", getMapAddress());
323 printf(" getMutexSecs %d\n", getMutexSecs());
324 printf(" getMutexUSecs %d\n", getMutexUSecs());
325 printf(" getMutexRetries %d\n", getMutexRetries());
326 printf(" getLockSecs %d\n", getLockSecs());
327 printf(" getLockUSecs %d\n", getLockUSecs());
328 printf(" getLockRetries %d\n", getLockRetries());
329 printf(" isDurable %d\n", useDurability());
330 printf(" useCache %d\n", useCache());
331 printf(" getCacheID %d\n", getCacheID());
332 printf(" getDSN %s\n", getDSN());
333 printf(" getTableConfigFile %s\n", getTableConfigFile());
334 printf(" isTwoWayCache %d\n", useTwoWayCache());
335 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
336 printf(" useCsqlSqlServer %d\n", useCsqlSqlServer());
337 printf(" getPort %d\n", getPort());
338 printf(" getShmIDKey %d\n", getShmIDKey());
339 //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
340 //printf(" getNetworkID %d\n", getNetworkID());
341 //printf(" getCacheNetworkID %d\n", getCacheNetworkID());