Recover cache tables from target database
[csql.git] / src / server / Config.cxx
blob2ee65ac2b865e7d5716cd9b5386819b4ad2169b1
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_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, "CACHE_TABLE_FILE") != NULL)
72 { strcpy(cVal.cacheTableFile , value); }
73 else return 1;
74 return 0;
76 int Config::validateValues()
78 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
80 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
81 return 1;
83 if (cVal.pageSize % 1024 !=0 )
85 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
86 return 1;
88 if (cVal.maxTrans < 10 || cVal.maxTrans > 8192)
90 printError(ErrBadArg, "MAX_TRANS should be >= 10 and <= 8192");
91 return 1;
93 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
95 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
96 return 1;
98 if (cVal.maxThreads < 1 || cVal.maxThreads > 64)
100 printError(ErrBadArg, "MAX_THREADS should be >= 1 and <= 64");
101 return 1;
103 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
105 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
106 return 1;
108 if (cVal.maxSysSize % 8192 !=0 )
110 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
111 return 1;
114 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > 1024 *1024 *1024)
116 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
117 return 1;
119 if (cVal.maxDbSize % 8192 !=0)
121 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
122 return 1;
125 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
127 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
128 return 1;
130 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
132 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
133 return 1;
135 if ( cVal.sysDbKey == cVal.userDbKey)
137 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
138 return 1;
140 if (0 == strcmp(cVal.logFile,""))
142 //TODO::check whether file exists
143 printError(ErrBadArg, "LOG_FILE is set to NULL");
144 return 1;
146 //TODO::Check for the existence of the log file
147 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
149 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
150 return 1;
152 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
154 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
155 return 1;
157 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
159 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
160 return 1;
162 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
164 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
165 return 1;
167 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
169 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
170 return 1;
172 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
174 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
175 return 1;
177 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
179 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
180 return 1;
182 if (0 == strcmp(cVal.dsn,""))
184 printError(ErrBadArg, "DSN is set to NULL");
185 return 1;
187 if (0 == strcmp(cVal.cacheTableFile,""))
189 //TODO::check whether file exists
190 printError(ErrBadArg, "CACHE_TABLE_FILE is set to NULL");
191 return 1;
193 return 0;
196 int Config::readAllValues(char *fileName)
198 FILE *fp;
200 fp = fopen(fileName,"r");
201 if( fp == NULL ) {
202 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
203 return 1;
206 int hasData = 1;
207 char buffer[1024];
208 char key[1024];
209 char value[1024];
210 while (hasData)
212 memset(buffer, 0, 1024);
213 //int ret = fscanf(fp,"%s\r",buffer);
214 int ret = readLine(fp, buffer);
215 if (ret == EOF) break;
216 bool isComment= false;
217 int posEqual =0;
218 for (int i = 0; i <1024; i++)
220 if (buffer[i] == '=' ) posEqual=i;
221 if (buffer[i] == '#' ) { isComment = true; break; }
222 if (buffer[i] == '\n') { break; }
223 if (buffer[i] == '\0') { break; }
225 if (isComment) continue;
226 if (!posEqual) continue;
227 strncpy(key, buffer, posEqual);
228 key[posEqual] = '\0';
229 posEqual++;
230 strcpy(value, &buffer[posEqual]);
231 storeKeyVal(key, value);
233 fclose(fp);
234 if (validateValues())
236 return 1;
239 return 0;
241 void Config::print()
243 printf("ConfigValues\n");
244 printf(" getPageSize %d\n", getPageSize());
245 printf(" getMaxTrans %d\n", getMaxTrans());
246 printf(" getMaxProcs %d\n", getMaxProcs());
247 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
248 printf(" getMaxDbSize %ld\n", getMaxDbSize());
249 printf(" getSysDbKey %d\n", getSysDbKey());
250 printf(" getUserDbKey %d\n", getUserDbKey());
251 printf(" getLogFile %s\n", getLogFile());
252 printf(" getMapAddress %ld\n", getMapAddress());
253 printf(" getMutexSecs %d\n", getMutexSecs());
254 printf(" getMutexUSecs %d\n", getMutexUSecs());
255 printf(" getMutexRetries %d\n", getMutexRetries());
256 printf(" getLockSecs %d\n", getLockSecs());
257 printf(" getLockUSecs %d\n", getLockUSecs());
258 printf(" getLockRetries %d\n", getLockRetries());
259 printf(" getDSN %s\n", getDSN());
260 printf(" getCacheTableFile %s\n", getCacheTableFile());