Commit f8e7fa87 authored by lw's avatar lw

*** empty log message ***

parent c981ec00
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include "db_cxx.h"
#include "wb_ldh.h"
#include "wb_destination.h"
#include "wb_db.h"
//#include "wb_import.h"
#include "wb_export.h"
wb_db_class::wb_db_class(wb_db *db, wb_db_txn *txn, pwr_tCid cid) :
m_db(db), m_key(&m_k, sizeof(m_k))
{
m_k.oid = pwr_cNOid;
m_k.cid = cid;
m_db->m_class->cursor(txn, &m_dbc, 0);
}
bool wb_db_class::succ(pwr_tOid oid)
{
m_k.oid = oid;
m_k.oid.oix += 1;
int ret = m_dbc->get(&m_key, 0, DB_SET_RANGE);
return ret == 0;
}
bool wb_db_class::pred(pwr_tOid oid)
{
m_k.oid = oid;
int ret = m_dbc->get(&m_key, 0, DB_SET_RANGE);
if (ret != 0)
return false;
ret = m_dbc->get(&m_key, 0, DB_PREV);
return ret == 0;
}
void wb_db_class::put(wb_db_txn *txn)
{
m_db->m_class->put(txn, &m_key, 0, 0);
}
void wb_db_class::del(wb_db_txn *txn)
{
m_db->m_class->del(txn, &m_key, 0);
}
wb_db_class::~wb_db_class()
{
m_dbc->close();
}
wb_db_name::wb_db_name(wb_db *db, wb_db_txn *txn) :
m_db(db), m_key(&m_k, sizeof(m_k)), m_data(&m_d, sizeof(m_d))
{
m_k.poid = pwr_cNOid;
}
void wb_db_name::put(wb_db_txn *txn)
{
m_db->m_name->put(txn, &m_key, 0, 0);
}
wb_db_ohead::wb_db_ohead(wb_db *db) :
m_db(db), m_key(&m_o.oid, sizeof(m_o.oid)), m_data(&m_o, sizeof(m_o))
{
}
wb_db_ohead::wb_db_ohead(wb_db *db, pwr_tOid oid) :
m_db(db), m_key(&m_o.oid, sizeof(m_o.oid)), m_data(&m_o, sizeof(m_o))
{
m_o.oid = oid;
}
wb_db_ohead::wb_db_ohead(wb_db *db, wb_db_txn *txn, pwr_tOid oid) :
m_db(db), m_key(&m_o.oid, sizeof(m_o.oid)), m_data(&m_o, sizeof(m_o))
{
m_o.oid = oid;
get(txn);
}
wb_db_ohead &wb_db_ohead::get(wb_db_txn *txn)
{
m_db->m_ohead->get(txn, &m_key, &m_data, 0);
return *this;
}
void wb_db_ohead::put(wb_db_txn *txn)
{
m_db->m_ohead->put(txn, &m_key, &m_data, 0);
}
wb_db_ohead &wb_db_ohead::get(wb_db_txn *txn, pwr_tOid oid)
{
m_o.oid = oid;
m_db->m_ohead->get(txn, &m_key, &m_data, 0);
return *this;
}
wb_db_ohead &wb_db_ohead::get(pwr_tOid oid)
{
m_o.oid = oid;
m_db->m_ohead->get(m_db->m_txn, &m_key, &m_data, 0);
return *this;
}
void wb_db_ohead::del(wb_db_txn *txn)
{
m_db->m_ohead->del(txn, &m_key, 0);
}
void wb_db::close()
{
m_ohead->close(0);
m_obody->close(0);
m_class->close(0);
m_name->close(0);
m_info->close(0);
m_env->close(0);
}
void wb_db::open(char *name)
{
struct stat sb;
/* Create the directory, read/write/access owner only. */
if (stat(name, &sb) != 0) {
if (mkdir(name, S_IRWXU) != 0) {
fprintf(stderr, "txnapp: mkdir: %s, %s\n", name, strerror(errno));
//exit(1);
}
}
m_env = new DbEnv(0/*DB_CXX_NO_EXCEPTIONS*/);
m_env->set_errpfx("PWR db");
m_env->open(name,
DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER,
S_IRUSR | S_IWUSR);
m_ohead = new Db(m_env, 0);
m_obody = new Db(m_env, 0);
m_class = new Db(m_env, 0);
m_name = new Db(m_env, 0);
m_info = new Db(m_env, 0);
m_ohead->open("ohead", NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR);
m_obody->open("obody", NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR);
m_class->open("class", NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR);
m_name->open("name", NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR);
m_info->open("info", NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR);
}
pwr_tOid wb_db::new_oid(wb_db_txn *txn)
{
int index = 1;
int ret;
Dbt key(&index, sizeof(index));
pwr_tOid oid;
Dbt data(&oid, sizeof(oid));
ret = m_info->get(txn, &key, &data, 0);
oid.oix++;
ret = m_info->put(txn, &key, &data, 0);
return oid;
}
int wb_db::del_family(wb_db_txn *txn, Dbc *cp, pwr_tOid poid)
{
int ret = 0;
#if 0
dbName name;
name.poid = poid;
name.normname = "";
pwr_tOid oid = 0;
FamilyKey nk(po, );
FamiltData nd(&oid, sizeof(oid));
Dbc *ncp;
cp->dup(*ncp, 0);
while ((ret = cp->get(&nk, &nd, DB_NEXT)) != DB_NOTFOUND) {
del_family(txn, ncp, oid);
cp->del(0);
oh_k ok(nd);
oh_d od();
m_db_ohead->get(txn, &ok, &od, 0);
wb_DbClistK ck(od);
m_db_class->del(txn, &ck, 0);
wb_DbBodyK bk(od);
m_db_obody->del(txn, &bk, 0);
m_db_ohead->del(txn, &ok, 0);
}
ncp->close();
ret = m_db_name->del(txn, &key, 0);
#endif
return ret;
}
//
// Save all changes done in the current transaction.
//
bool wb_db::commit(wb_db_txn *txn)
{
txn->commit(0);
m_env->txn_checkpoint(0, 0, 0);
return true;
}
//
// Abort the current transactionm, the database is restored to
// the state it had before the current transaction started.
//
bool wb_db::abort(wb_db_txn *txn)
{
txn->abort();
return true;
}
bool wb_db::deleteFamily(pwr_tStatus *sts, wb_db_ohead *o)
{
DbTxn *txn = 0;
m_env->txn_begin(m_txn, &txn, 0);
try {
//unadopt(txn, wb_Position(o));
//del_ohead(txn, o);
//del_clist(txn, o);
//del_body(txn, o);
txn->commit(0);
//o->mark(is_deleted);
}
catch (DbException &e) {
txn->abort();
}
return true;
}
#if 0
bool wb_db::deleteOset(pwr_tStatus *sts, wb_oset *o)
{
DbTxn *txn = 0;
m_env->txn_begin(m_txn, &txn, 0);
try {
//del_family(txn, o);
//unadopt(txn, wb_Position(o));
//del_ohead(txn, o);
//del_clist(txn, o);
//del_name(txn, o);
//del_body(txn, o);
txn->commit(0);
}
catch (DbException &e) {
txn->abort();
}
return true;
}
#endif
bool wb_db::importVolume(wb_export *e)
{
m_env->txn_begin(0, (DbTxn **)&m_txn, 0);
e->exportHead(*this);
e->exportRbody(*this);
e->exportDbody(*this);
e->exportMeta(*this);
m_txn->commit(0);
m_env->txn_checkpoint(0, 0, 0);
return true;
}
bool wb_db::importHead(pwr_tOid oid, pwr_tCid cid, pwr_tOid poid,
pwr_tOid boid, pwr_tOid aoid, pwr_tOid foid, pwr_tOid loid,
pwr_tObjName name, pwr_tObjName normname,
pwr_tTime ohTime, pwr_tTime rbTime, pwr_tTime dbTime,
size_t rbSize, size_t dbSize)
{
wb_db_ohead o(this, oid, cid, poid, boid, aoid, foid, loid, name, normname, ohTime, rbTime, dbTime, rbSize, dbSize);
o.put(m_txn);
wb_db_name n(this, poid, normname);
n.put(m_txn);
wb_db_class c(this, cid, oid);
c.put(m_txn);
return true;
}
bool wb_db::importRbody(pwr_tOid oid, size_t size, void *body)
{
wb_db_body rb(this, oid, size, body);
rb.put(m_txn);
return true;
}
bool wb_db::importDbody(pwr_tOid oid, size_t size, void *body)
{
wb_db_body db(this, oid, size, body);
db.put(m_txn);
return true;
}
bool wb_db::importMeta(const char *fileName)
{
return true;
}
#ifndef wb_db_h
#define wb_db_h
#include "pwr.h"
#include "db_cxx.h"
#include "wb_import.h"
class wb_name;
typedef struct {
pwr_tOid oid; /**< object identifier */
pwr_tCid cid; /**< class identifier */
pwr_tOid poid; /**< object identifier of parent */
pwr_tObjName name; /**< name of object */
pwr_tObjName normname; /**< normalized object name. */
pwr_tTime time; /**< time of last change in object header */
pwr_tOid boid; /**< object before this object. */
pwr_tOid aoid; /**< object after this object. */
pwr_tOid foid; /**< first child object. */
pwr_tOid loid; /**< last child object. */
pwr_mClassDef flags;
struct {
pwr_tTime time;
pwr_tUInt32 size;
} body[2]; /**< bodies */
} db_sObject;
class wb_db_txn;
class wb_db_ohead;
class wb_destination;
class wb_db : public wb_import
{
public:
pwr_tVid m_vid;
DbEnv *m_env;
Db *m_ohead;
Db *m_obody;
Db *m_class;
Db *m_name;
Db *m_info;
wb_db_txn *m_txn;
//wb_db_ohead m_ohead;
public:
wb_db();
//~wb_db();
pwr_tOid new_oid(wb_db_txn *txn);
void close();
void open(char *name);
int del_family(wb_db_txn *txn, Dbc *cp, pwr_tOid poid);
wb_db_txn *begin(wb_db_txn *txn);
bool commit(wb_db_txn *txn);
bool abort(wb_db_txn *txn);
void adopt(wb_db_txn *txn, wb_db_ohead &o, wb_destination &dest);
void unadopt(wb_db_txn *txn, wb_db_ohead &o);
bool deleteFamily(pwr_tStatus *sts, wb_db_ohead *o);
//bool deleteOset(pwr_tStatus *sts, wb_oset *o);
bool importVolume(wb_export *e);
bool importHead(pwr_tOid oid, pwr_tCid cid, pwr_tOid poid, pwr_tOid boid, pwr_tOid aoid, pwr_tOid foid,
pwr_tOid loid, pwr_tObjName name, pwr_tObjName normname, pwr_tTime ohTime,
pwr_tTime rbTime, pwr_tTime dbTime, size_t rbSize, size_t dbSize);
bool importRbody(pwr_tOid oid, size_t size, void *body);
bool importDbody(pwr_tOid oid, size_t size, void *body);
bool importMeta(const char *fileName);
};
class wb_db_ohead
{
public:
db_sObject m_o;
wb_db *m_db;
Dbt m_key;
Dbt m_data;
wb_db_ohead();
wb_db_ohead(wb_db *db);
wb_db_ohead(wb_db *db, pwr_tOid oid);
wb_db_ohead(wb_db *db, wb_db_txn *txn, pwr_tOid oid);
wb_db_ohead(wb_db *db, pwr_tOid oid, pwr_tCid cid,
pwr_tOid poid, pwr_tOid boid, pwr_tOid aoid, pwr_tOid foid, pwr_tOid loid,
pwr_tObjName name, pwr_tObjName normname,
pwr_tTime ohTime, pwr_tTime rbTime, pwr_tTime dbTime,
size_t rbSize, size_t dbSize);
wb_db_ohead &get(wb_db_txn *txn);
wb_db_ohead &get(pwr_tOid oid);
wb_db_ohead &get(wb_db_txn *txn, pwr_tOid oid);
void put(wb_db_txn *txn);
void del(wb_db_txn *txn);
pwr_tOid oid() { return m_o.oid;}
pwr_tCid cid() { return m_o.cid;}
pwr_tOid poid() { return m_o.poid;}
pwr_tOid foid() { return m_o.foid;}
pwr_tOid loid() { return m_o.loid;}
pwr_tOid boid() { return m_o.boid;}
pwr_tOid aoid() { return m_o.aoid;}
char *name();
void name(wb_name &name);
void poid(pwr_tOid oid) { m_o.poid = oid;}
void foid(pwr_tOid oid) { m_o.foid = oid;}
void loid(pwr_tOid oid) { m_o.loid = oid;}
void boid(pwr_tOid oid) { m_o.boid = oid;}
void aoid(pwr_tOid oid) { m_o.aoid = oid;}
void clear();
};
class wb_db_name
{
public:
struct
{
pwr_tOid poid;
pwr_tObjName normname;
} m_k;
struct
{
pwr_tOid oid;
//pwr_tCid cid; // saved here to optimize tree traversal
//pwr_mClassDef flags; // saved here to optimize tree traversal
} m_d;
wb_db *m_db;
Dbt m_key;
Dbt m_data;
wb_db_name(wb_db *db, wb_db_txn *txn);
wb_db_name(wb_db *db, wb_db_ohead &o);
wb_db_name(wb_db *db, pwr_tOid, char *name);
wb_db_name(wb_db *db, pwr_tOid poid, const char *name);
wb_db_name(wb_db *db, wb_db_txn *txn, pwr_tOid poid, wb_name name);
void get(wb_db_txn *txn);
void put(wb_db_txn *txn);
void del(wb_db_txn *txn);
void name(wb_name &name);
pwr_tOid oid() { return m_d.oid;}
};
class wb_db_class
{
public:
struct
{
pwr_tCid cid;
pwr_tOid oid;
} m_k;
wb_db *m_db;
Dbt m_key;
Dbc *m_dbc;
wb_db_class(wb_db *db, pwr_tCid cid);
wb_db_class(wb_db *db, pwr_tCid cid, pwr_tOid oid);
wb_db_class(wb_db *db, wb_db_ohead &o);
wb_db_class(wb_db *db, wb_db_txn *txn, pwr_tCid cid);
~wb_db_class();
bool succ(pwr_tOid oid);
bool pred(pwr_tOid oid);
void put(wb_db_txn *txn);
void del(wb_db_txn *txn);
pwr_tCid cid() { return m_k.cid;}
pwr_tOid oid() { return m_k.oid;}
};
class wb_db_body
{
public:
wb_db_body(wb_db *db, pwr_tOid oid, size_t size, void *p);
void put(wb_db_txn *txn);
};
class wb_db_txn : public DbTxn
{
};
#endif
#ifndef wb_export_h
#define wb_export_h
class wb_import;
class wb_export
{
public:
virtual bool exportVolume(wb_import &e) = 0;
virtual bool exportHead(wb_import &e) = 0;
virtual bool exportRbody(wb_import &e) = 0;
virtual bool exportDbody(wb_import &e) = 0;
virtual bool exportMeta(wb_import &e) = 0;
};
#endif
#ifndef wb_import_h
#define wb_import_h
#include "co_dbs.h"
class wb_export;
class wb_import
{
public:
virtual bool importVolume(wb_export &e) = 0;
virtual bool importHead(pwr_tOid oid, pwr_tCid cid, pwr_tOid poid,
pwr_tOid boid, pwr_tOid aoid, pwr_tOid foid, pwr_tOid loid,
pwr_tObjName name, pwr_tObjName normname,
pwr_tTime ohTime, pwr_tTime rbTime, pwr_tTime dbTime,
size_t rbSize, size_t dbSize) = 0;
virtual bool importRbody(pwr_tOid oid, size_t size, void *body) = 0;
virtual bool importDbody(pwr_tOid oid, size_t size, void *body) = 0;
virtual bool importMeta(dbs_sEnv *ep) = 0;
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment