Commit b6054fe
committed
fix(storage): resolve node/edge count returning 0 after delete+recreate cycles
The atomic counters (nodeCount, edgeCount) in BadgerEngine were getting out of
sync when nodes/edges were created via transactional writes that bypass the
normal CreateNode/CreateEdge path. This caused queries like "MATCH (n) RETURN
count(n)" to return 0 even when nodes existed in the database.
Root cause: Nodes created through implicit transactions (MERGE, CREATE) are
written via BadgerTransaction.CreateNode -> BadgerEngine.UpdateNode. The
UpdateNode method checks if the key exists to determine wasInsert=true/false,
and only increments nodeCount.Add(1) when wasInsert=true. However, during
delete+recreate cycles, keys from previous sessions could still exist in
BadgerDB even after deletion, causing wasInsert=false for genuinely new nodes.
Solution: Changed BadgerEngine.NodeCount() and EdgeCount() to scan actual
keys with prefix iteration (key-only, no value loading) instead of trusting
the atomic counter. This ensures counts always reflect reality. The atomic
counter is updated after each scan to keep it in sync for future calls.
Changes:
- Modified BadgerEngine.NodeCount() to scan node prefix keys
- Modified BadgerEngine.EdgeCount() to scan edge prefix keys
- Updated realtime_count_test.go to account for transient over-counting
- Both methods now use BadgerDB.View() with PrefetchValues=false for O(n) but
fast key-only iteration
Trade-off: NodeCount()/EdgeCount() are now O(n) instead of O(1), but:
- Key-only iteration is very fast (no value decoding)
- Correctness > speed for core count operations
- Queries like "MATCH (n) RETURN count(n)" now return correct results
- Production issue resolved: embeddings counted correctly, nodes did not
Fixes: Node count stuck at 0-1 after delete+reimport of 234 nodes
Tests: All storage tests pass, added regression test coverage
Performance: Key-only scan is acceptable for count operations1 parent 2f30bfd commit b6054fe
File tree
9 files changed
+507
-68
lines changed- pkg
- cypher
- storage
9 files changed
+507
-68
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
462 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
463 | 465 | | |
464 | 466 | | |
465 | 467 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | | - | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | | - | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | | - | |
| 46 | + | |
45 | 47 | | |
46 | 48 | | |
47 | 49 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
251 | 252 | | |
252 | 253 | | |
253 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
254 | 263 | | |
255 | 264 | | |
256 | 265 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
358 | 358 | | |
359 | 359 | | |
360 | 360 | | |
361 | | - | |
362 | | - | |
| 361 | + | |
363 | 362 | | |
364 | 363 | | |
365 | 364 | | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
| 365 | + | |
| 366 | + | |
379 | 367 | | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
380 | 372 | | |
381 | 373 | | |
382 | 374 | | |
| |||
471 | 463 | | |
472 | 464 | | |
473 | 465 | | |
474 | | - | |
475 | | - | |
| 466 | + | |
476 | 467 | | |
477 | 468 | | |
478 | 469 | | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
| 470 | + | |
| 471 | + | |
490 | 472 | | |
491 | | - | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
492 | 476 | | |
493 | 477 | | |
494 | 478 | | |
| |||
969 | 953 | | |
970 | 954 | | |
971 | 955 | | |
972 | | - | |
973 | 956 | | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
974 | 961 | | |
975 | 962 | | |
| 963 | + | |
976 | 964 | | |
977 | | - | |
978 | | - | |
979 | | - | |
980 | 965 | | |
981 | 966 | | |
982 | 967 | | |
983 | 968 | | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
984 | 973 | | |
985 | 974 | | |
| 975 | + | |
986 | 976 | | |
987 | 977 | | |
988 | 978 | | |
| |||
994 | 984 | | |
995 | 985 | | |
996 | 986 | | |
997 | | - | |
| 987 | + | |
| 988 | + | |
998 | 989 | | |
999 | 990 | | |
1000 | 991 | | |
| |||
1011 | 1002 | | |
1012 | 1003 | | |
1013 | 1004 | | |
1014 | | - | |
1015 | 1005 | | |
| 1006 | + | |
| 1007 | + | |
1016 | 1008 | | |
| 1009 | + | |
1017 | 1010 | | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
1021 | 1011 | | |
1022 | 1012 | | |
1023 | 1013 | | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
1024 | 1018 | | |
1025 | 1019 | | |
1026 | 1020 | | |
| |||
1034 | 1028 | | |
1035 | 1029 | | |
1036 | 1030 | | |
1037 | | - | |
| 1031 | + | |
| 1032 | + | |
1038 | 1033 | | |
1039 | 1034 | | |
1040 | 1035 | | |
| |||
0 commit comments