Main Page | Class List | File List | Class Members

/home/alex/STDB/stdb/include/stdb.h

00001 /***************************************************************************
00002                           stdb.h  -  Simple Tansactional Data Base
00003                              -------------------
00004     begin                : 16/mar/2004
00005     copyright            : (C) 2004 by Andreu Moreno
00006     email                : amoreno@euss.es
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *  This library is free software; you can redistribute it and/or
00012  *  modify it under the terms of the GNU Lesser General Public
00013  *  License as published by the Free Software Foundation; either
00014  *  version 2.1 of the License, or (at your option) any later version.
00015 
00016  *  This library is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *  Lesser General Public License for more details.
00020 
00021  *  You should have received a copy of the GNU Lesser General Public
00022  *  License along with this library; if not, write to the Free Software
00023  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00024  *                                                                         *
00025  ***************************************************************************/
00026 
00027 /*
00028  *
00029  * $Author: alex_blanquer $
00030  * $Revision: 1.3 $
00031  * $Log: stdb.h,v $
00032  * Revision 1.3  2005/05/11 23:56:16  alex_blanquer
00033  * Bug fixed test1.cpp and modified Doxygen config/doc stuff
00034  *
00035  * Revision 1.2  2005/05/10 10:59:52  amoreno
00036  * Added comments, doxygen formatted and bug cleaning
00037  *
00038  * Revision 1.1.1.1  2004/03/23 10:13:10  amoreno
00039  * Initial import.
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 &copy){
00207             if (this == &copy) 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); //Parser line Delete Out
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

Generated on Thu May 12 17:58:08 2005 for SimpleTransactionalDatabase by  doxygen 1.4.0