Main Page | Class List | File List | Class Members

Tstdb Class Reference

Simple Transactional DataBase. More...

Collaboration diagram for Tstdb:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Tstdb (const char *TCDBName, const char *TCDBLogName, const int ParMaxLogSize, const int ParMaxTime)
void Initialize (void)
bool Insert (char *key, unsigned int lkey, char *data, unsigned int ldata)
bool Update (char *key, unsigned int lkey, char *data, unsigned int ldata)
bool Fetch (char *key, unsigned int lkey, const char **data, unsigned int *ldata)
bool Delete (char *key, unsigned int lkey)
bool Commit (void)
bool Reorganize ()
bool Rollback (void)
bool Firstkey (const char **key, unsigned int *lkey, const char **data, unsigned int *ldata)
bool Nextkey (const char **key, unsigned int *lkey, const char **data, unsigned int *ldata)
bool Getstatus (int *LogStatus, int *CDBStatus)
void Finalize (void)
 ~Tstdb ()

Private Types

typedef map< string, string,
less< string > > 
TDBMemory
typedef TDBMemory::value_type TValue
enum  { LogOK, LogCORRUPTION, LogNOFILE }
enum  { CDBOK, CDBCORRUPTION, CDBNOFILE }
enum  TFunction { INSERT, UPDATE, DELETE }

Private Member Functions

bool Getcdbline (ifstream &in, TAction &action, bool &final)
bool Getlogline (ifstream &in, TAction &action, bool &commit, int &size, bool &final)
bool Getline (ifstream &in, TAction &action, int &size)
bool ParserLineOut (string &line, TAction &action)
bool ParserLineDeleteOut (string &line, TAction &action)
bool ReorganizeNecessary ()

Private Attributes

char * db
char * log
int MaxLogSize
int MaxTime
TDBMemory DBMemory
TDBMemory::iterator index
enum Tstdb:: { ... }  OpenLogStatus
enum Tstdb:: { ... }  OpenCDBStatus
int ReadBytes
stack< TAction, vector< TAction > > RollBack
queue< TAction, list< TAction > > RedoLog

Classes

class  TAction
 Class TAction to contain an action to be done. More...

Detailed Description

The Core Class: the database will be stored in memory inside an Tstdb object.

Definition at line 94 of file stdb.h.


Member Typedef Documentation

typedef map<string,string,less<string> > Tstdb::TDBMemory [private]
 

TDBMemory type definition.

The database is read from the db file to memory and it is stored into the object DBMemory, a TDBMemory map containing key/value pairs.

Definition at line 123 of file stdb.h.

typedef TDBMemory::value_type Tstdb::TValue [private]
 

TValue type definition.

This is an abstraction of every single object inside the DBMemory map.

Definition at line 129 of file stdb.h.


Member Enumeration Documentation

anonymous enum [private]
 

Log file possible status.

OpenLogStatus indicates the LOG file status, which could be correct corrupted or not existing, resp.

Definition at line 149 of file stdb.h.

00149 {LogOK, LogCORRUPTION, LogNOFILE} OpenLogStatus;

anonymous enum [private]
 

Database file possible status.

OpenCDBStatus reports the DB file status: OK, CORRUPTED OR NOT EXISTING.

Definition at line 156 of file stdb.h.

00156 {CDBOK, CDBCORRUPTION, CDBNOFILE} OpenCDBStatus;

enum Tstdb::TFunction [private]
 

These are possible TAction actions to be done.

Definition at line 166 of file stdb.h.

00166 { INSERT, UPDATE, DELETE };


Constructor & Destructor Documentation

Tstdb::Tstdb const char *  TCDBName,
const char *  TCDBLogName,
const int  ParMaxLogSize,
const int  ParMaxTime
 

Constructor Tstdb.

Features:

Truncate the log file if bad format founded

Autodetection of cdb remake operation interrumped

Parameters:
TCDBName Name of cdb file
TCDBLogName Name of log file
ParMaxLogSize Maximun log size to remake the cdbfile
ParMaxTime Max time between cdb and log files to remake db
Returns:
The Database Object

Definition at line 68 of file stdb.cpp.

