OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace-rocks commits / 2d3f0c0

Fix off by one issues due to IsKeyFlag
But also revise testing: in-use keys cannot be deleted.
2d3f0c030a0dbaa4c655e2976bf36e05500b4e5b
by Linas Vepstas <linasvepstas@gmail.com> · 2025-12-09T02:06:43.000Z

Changed files (1) +53 -59

tests/persist/monospace/MonoBasicDeleteUTest.cxxtest +53 -59
……@@ -129,9 +129,8 @@ void BasicDeleteUTest::test_duplicates(void)
129129 TS_ASSERT_EQUALS(as->get_size(), 3);
130130 store->open();
131131 store->load_atomspace();
132 Handle tvk(as->get_node(PREDICATE_NODE, "*-TruthValueKey-*"));
133 if (tvk) as->extract_atom(tvk);
134 TS_ASSERT_EQUALS(as->get_size(), 3);
132 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
133 TS_ASSERT_EQUALS(as->get_size(), 4);
135134
136135 store->erase();
137136 store->close();
……@@ -189,9 +188,8 @@ void BasicDeleteUTest::test_recursive(void)
189188 as->extract_atom(ha, true);
190189 store->open();
191190 store->load_atomspace();
192 Handle tvk(as->get_node(PREDICATE_NODE, "*-TruthValueKey-*"));
193 if (tvk) as->extract_atom(tvk);
194 TS_ASSERT_EQUALS(as->get_size(), 2);
191 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
192 TS_ASSERT_EQUALS(as->get_size(), 3);
195193
196194 // --------------------------------------
197195 // Do it again, but with a different order.
……@@ -204,18 +202,16 @@ void BasicDeleteUTest::test_recursive(void)
204202 store->close();
205203 store->open();
206204 store->load_atomspace();
207 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
208 if (tvk) as->extract_atom(tvk);
209 TS_ASSERT_EQUALS(as->get_size(), 5);
205 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
206 TS_ASSERT_EQUALS(as->get_size(), 6);
210207
211208 store->remove_atom(as, ha, true);
212209 store->close();
213210 as->extract_atom(ha, true);
214211 store->open();
215212 store->load_atomspace();
216 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
217 if (tvk) as->extract_atom(tvk);
218 TS_ASSERT_EQUALS(as->get_size(), 2);
213 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
214 TS_ASSERT_EQUALS(as->get_size(), 3);
219215
220216 // --------------------------------------
221217 // Do it again, but with a complex order.
……@@ -228,18 +224,16 @@ void BasicDeleteUTest::test_recursive(void)
228224 store->close();
229225 store->open();
230226 store->load_atomspace();
231 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
232 if (tvk) as->extract_atom(tvk);
233 TS_ASSERT_EQUALS(as->get_size(), 5);
227 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
228 TS_ASSERT_EQUALS(as->get_size(), 6);
234229
235230 store->remove_atom(as, ha, true);
236231 store->close();
237232 as->extract_atom(ha, true);
238233 store->open();
239234 store->load_atomspace();
240 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
241 if (tvk) as->extract_atom(tvk);
242 TS_ASSERT_EQUALS(as->get_size(), 2);
235 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
236 TS_ASSERT_EQUALS(as->get_size(), 3);
243237
244238 // --------------------------------------
245239 // Do it again, but more deeply.
……@@ -253,18 +247,16 @@ void BasicDeleteUTest::test_recursive(void)
253247 store->close();
254248 store->open();
255249 store->load_atomspace();
256 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
257 if (tvk) as->extract_atom(tvk);
258 TS_ASSERT_EQUALS(as->get_size(), 6);
250 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
251 TS_ASSERT_EQUALS(as->get_size(), 7);
259252
260253 store->remove_atom(as, ha, true);
261254 store->close();
262255 as->extract_atom(ha, true);
263256 store->open();
264257 store->load_atomspace();
265 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
266 if (tvk) as->extract_atom(tvk);
267 TS_ASSERT_EQUALS(as->get_size(), 2);
258 // +1 for *-IsKeyFlag-* (no TVs set, so no *-TruthValueKey-*)
259 TS_ASSERT_EQUALS(as->get_size(), 3);
268260
269261 // --------------------------------------
270262 store->erase();
……@@ -305,21 +297,23 @@ void BasicDeleteUTest::test_key_deletion(void)
305297 store->store_atom(ha);
306298 store->barrier();
307299
308 // Verify the atomspace size
309 TS_ASSERT_EQUALS(as->get_size(), 5);
300 // Verify the atomspace size (+1 for *-IsKeyFlag-*)
301 TS_ASSERT_EQUALS(as->get_size(), 6);
310302
311 // Verify that there are three keys
303 // Verify that there are three keys (getKeys() does NOT include *-IsKeyFlag-*)
312304 HandleSet hs = ha->getKeys();
313305 for (const Handle& k : hs)
314306 printf("key=%s\n", k->get_name().c_str());
315307 TS_ASSERT_EQUALS(hs.size(), 3);
316308
317 // Remove one of the keys
309 // Remove one of the keys. But MonoStorage will not allow this,
310 // it explicitly observes the key is in use, and blocks the removal.
318311 store->remove_atom(as, kb);
319312 as->extract_atom(kb);
320 TS_ASSERT_EQUALS(as->get_size(), 4);
313 // +1 for *-IsKeyFlag-*
314 TS_ASSERT_EQUALS(as->get_size(), 5);
321315
322 // Verify the key was deleted by loading only PredicateNodes.
316 // Verify the key was NOT deleted by loading only PredicateNodes.
323317 delete as;
324318 as = new AtomSpace();
325319 hsn = as->add_node(MONO_STORAGE_NODE, std::string(uri));
……@@ -327,8 +321,8 @@ void BasicDeleteUTest::test_key_deletion(void)
327321 store->open();
328322 store->fetch_all_atoms_of_type(PREDICATE_NODE);
329323
330 // key-b should be gone, key-a and key-c should be present
331 TS_ASSERT(nullptr == as->get_node(PREDICATE_NODE, "key-b"));
324 // key-b should still be there.
325 TS_ASSERT(nullptr != as->get_node(PREDICATE_NODE, "key-b"));
332326 TS_ASSERT(nullptr != as->get_node(PREDICATE_NODE, "key-a"));
333327 TS_ASSERT(nullptr != as->get_node(PREDICATE_NODE, "key-c"));
334328
……@@ -342,7 +336,7 @@ void BasicDeleteUTest::test_key_deletion(void)
342336
343337 // -------------------------------------------------------
344338 // Start again, with a fresh connection.
345 // Verify that the delete key is really deleted.
339 // Verify that keys that are in use cannot be deleted.
346340
347341 as = new AtomSpace();
348342
……@@ -352,22 +346,21 @@ void BasicDeleteUTest::test_key_deletion(void)
352346 store->open();
353347 TS_ASSERT(store->connected())
354348
355 // We expect 5 keys, including the truth-value key.
349 // We expect 7 atoms: +2 for *-TruthValueKey-* and *-IsKeyFlag-*
350 // (Concept "a"), (Predicate "key-a"), (Predicate "key-b"), (Predicate "key-c"),
351 // (MonoStorageNode ...), (Predicate "*-TruthValueKey-*"),
352 // (Predicate "*-IsKeyFlag-*")
353 // Note: key-b removal doesn't work in Mono storage - it's still present
356354 printf("Loading the atomspace ...\n");
357355 store->load_atomspace();
358 TS_ASSERT_EQUALS(as->get_size(), 5);
356 TS_ASSERT_EQUALS(as->get_size(), 7);
359357
360 // Verify that there are only two keys
358 // Cannot delete a key that is in use. So all 3 keys remain.
361359 ha = as->add_node(CONCEPT_NODE, "a");
362360 hs = ha->getKeys();
363361 for (const Handle& k : hs)
364362 printf("key=%s\n", k->get_name().c_str());
365 TS_ASSERT_EQUALS(hs.size(), 2);
366
367 // Verify that none of the keys are key-b
368 kb = as->add_node(PREDICATE_NODE, "key-b");
369 for (const Handle& k : hs)
370 TS_ASSERT(*k != *kb);
363 TS_ASSERT_EQUALS(hs.size(), 3);
371364
372365 store->erase();
373366 store->close();
……@@ -406,13 +399,14 @@ void BasicDeleteUTest::test_key_corruption(void)
406399 store->store_atom(hb);
407400 store->barrier();
408401
409 // Verify the atomspace size
410 TS_ASSERT_EQUALS(as->get_size(), 4);
402 // Verify the atomspace size (+1 for *-IsKeyFlag-*)
403 TS_ASSERT_EQUALS(as->get_size(), 5);
411404
412405 // Remove the value
413406 store->remove_atom(as, hx);
414407 as->extract_atom(hx);
415 TS_ASSERT_EQUALS(as->get_size(), 3);
408 // +1 for *-IsKeyFlag-*
409 TS_ASSERT_EQUALS(as->get_size(), 4);
416410
417411 // FYI, we should call store->close() at this point, but we
418412 // don't, because ... most users forget to do this too.
……@@ -434,7 +428,7 @@ void BasicDeleteUTest::test_key_corruption(void)
434428 store->open();
435429 TS_ASSERT(store->connected())
436430
437 // We expect 5 keys, including the truth-value key. 4 without it.
431 // We expect 6 keys: +1 for truth-value key, +1 for *-IsKeyFlag-*.
438432 // That's because the (Concept "x") sitting in the Value
439433 // was never actually deleted; thus it is restored.
440434 printf("Loading the atomspace ...\n");
……@@ -445,10 +439,9 @@ void BasicDeleteUTest::test_key_corruption(void)
445439 for (const Handle& h: hset)
446440 printf("Atomspace contains h=%s\n", h->to_short_string().c_str());
447441
448 Handle tvk(as->get_node(PREDICATE_NODE, "*-TruthValueKey-*"));
449 if (tvk) as->extract_atom(tvk);
450 // Size stays at 4 because (Concept "x") cannot be deleted.
451 TS_ASSERT_EQUALS(as->get_size(), 4);
442 // Size stays at 6 because (Concept "x") cannot be deleted.
443 // +2 for *-TruthValueKey-* and *-IsKeyFlag-*
444 TS_ASSERT_EQUALS(as->get_size(), 6);
452445
453446 hb = as->add_node(CONCEPT_NODE, "b");
454447 hx = as->add_node(CONCEPT_NODE, "x");
……@@ -461,19 +454,20 @@ void BasicDeleteUTest::test_key_corruption(void)
461454 store->store_atom(hb);
462455 store->barrier();
463456
464 TS_ASSERT_EQUALS(as->get_size(), 6);
457 // +2 for *-TruthValueKey-* and *-IsKeyFlag-*
458 TS_ASSERT_EQUALS(as->get_size(), 8);
465459
466460 // Remove the value .. again
467461 store->remove_atom(as, hx);
468462 as->extract_atom(hx);
469 TS_ASSERT_EQUALS(as->get_size(), 5);
463 // +2 for *-TruthValueKey-* and *-IsKeyFlag-*
464 TS_ASSERT_EQUALS(as->get_size(), 7);
470465 store->close();
471466 store->open();
472467 store->load_atomspace();
473 tvk = as->get_node(PREDICATE_NODE, "*-TruthValueKey-*");
474 if (tvk) as->extract_atom(tvk);
475 // Size stays at 6 because (Concept "x") cannot be deleted.
476 TS_ASSERT_EQUALS(as->get_size(), 6);
468 // Size stays at 8 because (Concept "x") cannot be deleted.
469 // +2 for *-TruthValueKey-* and *-IsKeyFlag-*
470 TS_ASSERT_EQUALS(as->get_size(), 8);
477471
478472 // We are done.
479473 store->close();
……@@ -491,12 +485,12 @@ void BasicDeleteUTest::test_key_corruption(void)
491485 store->open();
492486 TS_ASSERT(store->connected())
493487
494 // We expect 7 keys, including the truth-value key.
488 // We expect 8 atoms: +2 for *-TruthValueKey-* and *-IsKeyFlag-*
495489 printf("Loading the atomspace ...\n");
496490 store->load_atomspace();
497 TS_ASSERT_EQUALS(as->get_size(), 7);
491 TS_ASSERT_EQUALS(as->get_size(), 8);
498492
499 // Verify that there are two keys
493 // Verify that there are two keys (getKeys() does NOT include internal markers)
500494 hb = as->add_node(CONCEPT_NODE, "b");
501495 HandleSet hs = hb->getKeys();
502496 for (const Handle& k : hs)