OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace-storage commits / be84e38

Use a target atomspace for all operations
be84e380a5e83fa6836500c55091e23bfd8a9075
by Linas Vepstas <linasvepstas@gmail.com> · 2026-01-18T18:48:06.000Z

Changed files (2) +46 -16

opencog/persist/api/StorageNode.cc +45 -16
……@@ -39,7 +39,8 @@ template class ObjectCRTP<StorageNode>;
3939// ====================================================================
4040
4141StorageNode::StorageNode(Type t, std::string uri) :
42 ObjectCRTP<StorageNode>(t, std::move(uri))
42 ObjectCRTP<StorageNode>(t, std::move(uri)),
43 _target_as(nullptr)
4344{
4445 if (not nameserver().isA(t, STORAGE_NODE))
4546 throw RuntimeException(TRACE_INFO, "Bad inheritance!");
……@@ -124,21 +125,46 @@ void StorageNode::setValue(const Handle& key, const ValuePtr& value)
124125 const std::string& pred = key->get_name();
125126 switch (dispatch_hash(pred.c_str()))
126127 {
127 case p_open:
128 case p_open: {
128129 COLL("*-open-*");
130 ValuePtr vp(value);
131 if (vp->is_type(ATOM))
132 {
133 Handle h(HandleCast(value));
134 if (h->is_executable())
135 vp = h->execute();
136 }
137
138 if (vp->is_type(ATOM_SPACE))
139 _target_as = AtomSpaceCast(value);
140 else if (nullptr == vp or 0 == vp->size())
141 _target_as = AtomSpaceCast(getAtomSpace()->shared_from_this());
142 else
143 throw RuntimeException(TRACE_INFO,
144 "Expected *-open-* message expects AtomSpace; got %s",
145 value->to_string().c_str());
146
129147 open();
130148 return;
149 }
131150 case p_close:
132151 COLL("*-close-*");
133152 close();
153 _target_as = nullptr;
134154 return;
135155 case p_load_atomspace:
136156 COLL("*-load-atomspace-*");
137 load_atomspace(AtomSpaceCast(value).get());
157 if (nullptr == value or 0 == value->size())
158 load_atomspace(_target_as.get());
159 else
160 load_atomspace(AtomSpaceCast(value).get());
138161 return;
139162 case p_store_atomspace:
140163 COLL("*-store-atomspace-*");
141 store_atomspace(AtomSpaceCast(value).get());
164 if (nullptr == value or 0 == value->size())
165 store_atomspace(_target_as.get());
166 else
167 store_atomspace(AtomSpaceCast(value).get());
142168 return;
143169 case p_load_atoms_of_type: {
144170 COLL("*-load-atoms-of-type-*");
……@@ -186,7 +212,7 @@ void StorageNode::setValue(const Handle& key, const ValuePtr& value)
186212 }
187213 if (not value->is_type(LINK_VALUE)) return;
188214 const ValueSeq& vsq(LinkValueCast(value)->value());
189 AtomSpace* as = getAtomSpace();
215 AtomSpace* as = _target_as.get();
190216 for (const ValuePtr& vp : vsq)
191217 {
192218 if (vp->is_type (ATOM_SPACE))
……@@ -225,7 +251,7 @@ void StorageNode::setValue(const Handle& key, const ValuePtr& value)
225251 }
226252 if (not value->is_type(LINK_VALUE)) return;
227253 const ValueSeq& vsq(LinkValueCast(value)->value());
228 AtomSpace* as = getAtomSpace();
254 AtomSpace* as = _target_as.get();
229255 for (const ValuePtr& vp : vsq)
230256 {
231257 if (vp->is_type (ATOM_SPACE))
……@@ -275,7 +301,10 @@ void StorageNode::setValue(const Handle& key, const ValuePtr& value)
275301 return;
276302 case p_barrier:
277303 COLL("*-barrier-*");
278 barrier(AtomSpaceCast(value).get());
304 if (nullptr == value or 0 == value->size())
305 barrier(_target_as.get());
306 else
307 barrier(AtomSpaceCast(value).get());
279308 return;
280309 case p_store_frames:
281310 COLL("*-store-frames-*");
……@@ -335,7 +364,7 @@ ValuePtr StorageNode::getValue(const Handle& key) const
335364
336365void StorageNode::barrier(AtomSpace* as)
337366{
338 if (nullptr == as) as = getAtomSpace();
367 if (nullptr == as) as = _target_as.get();
339368 as->barrier();
340369}
341370
……@@ -372,7 +401,7 @@ void StorageNode::remove_atom(AtomSpace* as, Handle h, bool recursive)
372401Handle StorageNode::fetch_atom(const Handle& h, AtomSpace* as)
373402{
374403 if (nullptr == h) return Handle::UNDEFINED;
375 if (nullptr == as) as = getAtomSpace();
404 if (nullptr == as) as = _target_as.get();
376405
377406 // Now, get the latest values from the backing store.
378407 // The operation here is to CLOBBER the values, NOT to merge them!
……@@ -389,7 +418,7 @@ Handle StorageNode::fetch_atom(const Handle& h, AtomSpace* as)
389418Handle StorageNode::fetch_value(const Handle& h, const Handle& key,
390419 AtomSpace* as)
391420{
392 if (nullptr == as) as = getAtomSpace();
421 if (nullptr == as) as = _target_as.get();
393422 Handle lkey = as->add_atom(key);
394423 Handle lh = as->add_atom(h);
395424 loadValue(lh, lkey);
……@@ -399,7 +428,7 @@ Handle StorageNode::fetch_value(const Handle& h, const Handle& key,
399428Handle StorageNode::fetch_incoming_set(const Handle& h, bool recursive,
400429 AtomSpace* as)
401430{
402 if (nullptr == as) as = getAtomSpace();
431 if (nullptr == as) as = _target_as.get();
403432 Handle lh = as->get_atom(h);
404433 if (nullptr == lh) return lh;
405434
……@@ -418,7 +447,7 @@ Handle StorageNode::fetch_incoming_set(const Handle& h, bool recursive,
418447Handle StorageNode::fetch_incoming_by_type(const Handle& h, Type t,
419448 AtomSpace* as)
420449{
421 if (nullptr == as) as = getAtomSpace();
450 if (nullptr == as) as = _target_as.get();
422451 Handle lh = as->get_atom(h);
423452 if (nullptr == lh) return lh;
424453
……@@ -436,7 +465,7 @@ Handle StorageNode::fetch_query(const Handle& query, const Handle& key,
436465 if (not query->is_executable() and not query->is_evaluatable())
437466 throw RuntimeException(TRACE_INFO, "Not executable!");
438467
439 if (nullptr == as) as = getAtomSpace();
468 if (nullptr == as) as = _target_as.get();
440469 Handle lkey = as->add_atom(key);
441470 Handle lq = as->add_atom(query);
442471 Handle lmeta = metadata;
……@@ -448,7 +477,7 @@ Handle StorageNode::fetch_query(const Handle& query, const Handle& key,
448477
449478void StorageNode::load_atomspace(AtomSpace* as)
450479{
451 if (nullptr == as) as = getAtomSpace();
480 if (nullptr == as) as = _target_as.get();
452481 loadAtomSpace(as);
453482}
454483
……@@ -457,13 +486,13 @@ void StorageNode::load_atomspace(AtomSpace* as)
457486 */
458487void StorageNode::store_atomspace(AtomSpace* as)
459488{
460 if (nullptr == as) as = getAtomSpace();
489 if (nullptr == as) as = _target_as.get();
461490 storeAtomSpace(as);
462491}
463492
464493void StorageNode::fetch_all_atoms_of_type(Type t, AtomSpace* as)
465494{
466 if (nullptr == as) as = getAtomSpace();
495 if (nullptr == as) as = _target_as.get();
467496 loadType(as, t);
468497}
469498
opencog/persist/api/StorageNode.h +1 -0
……@@ -47,6 +47,7 @@ class StorageNode : public ObjectCRTP<StorageNode>, protected BackingStore
4747 friend class ReadWriteProxy;
4848 friend class ObjectCRTP<StorageNode>;
4949protected:
50 AtomSpacePtr _target_as;
5051
5152 // Friendship cannot be inherited in c++. So we pass through selected
5253 // functions to derived classes that will need them.