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 (strcasestr(key
, "PAGE_SIZE") != NULL
)
38 { cVal
.pageSize
= atoi(value
); }
39 else if (strcasestr(key
, "MAX_TRANS") != NULL
)
40 { cVal
.maxTrans
= atoi(value
); }
41 else if (strcasestr(key
, "MAX_PROCS") != NULL
)
42 { cVal
.maxProcs
= atoi(value
); }
43 else if (strcasestr(key
, "MAX_THREADS") != NULL
)
44 { cVal
.maxThreads
= atoi(value
); }
45 else if (strcasestr(key
, "MAX_SYS_DB_SIZE") != NULL
)
46 { cVal
.maxSysSize
= atol(value
); }
47 else if (strcasestr(key
, "MAX_DB_SIZE") != NULL
)
48 { cVal
.maxDbSize
= atol(value
); }
49 else if (strcasestr(key
, "SYS_DB_KEY") != NULL
)
50 { cVal
.sysDbKey
= atoi(value
); }
51 else if (strcasestr(key
, "USER_DB_KEY") != NULL
)
52 { cVal
.userDbKey
= atoi(value
); }
53 else if (strcasestr(key
, "LOG_FILE") != NULL
)
54 { strcpy(cVal
.logFile
, value
); }
55 else if (strcasestr(key
, "MAP_ADDRESS") != NULL
)
56 { cVal
.mapAddr
= atol(value
); }
57 else if (strcasestr(key
, "MUTEX_TIMEOUT_SECS") != NULL
)
58 { cVal
.mutexSecs
= atoi(value
); }
59 else if (strcasestr(key
, "MUTEX_TIMEOUT_USECS") != NULL
)
60 { cVal
.mutexUSecs
= atoi(value
); }
61 else if (strcasestr(key
, "MUTEX_TIMEOUT_RETRIES") != NULL
)
62 { cVal
.mutexRetries
= atoi(value
); }
63 else if (strcasestr(key
, "LOCK_TIMEOUT_SECS") != NULL
)
64 { cVal
.lockSecs
= atoi(value
); }
65 else if (strcasestr(key
, "LOCK_TIMEOUT_USECS") != NULL
)
66 { cVal
.lockUSecs
= atoi(value
); }
67 else if (strcasestr(key
, "LOCK_TIMEOUT_RETRIES") != NULL
)
68 { cVal
.lockRetries
= atoi(value
); }
69 else if (strcasestr(key
, "DSN") != NULL
)
70 { strcpy(cVal
.dsn
, value
); }
71 else if (strcasestr(key
, "TABLE_CONFIG_FILE") != NULL
)
72 { strcpy(cVal
.tableConfigFile
, value
); }
73 else if (strcasestr(key
, "CACHE") != NULL
)
74 { cVal
.isCache
= os::atobool(value
); }
75 else if (strcasestr(key
, "REPLICATION") != NULL
)
76 { cVal
.isReplication
= os::atobool(value
); }
77 else if (strcasestr(key
, "NETWORK_CONFIG_FILE") != NULL
)
78 { strcpy(cVal
.replConfigFile
, value
); }
79 else if (strcasestr(key
, "MAX_LOG_STORE_SIZE") != NULL
)
80 { cVal
.logStoreSize
= atol(value
); }
81 else if (strcasestr(key
, "NETWORK_ID") != NULL
)
82 { cVal
.networkID
= 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
); }
90 int Config::validateValues()
92 if (cVal
.pageSize
< 8192 || cVal
.pageSize
> 1024 * 1024 * 10 )
94 printError(ErrBadArg
, "PAGE_SIZE should be >= 8192 and <= 10 MB");
97 if (cVal
.pageSize
% 1024 !=0 )
99 printError(ErrBadArg
, "PAGE_SIZE should be multiples of 1024");
102 if (cVal
.maxTrans
< 10 || cVal
.maxTrans
> 8192)
104 printError(ErrBadArg
, "MAX_TRANS should be >= 10 and <= 8192");
107 if (cVal
.maxProcs
< 10 || cVal
.maxProcs
> 8192)
109 printError(ErrBadArg
, "MAX_PROCS should be >= 10 and <= 8192");
112 if (cVal
.maxThreads
< 1 || cVal
.maxThreads
> 64)
114 printError(ErrBadArg
, "MAX_THREADS should be >= 1 and <= 64");
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");
122 if (cVal
.maxSysSize
% 8192 !=0 )
124 printError(ErrBadArg
, "MAX_SYS_DB_SIZE should be multiples of 8192");
128 if (cVal
.maxDbSize
< 1024 * 1024 || cVal
.maxDbSize
> (1024*1024*1024))
130 printError(ErrBadArg
, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
133 if (cVal
.maxDbSize
% 8192 !=0)
135 printError(ErrBadArg
, "MAX_DB_SIZE should be multiples of 8192");
139 if (cVal
.sysDbKey
< 10 || cVal
.sysDbKey
> 8192)
141 printError(ErrBadArg
, "SYS_DB_KEY should be >= 10 and <= 8192");
144 if (cVal
.userDbKey
< 10 || cVal
.userDbKey
> 8192)
146 printError(ErrBadArg
, "USER_DB_KEY should be >= 10 and <= 8192");
149 if ( cVal
.sysDbKey
== cVal
.userDbKey
)
151 printError(ErrBadArg
, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal
.userDbKey
);
154 if (0 == strcmp(cVal
.logFile
,""))
156 //TODO::check whether file exists
157 printError(ErrBadArg
, "LOG_FILE is set to NULL");
160 //TODO::Check for the existence of the log file
161 if (cVal
.mapAddr
< 400000000 || cVal
.mapAddr
> 2000000000)
163 printError(ErrBadArg
, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
166 if (cVal
.mutexSecs
< 0 || cVal
.mutexSecs
> 360)
168 printError(ErrBadArg
, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
171 if (cVal
.mutexUSecs
< 0 || cVal
.mutexUSecs
> 1000000)
173 printError(ErrBadArg
, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
176 if (cVal
.mutexRetries
< 0 || cVal
.mutexRetries
> 100)
178 printError(ErrBadArg
, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
181 if (cVal
.lockSecs
< 0 || cVal
.lockSecs
> 360)
183 printError(ErrBadArg
, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
186 if (cVal
.lockUSecs
< 0 || cVal
.lockUSecs
> 1000000)
188 printError(ErrBadArg
, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
191 if (cVal
.lockRetries
< 0 || cVal
.lockRetries
> 100)
193 printError(ErrBadArg
, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
196 if (cVal
.isCache
&& cVal
.isReplication
) {
197 printError(ErrBadArg
, "Either caching or replication option should be set."
198 " Both options are not supported together");
202 if (0 == strcmp(cVal
.dsn
,""))
204 printError(ErrBadArg
, "DSN is set to NULL");
208 if (cVal
.isReplication
|| cVal
.isCache
) {
209 if (0 == strcmp(cVal
.replConfigFile
,""))
211 //TODO::check whether file exists
212 printError(ErrBadArg
, "NETWORK_CONFIG_FILE is set to NULL");
215 if (0 == strcmp(cVal
.tableConfigFile
,""))
217 //TODO::check whether file exists
218 printError(ErrBadArg
, "TABLE_CONFIG_FILE is set to NULL");
222 if (cVal
.logStoreSize
< 1024 * 1024 || cVal
.logStoreSize
> 1024 *1024 *1024)
224 printError(ErrBadArg
, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
227 if (cVal
.logStoreSize
% 8192 !=0)
229 printError(ErrBadArg
, "MAX_LOG_STORE_SIZE should be multiples of 8192");
232 if (cVal
.nwResponseTimeout
<0 || cVal
.nwResponseTimeout
> 60)
234 printError(ErrBadArg
, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
237 if (cVal
.nwConnectTimeout
<0 || cVal
.nwConnectTimeout
> 60)
239 printError(ErrBadArg
, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
242 //TODO::validate networkid
246 int Config::readAllValues(char *fileName
)
250 fp
= fopen(fileName
,"r");
252 printError(ErrSysInit
, "Invalid path/filename in CSQL_CONFIG_FILE.");
262 memset(buffer
, 0, 1024);
263 //int ret = fscanf(fp,"%s\r",buffer);
264 int ret
= readLine(fp
, buffer
);
265 if (ret
== EOF
) break;
266 bool isComment
= false;
268 for (int i
= 0; i
<1024; i
++)
270 if (buffer
[i
] == '=' ) posEqual
=i
;
271 else if (buffer
[i
] == '#' ) { isComment
= true; break; }
272 else if (buffer
[i
] == '\n') { break; }
273 else if (buffer
[i
] == '\0') { break; }
275 if (isComment
) continue;
276 if (!posEqual
) continue;
277 strncpy(key
, buffer
, posEqual
);
278 key
[posEqual
] = '\0';
280 strcpy(value
, &buffer
[posEqual
]);
281 storeKeyVal(key
, value
);
284 if (validateValues())
293 printf("ConfigValues\n");
294 printf(" getPageSize %d\n", getPageSize());
295 printf(" getMaxTrans %d\n", getMaxTrans());
296 printf(" getMaxProcs %d\n", getMaxProcs());
297 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
298 printf(" getMaxDbSize %ld\n", getMaxDbSize());
299 printf(" getSysDbKey %d\n", getSysDbKey());
300 printf(" getUserDbKey %d\n", getUserDbKey());
301 printf(" getLogFile %s\n", getLogFile());
302 printf(" getMapAddress %ld\n", getMapAddress());
303 printf(" getMutexSecs %d\n", getMutexSecs());
304 printf(" getMutexUSecs %d\n", getMutexUSecs());
305 printf(" getMutexRetries %d\n", getMutexRetries());
306 printf(" getLockSecs %d\n", getLockSecs());
307 printf(" getLockUSecs %d\n", getLockUSecs());
308 printf(" getLockRetries %d\n", getLockRetries());
309 printf(" useCache %d\n", useCache());
310 printf(" getDSN %s\n", getDSN());
311 printf(" getTableConfigFile %s\n", getTableConfigFile());
312 printf(" useReplication %d\n", useReplication());
313 printf(" getReplConfigFile %s\n", getReplConfigFile());
314 printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
315 printf(" getNetworkID %d\n", getNetworkID());