References db, Initialize(), log, MaxLogSize, and MaxTime.

00068                                                                                                         {
00069     
00070     //  MaxLongLog
00071     MaxLogSize = ParMaxLogSize;
00072     
00073     //  MaxTime
00074     MaxTime = ParMaxTime * 24 * 60 * 60; //In seconds
00075     
00076     //  Save the path files
00077     db=strdup(TCDBName);
00078     log=strdup(TCDBLogName);
00079     
00080     //  Transfer database information from files to memory
00081     Initialize();
00082 }

Tstdb::~Tstdb  ) 
 

Destructor Tstdb.

Definition at line 785 of file stdb.cpp.

00785              {
00786 }


Member Function Documentation

bool Tstdb::Commit void   ) 
 

Commit transaction.

Validate current transaction, beginning of the next one is implicit. It's essential to assure transaction integrity.

Returns:
False if anything's wrong

Definition at line 508 of file stdb.cpp.

References Tstdb::TAction::GetFunction(), log, ParserLineDeleteOut(), ParserLineOut(), RedoLog, ReorganizeNecessary(), and RollBack.

00508                       {
00509 
00510     //  Are there movements to log?
00511     if(RedoLog.empty()) 
00512         return true;
00513     
00514     //  Transfer the redolog 
00515     //  queue to LOG file
00516     ofstream logOut(log, ios::out|ios::app);
00517     while(!RedoLog.empty()){
00518         TAction &action = RedoLog.front();
00519         
00520         switch(action.GetFunction()){
00521             
00522             case INSERT:
00523             
00524             case UPDATE:{
00525                 //  Parser line out:
00526                 //  Output formatting
00527                 string buffer;
00528                 ParserLineOut(buffer, action);
00529                 //  Writting line into LOG file
00530                 logOut << buffer;
00531                 break;
00532             }
00533             case DELETE:{
00534                 //  Parser Delete line out:
00535                 //  Output formatting
00536                 string buffer;
00537                 ParserLineDeleteOut(buffer, action);
00538                 //  Writting line into log file
00539                 logOut << buffer;
00540                 break;
00541             }               
00542         }
00543         RedoLog.pop();
00544     }
00545     //  Intro COMMIT constant before
00546     //  closing LOG file
00547     logOut << COMMIT << endl;
00548 
00549     //  Flushing the buffer
00550     logOut.flush();
00551     
00552     //  Erase the RollBack stack
00553     //  Pop all Actions from stack
00554     while(!RollBack.empty())
00555         RollBack.pop();
00556 
00557     //  Verify if Reorganize() is necessary
00558     ReorganizeNecessary();
00559 
00560     return true;
00561 }   

bool Tstdb::Delete char *  key,
unsigned int  lkey
 

Delete key/value pair from database.

Parameters:
key Key to be deleted
lkey Size of deleted key
Returns:
False if key do not exists

Definition at line 477 of file stdb.cpp.

References DBMemory, RedoLog, and RollBack.

00477                                              {
00478     string keys(key,key+lkey);
00479     //  Search (key,data) into memory map
00480     TDBMemory::iterator p = DBMemory.find(keys);
00481     if (p != DBMemory.end()){
00482         //  Key found
00483         //  Pushing an Insert to the RollBack stack
00484         TAction action1(INSERT, keys, p->second);
00485         RollBack.push(action1);
00486 
00487         //  Pushing a Delete to the RedoLog queue
00488         TAction action2(DELETE, keys, "");
00489         RedoLog.push(action2);
00490         
00491         //  Erasing (key,data) from map object
00492         DBMemory.erase(keys);
00493         
00494         return true;
00495     }
00496     else{
00497         //  Not found
00498         //  Nothing to do here
00499         return false;
00500     }
00501 }       

bool Tstdb::Fetch char *  key,
unsigned int  lkey,
const char **  data,
unsigned int *  ldata
 

Fetch the value for a given key from the database.

Parameters:
key Key to be fetched
lkey Size of the fetched key
data Returns a pointer to data
ldata Returns a pointer to size of data
Returns:
False if key do not exists

