mirror of https://github.com/milvus-io/milvus.git
feat(cpp/db): update for memory and meta operations
Former-commit-id: 34cc6ca2c596870ad8152ab0da4d187fb518093apull/191/head
parent
c6f36c8fba
commit
3e8213ff47
|
@ -25,6 +25,9 @@ public:
|
||||||
const int date_delta_,
|
const int date_delta_,
|
||||||
GroupFilesSchema& group_files_info_) = 0;
|
GroupFilesSchema& group_files_info_) = 0;
|
||||||
|
|
||||||
|
virtual Status add_vectors(const std::string& group_id_,
|
||||||
|
size_t n, const float* vectors) = 0;
|
||||||
|
|
||||||
DB() = default;
|
DB() = default;
|
||||||
DB(const DB&) = delete;
|
DB(const DB&) = delete;
|
||||||
DB& operator=(const DB&) = delete;
|
DB& operator=(const DB&) = delete;
|
||||||
|
|
|
@ -9,7 +9,8 @@ DBImpl::DBImpl(const Options& options_, const std::string& name_)
|
||||||
_options(options_),
|
_options(options_),
|
||||||
_bg_work_finish_signal(_mutex),
|
_bg_work_finish_signal(_mutex),
|
||||||
_bg_compaction_scheduled(false),
|
_bg_compaction_scheduled(false),
|
||||||
_pMeta(new DBMetaImpl(*(_options.pMetaOptions))) {
|
_pMeta(new DBMetaImpl(*(_options.pMetaOptions))),
|
||||||
|
_pMemMgr(new MemManager(_pMeta)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Status DBImpl::add_group(const GroupOptions& options_,
|
Status DBImpl::add_group(const GroupOptions& options_,
|
||||||
|
@ -36,6 +37,11 @@ Status DBImpl::get_group_files(const std::string& group_id_,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Status DBImpl::add_vectors(const std::string& group_id_,
|
||||||
|
size_t n, const float* vectors, IDNumbers& vector_ids_) {
|
||||||
|
return _pMemMgr->add_vectors(group_id_, n, vectors, vector_ids_);
|
||||||
|
}
|
||||||
|
|
||||||
void DBImpl::try_schedule_compaction() {
|
void DBImpl::try_schedule_compaction() {
|
||||||
if (_bg_compaction_scheduled) return;
|
if (_bg_compaction_scheduled) return;
|
||||||
if (!_bg_error.ok()) return;
|
if (!_bg_error.ok()) return;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
#include "memvectors.h"
|
#include "memvectors.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
namespace zilliz {
|
namespace zilliz {
|
||||||
namespace vecwise {
|
namespace vecwise {
|
||||||
|
@ -27,11 +28,15 @@ public:
|
||||||
const int date_delta_,
|
const int date_delta_,
|
||||||
GroupFilesSchema& group_files_info_) override;
|
GroupFilesSchema& group_files_info_) override;
|
||||||
|
|
||||||
void try_schedule_compaction();
|
virtual Status add_vectors(const std::string& group_id_,
|
||||||
|
size_t n, const float* vectors, IDNumbers& vector_ids_) override;
|
||||||
|
|
||||||
virtual ~DBImpl();
|
virtual ~DBImpl();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
void try_schedule_compaction();
|
||||||
|
|
||||||
static void BGWork(void* db);
|
static void BGWork(void* db);
|
||||||
void background_call();
|
void background_call();
|
||||||
void background_compaction();
|
void background_compaction();
|
||||||
|
@ -45,8 +50,8 @@ private:
|
||||||
bool _bg_compaction_scheduled;
|
bool _bg_compaction_scheduled;
|
||||||
Status _bg_error;
|
Status _bg_error;
|
||||||
|
|
||||||
MemManager _memMgr;
|
|
||||||
std::shared_ptr<Meta> _pMeta;
|
std::shared_ptr<Meta> _pMeta;
|
||||||
|
std::shared_ptr<MemManager> _pMemMgr;
|
||||||
|
|
||||||
}; // DBImpl
|
}; // DBImpl
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ struct GroupSchema {
|
||||||
size_t files_cnt = 0;
|
size_t files_cnt = 0;
|
||||||
uint16_t dimension;
|
uint16_t dimension;
|
||||||
std::string location = "";
|
std::string location = "";
|
||||||
|
std::string next_file_location = "";
|
||||||
}; // GroupSchema
|
}; // GroupSchema
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,12 @@
|
||||||
#define UTILS_ID_GENERATORS_H_
|
#define UTILS_ID_GENERATORS_H_
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
namespace zilliz {
|
namespace zilliz {
|
||||||
namespace vecwise {
|
namespace vecwise {
|
||||||
namespace engine {
|
namespace engine {
|
||||||
|
|
||||||
#define uint64_t IDNumber;
|
|
||||||
#define IDNumber* IDNumberPtr;
|
|
||||||
#define std::vector<IDNumber> IDNumbers;
|
|
||||||
|
|
||||||
class IDGenerator {
|
class IDGenerator {
|
||||||
public:
|
public:
|
||||||
virtual IDNumber getNextIDNumber() = 0;
|
virtual IDNumber getNextIDNumber() = 0;
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <index_io.h>
|
#include <index_io.h>
|
||||||
|
|
||||||
#include "memvectors.h"
|
#include "memvectors.h"
|
||||||
|
#include "db_meta.h"
|
||||||
|
|
||||||
|
|
||||||
namespace vecengine {
|
namespace vecengine {
|
||||||
|
@ -15,10 +16,9 @@ MemVectors::MemVectors(size_t dimension_, const std::string& file_location_) :
|
||||||
_pIdMapIndex = new faiss::IndexIDMap(_pInnerIndex) {
|
_pIdMapIndex = new faiss::IndexIDMap(_pInnerIndex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
IDNumbers&& MemVectors::add(size_t n, const float* vectors) {
|
void MemVectors::add(size_t n_, const float* vectors_, IDNumbers& vector_ids_) {
|
||||||
IDNumbers&& ids = _pIdGenerator->getNextIDNumbers(n);
|
vector_ids_ = _pIdGenerator->getNextIDNumbers(n_);
|
||||||
_pIdMapIndex->add_with_ids(n, vectors, pIds, &ids[0]);
|
_pIdMapIndex->add_with_ids(n_, vectors_, &vector_ids_[0]);
|
||||||
return ids;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t MemVectors::total() const {
|
size_t MemVectors::total() const {
|
||||||
|
@ -57,27 +57,33 @@ MemVectors* MemManager::get_mem_by_group(const std::string& group_id_) {
|
||||||
if memIt != _memMap.end() {
|
if memIt != _memMap.end() {
|
||||||
return &(memIt->second);
|
return &(memIt->second);
|
||||||
}
|
}
|
||||||
// PXU TODO:
|
|
||||||
// 1. Read Group meta info
|
GroupSchema group_info;
|
||||||
// 2. Initalize MemVectors base meta info
|
Status status = _pMeta->get_group(group_id_, group_info);
|
||||||
return nullptr;
|
if (!status.ok()) {
|
||||||
/* GroupMetaInfo info; */
|
return nullptr;
|
||||||
/* bool succ = env->getGroupMeta(group_id, &info); */
|
}
|
||||||
/* if (!succ) { */
|
_memMap[group_id] = MemVectors(group_info.dimension, group_info.next_file_location);
|
||||||
/* return nullptr; */
|
return &(_memMap[group_id]);
|
||||||
/* } */
|
|
||||||
/* _memMap[group_id] = MemVectors(info.dimension, info.next_file_location); */
|
|
||||||
/* return &(_memMap[group_id]); */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IDNumbers&& MemManager::add_vectors_no_lock(const std::string& group_id_,
|
Status MemManager::add_vectors(const std::string& group_id_,
|
||||||
|
size_t n_,
|
||||||
|
const float* vectors_,
|
||||||
|
IDNumbers& vector_ids_) {
|
||||||
|
// PXU TODO
|
||||||
|
return add_vectors_no_lock(group_id_, n_, vectors_, vector_ids_);
|
||||||
|
}
|
||||||
|
|
||||||
|
Status MemManager::add_vectors_no_lock(const std::string& group_id_,
|
||||||
size_t n,
|
size_t n,
|
||||||
const float* vectors) {
|
const float* vectors,
|
||||||
auto mem = get_group_mem(group_id_);
|
IDNumbers& vector_ids_) {
|
||||||
|
auto mem = get_mem_by_group(group_id_);
|
||||||
if (mem == nullptr) {
|
if (mem == nullptr) {
|
||||||
return IDNumbers();
|
return Status::NotFound("Group " + group_id_ " not found!");
|
||||||
}
|
}
|
||||||
return mem->add(n, vectors);
|
return mem->add(n, vectors, vector_ids_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "id_generators.h"
|
#include "id_generators.h"
|
||||||
|
#include "status.h"
|
||||||
|
|
||||||
class faiss::IndexIDMap;
|
class faiss::IndexIDMap;
|
||||||
class faiss::Index;
|
class faiss::Index;
|
||||||
|
@ -37,19 +38,24 @@ private:
|
||||||
}; // MemVectors
|
}; // MemVectors
|
||||||
|
|
||||||
|
|
||||||
|
class Meta;
|
||||||
|
|
||||||
class MemManager {
|
class MemManager {
|
||||||
public:
|
public:
|
||||||
MemManager() = default;
|
MemManager(const std::shared_ptr<Meta>& meta_) : _pMeta(meta_) {}
|
||||||
|
|
||||||
MemVectors* get_mem_by_group(const std::string& group_id_);
|
MemVectors* get_mem_by_group(const std::string& group_id_);
|
||||||
|
|
||||||
|
Status add_vectors(const std::string& group_id_,
|
||||||
|
size_t n_, const float* vectors_, IDNumbers& vector_ids_);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IDNumbers&& add_vectors_no_lock(const std::string& group_id_,
|
Status add_vectors_no_lock(const std::string& group_id_,
|
||||||
size_t n,
|
size_t n_, const float* vectors_, IDNumbers& vector_ids_);
|
||||||
const float* vectors);
|
|
||||||
|
|
||||||
typedef std::map<std::string, MemVectors> MemMap;
|
typedef std::map<std::string, MemVectors> MemMap;
|
||||||
MemMap _memMap;
|
MemMap _memMap;
|
||||||
|
std::shared_ptr<Meta> _pMeta;
|
||||||
}; // MemManager
|
}; // MemManager
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace vecwise {
|
||||||
namespace engine {
|
namespace engine {
|
||||||
|
|
||||||
class MetaOptions;
|
class MetaOptions;
|
||||||
|
class Env;
|
||||||
|
|
||||||
struct Options {
|
struct Options {
|
||||||
uint16_t memory_sync_interval = 10;
|
uint16_t memory_sync_interval = 10;
|
||||||
|
|
|
@ -17,14 +17,20 @@ public:
|
||||||
Status& operator=(const Status& rhs_) noexcept;
|
Status& operator=(const Status& rhs_) noexcept;
|
||||||
|
|
||||||
static Status OK() { return Status(); }
|
static Status OK() { return Status(); }
|
||||||
|
static Status NotFound(const std::string& msg_, const std::string& msg2_="") {
|
||||||
|
return Status(kNotFound, msg_, msg2_);
|
||||||
|
}
|
||||||
|
|
||||||
bool ok() const { return _state == nullptr; }
|
bool ok() const { return _state == nullptr; }
|
||||||
|
|
||||||
|
bool IsNotFound() const { return code() == kNotFound; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* _state;
|
const char* _state;
|
||||||
|
|
||||||
enum Code {
|
enum Code {
|
||||||
kOK = 0,
|
kOK = 0,
|
||||||
|
kNotFound,
|
||||||
};
|
};
|
||||||
|
|
||||||
Code code() const {
|
Code code() const {
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef VECENGINE_TYPES_H_
|
||||||
|
#define VECENGINE_TYPES_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace zilliz {
|
||||||
|
namespace vecwise {
|
||||||
|
namespace engine {
|
||||||
|
|
||||||
|
#define uint64_t IDNumber;
|
||||||
|
#define IDNumber* IDNumberPtr;
|
||||||
|
#define std::vector<IDNumber> IDNumbers;
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace engine
|
||||||
|
} // namespace vecwise
|
||||||
|
} // namespace zilliz
|
||||||
|
|
||||||
|
#endif // VECENGINE_TYPES_H_
|
Loading…
Reference in New Issue