Renamed server directory to storage directory in src
[csql.git] / src / storage / Config.cxx
blob05545c3f92143527b5eb5b6c5f1626bb01f4e3c4
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, "REPLICATION") != NULL)
74 { cVal.isReplication = os::atobool(value); }
75 else if (strcasestr(key, "NETWORK_CONFIG_FILE") != NULL)
76 { strcpy(cVal.replConfigFile , value); }
77 else if (strcasestr(key, "MAX_LOG_STORE_SIZE") != NULL)
78 { cVal.logStoreSize = atol(value); }
79 else if (strcasestr(key, "MY_NETWORK_ID") != NULL)
80 { cVal.networkID = atoi(value); }
81 else if (strcasestr(key, "CACHE_NETWORK_ID") != NULL)
82 { cVal.cacheNetworkID = atoi(value); }
83 else if (strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
84 { cVal.nwResponseTimeout = atoi(value); }
85 else if (strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
86 { cVal.nwConnectTimeout = atoi(value); }
87 else if (strcasestr(key, "ENABLE_BIDIRECTIONAL_CACHE") != NULL)
88 { cVal.isTwoWay = os::atobool(value); }
89 else if (strcasestr(key, "CACHE_RECEIVER_WAIT_SECS") != NULL)
90 { cVal.cacheWaitSecs = atoi(value); }
91 else return 1;
92 return 0;
94 int Config::validateValues()
96 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
98 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
99 return 1;
101 if (cVal.pageSize % 1024 !=0 )
103 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
104 return 1;
106 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
108 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
109 return 1;
111 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
113 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
114 return 1;
116 if (cVal.maxSysSize % 8192 !=0 )
118 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
119 return 1;
122 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > (1024*1024*1024))
124 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
125 return 1;
127 if (cVal.maxDbSize % 8192 !=0)
129 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
130 return 1;
133 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
135 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
136 return 1;
138 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
140 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
141 return 1;
143 if ( cVal.sysDbKey == cVal.userDbKey)
145 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
146 return 1;
148 if (0 == strcmp(cVal.logFile,""))
150 //TODO::check whether file exists
151 printError(ErrBadArg, "LOG_FILE is set to NULL");
152 return 1;
154 if (0 == strcmp(cVal.dbFile,""))
156 printError(ErrBadArg, "LOG_FILE is set to NULL");
157 return 1;
159 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
161 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
162 return 1;
164 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
166 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
167 return 1;
169 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
171 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
172 return 1;
174 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
176 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
177 return 1;
179 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
181 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
182 return 1;
184 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
186 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
187 return 1;
189 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
191 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
192 return 1;
194 if (cVal.isCache && cVal.isReplication) {
195 printError(ErrBadArg, "Either caching or replication option should be set."
196 " Both options are not supported together");
197 return 1;
199 if (cVal.isCache) {
200 if (0 == strcmp(cVal.dsn,""))
202 printError(ErrBadArg, "DSN is set to NULL");
203 return 1;
206 if (cVal.isReplication || cVal.isCache) {
207 if (0 == strcmp(cVal.replConfigFile,""))
209 //TODO::check whether file exists
210 printError(ErrBadArg, "NETWORK_CONFIG_FILE is set to NULL");
211 return 1;
213 if (0 == strcmp(cVal.tableConfigFile,""))
215 //TODO::check whether file exists
216 printError(ErrBadArg, "TABLE_CONFIG_FILE is set to NULL");
217 return 1;
219 /*FILE *fp = fopen(cVal.replConfigFile,"r");
220 if( fp == NULL ) {
221 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
222 return 1;
224 int count =0;
225 int nwid, port;
226 char hostname[IDENTIFIER_LENGTH];
227 char nwmode;
229 while(!feof(fp)) {
230 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
231 count++;
233 if (count >2) {
234 printError(ErrSysInit, "NETWORK_CONFIG_FILE has more than 2 entries\n");
235 return 1;
239 /*if (cVal.isCache)
242 if (cVal.cacheNetworkID == -1)
244 printError(ErrBadArg, "CACHE_NETWORK_ID should not be -1");
245 return 1;
246 }else {
247 FILE *fp;
248 int nwid;
249 char hostname[IDENTIFIER_LENGTH];
250 char nwmode;
251 int port;
252 fp = fopen(Conf::config.getReplConfigFile(),"r");
253 if( fp == NULL ) {
254 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
255 return 1;
257 bool found = false;
258 while(!feof(fp)) {
259 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
260 if (cVal.cacheNetworkID == nwid) found = true;
262 if (!found) return 1;
265 if (cVal.logStoreSize < 1024 * 1024 || cVal.logStoreSize > 1024 *1024 *1024)
267 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
268 return 1;
270 if (cVal.logStoreSize % 8192 !=0)
272 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be multiples of 8192");
273 return 1;
275 if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
277 printError(ErrBadArg, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
278 return 1;
280 if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
282 printError(ErrBadArg, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
283 return 1;
285 if (cVal.cacheWaitSecs <1)
287 printError(ErrBadArg, "CACHE_RECEIVER_WAIT_SECS should be >1");
288 return 1;
290 return 0;
293 int Config::readAllValues(char *fileName)
295 FILE *fp;
297 fp = fopen(fileName,"r");
298 if( fp == NULL ) {
299 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
300 return 1;
303 int hasData = 1;
304 char buffer[1024];
305 char key[1024];
306 char value[1024];
307 while (hasData)
309 memset(buffer, 0, 1024);
310 //int ret = fscanf(fp,"%s\r",buffer);
311 int ret = readLine(fp, buffer);
312 if (ret == EOF) break;
313 bool isComment= false;
314 int posEqual =0;
315 for (int i = 0; i <1024; i++)
317 if (buffer[i] == '=' ) posEqual=i;
318 else if (buffer[i] == '#' ) { isComment = true; break; }
319 else if (buffer[i] == '\n') { break; }
320 else if (buffer[i] == '\0') { break; }
322 if (isComment) continue;
323 if (!posEqual) continue;
324 strncpy(key, buffer, posEqual);
325 key[posEqual] = '\0';
326 posEqual++;
327 strcpy(value, &buffer[posEqual]);
328 storeKeyVal(key, value);
330 fclose(fp);
331 if (validateValues())
333 return 1;
336 return 0;
338 void Config::print()
340 printf("ConfigValues\n");
341 printf(" getPageSize %d\n", getPageSize());
342 printf(" getMaxProcs %d\n", getMaxProcs());
343 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
344 printf(" getMaxDbSize %ld\n", getMaxDbSize());
345 printf(" getSysDbKey %d\n", getSysDbKey());
346 printf(" getUserDbKey %d\n", getUserDbKey());
347 printf(" getLogFile %s\n", getLogFile());
348 printf(" getDatabaseFile %s\n", getDbFile());
349 printf(" getMapAddress %ld\n", getMapAddress());
350 printf(" getMutexSecs %d\n", getMutexSecs());
351 printf(" getMutexUSecs %d\n", getMutexUSecs());
352 printf(" getMutexRetries %d\n", getMutexRetries());
353 printf(" getLockSecs %d\n", getLockSecs());
354 printf(" getLockUSecs %d\n", getLockUSecs());
355 printf(" getLockRetries %d\n", getLockRetries());
356 printf(" useCache %d\n", useCache());
357 printf(" getDSN %s\n", getDSN());
358 printf(" getTableConfigFile %s\n", getTableConfigFile());
359 printf(" isTwoWayCache %d\n", useTwoWayCache());
360 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
361 //printf(" useReplication %d\n", useReplication());
362 //printf(" getReplConfigFile %s\n", getReplConfigFile());
363 //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
364 //printf(" getNetworkID %d\n", getNetworkID());
365 //printf(" getCacheNetworkID %d\n", getCacheNetworkID());