feat(cpp/db): add meta and status

Former-commit-id: 3001a50219a1365ffd0d43fc89cd482344b99ad2
This commit is contained in:
Xu Peng 2019-04-14 17:17:33 +08:00
parent 02bcac92a1
commit c221786d22
10 changed files with 344 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#ifndef VECENGINE_DB_H_
#define VECENGINE_DB_H_
#include <string>
#include "options.h"
namespace vecengine {
@ -11,6 +12,9 @@ class DB {
public:
static DB* Open(const Options& options_, const std::string& name_);
virtual std::string add_group(GroupOptions options_,
const std::string& group_id_) = 0;
DB() = default;
DB(const DB&) = delete;
DB& operator=(const DB&) = delete;

View File

@ -1,11 +1,31 @@
include "db_impl.h"
#include <assert.h>
#include "db_impl.h"
namespace vecengine {
DB::DB(const Options& options_, const std::string& name_)
DBImpl::DBImpl(const Options& options_, const std::string& name_)
: _dbname(name_),
_env(options_.env),
_options(options_) {
}
Status DBImpl::add_group(const GroupOptions& options_,
const std::string& group_id_,
std::string& gid_) {
assert((!options_.has_id) ||
(options_.has_id && ("" != group_id_)));
}
/*
* DB
*/
DB::~DB() {}
DB* DB::Open(const Options& options_, const std::string& name_) {
DBImpl* impl = new DBImpl(options_, name_);
return impl;
}
} // namespace vecengine

View File

@ -11,8 +11,16 @@ class DBImpl : public DB {
public:
DBImpl(const Options& options_, const std::string& name_);
virtual Status add_group(GroupOptions options_,
const std::string& group_id_,
std::string& gid_) override;
virtual ~DBImpl();
private:
Status meta_add_group(const std::string& group_id_);
Status meta_add_group_file(const std::string& group_id_);
const _dbname;
Env* const _env;
const Options _options;

14
cpp/src/db_meta.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "db_meta.h"
namespace zilliz {
namespace vecwise {
namespace engine {
Meta* Meta::Default() {
static DefaultMeta meta;
return *meta;
}
} // namespace engine
} // namespace vecwise
} // namespace zilliz

60
cpp/src/db_meta.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef VECENGINE_DB_META_H_
#define VECENGINE_DB_META_H_
namespace zilliz {
namespace vecwise {
namespace engine {
struct GroupSchema {
size_t id;
std::string group_id;
size_t files_cnt = 0;
uint16_t dimension;
std::string location = "";
}; // GroupSchema
struct GroupFileSchema {
typedef enum {
RAW,
INDEX
} FILE_TYPE;
size_t id;
std::string group_id;
std::string file_id;
int files_type = RAW;
size_t rows;
std::string location = "";
}; // GroupFileSchema
typedef std::vector<GroupFileSchema> GroupFilesSchema;
class Meta {
public:
virtual Status add_group(const std::string& group_id_, GroupSchema& group_info) = 0;
virtual Status get_group(const std::string& group_id_, GroupSchema& group_info) = 0;
virtual Status has_group(const std::string& group_id_, bool& has_or_not) = 0;
virtual Status add_group_file(const std::string& group_id,
GroupFileSchema& group_file_info) = 0;
virtual Status has_group_file(const std::string& group_id,
const std::string& file_id,
bool& has_or_not) = 0;
virtual Status get_group_file(const std::string& group_id,
const std::string& file_id,
GroupFileSchema& group_file_info) = 0;
virtual Status mark_group_file_as_index(const std::string& group_id,
const std::string& file_id) = 0;
virtual Status get_group_files(const std::string& group_id,
GroupFilesSchema& group_files_info) = 0;
}; // MetaData
} // namespace engine
} // namespace vecwise
} // namespace zilliz
#endif // VECENGINE_DB_META_H_

66
cpp/src/db_meta_impl.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "db_meta_impl.h"
namespace zilliz {
namespace vecwise {
namespace engine {
Status DBMetaImpl::DBMetaImpl(DBMetaOptions options_)
: _options(options_) {
initialize();
}
Status DBMetaImpl::initialize() {
// PXU TODO: Create DB Connection
return Status.OK();
}
Status DBMetaImpl::add_group(const std::string& group_id_, GroupSchema& group_info) {
//PXU TODO
return Status.OK();
}
Status DBMetaImpl::get_group(const std::string& group_id_, GroupSchema& group_info) {
//PXU TODO
return Status.OK();
}
Status DBMetaImpl::has_group(const std::string& group_id_, bool& has_or_not) {
//PXU TODO
return Status.OK();
}
Status DBMetaImpl::add_group_file(const std::string& group_id,
GroupFileSchema& group_file_info) {
//PXU TODO
return Status.OK();
}
Status DBMetaImpl::has_group_file(const std::string& group_id,
const std::string& file_id,
bool& has_or_not) {
//PXU TODO
return Status.OK();
}
Status DBMetaImpl::get_group_file(const std::string& group_id,
const std::string& file_id,
GroupFileSchema& group_file_info) {
//PXU TODO
return Status.OK();
}
Status DBMetaImpl::get_group_files(const std::string& group_id,
GroupFilesSchema& group_files_info) {
// PXU TODO
return Status.OK();
}
Status DBMetaImpl::mark_group_file_as_index(const std::string& group_id,
const std::string& file_id) {
//PXU TODO
return Status.OK();
}
} // namespace engine
} // namespace vecwise
} // namespace zilliz

45
cpp/src/db_meta_impl.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef VECENGINE_DB_META_IMPL_H_
#define VECENGINE_DB_META_IMPL_H_
#include "db_meta.h"
#include "options.h"
namespace zilliz {
namespace vecwise {
namespace engine {
class DBMetaImpl : public Meta {
public:
DBMetaImpl(DBMetaOptions& options_);
virtual Status add_group(const std::string& group_id_, GroupSchema& group_info) override;
virtual Status get_group(const std::string& group_id_, GroupSchema& group_info) override;
virtual Status has_group(const std::string& group_id_, bool& has_or_not) override;
virtual Status add_group_file(const std::string& group_id,
GroupFileSchema& group_file_info) override;
virtual Status has_group_file(const std::string& group_id,
const std::string& file_id,
bool& has_or_not) override;
virtual Status get_group_file(const std::string& group_id,
const std::string& file_id,
GroupFileSchema& group_file_info) override;
virtual Status mark_group_file_as_index(const std::string& group_id,
const std::string& file_id) override;
virtual Status get_group_files(const std::string& group_id,
GroupFilesSchema& group_files_info) override;
private:
Status initialize();
const DBMetaOptions _options;
}; // DBMetaImpl
} // namespace engine
} // namespace vecwise
} // namespace zilliz
#endif // VECENGINE_DB_META_IMPL_H_

View File

@ -1,12 +1,30 @@
#ifndef VECENGINE_OPTIONS_H_
#define VECENGINE_OPTIONS_H_
#include <string>
namespace vecengine {
struct Options {
}; // Options
struct GroupOptions {
size_t dimension;
bool has_id = false;
}; // GroupOptions
struct MetaOptions {
}; // MetaOptions
struct DBMetaOptions : public MetaOptions {
std::string db_uri;
}; // DBMetaOptions
} // namespace vecengine
#endif // VECENGINE_OPTIONS_H_

52
cpp/src/status.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "status.h"
namespace vecengine {
const char* Status::CopyState(const char* state_) {
uint32_t size;
memcpy(&size, state_, sizeof(size));
char result = new char[size+5];
memcpy(result, state_, size+5);
return result;
}
Status::Status(Code code_, const std::string& msg_, const std::string& msg2_) {
assert(code_ != kOK);
const uint32_t len1 = msg_.size();
const uint32_t len2 = msg2_.size();
const uint32_t size = len1 + (len2 ? (2+len2) : 0);
char* result = new char[size+5];
memcpy(result, &size, sizeof(size));
result[4] = static_cast<char>(code);
memcpy(result+5, msg_.data(), len1);
if (len2) {
result[5 + len1] = ':';
result[6 + len1] = ' ';
memcpy(result + 7 + len1, msg2_.data(), len2);
}
_state = result;
}
std::string Status::ToString() const {
if (_state == nullptr) return "OK";
char tmp[30];
const char* type;
switch (code()) {
case kOK:
type = "OK";
break;
default:
snprintf(tmp, sizeof(tmp), "Unkown code(%d): ",
static_cast<int>(code()));
type = tmp;
break;
}
std::string result(type);
uint32_t length;
memcpy(&length, state_, sizeof(length));
result.append(state_ + 5, length);
return result;
}
} // namespace vecengine

55
cpp/src/status.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef VECENGINE_STATUS_H_
#define VECENGINE_STATUS_H_
namespace vecengine {
class Status {
public:
Status() noexcept : _state(nullptr) {}
~Status() { delete[] _state; }
Status(const Status& rhs_);
Status& operator=(const Status& rhs_);
Status(const Status&& rhs_) noexcept : _state(rhs_._state) { rhs_._state = nullptr; }
Status& operator=(const Status& rhs_) noexcept;
static Status OK() { return Status(); }
bool ok() const { return _state == nullptr; }
private:
const char* _state;
enum Code {
kOK = 0,
};
Code code() const {
return (_state == nullptr) ? kOK : static_cast<Code>(_state[4])
}
static const char* CopyState(const char* s);
}; // Status
inline Status::Status(const Status* rhs_) {
_state = (rhs_._state == nullptr) ? nullptr : CopyState(rhs_._state);
}
inline Status& Status::operator=(const Status& rhs_) {
if (_state != rhs_._state) {
delete[] state_;
_state = (rhs_._state == nullptr) ? nullptr : CopyState(rhs_._state);
}
return *this;
}
inline Status& Status::operator=(Status&& rhs_) noexcept {
std::swap(_state, rhs_._state);
return *this;
}
} // namespace vecengine
#endif // VECENGINE_STATUS_H_