2016-09-25 22:14:36 +08:00
|
|
|
/*=============================================================================
|
|
|
|
# Filename: KVstore.h
|
2017-01-16 14:12:57 +08:00
|
|
|
# Author: Bookug Lobert
|
2016-09-25 22:14:36 +08:00
|
|
|
# Mail: 1181955272@qq.com
|
|
|
|
# Last Modified: 2015-10-23 14:23
|
2017-01-16 14:12:57 +08:00
|
|
|
# Description: Modified by Wang Libo
|
2016-09-25 22:14:36 +08:00
|
|
|
=============================================================================*/
|
|
|
|
|
|
|
|
#ifndef _KVSTORE_KVSTORE_H
|
|
|
|
#define _KVSTORE_KVSTORE_H
|
|
|
|
|
|
|
|
#include "../Util/Util.h"
|
|
|
|
#include "Tree.h"
|
|
|
|
|
|
|
|
class KVstore
|
|
|
|
{
|
|
|
|
public:
|
2017-01-16 14:12:57 +08:00
|
|
|
static const int READ_WRITE_MODE = 1; //Open a B tree, which must exist
|
|
|
|
static const int CREATE_MODE = 2; //Build a new B tree and delete existing ones (if any)
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
KVstore(std::string _store_path = ".");
|
|
|
|
~KVstore();
|
|
|
|
void flush();
|
|
|
|
void release();
|
|
|
|
void open();
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//===============================================================================
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//including IN-neighbor & OUT-neighbor
|
2017-03-24 20:10:43 +08:00
|
|
|
unsigned getEntityDegree(TYPE_ENTITY_LITERAL_ID _entity_id) const;
|
|
|
|
unsigned getEntityInDegree(TYPE_ENTITY_LITERAL_ID _entity_id) const;
|
|
|
|
unsigned getEntityOutDegree(TYPE_ENTITY_LITERAL_ID _entity_id) const;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
unsigned getLiteralDegree(TYPE_ENTITY_LITERAL_ID _literal_id) const;
|
|
|
|
unsigned getPredicateDegree(TYPE_PREDICATE_ID _predicate_id) const;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
unsigned getSubjectPredicateDegree(TYPE_ENTITY_LITERAL_ID _subid, TYPE_PREDICATE_ID _preid) const;
|
|
|
|
unsigned getObjectPredicateDegree(TYPE_ENTITY_LITERAL_ID _objid, TYPE_PREDICATE_ID _preid) const;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//===============================================================================
|
|
|
|
//Before calling these functions, we are sure that the triples doesn't exist.
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool updateTupleslist_insert(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateTupleslist_remove(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool updateInsert_s2values(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateRemove_s2values(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateInsert_s2values(TYPE_ENTITY_LITERAL_ID _subid, const std::vector<unsigned>& _pidoidlist);
|
|
|
|
bool updateRemove_s2values(TYPE_ENTITY_LITERAL_ID _subid, const std::vector<unsigned>& _pidoidlist);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool updateInsert_o2values(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateRemove_o2values(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateInsert_o2values(TYPE_ENTITY_LITERAL_ID _objid, const std::vector<unsigned>& _pidsidlist);
|
|
|
|
bool updateRemove_o2values(TYPE_ENTITY_LITERAL_ID _objid, const std::vector<unsigned>& _pidsidlist);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool updateInsert_p2values(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateRemove_p2values(TYPE_ENTITY_LITERAL_ID _sub_id, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _obj_id);
|
|
|
|
bool updateInsert_p2values(TYPE_PREDICATE_ID _preid, const std::vector<unsigned>& _sidoidlist);
|
|
|
|
bool updateRemove_p2values(TYPE_PREDICATE_ID _preid, const std::vector<unsigned>& _sidoidlist);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//===============================================================================
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//for entity2id
|
2016-09-25 22:14:36 +08:00
|
|
|
bool open_entity2id(int _mode);
|
|
|
|
bool close_entity2id();
|
|
|
|
bool subIDByEntity(std::string _entity);
|
2017-03-24 20:10:43 +08:00
|
|
|
TYPE_ENTITY_LITERAL_ID getIDByEntity(std::string _entity) const;
|
|
|
|
bool setIDByEntity(std::string _entity, TYPE_ENTITY_LITERAL_ID _id);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//for id2entity
|
2016-09-25 22:14:36 +08:00
|
|
|
bool open_id2entity(int _mode);
|
|
|
|
bool close_id2entity();
|
2017-03-24 20:10:43 +08:00
|
|
|
bool subEntityByID(TYPE_ENTITY_LITERAL_ID _id);
|
|
|
|
std::string getEntityByID(TYPE_ENTITY_LITERAL_ID _id) const;
|
|
|
|
bool setEntityByID(TYPE_ENTITY_LITERAL_ID _id, std::string _entity);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//for predicate2id
|
2016-09-25 22:14:36 +08:00
|
|
|
bool open_predicate2id(int _mode);
|
|
|
|
bool close_predicate2id();
|
|
|
|
bool subIDByPredicate(std::string _predicate);
|
2017-03-24 20:10:43 +08:00
|
|
|
TYPE_PREDICATE_ID getIDByPredicate(std::string _predicate) const;
|
|
|
|
bool setIDByPredicate(std::string _predicate, TYPE_PREDICATE_ID _id);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//for id2predicate
|
2016-09-25 22:14:36 +08:00
|
|
|
bool open_id2predicate(int _mode);
|
|
|
|
bool close_id2predicate();
|
2017-03-24 20:10:43 +08:00
|
|
|
bool subPredicateByID(TYPE_PREDICATE_ID _id);
|
|
|
|
std::string getPredicateByID(TYPE_PREDICATE_ID _id) const;
|
|
|
|
bool setPredicateByID(TYPE_PREDICATE_ID _id, std::string _predicate);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//for literal2id
|
2016-09-25 22:14:36 +08:00
|
|
|
bool open_literal2id(int _mode);
|
|
|
|
bool close_literal2id();
|
|
|
|
bool subIDByLiteral(std::string _literal);
|
2017-03-24 20:10:43 +08:00
|
|
|
TYPE_ENTITY_LITERAL_ID getIDByLiteral(std::string _literal) const;
|
|
|
|
bool setIDByLiteral(std::string _literal, TYPE_ENTITY_LITERAL_ID _id);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//for id2literal
|
|
|
|
bool open_id2literal(int _mode);
|
|
|
|
bool close_id2literal();
|
2017-03-24 20:10:43 +08:00
|
|
|
bool subLiteralByID(TYPE_ENTITY_LITERAL_ID _id);
|
|
|
|
std::string getLiteralByID(TYPE_ENTITY_LITERAL_ID _id) const;
|
|
|
|
bool setLiteralByID(TYPE_ENTITY_LITERAL_ID _id, std::string _literal);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
//===============================================================================
|
|
|
|
|
|
|
|
//for subID2values
|
|
|
|
bool open_subID2values(int _mode);
|
|
|
|
bool close_subID2values();
|
2017-03-29 13:48:39 +08:00
|
|
|
bool build_subID2values(ID_TUPLE* _p_id_tuples, TYPE_TRIPLE_NUM _triples_num);
|
2017-03-24 20:10:43 +08:00
|
|
|
bool getpreIDlistBysubID(TYPE_ENTITY_LITERAL_ID _subid, unsigned*& _preidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getobjIDlistBysubID(TYPE_ENTITY_LITERAL_ID _subid, unsigned*& _objidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getobjIDlistBysubIDpreID(TYPE_ENTITY_LITERAL_ID _subid, TYPE_PREDICATE_ID _preid, unsigned*& _objidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getpreIDobjIDlistBysubID(TYPE_ENTITY_LITERAL_ID _subid, unsigned*& _preid_objidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
2017-01-16 14:12:57 +08:00
|
|
|
|
|
|
|
//for objID2values
|
|
|
|
bool open_objID2values(int _mode);
|
|
|
|
bool close_objID2values();
|
2017-03-29 13:48:39 +08:00
|
|
|
bool build_objID2values(ID_TUPLE* _p_id_tuples, TYPE_TRIPLE_NUM _triples_num);
|
2017-03-24 20:10:43 +08:00
|
|
|
bool getpreIDlistByobjID(TYPE_ENTITY_LITERAL_ID _objid, unsigned*& _preidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getsubIDlistByobjID(TYPE_ENTITY_LITERAL_ID _objid, unsigned*& _subidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getsubIDlistByobjIDpreID(TYPE_ENTITY_LITERAL_ID _objid, TYPE_PREDICATE_ID _preid, unsigned*& _subidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getpreIDsubIDlistByobjID(TYPE_ENTITY_LITERAL_ID _objid, unsigned*& _preid_subidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
2017-01-16 14:12:57 +08:00
|
|
|
|
|
|
|
//for preID2values
|
|
|
|
bool open_preID2values(int _mode);
|
|
|
|
bool close_preID2values();
|
2017-03-29 13:48:39 +08:00
|
|
|
bool build_preID2values(ID_TUPLE* _p_id_tuples, TYPE_TRIPLE_NUM _triples_num);
|
2017-03-24 20:10:43 +08:00
|
|
|
bool getsubIDlistBypreID(TYPE_PREDICATE_ID _preid, unsigned*& _subidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getobjIDlistBypreID(TYPE_PREDICATE_ID _preid, unsigned*& _objidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
|
|
|
bool getsubIDobjIDlistBypreID(TYPE_PREDICATE_ID _preid, unsigned*& _subid_objidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
2017-01-16 14:12:57 +08:00
|
|
|
|
|
|
|
//for so2p
|
2017-03-24 20:10:43 +08:00
|
|
|
bool getpreIDlistBysubIDobjID(TYPE_ENTITY_LITERAL_ID _subID, TYPE_ENTITY_LITERAL_ID _objID, unsigned*& _preidlist, unsigned& _list_len, bool _no_duplicate = false) const;
|
2017-01-16 14:12:57 +08:00
|
|
|
|
2016-09-25 22:14:36 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string store_path;
|
2017-01-16 14:12:57 +08:00
|
|
|
|
2016-09-25 22:14:36 +08:00
|
|
|
SITree* entity2id;
|
|
|
|
ISTree* id2entity;
|
|
|
|
static std::string s_entity2id;
|
|
|
|
static std::string s_id2entity;
|
2017-01-16 14:12:57 +08:00
|
|
|
static unsigned short buffer_entity2id_build;
|
|
|
|
static unsigned short buffer_id2entity_build;
|
|
|
|
static unsigned short buffer_entity2id_query;
|
|
|
|
static unsigned short buffer_id2entity_query;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
|
|
|
SITree* predicate2id;
|
|
|
|
ISTree* id2predicate;
|
|
|
|
static std::string s_predicate2id;
|
|
|
|
static std::string s_id2predicate;
|
2017-01-16 14:12:57 +08:00
|
|
|
static unsigned short buffer_predicate2id_build;
|
|
|
|
static unsigned short buffer_id2predicate_build;
|
|
|
|
static unsigned short buffer_predicate2id_query;
|
|
|
|
static unsigned short buffer_id2predicate_query;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
|
|
|
SITree* literal2id;
|
|
|
|
ISTree* id2literal;
|
|
|
|
static std::string s_literal2id;
|
|
|
|
static std::string s_id2literal;
|
2017-01-16 14:12:57 +08:00
|
|
|
static unsigned short buffer_literal2id_build;
|
|
|
|
static unsigned short buffer_id2literal_build;
|
|
|
|
static unsigned short buffer_literal2id_query;
|
|
|
|
static unsigned short buffer_id2literal_query;
|
|
|
|
|
|
|
|
ISTree* subID2values;
|
|
|
|
ISTree* objID2values;
|
|
|
|
ISTree* preID2values;
|
|
|
|
static std::string s_sID2values;
|
|
|
|
static std::string s_oID2values;
|
|
|
|
static std::string s_pID2values;
|
|
|
|
static unsigned short buffer_sID2values_build;
|
|
|
|
static unsigned short buffer_oID2values_build;
|
|
|
|
static unsigned short buffer_pID2values_build;
|
|
|
|
static unsigned short buffer_sID2values_query;
|
|
|
|
static unsigned short buffer_oID2values_query;
|
|
|
|
static unsigned short buffer_pID2values_query;
|
|
|
|
|
|
|
|
//===============================================================================
|
|
|
|
|
|
|
|
bool open(SITree* & _p_btree, std::string _tree_name, int _mode, unsigned long long _buffer_size);
|
|
|
|
bool open(ISTree* & _p_btree, std::string _tree_name, int _mode, unsigned long long _buffer_size);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
|
|
|
void flush(SITree* _p_btree);
|
|
|
|
void flush(ISTree* _p_btree);
|
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool addValueByKey(SITree* _p_btree, const char* _key, unsigned _klen, unsigned _val);
|
|
|
|
bool addValueByKey(ISTree* _p_btree, unsigned _key, const char* _val, unsigned _vlen);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool setValueByKey(SITree* _p_btree, const char* _key, unsigned _klen, unsigned _val);
|
|
|
|
bool setValueByKey(ISTree* _p_btree, unsigned _key, const char* _val, unsigned _vlen);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool getValueByKey(SITree* _p_btree, const char* _key, unsigned _klen, unsigned* _val) const;
|
|
|
|
bool getValueByKey(ISTree* _p_btree, unsigned _key, char*& _val, unsigned& _vlen) const;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
TYPE_ENTITY_LITERAL_ID getIDByStr(SITree* _p_btree, const char* _key, unsigned _klen) const;
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
bool removeKey(SITree* _p_btree, const char* _key, unsigned _klen);
|
|
|
|
bool removeKey(ISTree* _p_btree, unsigned _key);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
static std::vector<unsigned> intersect(const unsigned* _list1, const unsigned* _list2, unsigned _len1, unsigned _len2);
|
|
|
|
static unsigned binarySearch(unsigned key, const unsigned* _list, unsigned _list_len, int step = 1);
|
|
|
|
static bool isEntity(TYPE_ENTITY_LITERAL_ID id);
|
2016-09-25 22:14:36 +08:00
|
|
|
};
|
|
|
|
|
2017-03-23 21:32:41 +08:00
|
|
|
#endif //_KVSTORE_KVSTORE_H
|
2017-03-24 20:10:43 +08:00
|
|
|
|