Definition at line 456 of file stdb.cpp.

References DBMemory.

00456                                                                                     {
00457     string keys(key,key+lkey);
00458     //  Search (key,data) into memory map
00459     TDBMemory::iterator p = DBMemory.find(keys);
00460     if (p != DBMemory.end()){       
00461         // Key has been found
00462         *ldata = p->second.length();
00463         *data = p->second.c_str();
00464         return true;
00465     }
00466     else{
00467         // Key has not been found
00468         return false;
00469     }
00470 }       

void Tstdb::Finalize void   ) 
 

Finalize: deletes the DB and LOG file.

It's not clear... why to delete these?

bool Tstdb::Firstkey const char **  key,
unsigned int *  lkey,
const char **  data,
unsigned int *  ldata
 

Find first key in database.

Parameters:
key Returns a pointer to the first key
lkey Returns a pointer to the size of the first key
data Returns a pointer to the data of the first key
ldata Returns a pointer to size of data of the 1st key
Returns:
False if anything's wrong

Definition at line 730 of file stdb.cpp.

References DBMemory, and index.

00730                                                                                                {
00731     //  Iterator at the beginning of the map
00732     index = DBMemory.begin();
00733 
00734     //  Verify if there are elements
00735     if (index == DBMemory.end()) 
00736         return false;
00737 
00738     *lkey = index->first.length();
00739     *key = index->first.c_str();
00740 
00741     *ldata = index->second.length();
00742     *data = index->second.c_str();
00743 
00744     index++;
00745         
00746     return true;
00747 }

bool Tstdb::Getcdbline ifstream &  in,
TAction action,
bool &  final
[private]
 

Parser Get database file lines.

Getcdbline is designed to read an entry from the database file and recover the action it stores.

Parameters:
in The stream we are reading from
action The action returned by reference
final Indicates if it's the end
Returns:
True if DB line recovery went OK

Definition at line 234 of file stdb.cpp.

References Getline().

Referenced by Initialize().

00234                                                                 {
00235 
00236     //  "+" check
00237     char c;
00238     in.get(c);
00239     if (in.eof()) return false;
00240     if (c=='\n'){
00241         final=true;
00242         in.get(c);
00243         return true;
00244     }
00245     final=false;
00246     if(c!='+')  return false;
00247 
00248     int size;
00249     return Getline(in, action, size);
00250 }

bool Tstdb::Getline ifstream &  in,
TAction action,
int &  size
[private]
 

Parser generic Get line.

Getline is designed to read a formatted entry from both the DB and the LOG file. It is called from Getcdbline and Getlogline functions.

Parameters:
in The stream we are reading from
action The action returned by reference
size The size of read entry
Returns:
True if line recovery went OK

Definition at line 293 of file stdb.cpp.

Referenced by Getcdbline(), and Getlogline().

00293                                                            {
00294     
00295     //  Key long
00296     char buffer[BUFFSIZE],c;
00297     size=1;         //  Start with '+' character, counts one byte
00298     in.get(buffer, BUFFSIZE, ',');
00299     int lkey;
00300     lkey = atol(buffer);
00301     size+=strlen(buffer);
00302     in.get(c);
00303     if (in.eof())
00304         return false;
00305     if(c!=',')  
00306         return false;
00307     size+=1;
00308 
00309     //  Data long
00310 
00311     int ldata;
00312 
00313     in.get(buffer, BUFFSIZE, ':');
00314     if (in.eof()) 
00315         return false;
00316     if(buffer[0]=='-'){
00317         //  It's a deleted key
00318         in.get(buffer[1]);
00319         if (in.eof()) 
00320             return false;   
00321         if(buffer[1]=='1'){
00322             ldata=0;
00323             size+=2;
00324         }
00325         else{
00326             //  Format error
00327             return false;
00328         }
00329     }
00330     else{
00331         //  Usual key
00332         if (in.eof())
00333             return false;   
00334         ldata = atol(buffer);
00335         size+=strlen(buffer);
00336     }
00337     
00338     in.get(c);
00339     if (in.eof()) 
00340         return false;
00341     if(c!=':')  
00342         return false;
00343     size+=1;
00344     
00345     //  Get key
00346     in.read(buffer, lkey);
00347     if (in.eof()) 
00348         return false;
00349     string keys(buffer, buffer+lkey);
00350     size+=lkey;
00351 
00352     //  Get "->"
00353 
00354     in.get(c);
00355     if (in.eof()) return false;
00356     if(c!='-')  return false;
00357     in.get(c);
00358     if (in.eof()) return false;
00359     if(c!='>')  return false;
00360     size+=2;
00361     
00362     //  Get data
00363 
00364     if(ldata == 0){
00365         //  Deleted key
00366         TAction action1(DELETE,keys,"");
00367         action=action1;
00368     }
00369     else{
00370         //  Usual key
00371         in.read(buffer, ldata);
00372         string datas(buffer, buffer+ldata);
00373         TAction action1(INSERT,keys,datas);
00374         action=action1;
00375     }
00376     size+=ldata;
00377     
00378     //  Get '\n', end of line/entry
00379     in.get(c);
00380     if (in.eof()) 
00381         return false;   
00382     if(c!='\n')  
00383         return false;
00384     size+=1;
00385     return true;
00386 }

