Node.cpp 1.7 KB
Newer Older
X
xj.lin 已提交
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
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/

#include <atomic>
#include "Node.h"


namespace zilliz {
namespace milvus {
namespace engine {

Node::Node() {
    static std::atomic_uint_fast8_t counter(0);
    id_ = counter++;
}

void Node::DelNeighbour(const NeighbourNodePtr &neighbour_ptr) {
    std::lock_guard<std::mutex> lk(mutex_);
    if (auto s = neighbour_ptr.lock()) {
        auto search = neighbours_.find(s->id_);
        if (search != neighbours_.end()) {
            neighbours_.erase(search);
        }
    }
}

bool Node::IsNeighbour(const NeighbourNodePtr &neighbour_ptr) {
    std::lock_guard<std::mutex> lk(mutex_);
    if (auto s = neighbour_ptr.lock()) {
        auto search = neighbours_.find(s->id_);
        if (search != neighbours_.end()) {
            return true;
        }
    }
    return false;
}

std::vector<Neighbour> Node::GetNeighbours() {
    std::lock_guard<std::mutex> lk(mutex_);
    std::vector<Neighbour> ret;
    for (auto &e : neighbours_) {
        ret.push_back(e.second);
    }
    return ret;
}

std::string Node::Dump() {
    // TODO(linxj): what's that?
    return std::__cxx11::string();
}

void Node::AddNeighbour(const NeighbourNodePtr &neighbour_node, Connection &connection) {
    std::lock_guard<std::mutex> lk(mutex_);
    if (auto s = neighbour_node.lock()) {
        Neighbour neighbour(neighbour_node, connection);
        neighbours_[s->id_] = neighbour;
    }
    // else do nothing, consider it..
}

}
}
}