* Enclose most operations that update the database in transactions.
* Open all database tables (Db objects) at initialisation time, not every time they are used. This is necessary because tables have to outlive all transactions that refer to them.
This commit is contained in:
parent
177a7782ae
commit
06d3d7355d
6 changed files with 145 additions and 96 deletions
25
src/db.hh
25
src/db.hh
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
#include <db_cxx.h>
|
||||
|
||||
|
|
@ -26,6 +27,7 @@ public:
|
|||
Transaction(Database & _db);
|
||||
~Transaction();
|
||||
|
||||
void abort();
|
||||
void commit();
|
||||
};
|
||||
|
||||
|
|
@ -33,6 +35,9 @@ public:
|
|||
#define noTxn Transaction()
|
||||
|
||||
|
||||
typedef unsigned int TableId; /* table handles */
|
||||
|
||||
|
||||
class Database
|
||||
{
|
||||
friend class Transaction;
|
||||
|
|
@ -40,10 +45,12 @@ class Database
|
|||
private:
|
||||
DbEnv * env;
|
||||
|
||||
TableId nextId;
|
||||
map<TableId, Db *> tables;
|
||||
|
||||
void requireEnv();
|
||||
|
||||
Db * openDB(const Transaction & txn,
|
||||
const string & table, bool create);
|
||||
Db * getDb(TableId table);
|
||||
|
||||
public:
|
||||
Database();
|
||||
|
|
@ -51,24 +58,24 @@ public:
|
|||
|
||||
void open(const string & path);
|
||||
|
||||
void createTable(const string & table);
|
||||
TableId openTable(const string & table);
|
||||
|
||||
bool queryString(const Transaction & txn, const string & table,
|
||||
bool queryString(const Transaction & txn, TableId table,
|
||||
const string & key, string & data);
|
||||
|
||||
bool queryStrings(const Transaction & txn, const string & table,
|
||||
bool queryStrings(const Transaction & txn, TableId table,
|
||||
const string & key, Strings & data);
|
||||
|
||||
void setString(const Transaction & txn, const string & table,
|
||||
void setString(const Transaction & txn, TableId table,
|
||||
const string & key, const string & data);
|
||||
|
||||
void setStrings(const Transaction & txn, const string & table,
|
||||
void setStrings(const Transaction & txn, TableId table,
|
||||
const string & key, const Strings & data);
|
||||
|
||||
void delPair(const Transaction & txn, const string & table,
|
||||
void delPair(const Transaction & txn, TableId table,
|
||||
const string & key);
|
||||
|
||||
void enumTable(const Transaction & txn, const string & table,
|
||||
void enumTable(const Transaction & txn, TableId table,
|
||||
Strings & keys);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue