void Chord::InitChord(const Node::KEY &startID) { if (startID > Node::MaxKey()) return; Node *startNode = new Node(startID); if (!nodes.insert({startID, startNode}).second) { delete startNode; return; } startNode->Create(); }
const bool Chord::AddNode(const Node::KEY &ID) { if (ID > Node::MaxKey()) return false; Node *startNode = nullptr; Node *newNode = new Node(ID); if (!nodes.empty()) startNode = nodes.begin()->second; if (!nodes.insert({newNode->GetID(), newNode}).second) { delete newNode; return false; } if (nodes.size() == 1) { // I'm the only one newNode->Create(); return true; } else { // There's someone else here, I have to say hi newNode->Join(startNode); } newNode->Stabilize(); newNode->FixFingers(); //TODO Think of a way to fix fingers faster, B-Tree O(log n) maybe? for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) { it->second->Stabilize(); it->second->FixFingers(); } // invalidIDs.clear(); // I'm not going to move data to myself lol if (newNode->Successor()->HasData()) { if (newNode != newNode->Successor()) newNode->Successor()->MoveDataToPredecessor(); } return true; }