Code Reorganization
[csql.git] / src / server / Config.cxx
bloba00e9d3195712c845f8ef8acc261e10ddc862ea9
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 return 1;
70 return 0;
72 int Config::validateValues()
74 if (cVal.pageSize < 1024 || cVal.pageSize > 1024 * 1024 * 10 )
76 printError(ErrBadArg, "PAGE_SIZE should be >= 1024 and <= 10 MB");
77 return 1;
79 if (cVal.maxTrans < 10 || cVal.maxTrans > 8192)
81 printError(ErrBadArg, "MAX_TRANS should be >= 10 and <= 8192");
82 return 1;
84 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
86 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
87 return 1;
89 if (cVal.maxThreads < 1 || cVal.maxThreads > 64)
91 printError(ErrBadArg, "MAX_THREADS should be >= 1 and <= 64");
92 return 1;
94 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
96 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
97 return 1;
99 if (cVal.maxSysSize % 8192 !=0 )
101 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
102 return 1;
105 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > 1024 *1024 *1024)
107 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 1 GB");
108 return 1;
110 if (cVal.maxDbSize % 8192 !=0)
112 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
113 return 1;
116 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
118 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
119 return 1;
121 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
123 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
124 return 1;
126 if ( cVal.sysDbKey == cVal.userDbKey)
128 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
129 return 1;
131 if (0 == strcmp(cVal.logFile,""))
133 printError(ErrBadArg, "LOG_FILE is set to NULL");
134 return 1;
136 //TODO::Check for the existence of the log file
137 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 4000000000)
139 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 4000000000");
140 return 1;
142 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
144 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
145 return 1;
147 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
149 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
150 return 1;
152 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
154 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
155 return 1;
157 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
159 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
160 return 1;
162 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
164 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
165 return 1;
167 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
169 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
170 return 1;
172 return 0;
175 int Config::readAllValues(char *fileName)
177 FILE *fp;
179 fp = fopen(fileName,"r");
180 if( fp == NULL ) {
181 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
182 return 1;
185 int hasData = 1;
186 char buffer[1024];
187 char key[1024];
188 char value[1024];
189 while (hasData)
191 memset(buffer, 0, 1024);
192 //int ret = fscanf(fp,"%s\r",buffer);
193 int ret = readLine(fp, buffer);
194 if (ret == EOF) break;
195 bool isComment= false;
196 int posEqual =0;
197 for (int i = 0; i <1024; i++)
199 if (buffer[i] == '=' ) posEqual=i;
200 if (buffer[i] == '#' ) { isComment = true; break; }
201 if (buffer[i] == '\n') { break; }
202 if (buffer[i] == '\0') { break; }
204 if (isComment) continue;
205 if (!posEqual) continue;
206 strncpy(key, buffer, posEqual);
207 key[posEqual] = '\0';
208 posEqual++;
209 strcpy(value, &buffer[posEqual]);
210 storeKeyVal(key, value);
212 fclose(fp);
213 if (validateValues())
215 return 1;
218 return 0;
220 void Config::print()
222 printf("ConfigValues\n");
223 printf(" getPageSize %d\n", getPageSize());
224 printf(" getMaxTrans %d\n", getMaxTrans());
225 printf(" getMaxProcs %d\n", getMaxProcs());
226 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
227 printf(" getMaxDbSize %ld\n", getMaxDbSize());
228 printf(" getSysDbKey %d\n", getSysDbKey());
229 printf(" getUserDbKey %d\n", getUserDbKey());
230 printf(" getLogFile %s\n", getLogFile());
231 printf(" getMapAddress %ld\n", getMapAddress());
232 printf(" getMutexSecs %d\n", getMutexSecs());
233 printf(" getMutexUSecs %d\n", getMutexUSecs());
234 printf(" getMutexRetries %d\n", getMutexRetries());
235 printf(" getLockSecs %d\n", getLockSecs());
236 printf(" getLockUSecs %d\n", getLockUSecs());
237 printf(" getLockRetries %d\n", getLockRetries());