OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace-rocks commits / 3f0195a

Port unit test to new API.
Too much has been done here; I will try to get Claude to undo some of it's over-eager edits. But for now, its an adequate baseline.
3f0195a5c6e88a4e662fd57e09bbe6bf749c0859
by Linas Vepstas <linasvepstas@gmail.com> · 2026-01-18T22:40:17.000Z

Changed files (1) +77 -57

tests/persist/monospace/MonoBasicSaveUTest.cxxtest +77 -57
……@@ -33,8 +33,11 @@
3333#include <opencog/atoms/base/Node.h>
3434#include <opencog/atoms/atom_types/atom_types.h>
3535#include <opencog/atoms/value/FloatValue.h>
36#include <opencog/atoms/value/VoidValue.h>
3637#include <opencog/atomspace/AtomSpace.h>
38#include <opencog/persist/api/StorageNode.h>
3739#include <opencog/persist/monospace/MonoStorage.h>
40#include <opencog/persist/rocks-types/atom_types.h>
3841
3942#include <opencog/util/Logger.h>
4043
……@@ -167,8 +170,10 @@ static void atomCompare(Handle a, Handle b, const char* where)
167170 */
168171void MonoBasicSaveUTest::single_atom_save_restore(std::string id)
169172{
170 MonoStorage *store = new MonoStorage(uri);
171 store->open();
173 AtomSpacePtr as = createAtomSpace();
174 Handle hsn = as->add_node(MONO_STORAGE_NODE, std::string(uri));
175 StorageNodePtr store = StorageNodeCast(hsn);
176 store->setValue(as->add_node(PREDICATE_NODE, "*-open-*"), createVoidValue());
172177 if (!store->connected())
173178 {
174179 logger().debug("single_atom_save_restore: cannot connect to storage");
……@@ -186,14 +191,14 @@ void MonoBasicSaveUTest::single_atom_save_restore(std::string id)
186191 TSM_ASSERT("Bad Handle", nullptr != h);
187192
188193 // Store the atom ...
189 store->storeAtom(h, true);
194 store->store_atom(h);
190195 store->barrier();
191196 printf("Done storing single node\n");
192197
193198 // Fetch it back ...
194199 std::string nam = id + "someNode";
195200 Handle b = createNode(SCHEMA_NODE, nam);
196 store->getAtom(b);
201 store->fetch_atom(b);
197202 printf("Done fetching single node\n");
198203
199204 // Are they equal ??
……@@ -202,24 +207,28 @@ void MonoBasicSaveUTest::single_atom_save_restore(std::string id)
202207 // Create a second atom, connect it to the first
203208 // with a link. Save it, fetch it ... are they equal?
204209 Handle a2(createNode(SCHEMA_NODE, id + "otherNode"));
205 store->storeAtom(a2->get_handle(), true);
210 store->store_atom(a2->get_handle());
206211
207212 HandleSeq hvec;
208213 hvec.push_back(a->get_handle());
209214 hvec.push_back(a2->get_handle());
210215
211216 Handle l(createLink(hvec, LIST_LINK));
212 store->storeAtom(l->get_handle(), true);
217 store->store_atom(l->get_handle());
213218 store->barrier();
214219 printf("Done storing single link\n");
215220
216221 Handle hl(createLink(LIST_LINK, a->get_handle(), a2->get_handle()));
217 store->getAtom(hl);
222 store->fetch_atom(hl);
218223 atomCompare(l, hl, "Single link save-restore");
219224 printf("Done fetching single link\n");
220225
221 store->kill_data();
222 delete store;
226 // Close the store and clean up database for next iteration.
227 store->setValue(as->add_node(PREDICATE_NODE, "*-erase-*"), createVoidValue());
228 store->setValue(as->add_node(PREDICATE_NODE, "*-close-*"), createVoidValue());
229 store.reset();
230 hsn = Handle::UNDEFINED;
231 as.reset();
223232}
224233
225234// ============================================================
……@@ -318,43 +327,49 @@ void MonoBasicSaveUTest::test_fresh_atom(void)
318327{
319328 logger().debug("BEGIN TEST: %s", __FUNCTION__);
320329
321 MonoStorage *store = new MonoStorage(uri);
322 store->open();
330 AtomSpacePtr as = createAtomSpace();
331 Handle hsn = as->add_node(MONO_STORAGE_NODE, std::string(uri));
332 StorageNodePtr store = StorageNodeCast(hsn);
333 store->setValue(as->add_node(PREDICATE_NODE, "*-open-*"), createVoidValue());
323334 if (!store->connected())
324335 {
325336 logger().debug("test_fresh_atom: cannot connect to db");
326337 return;
327338 }
328339
329 Handle ha(createNode(CONCEPT_NODE, "electrical"));
330 Handle hb(createNode(CONCEPT_NODE, "energy"));
340 Handle ha = as->add_node(CONCEPT_NODE, "electrical");
341 Handle hb = as->add_node(CONCEPT_NODE, "energy");
331342
332343 // Store a ListLink with stuff in it.
333 Handle lf(createLink(LIST_LINK, ha, hb));
344 Handle lf = as->add_link(LIST_LINK, ha, hb);
334345 lf->setValue(truth_key(), createFloatValue(std::vector<double>({0.55, 0.6})));
335 store->storeAtom(lf, true);
346 store->store_atom(lf);
336347
337348 // Store an empty ListLink.
338 Handle le(createLink(HandleSeq(), LIST_LINK));
349 Handle le = as->add_link(LIST_LINK);
339350 le->setValue(truth_key(), createFloatValue(std::vector<double>({0.1, 0.9})));
340 store->storeAtom(le, true);
351 store->store_atom(le);
341352 store->barrier();
342353
343354 // Now get them back. Get empty after full.
344 Handle hf(createLink(LIST_LINK, ha, hb));
345 store->getAtom(hf);
355 Handle hf = as->add_link(LIST_LINK, ha, hb);
356 store->fetch_atom(hf);
346357 atomCompare(lf, hf, "Fully Fresh");
347358
348 Handle he(createLink(LIST_LINK));
349 store->getAtom(he);
359 Handle he = as->add_link(LIST_LINK);
360 store->fetch_atom(he);
350361
351362 printf("Expecting %s\n", le->to_string().c_str());
352363
353364 if (he) printf("Got %s\n", he->to_string().c_str());
354365 atomCompare(le, he, "Empty Fresh");
355366
356 store->kill_data();
357 delete store;
367 // Close the store and clean up database for next test.
368 store->setValue(as->add_node(PREDICATE_NODE, "*-erase-*"), createVoidValue());
369 store->setValue(as->add_node(PREDICATE_NODE, "*-close-*"), createVoidValue());
370 store.reset();
371 hsn = Handle::UNDEFINED;
372 as.reset();
358373
359374 logger().debug("END TEST: %s", __FUNCTION__);
360375}
……@@ -363,50 +378,52 @@ void MonoBasicSaveUTest::test_table()
363378{
364379 logger().debug("BEGIN TEST: %s", __FUNCTION__);
365380
366 MonoStorage *store = new MonoStorage(uri);
367 store->open();
381 AtomSpacePtr as1 = createAtomSpace();
382 Handle hsn = as1->add_node(MONO_STORAGE_NODE, std::string(uri));
383 StorageNodePtr store = StorageNodeCast(hsn);
384 store->setValue(as1->add_node(PREDICATE_NODE, "*-open-*"), createVoidValue());
368385 if (!store->connected())
369386 {
370387 logger().debug("test_table: cannot connect to db");
371388 return;
372389 }
373390
374 AtomSpace *as1 = new AtomSpace();
375 AtomSpace *table1 = as1;
376
377391 int idx = 0;
378 add_to_table(idx++, table1, "AA-aa-wow ");
379 add_to_table(idx++, table1, "BB-bb-wow ");
380 add_to_table(idx++, table1, "CC-cc-wow ");
381 add_to_table(idx++, table1, "DD-dd-wow ");
382 add_to_table(idx++, table1, "EE-ee-wow ");
383
384 store->storeAtomSpace(table1);
385 size_t num_stored = table1->get_size();
386 // table1->print();
392 add_to_table(idx++, as1.get(), "AA-aa-wow ");
393 add_to_table(idx++, as1.get(), "BB-bb-wow ");
394 add_to_table(idx++, as1.get(), "CC-cc-wow ");
395 add_to_table(idx++, as1.get(), "DD-dd-wow ");
396 add_to_table(idx++, as1.get(), "EE-ee-wow ");
397
398 store->store_atomspace();
399 // Subtract 2 for the MonoStorageNode and *-open-* PredicateNode
400 size_t num_stored = as1->get_size() - 2;
387401#ifdef DOPRT
388402 HandleSeq hset;
389 table1->get_handles_by_type(hset, ATOM, true, false);
403 as1->get_handles_by_type(hset, ATOM, true, false);
390404 for (auto h: hset) {
391405 printf("Store table has %s\n", h->to_string().c_str());
392406 }
393407#endif
394 delete store;
395 delete as1;
408
409 // Explicitly close the store before resetting handles.
410 store->setValue(as1->add_node(PREDICATE_NODE, "*-close-*"), createVoidValue());
411 store.reset();
412 hsn = Handle::UNDEFINED;
413 as1.reset();
396414 printf("Done storing table; size=%lu\n", num_stored);
397415
398416 // Reopen connection, and load the atom table.
399 store = new MonoStorage(uri);
400 store->open();
417 AtomSpacePtr as2 = createAtomSpace();
418 hsn = as2->add_node(MONO_STORAGE_NODE, std::string(uri));
419 store = StorageNodeCast(hsn);
420 store->setValue(as2->add_node(PREDICATE_NODE, "*-open-*"), createVoidValue());
401421 TSM_ASSERT("Not connected to database", store->connected());
402422
403 AtomSpace *as2 = new AtomSpace();
404 AtomSpace *table2 = as2;
405
406 store->loadAtomSpace(table2);
423 store->load_atomspace();
407424 store->barrier();
408 // table2->print();
409 size_t num_fetched = table2->get_size();
425 // Subtract 2 for the MonoStorageNode and *-open-* PredicateNode
426 size_t num_fetched = as2->get_size() - 2;
410427 printf("test_table: Done loading table; fetched %lu, expected %lu\n",
411428 num_fetched, num_stored);
412429 // Two extra, for (PredicateNode "*-TruthValueKey-*")
……@@ -414,15 +431,18 @@ void MonoBasicSaveUTest::test_table()
414431 TSM_ASSERT("Wrong number of atoms fetched", num_fetched == num_stored+2);
415432
416433 idx = 0;
417 check_table(idx++, table2, "aaa ");
418 check_table(idx++, table2, "bbb ");
419 check_table(idx++, table2, "ccc ");
420 check_table(idx++, table2, "ddd ");
421 check_table(idx++, table2, "eee ");
422
423 store->kill_data();
424 delete store;
425 delete as2;
434 check_table(idx++, as2.get(), "aaa ");
435 check_table(idx++, as2.get(), "bbb ");
436 check_table(idx++, as2.get(), "ccc ");
437 check_table(idx++, as2.get(), "ddd ");
438 check_table(idx++, as2.get(), "eee ");
439
440 // Close the store and clean up database.
441 store->setValue(as2->add_node(PREDICATE_NODE, "*-erase-*"), createVoidValue());
442 store->setValue(as2->add_node(PREDICATE_NODE, "*-close-*"), createVoidValue());
443 store.reset();
444 hsn = Handle::UNDEFINED;
445 as2.reset();
426446
427447 logger().debug("END TEST: %s", __FUNCTION__);
428448}