| … | … | @@ -33,8 +33,11 @@ |
| 33 | 33 | #include <opencog/atoms/base/Node.h> |
| 34 | 34 | #include <opencog/atoms/atom_types/atom_types.h> |
| 35 | 35 | #include <opencog/atoms/value/FloatValue.h> |
| 36 | #include <opencog/atoms/value/VoidValue.h> |
| 36 | 37 | #include <opencog/atomspace/AtomSpace.h> |
| 38 | #include <opencog/persist/api/StorageNode.h> |
| 37 | 39 | #include <opencog/persist/monospace/MonoStorage.h> |
| 40 | #include <opencog/persist/rocks-types/atom_types.h> |
| 38 | 41 | |
| 39 | 42 | #include <opencog/util/Logger.h> |
| 40 | 43 | |
| … | … | @@ -167,8 +170,10 @@ static void atomCompare(Handle a, Handle b, const char* where) |
| 167 | 170 | */ |
| 168 | 171 | void MonoBasicSaveUTest::single_atom_save_restore(std::string id) |
| 169 | 172 | { |
| 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()); |
| 172 | 177 | if (!store->connected()) |
| 173 | 178 | { |
| 174 | 179 | logger().debug("single_atom_save_restore: cannot connect to storage"); |
| … | … | @@ -186,14 +191,14 @@ void MonoBasicSaveUTest::single_atom_save_restore(std::string id) |
| 186 | 191 | TSM_ASSERT("Bad Handle", nullptr != h); |
| 187 | 192 | |
| 188 | 193 | // Store the atom ... |
| 189 | | store->storeAtom(h, true); |
| 194 | store->store_atom(h); |
| 190 | 195 | store->barrier(); |
| 191 | 196 | printf("Done storing single node\n"); |
| 192 | 197 | |
| 193 | 198 | // Fetch it back ... |
| 194 | 199 | std::string nam = id + "someNode"; |
| 195 | 200 | Handle b = createNode(SCHEMA_NODE, nam); |
| 196 | | store->getAtom(b); |
| 201 | store->fetch_atom(b); |
| 197 | 202 | printf("Done fetching single node\n"); |
| 198 | 203 | |
| 199 | 204 | // Are they equal ?? |
| … | … | @@ -202,24 +207,28 @@ void MonoBasicSaveUTest::single_atom_save_restore(std::string id) |
| 202 | 207 | // Create a second atom, connect it to the first |
| 203 | 208 | // with a link. Save it, fetch it ... are they equal? |
| 204 | 209 | Handle a2(createNode(SCHEMA_NODE, id + "otherNode")); |
| 205 | | store->storeAtom(a2->get_handle(), true); |
| 210 | store->store_atom(a2->get_handle()); |
| 206 | 211 | |
| 207 | 212 | HandleSeq hvec; |
| 208 | 213 | hvec.push_back(a->get_handle()); |
| 209 | 214 | hvec.push_back(a2->get_handle()); |
| 210 | 215 | |
| 211 | 216 | Handle l(createLink(hvec, LIST_LINK)); |
| 212 | | store->storeAtom(l->get_handle(), true); |
| 217 | store->store_atom(l->get_handle()); |
| 213 | 218 | store->barrier(); |
| 214 | 219 | printf("Done storing single link\n"); |
| 215 | 220 | |
| 216 | 221 | Handle hl(createLink(LIST_LINK, a->get_handle(), a2->get_handle())); |
| 217 | | store->getAtom(hl); |
| 222 | store->fetch_atom(hl); |
| 218 | 223 | atomCompare(l, hl, "Single link save-restore"); |
| 219 | 224 | printf("Done fetching single link\n"); |
| 220 | 225 | |
| 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(); |
| 223 | 232 | } |
| 224 | 233 | |
| 225 | 234 | // ============================================================ |
| … | … | @@ -318,43 +327,49 @@ void MonoBasicSaveUTest::test_fresh_atom(void) |
| 318 | 327 | { |
| 319 | 328 | logger().debug("BEGIN TEST: %s", __FUNCTION__); |
| 320 | 329 | |
| 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()); |
| 323 | 334 | if (!store->connected()) |
| 324 | 335 | { |
| 325 | 336 | logger().debug("test_fresh_atom: cannot connect to db"); |
| 326 | 337 | return; |
| 327 | 338 | } |
| 328 | 339 | |
| 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"); |
| 331 | 342 | |
| 332 | 343 | // 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); |
| 334 | 345 | lf->setValue(truth_key(), createFloatValue(std::vector<double>({0.55, 0.6}))); |
| 335 | | store->storeAtom(lf, true); |
| 346 | store->store_atom(lf); |
| 336 | 347 | |
| 337 | 348 | // Store an empty ListLink. |
| 338 | | Handle le(createLink(HandleSeq(), LIST_LINK)); |
| 349 | Handle le = as->add_link(LIST_LINK); |
| 339 | 350 | le->setValue(truth_key(), createFloatValue(std::vector<double>({0.1, 0.9}))); |
| 340 | | store->storeAtom(le, true); |
| 351 | store->store_atom(le); |
| 341 | 352 | store->barrier(); |
| 342 | 353 | |
| 343 | 354 | // 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); |
| 346 | 357 | atomCompare(lf, hf, "Fully Fresh"); |
| 347 | 358 | |
| 348 | | Handle he(createLink(LIST_LINK)); |
| 349 | | store->getAtom(he); |
| 359 | Handle he = as->add_link(LIST_LINK); |
| 360 | store->fetch_atom(he); |
| 350 | 361 | |
| 351 | 362 | printf("Expecting %s\n", le->to_string().c_str()); |
| 352 | 363 | |
| 353 | 364 | if (he) printf("Got %s\n", he->to_string().c_str()); |
| 354 | 365 | atomCompare(le, he, "Empty Fresh"); |
| 355 | 366 | |
| 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(); |
| 358 | 373 | |
| 359 | 374 | logger().debug("END TEST: %s", __FUNCTION__); |
| 360 | 375 | } |
| … | … | @@ -363,50 +378,52 @@ void MonoBasicSaveUTest::test_table() |
| 363 | 378 | { |
| 364 | 379 | logger().debug("BEGIN TEST: %s", __FUNCTION__); |
| 365 | 380 | |
| 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()); |
| 368 | 385 | if (!store->connected()) |
| 369 | 386 | { |
| 370 | 387 | logger().debug("test_table: cannot connect to db"); |
| 371 | 388 | return; |
| 372 | 389 | } |
| 373 | 390 | |
| 374 | | AtomSpace *as1 = new AtomSpace(); |
| 375 | | AtomSpace *table1 = as1; |
| 376 | | |
| 377 | 391 | 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; |
| 387 | 401 | #ifdef DOPRT |
| 388 | 402 | HandleSeq hset; |
| 389 | | table1->get_handles_by_type(hset, ATOM, true, false); |
| 403 | as1->get_handles_by_type(hset, ATOM, true, false); |
| 390 | 404 | for (auto h: hset) { |
| 391 | 405 | printf("Store table has %s\n", h->to_string().c_str()); |
| 392 | 406 | } |
| 393 | 407 | #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(); |
| 396 | 414 | printf("Done storing table; size=%lu\n", num_stored); |
| 397 | 415 | |
| 398 | 416 | // 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()); |
| 401 | 421 | TSM_ASSERT("Not connected to database", store->connected()); |
| 402 | 422 | |
| 403 | | AtomSpace *as2 = new AtomSpace(); |
| 404 | | AtomSpace *table2 = as2; |
| 405 | | |
| 406 | | store->loadAtomSpace(table2); |
| 423 | store->load_atomspace(); |
| 407 | 424 | 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; |
| 410 | 427 | printf("test_table: Done loading table; fetched %lu, expected %lu\n", |
| 411 | 428 | num_fetched, num_stored); |
| 412 | 429 | // Two extra, for (PredicateNode "*-TruthValueKey-*") |
| … | … | @@ -414,15 +431,18 @@ void MonoBasicSaveUTest::test_table() |
| 414 | 431 | TSM_ASSERT("Wrong number of atoms fetched", num_fetched == num_stored+2); |
| 415 | 432 | |
| 416 | 433 | 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(); |
| 426 | 446 | |
| 427 | 447 | logger().debug("END TEST: %s", __FUNCTION__); |
| 428 | 448 | } |