Skip to content

Commit

Permalink
fix a bug in saving the tree index after removal.
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Dec 11, 2024
1 parent 453265d commit bf646a3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.4
2.3.5
4 changes: 2 additions & 2 deletions lib/NGT/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,8 @@ void NGT::Command::repair(Args &args) {
}
} else {
if (removedIDs.find(id) == removedIDs.end() && id < objSize) {
std::cerr << "Not found an object in the tree. However, it might be a duplicated object. " << id
<< std::endl;
std::cerr << "Not found an object in the tree. However, it might be a duplicated same object. " << id
<< "/" << removedIDs.size() << std::endl;
uninsertedTreeObjectCount++;
if (repair) {
try {
Expand Down
39 changes: 35 additions & 4 deletions lib/NGT/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,23 @@ void NeighborhoodGraph::removeEdgesReliably(ObjectID id) {
#else
GraphNode::iterator ei = std::lower_bound(n.begin(), n.end(), obj);
if ((ei == n.end()) || ((*ei).id != obj.id)) {
n.insert(ei, obj);
insertionA = true;
auto idx = distance(n.begin(), ei);
bool found = false;
for (int i = idx - 1; idx >= 0 && found == false; i--) {
if (n[idx - 1].distance != n[i].distance) break;
if (n[i].id == obj.id) found = true;
}
for (int i = idx + 1; idx < (static_cast<int>(n.size()) - 1) && found == false; i++) {
if (n[idx + 1].distance != n[i].distance) break;
if (n[i].id == obj.id) found = true;
}
if (found) {
std::cerr << "Warning! The distances calculated earlier are different now, "
<< "therefore the index might be inconsistent." << std::endl;
} else {
n.insert(ei, obj);
insertionA = true;
}
}
#endif
}
Expand All @@ -1024,8 +1039,24 @@ void NeighborhoodGraph::removeEdgesReliably(ObjectID id) {
#else
GraphNode::iterator ei = std::lower_bound(n.begin(), n.end(), obj);
if ((ei == n.end()) || ((*ei).id != obj.id)) {
n.insert(ei, obj);
insertionB = true;
auto idx = distance(n.begin(), ei);
bool found = false;
for (int i = idx - 1; idx >= 0 && found == false; i--) {
if (n[idx - 1].distance != n[i].distance) break;
if (n[i].id == obj.id) found = true;
}
for (int i = idx + 1; idx < (static_cast<int>(n.size()) - 1) && found == false; i++) {
if (n[idx + 1].distance != n[i].distance) break;
if (n[i].id == obj.id) found = true;
}
if (found) {
std::cerr << "Warning! The distances calculated earlier are different now, "
<< "therefore the index might be inconsistent." << std::endl;
} else {
n.insert(ei, obj);
insertionB = true;
}

}
#endif
}
Expand Down
9 changes: 6 additions & 3 deletions lib/NGT/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,13 @@ class LeafNode : public Node {
}
#endif // NGT_NODE_USE_VECTOR
if (pivot == 0) {
// Before insertion, parent ID == 0 and object size == 0, that indicates an empty index
if (parent.getID() != 0 || objectSize != 0) {
NGTThrowException("Node::write: pivot is null!");
if (parent.getID() != 0) {
NGTThrowException("Node::write: The pivot is null and the parent is invalid.");
}
if (objectSize != 0) {
NGTThrowException("Node::write: The pivot is null with no object assigned.");
}
// Before insertion, parent ID == 0 and object size == 0, that indicates an empty index
} else {
#ifdef NGT_SHARED_MEMORY_ALLOCATOR
std::cerr << "not implemented" << std::endl;
Expand Down
1 change: 1 addition & 0 deletions lib/NGT/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ void DVPTree::removeEmptyNodes(InternalNode &inode) {
LeafNode *ln = new LeafNode;
#endif
ln->parent = target->parent;
ln->pivot = PersistentObject::allocate(*objectSpace);
insertNode(ln);

InternalNode &in = *(InternalNode *)getNode(ln->parent);
Expand Down
5 changes: 4 additions & 1 deletion lib/NGT/Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,10 @@ class DVPTree {
auto objs = ln.getObjectIDs();
#endif
for (size_t idx = 0; idx < ln.objectSize; ++idx) {
ids.insert(objs[idx].id);
auto ret = ids.insert(objs[idx].id);
if (ret.second == false) {
std::cerr << "Warning! Duplicated ID in the tree. ID=" << objs[idx].id << std::endl;
}
}
}
}
Expand Down

0 comments on commit bf646a3

Please sign in to comment.