bool Tstdb::Getlogline ifstream &  in,
TAction action,
bool &  commit,
int &  size,
bool &  final
[private]
 

Parser Get log file lines.

Getlogline is designed to read an entry from the LOG file and recover the action it stores.

Parameters:
in The stream we are reading from
action The action returned by reference
commit Indicates end of transaction
size Returns the size of the current entry
final Indicates if it's the end
Returns:
True if LOG line recovery went OK

Definition at line 257 of file stdb.cpp.

References Getline().

Referenced by Initialize().

00257                                                                                          {
00258 
00259     //  "C" check
00260     char c;
00261     in.get(c);
00262     if (in.eof()){
00263         //  End of file detection
00264         final = true;
00265         in.get(c);
00266         return true;
00267     }
00268     final = false;
00269     if(c==COMMIT){
00270         //  A Commit
00271         commit = true;
00272         
00273         //  Get '\n', end of line
00274         in.get(c);
00275         if(in.eof()) return false;  
00276         if(c!='\n')  return false;
00277         size = 2;
00278         return true;
00279     }
00280     else{
00281         //  Not a Commit
00282         commit = false;
00283         if(c!='+')  return false;
00284         return Getline(in, action, size);
00285     }
00286 }

bool Tstdb::Getstatus int *  LogStatus,
int *  CDBStatus
 

Return status.

Parameters:
LogStatus Returns a pointer to status of log file
CDBStatus Returns a pointer to status of db file
Returns:
False if anything's wrong

void Tstdb::Initialize void   ) 
 

Initialize: transfers DB from file to memory.

Initialize reads data from de LOG and DB file and store transactions inside a new DB in memory.

Definition at line 89 of file stdb.cpp.

References db, DBMemory, Getcdbline(), Tstdb::TAction::GetData(), Tstdb::TAction::GetFunction(), Tstdb::TAction::GetKey(), Getlogline(), log, OpenCDBStatus, OpenLogStatus, ReadBytes, Rollback(), and RollBack.

Referenced by Tstdb().

