2003-05-26 13:45:00 +00:00
|
|
|
#ifndef __DB_H
|
|
|
|
#define __DB_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2003-07-31 16:05:35 +00:00
|
|
|
#include <map>
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
#include <db_cxx.h>
|
|
|
|
|
2003-07-07 09:25:26 +00:00
|
|
|
#include "util.hh"
|
|
|
|
|
2003-05-26 13:45:00 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
class Database;
|
|
|
|
|
|
|
|
|
|
|
|
class Transaction
|
|
|
|
{
|
|
|
|
friend class Database;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DbTxn * txn;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Transaction();
|
|
|
|
Transaction(Database & _db);
|
|
|
|
~Transaction();
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void abort();
|
2003-07-31 13:47:13 +00:00
|
|
|
void commit();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define noTxn Transaction()
|
|
|
|
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
typedef unsigned int TableId; /* table handles */
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
class Database
|
|
|
|
{
|
|
|
|
friend class Transaction;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DbEnv * env;
|
|
|
|
|
2003-10-14 15:33:00 +00:00
|
|
|
int fdLock;
|
|
|
|
int fdAccessors;
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
TableId nextId;
|
|
|
|
map<TableId, Db *> tables;
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
void requireEnv();
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
Db * getDb(TableId table);
|
2003-07-31 13:47:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Database();
|
|
|
|
~Database();
|
|
|
|
|
|
|
|
void open(const string & path);
|
2003-10-14 15:33:00 +00:00
|
|
|
void close();
|
2003-07-31 13:47:13 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
TableId openTable(const string & table);
|
2003-07-31 13:47:13 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
bool queryString(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, string & data);
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
bool queryStrings(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, Strings & data);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void setString(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, const string & data);
|
2003-07-07 09:25:26 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void setStrings(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, const Strings & data);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void delPair(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key);
|
2003-07-07 09:25:26 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void enumTable(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
Strings & keys);
|
|
|
|
};
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif /* !__DB_H */
|