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