00089                           {
00090     
00091     //  To truncate the log file if it founds format errors
00092     int ReadBytesTran = 0;
00093     
00094     char dbtmp[100];
00095     sprintf(dbtmp, "%s.tmp", db); 
00096 
00097     //  Checking if TMP exists, because it means something went wrong
00098     //  during last Reorganize. If it exists, it renames it from TMP
00099     //  to DB
00100     ifstream fdtmp(dbtmp, ios::in);
00101     if(fdtmp)
00102         rename(dbtmp,db);
00103     fdtmp.close();
00104     
00105     //  Opening DB file in read-only mode. Now we're going to transfer
00106     //  DB entries saved into the file to a new DBMemory object.    
00107     
00108     ifstream cdbIn(db , ios::in); 
00109     if(!cdbIn){
00110         //  If DB file doesn't exist
00111         OpenCDBStatus=CDBNOFILE;
00112     }
00113     else{
00114         //  DB file do exist    
00115         OpenCDBStatus=CDBOK;
00116         
00117         //  Loops DB file to fill the map object
00118         //  It reads lines from DB file while we don't reach the end of file.
00119         string buffer;
00120         while (!cdbIn.eof()){
00121             bool final;
00122             TAction action;
00123             if (!Getcdbline(cdbIn, action, final)){             
00124                 //  Format error encountered, file corrupted
00125                 OpenCDBStatus=CDBCORRUPTION;    
00126                 break;
00127             }
00128             if(!final){
00129                 //  Introduce (key,data) into map object
00130                 DBMemory.insert(TValue(action.GetKey(),action.GetData()));
00131             }
00132         }
00133     }
00134     
00135     //  Let's transfer the log file into memory map
00136     ifstream logIn(log, ios::in);
00137     if (!logIn){
00138         //  LOG file doesn't exist
00139         OpenLogStatus=LogNOFILE;    
00140     }
00141     else{   
00142         //  LOG file do exist
00143         ReadBytes=0;    
00144         //  Loop LOG entries/lines to fill map object
00145         string buffer;
00146         while (!logIn.eof()){
00147             //  Read line
00148             bool commit, final;
00149             int size;
00150             TAction action;
00151             
00152             if (!Getlogline(logIn, action, commit, size, final)){
00153                 //  Format error
00154                 OpenLogStatus=LogCORRUPTION;
00155                 break;
00156             }
00157             if(final) continue;
00158             if(commit){
00159                 //  End of transaction
00160                 ReadBytes+=ReadBytesTran+2;
00161                 ReadBytesTran = 0;
00162                 //  Pop all Actions from stack
00163                 while(!RollBack.empty()){
00164                     RollBack.pop();
00165                 }
00166             }
00167             else{
00168                 //  Not the end of transaction
00169                 ReadBytesTran+=size;
00170                 
00171                 //  Search (key,data) into memory map
00172                 TDBMemory::iterator p = DBMemory.find(action.GetKey());
00173 
00174                 //  Difference between DELETE, UPDATE and INSERT and others
00175                 switch(action.GetFunction()){
00176                     case DELETE:                    
00177                         //  DELETE case
00178                         if (p != DBMemory.end()){
00179                             //  Found
00180                             //  Update the rollback with update
00181                             TAction action2(INSERT, action.GetKey(), p->second);
00182                             RollBack.push(action2);
00183                             //  Erase (key,data) from map object
00184                             DBMemory.erase(action.GetKey());
00185                         }
00186                         else{
00187                             //  Not found
00188                             //  Nothing to do here
00189                         }
00190                         break;
00191                     
00192                     case UPDATE:
00193                     case INSERT:
00194                         //  Distinguish between INSERT and UPDATE
00195                         if (p != DBMemory.end()){
00196                             //  Found
00197                             //  UPDATE case
00198                             //  Update the rollback with update
00199                             TAction action2(UPDATE, action.GetKey(), action.GetData());
00200                             RollBack.push(action2);
00201                             //  Update (key,data) into map object
00202                             DBMemory[action.GetKey()] = action.GetData();
00203                         }
00204                         else{
00205                             //  Not found
00206                             //  INSERT case
00207                             //  Update the rollback with delete
00208                             TAction action2(DELETE, action.GetKey(), "");
00209                             RollBack.push(action2);
00210                             //  Introduce (key,data) into map object
00211                             DBMemory.insert(TValue(action.GetKey(),action.GetData()));
00212                         }
00213                         break;
00214                 }
00215             }
00216         }
00217         //  If EOF and rollback stack is not empty -> LOG file corrupted
00218         if (OpenLogStatus == LogCORRUPTION){
00219             //  Rollback the current transaction
00220             Rollback();
00221             //  Truncate the log file
00222             truncate(log, ReadBytes);
00223         }
00224         else
00225             OpenLogStatus=LogOK;
00226     }   
00227 }

bool Tstdb::Insert char *  key,
unsigned int  lkey,
char *  data,
unsigned int  ldata
 

Insert a key/value pair to database.

