00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifndef STDB_H
00045 #define STDB_H
00046
00047 #include <sys/time.h>
00048 #include <sys/types.h>
00049 #include <sys/stat.h>
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 #include <unistd.h>
00053 #include <error.h>
00054 #include <iostream>
00055 #include <fstream>
00056 #include <fcntl.h>
00057 #include <time.h>
00058 #include <map>
00059 #include <string>
00060 #include <stack>
00061 #include <vector>
00062 #include <queue>
00063 #include <list>
00064 #include <deque>
00065
00066 using namespace std;
00067
00068 #define uint32 unsigned long int
00069 #define COMMIT 'C'
00070 #define BUFFSIZE 128
00071
00089
00090
00094 class Tstdb{
00095 private:
00096
00098
00102 char *db,*log;
00103
00105
00109 int MaxLogSize;
00110
00112
00116 int MaxTime;
00117
00119
00123 typedef map<string,string,less<string> > TDBMemory;
00124
00126
00129 typedef TDBMemory::value_type TValue;
00130
00132
00135 TDBMemory DBMemory;
00136
00138
00142 TDBMemory::iterator index;
00143
00145
00149 enum {LogOK, LogCORRUPTION, LogNOFILE} OpenLogStatus;
00150
00152
00156 enum {CDBOK, CDBCORRUPTION, CDBNOFILE} OpenCDBStatus;
00157
00159
00163 int ReadBytes;
00164
00166 enum TFunction{ INSERT, UPDATE, DELETE };
00167
00168
00170
00174 class TAction{
00175 private:
00176
00178 TFunction Function;
00179
00181 string Key;
00182
00184 string Data;
00185
00186 public:
00187
00189 TAction(): Function(UPDATE), Key(""), Data("") { }
00190
00192 TAction(TFunction f, string k, string d): Function(f), Key(k), Data(d)
00193 { }
00194
00196 TAction(const TAction& arg){
00197 Function=arg.Function;
00198 Key=arg.Key;
00199 Data=arg.Data;
00200 }
00201
00203 ~TAction() { }
00204
00206 void operator=(const TAction ©){
00207 if (this == ©) return;
00208 Function=copy.Function;
00209 Key=copy.Key;
00210 Data=copy.Data;
00211 }
00212
00214 bool operator==(const TAction& arg) const {
00215 return (Function == arg.Function) &&
00216 (Key == arg.Key) &&
00217 (Data == arg.Data);
00218 }
00219
00221 bool operator<(const TAction& arg) const {
00222 return (Function < arg.Function) &&
00223 (Key < arg.Key) &&
00224 (Data < arg.Data);
00225 }
00226
00228 TFunction GetFunction(){ return Function;}
00229
00231 string &GetKey(){ return Key;}
00232
00234 string &GetData(){ return Data;}
00235 };
00236
00237
00239
00243 stack <TAction, vector<TAction> > RollBack;
00244
00246
00250 queue <TAction, list<TAction> > RedoLog;
00251
00253
00263 bool Getcdbline(ifstream &in, TAction &action, bool &final);
00264
00266
00278 bool Getlogline(ifstream &in, TAction &action, bool &commit, int &size, bool & final);
00279
00281
00291 bool Getline(ifstream &in, TAction &action, int &size);
00292
00294
00303 bool ParserLineOut(string &line, TAction &action);
00304
00306
00315 bool ParserLineDeleteOut(string &line, TAction &action);
00316
00318
00322 bool ReorganizeNecessary();
00323
00324
00325 public:
00327
00341 Tstdb( const char *TCDBName,
00342 const char *TCDBLogName,
00343 const int ParMaxLogSize,
00344 const int ParMaxTime);
00345
00347
00351 void Initialize(void);
00352
00354
00362 bool Insert(char *key,unsigned int lkey, char *data, unsigned int ldata);
00363
00365
00373 bool Update(char *key,unsigned int lkey, char *data, unsigned int ldata);
00374
00376
00384 bool Fetch(char *key,unsigned int lkey, const char **data, unsigned int *ldata);
00385
00387
00393 bool Delete(char *key,unsigned int lkey);
00394
00396
00402 bool Commit(void);
00403
00405
00411 bool Reorganize();
00412
00414
00419 bool Rollback(void);
00420
00422
00430 bool Firstkey(const char **key,unsigned int *lkey, const char **data, unsigned int *ldata);
00431
00433
00441 bool Nextkey(const char **key,unsigned int *lkey, const char **data, unsigned int *ldata);
00442
00444
00450 bool Getstatus(int *LogStatus, int *CDBStatus);
00451
00453
00456 void Finalize(void);
00457
00459 ~Tstdb();
00460 };
00461
00462 #endif // STDB_H