memgraph/src/storage/indexes/index.hpp

46 lines
1.4 KiB
C++
Raw Normal View History

2016-01-02 19:20:51 +08:00
#pragma once
#include <memory>
2016-01-02 19:20:51 +08:00
#include "data_structures/concurrent/concurrent_map.hpp"
#include "storage/indexes/index_record.hpp"
#include "storage/indexes/index_record_collection.hpp"
2016-07-07 00:37:05 +08:00
#include "storage/label/label.hpp"
2016-01-02 19:20:51 +08:00
2016-01-09 21:43:55 +08:00
template <class Key, class Item>
2016-01-02 19:20:51 +08:00
class Index
{
public:
using container_t = ConcurrentMap<Key, Item>;
2016-01-02 19:20:51 +08:00
Index() : index(std::make_unique<container_t>()) {}
auto update(const Label &label, VertexIndexRecord &&index_record)
{
auto accessor = index->access();
auto label_ref = label_ref_t(label);
// create Index Record Collection if it doesn't exist
if (!accessor.contains(label_ref)) {
accessor.insert_unique(label_ref,
std::move(VertexIndexRecordCollection()));
}
// add Vertex Index Record to the Record Collection
auto &record_collection = (*accessor.find(label_ref)).second;
record_collection.add(std::forward<VertexIndexRecord>(index_record));
2016-01-02 19:20:51 +08:00
}
VertexIndexRecordCollection& find(const Label& label)
{
// TODO: accessor should be outside?
// bacause otherwise GC could delete record that has just be returned
auto label_ref = label_ref_t(label);
auto accessor = index->access();
return (*accessor.find(label_ref)).second;
}
2016-01-02 19:20:51 +08:00
private:
std::unique_ptr<container_t> index;
2016-01-02 19:20:51 +08:00
};