00001 #ifndef LUX_STORAGE_VILLABTREE_H
00002 #define LUX_STORAGE_VILLABTREE_H
00003
00004 #include "lux/lux.h"
00005 #include "lux/util.h"
00006 #include "data_unit.h"
00007 #include <villa.h>
00008 #include <cassert>
00009 #include <iostream>
00010 #include <string>
00011
00012 namespace Lux {
00013
00014 class VillaBtree {
00015
00016 public:
00017 VillaBtree()
00018 {}
00019
00020 VillaBtree(const char *filename, db_flags_t open_flags)
00021 {
00022 this->open(filename, open_flags);
00023 }
00024
00025 VillaBtree(std::string filename, db_flags_t open_flags)
00026 {
00027 this->open(filename.c_str(), open_flags);
00028 }
00029
00030 bool open(const char *filename, db_flags_t open_flags)
00031 {
00032 assert(filename);
00033 int oflags = 0;
00034 if (open_flags & DB_RDONLY) {
00035 oflags = VL_OREADER;
00036 }
00037 if (open_flags & DB_RDWR) {
00038 oflags = VL_OWRITER;
00039 }
00040 if (open_flags & DB_CREAT) {
00041 oflags = VL_OWRITER | VL_OCREAT;
00042 mkdir(filename);
00043 }
00044 if (open_flags & DB_TRUNC) {
00045 oflags |= VL_OTRUNC;
00046 }
00047 if (!(villa_ = vlopen(filename, oflags, VL_CMPLEX))) {
00048 return false;
00049 }
00050 return true;
00051 }
00052
00053 bool close()
00054 {
00055 if (!vlclose(villa_)) {
00056 return false;
00057 }
00058 villa_ = NULL;
00059 return true;
00060 }
00061
00062 bool put(LuxDataUnit &key, LuxDataUnit &val)
00063 {
00064 return __put(key, val, DP_DOVER);
00065 }
00066
00067 bool put(LuxDataUnit *key, LuxDataUnit *val)
00068 {
00069 return put(*key, *val);
00070 }
00071
00072 bool append(LuxDataUnit &key, LuxDataUnit &val)
00073 {
00074 return __put(key, val, DP_DCAT);
00075 }
00076
00077 bool append(LuxDataUnit *key, LuxDataUnit *val)
00078 {
00079 return append(*key, *val);
00080 }
00081
00082 bool get(LuxDataUnit &key, LuxDataUnit &val)
00083 {
00084 assert(key.get_data());
00085 int val_size;
00086 void *val_data;
00087
00088 val_data = vlget(villa_, static_cast<const char *>(key.get_data()),
00089 key.get_size(), &(val_size));
00090
00091 if (val_data == NULL) {
00092 return false;
00093 }
00094
00095 val.set_cleanup_needed(true);
00096 val.set_data(val_data);
00097 val.set_size(static_cast<unsigned int>(val_size));
00098
00099 return true;
00100 }
00101
00102 bool get(LuxDataUnit *key, LuxDataUnit *val)
00103 {
00104 return get(*key, *val);
00105 }
00106
00107 bool del(LuxDataUnit &key)
00108 {
00109 assert(key.get_data());
00110 if (vlout(villa_, static_cast<const char *>(key.get_data()),
00111 key.get_size())) {
00112 return true;
00113 }
00114 return false;
00115 }
00116
00117 bool del(LuxDataUnit *key)
00118 {
00119 return del(*key);
00120 }
00121
00122 protected:
00123 ~VillaBtree()
00124 {
00125 if (villa_ != NULL) {
00126 if (vlclose(villa_)) {
00127 villa_ = NULL;
00128 }
00129 }
00130 }
00131
00132 private:
00133 VILLA *villa_;
00134
00135 bool __put(LuxDataUnit &key, LuxDataUnit &val, int mode)
00136 {
00137 assert(key.get_data() && val.get_data());
00138 if (vlput(villa_,
00139 static_cast<const char *>(key.get_data()),
00140 key.get_size(),
00141 static_cast<const char *>(val.get_data()),
00142 val.get_size(),
00143 mode)) {
00144 return true;
00145 }
00146 return false;
00147 }
00148 };
00149 }
00150
00151 #endif