Parameters:
key The key to be inserted
lkey The size of that key
data The data to be inserted
ldata The size of data
Returns:
False if key exists

Definition at line 425 of file stdb.cpp.

References DBMemory, RedoLog, and RollBack.

00425                                                                              {
00426     string keys(key,key+lkey);
00427     string datas(data,data+ldata);
00428     //  Search (key,data) into memory map
00429     TDBMemory::iterator p = DBMemory.find(keys);
00430     if (p != DBMemory.end()){
00431         //  Key found,
00432         //  not possible to Insert 
00433         return false;
00434     }
00435     else{
00436         //  Not found
00437         //  Pushing a Delete to the RollBack stack
00438         TAction action1(DELETE, keys, "");
00439         RollBack.push(action1);
00440 
00441         //  Pushing an Insert to the RedoLog queue
00442         TAction action2(INSERT, keys, datas);
00443         RedoLog.push(action2);
00444         
00445         //  Inserting (key,data) into map object
00446         DBMemory.insert(TValue(keys,datas));
00447         return true;
00448     }
00449 }           

bool Tstdb::Nextkey const char **  key,
unsigned int *  lkey,
const char **  data,
unsigned int *  ldata
 

Find next key in database.

Parameters:
key Returns a pointer to next key
lkey Returns a pointer to size of next key
data Returns a pointer of data
ldata: returns a pointer to size of data
Returns:
False if anything's wrong

Definition at line 754 of file stdb.cpp.

References DBMemory, and index.

00754                                                                                               {
00755 
00756     //  Verify if not elements
00757     if (index == DBMemory.end()) 
00758         return false;
00759 
00760     *lkey = index->first.length();
00761     *key = index->first.c_str();
00762 
00763     *ldata = index->second.length();
00764     *data = index->second.c_str();
00765 
00766     index++;
00767 
00768     return true;
00769 }

bool Tstdb::ParserLineDeleteOut string &  line,
TAction action
[private]
 

Parser line delete out.

ParserLineDeleteOut is designed to read a DELETE action and generate a plain text line, formatted to fit into the LOG file.

Parameters:
line The output string
action The action to be parsed
Returns:
True if parsing went OK

Definition at line 596 of file stdb.cpp.

References Tstdb::TAction::GetKey().

Referenced by Commit().

00596                                                             {
00597     char buffer[100];
00598     
00599     //  Output delete format defined here
00600     sprintf(buffer, "+%lu,-1:", (unsigned long) action.GetKey().size());
00601 
00602     char *d;
00603     d=buffer+strlen(buffer);
00604     memmove(d,action.GetKey().c_str(), action.GetKey().size()); 
00605     d+=action.GetKey().size();
00606     *(d++)='-'; 
00607     *(d++)='>';
00608     *(d++)='\n';
00609 
00610     string linermp(buffer,d);
00611     line = linermp;
00612 
00613     return true;
00614 }

bool Tstdb::ParserLineOut string &  line,
TAction action
[private]
 

Parser Line out.

ParserLineOut is designed to read a INSERT or UPDATE action an generate a plain text line, formatted to fit into the LOG file.

Parameters:
line The output string
action The action to be parsed
Returns:
True if parsing went OK

Definition at line 568 of file stdb.cpp.

References Tstdb::TAction::GetData(), and Tstdb::TAction::GetKey().

Referenced by Commit(), and Reorganize().

00568                                                       {
00569     char buffer[100];
00570 
00571     
00572     //  Output format is defined here
00573     sprintf(buffer, "+%lu,%lu:", (unsigned long)action.GetKey().size(), 
00574                                     (unsigned long)action.GetData().size());
00575     char *d;
00576     d=buffer+strlen(buffer);
00577     memmove(d,action.GetKey().c_str(), action.GetKey().size()); 
00578     d+=action.GetKey().size();
00579     *(d++)='-'; 
00580     *(d++)='>';
00581     memmove(d,action.GetData().c_str(), action.GetData().size()); 
00582     d+=action.GetData().size();
00583     *(d++)='\n';
00584 
00585     string linetmp(buffer,d); 
00586     line = linetmp;
00587 
00588     return true;
00589 }

bool Tstdb::Reorganize  ) 
 

Reorganize: transfers logged transactions to DB file.

Reorganize writes a new cdb incorporating all the logged changes, delete the log and reopen the new cdb.

Returns:
True if everything ok.

Definition at line 659 of file stdb.cpp.

References db, DBMemory, log, ParserLineOut(), and Rollback().

Referenced by ReorganizeNecessary().

00659                       {
00660     //  Rollback the current transaction if not finished
00661     Rollback();
00662 
00663     //  Rename the actual DB to DB.tmp
00664     char dbtmp[100];
00665     sprintf(dbtmp, "%s.tmp", db);
00666     rename(db, dbtmp);
00667     
00668     //  Checking if existing DB
00669     ofstream cdbOut(db);
00670     if (!cdbOut){
00671         return false;
00672     }
00673 
00674     //  Iterator at the beginning of
00675     //  the memory map 
00676     TDBMemory::iterator DBIter = DBMemory.begin();
00677 
00678     //  Loop: rebuilding the DB file
00679     while (DBIter != DBMemory.end()){
00680         TAction action(INSERT,DBIter->first, DBIter->second);
00681         //  Parser line out
00682         string buffer;
00683         ParserLineOut(buffer, action);
00684         cdbOut << buffer;
00685         DBIter++;
00686     }
00687         
00688     //  Closing DB file
00689     cdbOut << endl;
00690     
00691     //  Erasing the LOG file
00692     unlink(log);
00693 
00694     //  Erasing the TMP file
00695     unlink(dbtmp);
00696     
00697     return true;
00698 }

bool Tstdb::ReorganizeNecessary  )  [private]
 

Reorganize: check if is necessary to remake the cdb file.

Depending on the value of MaxLogSize i MaxTime, Commit function will call to Reorganize automatically, to update and rebuild the DB file.

Definition at line 705 of file stdb.cpp.

References db, log, MaxLogSize, MaxTime, and Reorganize().

Referenced by Commit().

00705                                {
00706 
00707     struct stat file_cdb_stats;
00708     struct stat file_log_stats;
00709     
00710     if (stat(log, &file_log_stats)!=-1){
00711         //  LOG exists
00712         //  Verify max size
00713         if (file_log_stats.st_size > MaxLogSize) 
00714             return Reorganize();
00715 
00716         //  Verify max time cdb to log file
00717         if (stat(db, &file_cdb_stats)!=-1){
00718             if(difftime(file_log_stats.st_mtime, file_cdb_stats.st_mtime) > MaxTime) 
00719                 return Reorganize();
00720         }
00721     }
00722     return true;
00723 }

bool Tstdb::Rollback void   ) 
 

RollBack transaction.

It's like an Undo function.

Returns:
False if anything's wrong

Definition at line 621 of file stdb.cpp.

References DBMemory, Tstdb::TAction::GetData(), Tstdb::TAction::GetFunction(), Tstdb::TAction::GetKey(), RedoLog, and RollBack.

Referenced by Initialize(), and Reorganize().

00621                         {
00622     //  Redo the last transaction, using the RollBack stack
00623     //  Transfer the RedoLog queue to LOG file
00624     while(!RollBack.empty()){
00625         TAction &action = RollBack.top();
00626         switch(action.GetFunction()){
00627             
00628             case INSERT:
00629                 //  Inserting (key,data) into map object
00630                 DBMemory.insert(TValue(action.GetKey(),action.GetData()));
00631                 break;
00632 
00633             case UPDATE:
00634                 //  Updating (key,data) into map object
00635                 DBMemory[action.GetKey()] = action.GetData();
00636                 break;
00637                 
00638             case DELETE:
00639                 //  Erasing (key,data) from map object
00640                 DBMemory.erase(action.GetKey());
00641                 break;
00642         }
00643         RollBack.pop();
00644     }
00645 
00646     //  Empty the RedoLog queue
00647     //  Pop all Actions from queue
00648     while(!RedoLog.empty())
00649         RedoLog.pop();
00650 
00651     return true;
00652 }           

