13 #ifdef HAVE_LIBMEMCACHED
19 #include <memcached.h>
21 #include <opencog/util/platform.h>
31 using namespace opencog;
33 AtomCache::AtomCache(
const std::string
server,
int portno)
36 mc = memcached_create(NULL);
38 memcached_server_st *servers;
39 const char *servername = server.c_str();
40 servers = memcached_server_list_append(NULL, (
char *) servername, portno, &rc);
42 connect_status = memcached_server_push(mc, servers);
44 memcached_server_list_free(servers);
49 AtomCache::~AtomCache()
56 #define CHECK_RC(rc) \
57 if(MEMCACHED_SUCCESS != rc) \
59 fprintf(stderr, "Error: memcachedb: %s\n", memcached_strerror(mc, rc)); \
63 void AtomCache::storeAtom(
Atom *
atom)
72 int rootlen = snprintf(keybuff, KBSIZE,
"%lu/", h);
73 char *p = &keybuff[rootlen];
82 int vlen = snprintf(valbuff, VBSIZE,
"%d", t);
84 rc = memcached_set (mc, keybuff, rootlen+4, valbuff, vlen, 0, 0);
92 const char *name = n->
getName().c_str();
94 rc = memcached_set (mc, keybuff, rootlen+4, name, vlen, 0, 0);
102 vlen = snprintf(valbuff, VBSIZE,
"(%d", arity);
105 for (
int i=0; i<arity; i++)
107 vlen += snprintf(valbuff+vlen, VBSIZE-vlen,
", %lu", out[i]);
110 vlen += snprintf(valbuff+vlen, VBSIZE-vlen,
")");
112 rc = memcached_set (mc, keybuff, rootlen+5, valbuff, vlen, 0, 0);
121 fprintf(stderr,
"Error: non-simple truth values are not handled\n");
125 vlen = snprintf(valbuff, VBSIZE,
"(%20.16g, %20.16g)", tv.
getMean(), tv.
getCount());
127 rc = memcached_set (mc, keybuff, rootlen+3, valbuff, vlen, 0, 0);
133 #define NCHECK_RC(rc, val) \
134 if(MEMCACHED_SUCCESS != rc) \
136 fprintf(stderr, "Error: memcachedb: %s\n", memcached_strerror(mc, rc)); \
137 if (val) free(val); \
148 char keybuff[KBSIZE];
149 int rootlen = snprintf(keybuff, KBSIZE,
"%lu/", h);
150 char *p = &keybuff[rootlen];
153 Atom *atom = TLB::getAtom(h);
159 val = memcached_get(mc, keybuff, rootlen+4, &vlen, &flags, &rc);
161 int atype = atoi(val);
168 val = memcached_get(mc, keybuff, rootlen+4, &vlen, &flags, &rc);
170 atom =
new Node(atype, val);
177 val = memcached_get(mc, keybuff, rootlen+5, &vlen, &flags, &rc);
180 int arity = atoi(val+1);
181 std::vector<Handle> outvec;
182 outvec.resize(arity);
184 char *comma = strchr(val+2,
',');
185 for (
int i=0; i<arity; i++)
190 atom =
new Link(atype, outvec);
199 fprintf(stderr,
"Error: non-simple truth values are not handled\n");
204 val = memcached_get(mc, keybuff, rootlen+3, &vlen, &flags, &rc);
207 double mean = atof(val+1);
208 char *comma = strchr(val+2,
',');
209 double count = atof(comma+1);
218 void AtomCache::load_list(
AtomTable &table,
int depth)
225 char keybuff[KBSIZE];
226 size_t klen = snprintf(keybuff, KBSIZE,
"depth-list-%d", depth);
227 val = memcached_get(mc, keybuff, klen, &vlen, &flags, &rc);
229 printf(
"duude depth=%d nodelist len=%d\n", depth, vlen);
230 if ((vlen == 0) || (NULL == val))
return;
232 unsigned long ilc = load_count;
239 if (load_count%1000 == 0)
241 fprintf(stderr,
"\tLoaded %lu atoms.\n", load_count);
246 fprintf(stderr,
"\tLoaded %lu atoms of depth %d.\n", load_count- ilc, depth);
252 for (
int i=0; i<MAX_LATTICE_DEPTH; i++)
260 int AtomCache::depth(
Atom *atom)
263 if (NULL == l)
return 0;
269 for (
int i=0; i<arity; i++)
272 int d = depth(TLB::getAtom(h));
273 if (maxd < d) maxd = d;
280 bool AtomCache::store_cb(
Atom *atom)
286 snprintf(hbuff, KBSIZE,
"%lu, ", h);
289 depth_list[d] += hbuff;
290 if (maxdepth < d) maxdepth = d;
295 if (store_count%1000 == 0)
297 fprintf(stderr,
"\tStored %lu atoms.\n", store_count);
302 void AtomCache::store(
const AtomTable &table)
308 for (i=0; i<MAX_LATTICE_DEPTH; i++)
313 table.foreach_atom(&AtomCache::store_cb,
this);
316 for (i=0; i<maxdepth; i++)
318 depth_list[i] +=
")";
320 char keybuff[KBSIZE];
321 size_t klen = snprintf(keybuff, KBSIZE,
"depth-list-%d", i);
322 rc = memcached_set (mc, keybuff, klen, depth_list[i].c_str(), depth_list[i].size(), 0, 0);
const HandleSeq & getOutgoingSet() const
virtual count_t getCount() const =0
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
void setTruthValue(TruthValuePtr)
Sets the TruthValue object of the atom.
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
const std::string & getName() const
TruthValuePtr getTruthValue()
unsigned short Type
type of Atoms, represented as short integer (16 bits)
virtual strength_t getMean() const =0