2015-11-06 19:13:39 +08:00
|
|
|
/*=============================================================================
|
|
|
|
# Filename: KVstore.h
|
|
|
|
# Author: Bookug Lobert
|
|
|
|
# Mail: 1181955272@qq.com
|
|
|
|
# Last Modified: 2015-10-23 14:23
|
|
|
|
# Description:
|
|
|
|
=============================================================================*/
|
|
|
|
|
|
|
|
#ifndef _KVSTORE_KVSTORE_H
|
|
|
|
#define _KVSTORE_KVSTORE_H
|
|
|
|
|
|
|
|
#include "../Util/Util.h"
|
2015-09-25 15:05:59 +08:00
|
|
|
#include "tree/Tree.h"
|
|
|
|
|
2015-11-06 19:13:39 +08:00
|
|
|
class KVstore
|
|
|
|
{
|
2014-11-21 12:46:37 +08:00
|
|
|
public:
|
|
|
|
static const int READ_WRITE_MODE = 1;
|
|
|
|
static const int CREATE_MODE = 2;
|
|
|
|
|
|
|
|
/* include IN-neighbor & OUT-neighbor */
|
|
|
|
int getEntityDegree(int _entity_id);
|
|
|
|
int getEntityInDegree(int _entity_id);
|
|
|
|
int getEntityOutDegree(int _entity_id);
|
|
|
|
|
|
|
|
/* there are two situation when we need to update tuples list: s2o o2s sp2o op2s s2po o2ps
|
|
|
|
* 1. insert triple
|
|
|
|
* 2. remove triple
|
|
|
|
*/
|
2015-02-02 15:53:47 +08:00
|
|
|
int updateTupleslist_insert(int _sub_id, int _pre_id, int _obj_id);
|
2014-11-21 12:46:37 +08:00
|
|
|
void updateTupleslist_remove(int _sub_id, int _pre_id, int _obj_id);
|
|
|
|
private:
|
|
|
|
|
|
|
|
/* insert <_x_id, _y_id> into _xylist(keep _xylist(<x,y>) in ascending order) */
|
|
|
|
bool insert_xy(int*& _xylist, int& _list_len,int _x_id, int _y_id);
|
|
|
|
|
|
|
|
/* insert _x_id into _xlist(keep _xlist in ascending order) */
|
|
|
|
bool insert_x(int*& _xlist, int& _list_len, int _x_id);
|
|
|
|
|
|
|
|
/* remove _x_id from _xlist */
|
|
|
|
bool remove_x(int*& _xlist, int& _list_len, int _x_id);
|
|
|
|
|
|
|
|
/* remove <_x_id, _y_id> from _xylist */
|
|
|
|
bool remove_xy(int*& _xylist, int& _list_len,int _x_id, int _y_id);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* for entity2id */
|
|
|
|
bool open_entity2id(const int _mode);
|
2015-11-06 19:13:39 +08:00
|
|
|
int getIDByEntity(const std::string _entity);
|
|
|
|
bool setIDByEntity(const std::string _entity, int _id);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* for id2entity */
|
|
|
|
bool open_id2entity(const int _mode);
|
2015-11-06 19:13:39 +08:00
|
|
|
std::string getEntityByID(int _id);
|
|
|
|
bool setEntityByID(int _id, std::string _entity);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
/* for predicate2id */
|
|
|
|
bool open_predicate2id(const int _mode);
|
2015-11-06 19:13:39 +08:00
|
|
|
int getIDByPredicate(const std::string _predicate);
|
|
|
|
bool setIDByPredicate(const std::string _predicate, int _id);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
/* for id2predicate */
|
|
|
|
bool open_id2predicate(const int _mode);
|
2015-11-06 19:13:39 +08:00
|
|
|
std::string getPredicateByID(int _id);
|
|
|
|
bool setPredicateByID(const int _id, std::string _predicate);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
/* for id2literal */
|
|
|
|
bool open_id2literal(const int _mode);
|
2015-11-06 19:13:39 +08:00
|
|
|
std::string getLiteralByID(int _id);
|
|
|
|
bool setLiteralByID(const int _id, std::string _literal);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* for literal2id */
|
|
|
|
bool open_literal2id(const int _mode);
|
2015-11-06 19:13:39 +08:00
|
|
|
int getIDByLiteral(std::string _literal);
|
|
|
|
bool setIDByLiteral(const std::string _literal, int _id);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
/* for subID 2 objIDlist */
|
|
|
|
bool open_subid2objidlist(const int _mode);
|
|
|
|
bool getobjIDlistBysubID(int _subid, int*& _objidlist, int& _list_len);
|
|
|
|
bool setobjIDlistBysubID(int _subid, const int* _objidlist, int _list_len);
|
|
|
|
|
|
|
|
/* for objID 2 subIDlist */
|
|
|
|
bool open_objid2subidlist(const int _mode);
|
|
|
|
bool getsubIDlistByobjID(int _objid, int*& _subidlist, int& _list_len);
|
|
|
|
bool setsubIDlistByobjID(int _objid, const int* _subidlist, int _list_len);
|
|
|
|
|
|
|
|
/* for subID&preID 2 objIDlist */
|
|
|
|
bool open_subIDpreID2objIDlist(const int _mode);
|
|
|
|
bool getobjIDlistBysubIDpreID(int _subid, int _preid, int*& _objidlist, int& _list_len);
|
|
|
|
bool setobjIDlistBysubIDpreID(int _subid, int _preid, const int* _objidlist, int _list_len);
|
|
|
|
|
|
|
|
/* for objID&preID 2 subIDlist */
|
|
|
|
bool open_objIDpreID2subIDlist(const int _mode);
|
|
|
|
bool getsubIDlistByobjIDpreID(int _objid, int _preid, int*& _subidlist, int& _list_len);
|
|
|
|
bool setsubIDlistByobjIDpreID(int _objid, int _preid, const int* _subidlist, int _list_len);
|
|
|
|
|
|
|
|
/* for subID 2 preID&objIDlist */
|
|
|
|
bool open_subID2preIDobjIDlist(const int _mode);
|
|
|
|
bool getpreIDobjIDlistBysubID(int _subid, int*& _preid_objidlist, int& _list_len);
|
|
|
|
bool setpreIDobjIDlistBysubID(int _subid, const int* _preid_objidlist, int _list_len);
|
|
|
|
|
|
|
|
/* for objID 2 preID&subIDlist */
|
|
|
|
bool open_objID2preIDsubIDlist(const int _mode);
|
|
|
|
bool getpreIDsubIDlistByobjID(int _objid, int*& _preid_subidlist, int& _list_len);
|
|
|
|
bool setpreIDsubIDlistByobjID(int _objid, const int* _preid_subidlist, int _list_len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _store_path denotes where to store the data
|
|
|
|
*/
|
2015-11-06 19:13:39 +08:00
|
|
|
KVstore(std::string _store_path = ".");
|
2014-11-21 12:46:37 +08:00
|
|
|
~KVstore();
|
|
|
|
void flush();
|
|
|
|
void release();
|
|
|
|
void open();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-11-06 19:13:39 +08:00
|
|
|
std::string store_path;
|
2014-11-21 12:46:37 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* map entity to its id, and id to the entity
|
|
|
|
* s_entity2id is relative store file name
|
|
|
|
*/
|
2015-09-25 15:05:59 +08:00
|
|
|
Tree* entity2id;
|
|
|
|
Tree* id2entity;
|
2015-11-06 19:13:39 +08:00
|
|
|
static std::string s_entity2id;
|
|
|
|
static std::string s_id2entity;
|
2014-11-21 12:46:37 +08:00
|
|
|
|
2015-09-25 15:05:59 +08:00
|
|
|
Tree* predicate2id;
|
|
|
|
Tree* id2predicate;
|
2015-11-06 19:13:39 +08:00
|
|
|
static std::string s_predicate2id;
|
|
|
|
static std::string s_id2predicate;
|
2014-11-21 12:46:37 +08:00
|
|
|
|
2015-09-25 15:05:59 +08:00
|
|
|
Tree* literal2id;
|
|
|
|
Tree* id2literal;
|
2015-11-06 19:13:39 +08:00
|
|
|
static std::string s_literal2id;
|
|
|
|
static std::string s_id2literal;
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
|
2015-09-25 15:05:59 +08:00
|
|
|
Tree* subID2objIDlist;
|
|
|
|
Tree* objID2subIDlist;
|
2015-11-06 19:13:39 +08:00
|
|
|
static std::string s_sID2oIDlist;
|
|
|
|
static std::string s_oID2sIDlist;
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
/* lack exist in update tuple */
|
2015-09-25 15:05:59 +08:00
|
|
|
Tree* subIDpreID2objIDlist;
|
|
|
|
Tree* objIDpreID2subIDlist;
|
2015-11-06 19:13:39 +08:00
|
|
|
static std::string s_sIDpID2oIDlist;
|
|
|
|
static std::string s_oIDpID2sIDlist;
|
2014-11-21 12:46:37 +08:00
|
|
|
|
2015-09-25 15:05:59 +08:00
|
|
|
Tree* subID2preIDobjIDlist;
|
|
|
|
Tree* objID2preIDsubIDlist;
|
2015-11-06 19:13:39 +08:00
|
|
|
static std::string s_sID2pIDoIDlist;
|
|
|
|
static std::string s_oID2pIDsIDlist;
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
|
2015-09-25 15:05:59 +08:00
|
|
|
void flush(Tree* _p_btree);
|
|
|
|
void release(Tree* _p_btree);
|
|
|
|
bool setValueByKey(Tree* _p_btree, const char* _key, int _klen, const char* _val, int _vlen);
|
|
|
|
bool getValueByKey(Tree* _p_btree, const char* _key, int _klen, char*& _val, int& _vlen);
|
|
|
|
int getIDByStr(Tree* _p_btree, const char* _key, int _klen);
|
|
|
|
bool removeKey(Tree* _p_btree, const char* _key, int _klen);
|
2014-11-21 12:46:37 +08:00
|
|
|
|
|
|
|
/* Open a btree according the mode */
|
|
|
|
/* CREATE_MODE: build a new btree and delete if exist */
|
|
|
|
/* READ_WRITE_MODE: open a btree, btree must exist */
|
2015-11-06 19:13:39 +08:00
|
|
|
bool open(Tree* & _p_btree, const std::string _tree_name, const int _mode);
|
2014-11-21 12:46:37 +08:00
|
|
|
};
|
|
|
|
|
2015-11-06 19:13:39 +08:00
|
|
|
#endif //_KVSTORE_KVSTORE_H
|
2014-11-21 12:46:37 +08:00
|
|
|
|