26 #include <sys/types.h> 40 void copy_ptr(
void *d,
size_t sz) {packraw((
char*)d,sz);}
46 Datum& operator<<(
const T& x) {pack(*
this,
"",const_cast<T&>(x));
return *
this;}
48 Datum& operator>>(T& x) {unpack(*
this,
"",x);
return *
this;}
50 template <
class T>
Datum& operator=(
const T& x){reseti(); pack(*
this,
"",const_cast<T&>(x));
return *
this;}
51 template <
class T>
operator T() {T x; reseto(); unpack(*
this,
"",x);
return x;}
62 void operator=(
const Db& x);
66 void open(
const char* filename,rw readwrite);
75 bool opened()
const {
return db!=NULL;}
76 Db(
const char* f,rw rdwr): db(NULL), dbc(NULL) {open(f,rdwr);}
82 template <
class key,
class val>
struct base_map:
public std::map<key,val>
84 void set(
const key& k,
Datum& v)
85 {v>>std::map<key,val>::operator[](k);}
86 val&
get(
const key& k) {
return std::map<key,val>::operator[](k);}
91 template <
class U,
class V>
94 bool operator()(
const std::pair<U,V>& x,
const std::pair<U,V>& y) {
return x.second<y.second;}
104 if (
size() < limit || x>*begin()) std::set<int>::insert(x);
105 if (
size() >= limit) erase(begin());
112 template<
class key,
class val>
118 classdesc::string filename;
119 typedef std::map<key,size_t> TSMap;
126 cachedDBM_base(): ts(1), max_elem(std::numeric_limits<int>::max()),
128 void init(
const char *fname,
char mode=
'w')
130 db.reset(
new Db((
char*)fname, (mode==
'w')? Db::write: Db::read));
132 if (!db->opened())
throw error(
"DBM file %s open failed",fname);
136 char *fname=args, *mode=args;
140 void close() {
if (db) {commit(); db->close(); db.reset();} clear();}
142 bool opened()
const {
return bool(db);}
143 bool load(
const key& k)
148 if (db && !db->fetch(dk,vv))
160 if (this->count(k))
return true;
165 val& operator[] (
const key& k)
168 if (Base::size()>=max_elem) {commit();}
172 Base::insert(std::make_pair(k,val()));
189 return operator[](args[0].get<key>())=args[1].get<val>();
190 else if (args.count==1)
191 return operator[](args[0].get<key>());
206 typename Base::iterator i, j;
209 for (
typename TSMap::const_iterator t=timestamp.begin(); t!=timestamp.end(); ++t)
211 size_t cut_time=sum_ts/2;
213 for (i=Base::begin(); i!=Base::end(); )
215 k=i->first; v=i->second;
218 if (readonly && db->fetch(k,v))
223 else if (!readonly && v.
data())
227 typename TSMap::iterator ts=timestamp.find(i->first);
230 if (ts->second<cut_time)
239 for (
typename TSMap::iterator t=timestamp.begin(); t!=timestamp.end(); ++t)
247 if (db && db->opened())
267 if (!(last=db->firstkey(kk,vv)))
279 if (!(last=db->nextkey(kk,vv)))
285 bool eof()
const {
return last || !db;}
289 ::pack(b,
"",filename);
290 ::pack(b,
"",readonly);
291 ::pack(b,
"",static_cast<Base&>(*
this));
293 classdesc::string nullstring;
294 ::pack(b,
"",nullstring);
295 ::pack(b,
"",static_cast<Base&>(*
this));
300 classdesc::string fname;
302 ::unpack(b,
"",filename);
304 ::unpack(b,
"",readonly);
305 init(filename.c_str(),readonly?
'r':
'w');
307 ::unpack(b,
"",static_cast<Base&>(*
this));
314 std::pair<key,val> keyValue;
318 if ((db.get()->*op)(k,v))
338 {
return (!db && !x.db) || (db && x.db && keyValue.first==x.keyValue.first);}
340 {
return !operator==(x);}
341 const std::pair<key, val> operator*()
const {
return keyValue;}
342 const std::pair<key, val>* operator->()
const {
return &keyValue;}
355 const key operator*()
const {
return KeyValueIterator::operator*().first;}
356 const key* operator->()
const 357 {
return &KeyValueIterator::operator*().first;}
375 template<
class key,
class val>
380 cachedDBM(
const char* f,
char mode=
'w'){this->init(f,mode);}
389 operator const char*()
const {
return c_str();}
391 {std::string::operator=(s);
return *
this;}
401 cachedDBM(
const char* f,
char mode=
'w'){this->init(f,mode);}
409 cachedDBM(
const char* f,
char mode=
'w'){this->init(f,mode);}
417 cachedDBM(
const char* f,
char mode=
'w'){init(f,mode);}
421 #pragma omit pack ecolab::Datum 422 #pragma omit unpack ecolab::Datum 423 #pragma omit pack ecolab::cachedDBM 424 #pragma omit unpack ecolab::cachedDBM 425 #pragma omit pack ecolab::cachedDBM_base 426 #pragma omit unpack ecolab::cachedDBM_base 428 #pragma omit pack ecolab::Db 429 #pragma omit unpack ecolab::Db 431 #pragma omit unpack Db 452 size_t size; unpack(t,
"",size);
459 size_t size; unpack(t,
"",size);
465 template <
class K,
class V>
473 template <
class K,
class V>
483 #pragma omit TCL_obj ecolab::base_map 485 #include "cachedDBM.cd" descriptor access to a class's privates
Definition: omp_rw_lock.h:87
Definition: omp_rw_lock.h:92
bool firstkey(ecolab::Datum &key, ecolab::Datum &val) const
KeyValueIterator()
initialises to an end() iterator
Definition: cachedDBM.h:328
Definition: cachedDBM.h:35
bool key_exists(const key &k) const
returns true if key is in data base or added with [] operator
Definition: cachedDBM.h:158
Definition: cachedDBM.h:387
EcoLab exception class.
Definition: error.h:25
iterator type for iterating over keys
Definition: cachedDBM.h:311
Definition: cachedDBM.h:57
size_t size() const
size of buffer
Definition: TCL_obj_base.h:154
Definition: cachedDBM.h:82
void copy_ptr(void *d, size_t sz)
Definition: cachedDBM.h:40
class to allow access to private members
Definition: classdesc_access.h:21
TCL_obj support for STL containers.
A read/write lock pattern for OpenMP.
Definition: cachedDBM.h:360
const char * data() const
actual buffer
Definition: TCL_obj_base.h:152
val elem(TCL_args args)
Definition: cachedDBM.h:186
class to allow access to private members
Definition: classdesc_access.h:22
#define CLASSDESC_ACCESS(type)
add friend statements for each accessor function
Definition: classdesc_access.h:36
KeyValueIterator(const string &fname)
initialises to a begin() iterator of database fname
Definition: cachedDBM.h:330
Definition: cachedDBM.h:97
void commit()
write any changes out to the file, and clear some of the cache
Definition: cachedDBM.h:203
Represent arguments to TCL commands.
Definition: TCL_obj_base.h:138
Keys keys
access an iterator range of keys [keys.begin()...keys.end())
Definition: cachedDBM.h:370
Definition: omp_rw_lock.h:86
void clear()
clear the cache
Definition: cachedDBM.h:200
bool eof() const
true if no further keys remain when iterating
Definition: cachedDBM.h:285
persistent map
Definition: cachedDBM.h:376
const char * data() const
actual buffer
Definition: pack_base.h:152
Definition: cachedDBM.h:92
implementation of cacheDBM common to all specialisations
Definition: cachedDBM.h:113
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
Definition: pack_base.h:124
Definition: TCL_obj_base.h:124
_OPENMP
Definition: accessor.h:16
size_t cacheSize() const
number of elements in cache
Definition: cachedDBM.h:197
size_t pos() const
position of read pointer
Definition: pack_base.h:155
key nextkey() const
advance to next key in database
Definition: cachedDBM.h:273
key firstkey() const
obtain first key for iteration through database.
Definition: cachedDBM.h:261
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55
Definition: cachedDBM.h:350
void del(key k)
delete entry associated with key k
Definition: cachedDBM.h:245