bool Tstdb::Update char *  key,
unsigned int  lkey,
char *  data,
unsigned int  ldata
 

Update a key/value pair to database.

Parameters:
key The key to be updated
lkey The size of updated key
data: The data to be updated
ldata: The size of the updated data
Returns:
False if not exists

Definition at line 393 of file stdb.cpp.

References DBMemory, RedoLog, and RollBack.

00393                                                                              {
00394     string keys(key,key+lkey);
00395     string datas(data,data+ldata);
00396     //  Search (key,data) into memory map
00397     TDBMemory::iterator p = DBMemory.find(keys);
00398     if (p != DBMemory.end()){
00399         //  Found key
00400         //  Update the rollback with update
00401         TAction action1(UPDATE, keys, p->second);
00402         RollBack.push(action1);
00403 
00404         //  Update the redolog with update
00405         TAction action2(UPDATE, keys, datas);
00406         RedoLog.push(action2);
00407         
00408         //  Update (key,data) into map object
00409         DBMemory[keys] = datas;
00410         
00411         return true;
00412     }
00413     else{
00414         //  Key not found
00415         //  Nothing to do
00416         return false;
00417     }
00418 }           


Member Data Documentation

char* Tstdb::db [private]
 

Filenames for the database file and the log file.

This "db" and "log" char arrays will contain the names of the database and log files, respectively.

Definition at line 102 of file stdb.h.

Referenced by Initialize(), Reorganize(), ReorganizeNecessary(), and Tstdb().

TDBMemory Tstdb::DBMemory [private]
 

Declaration of DBMemory map object.

This is the DBMemory object containg the database in memory.

Definition at line 135 of file stdb.h.

Referenced by Delete(), Fetch(), Firstkey(), Initialize(), Insert(), Nextkey(), Reorganize(), Rollback(), and Update().

TDBMemory::iterator Tstdb::index [private]
 

The index iterator.

An iterator is like a pointer designed for STL containers. The index iterator is designed to sequence objects stored into the DBMemory map.

Definition at line 142 of file stdb.h.

Referenced by Firstkey(), and Nextkey().

char * Tstdb::log [private]
 

Filenames for the database file and the log file.

This "db" and "log" char arrays will contain the names of the database and log files, respectively.

Definition at line 102 of file stdb.h.

Referenced by Commit(), Initialize(), Reorganize(), ReorganizeNecessary(), and Tstdb().

int Tstdb::MaxLogSize [private]
 

Maximum longitude of the log file to rebuild the database file.

The database should remake itself when the log file achieves this size, expressed in bytes.

Definition at line 109 of file stdb.h.

Referenced by ReorganizeNecessary(), and Tstdb().

int Tstdb::MaxTime [private]
 

Maximum time between cdb and log in days.

This integer variable stores the time between de database file and the log file.

Definition at line 116 of file stdb.h.

Referenced by ReorganizeNecessary(), and Tstdb().

enum { ... } Tstdb::OpenCDBStatus [private]
 

Database file possible status.

OpenCDBStatus reports the DB file status: OK, CORRUPTED OR NOT EXISTING.

Referenced by Initialize().

enum { ... } Tstdb::OpenLogStatus [private]
 

Log file possible status.

OpenLogStatus indicates the LOG file status, which could be correct corrupted or not existing, resp.

Referenced by Initialize().

int Tstdb::ReadBytes [private]
 

To truncate the log file if format error.

ReadBytes stores the current transaction lenght, in order to truncate the LOG file in case of data corruption.

Definition at line 163 of file stdb.h.

Referenced by Initialize().

queue<TAction, list<TAction> > Tstdb::RedoLog [private]
 

The RedoLog queue.

This queue is designed to store actions done to the database in order to store them into the LOG file.

Definition at line 250 of file stdb.h.

Referenced by Commit(), Delete(), Insert(), Rollback(), and Update().

stack<TAction, vector<TAction> > Tstdb::RollBack [private]
 

The RollBack stack.

This stack stores inverse actions in order to undo changes done to the database.

Definition at line 243 of file stdb.h.

Referenced by Commit(), Delete(), Initialize(), Insert(), Rollback(), and Update().


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