WvStreams
unicachegen.cc
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 2002 Net Integration Technologies, Inc.
00004  * 
00005  * A UniConf generator that stores keys in memory.
00006  */
00007 #include "uniconf.h"
00008 #include "unicachegen.h"
00009 #include "wvmoniker.h"
00010 #include "wvlinkerhack.h"
00011 
00012 WV_LINK(UniCacheGen);
00013 
00014 
00015 // if 'obj' is non-NULL and is a UniConfGen, wrap that; otherwise wrap the
00016 // given moniker.
00017 static IUniConfGen *creator(WvStringParm s, IObject *_obj)
00018 {
00019     return new UniCacheGen(wvcreate<IUniConfGen>(s, _obj));
00020 }
00021 
00022 static WvMoniker<IUniConfGen> reg("cache", creator);
00023 
00024 
00025 /***** UniCacheGen *****/
00026 
00027 UniCacheGen::UniCacheGen(IUniConfGen *_inner)
00028     : log("UniCache", WvLog::Debug1), inner(_inner)
00029 {
00030     if (inner)
00031         inner->add_callback(this, wv::bind(&UniCacheGen::deltacallback, this,
00032                                            _1, _2));
00033     refreshed_once = false;
00034 }
00035 
00036 
00037 UniCacheGen::~UniCacheGen()
00038 {
00039     inner->del_callback(this);
00040     WVRELEASE(inner);
00041 }
00042 
00043 
00044 bool UniCacheGen::isok()
00045 {
00046     return inner->isok();
00047 }
00048 
00049 
00050 bool UniCacheGen::refresh()
00051 {
00052     if (!refreshed_once)
00053     {
00054         bool ret = inner->refresh();
00055         loadtree();
00056         refreshed_once = true;
00057         return ret;
00058     }
00059     else
00060         return false;
00061 }
00062 
00063 
00064 void UniCacheGen::commit()
00065 {
00066     inner->commit();
00067 }
00068 
00069 
00070 void UniCacheGen::loadtree(const UniConfKey &key)
00071 {
00072     UniConfGen::Iter *i = inner->recursiveiterator(key);
00073     if (!i) return;
00074 
00075     //assert(false);
00076     for (i->rewind(); i->next(); )
00077     {
00078         WvString value(i->value());
00079         
00080         //fprintf(stderr, "Key: '%s'\n", i->key().cstr());
00081         //fprintf(stderr, "  Val: '%s'\n", value.cstr());
00082 
00083         if (!!value)
00084             UniTempGen::set(i->key(), value);
00085     }
00086 
00087     delete i;
00088 }
00089 
00090 
00091 void UniCacheGen::deltacallback(const UniConfKey &key, WvStringParm value)
00092 {
00093     UniTempGen::set(key, value);
00094 }
00095 
00096 void UniCacheGen::set(const UniConfKey &key, WvStringParm value)
00097 {
00098     inner->set(key, value);
00099 }
00100 
00101 WvString UniCacheGen::get(const UniConfKey &key)
00102 {
00103     //inner->get(key);
00104     inner->flush_buffers(); // update all pending notifications
00105     return UniTempGen::get(key);
00106 }