2016-09-16 16:56:18 +08:00
|
|
|
/*=============================================================================
|
2017-01-16 14:12:57 +08:00
|
|
|
# Filename: ISTree.h
|
2016-09-16 16:56:18 +08:00
|
|
|
# Author: syzz
|
|
|
|
# Mail: 1181955272@qq.com
|
|
|
|
# Last Modified: 2015-04-26 16:44
|
2017-03-28 16:56:16 +08:00
|
|
|
# Description: ID2string, including id2entity, id2literal and id2predicate
|
2016-09-16 16:56:18 +08:00
|
|
|
=============================================================================*/
|
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
#ifndef _KVSTORE_ISTREE_ISTREE_H
|
|
|
|
#define _KVSTORE_ISTREE_ISTREE_H
|
2016-09-16 16:56:18 +08:00
|
|
|
|
|
|
|
#include "../../Util/Util.h"
|
|
|
|
#include "../../Util/Stream.h"
|
2017-01-16 14:12:57 +08:00
|
|
|
#include "node/ISNode.h"
|
|
|
|
#include "node/ISIntlNode.h"
|
|
|
|
#include "node/ISLeafNode.h"
|
|
|
|
#include "storage/ISStorage.h"
|
2016-09-16 16:56:18 +08:00
|
|
|
|
|
|
|
class ISTree
|
2017-01-16 14:12:57 +08:00
|
|
|
{
|
2016-09-16 16:56:18 +08:00
|
|
|
protected:
|
2017-03-24 20:10:43 +08:00
|
|
|
unsigned height; //0 indicates an empty tree
|
2016-09-16 16:56:18 +08:00
|
|
|
ISNode* root;
|
|
|
|
ISNode* leaves_head; //the head of LeafNode-list
|
|
|
|
ISNode* leaves_tail; //the tail of LeafNode-list
|
|
|
|
std::string mode; //BETTER(to use enum)
|
|
|
|
ISStorage* TSM; //Tree-Storage-Manage
|
2017-01-16 14:12:57 +08:00
|
|
|
//BETTER:multiple stream maybe needed:)
|
2016-09-16 16:56:18 +08:00
|
|
|
Stream* stream;
|
|
|
|
|
|
|
|
//always alloc one more byte than length, then user can add a '\0'
|
|
|
|
//to get a real string, instead of new and copy
|
|
|
|
//other operations will be harmful to search, so store value in
|
|
|
|
//transfer temporally, while length adjusted.
|
|
|
|
//TODO: in multi-user case, multiple-search will cause problem,
|
|
|
|
//so lock is a must. Add lock to transfer is better than to add
|
|
|
|
//lock to every key/value. However, modify requires a lock for a
|
|
|
|
//key/value, and multiple search for different keys are ok!!!
|
2017-03-29 23:57:09 +08:00
|
|
|
//Bstr transfer[3]; //0:transfer value searched; 1:copy key-data from const char*; 2:copy val-data from const char*
|
|
|
|
//unsigned transfer_size[3];
|
2016-09-16 16:56:18 +08:00
|
|
|
|
|
|
|
//tree's operations should be atom(if read nodes)
|
|
|
|
//sum the request and send to ISStorage at last
|
|
|
|
//ensure that all nodes operated are in memory
|
|
|
|
long long request;
|
|
|
|
void prepare(ISNode* _np);
|
|
|
|
|
|
|
|
std::string storepath;
|
|
|
|
std::string filename; //ok for user to change
|
2017-01-16 14:12:57 +08:00
|
|
|
/* some private functions */
|
2016-09-16 16:56:18 +08:00
|
|
|
std::string getFilePath(); //in UNIX system
|
2017-03-29 23:57:09 +08:00
|
|
|
//void CopyToTransfer(const char* _str, unsigned _len, unsigned _index);
|
2016-09-16 16:56:18 +08:00
|
|
|
void release(ISNode* _np) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ISTree(); //always need to initial transfer
|
2017-01-16 14:12:57 +08:00
|
|
|
ISTree(std::string _storepath, std::string _filename, std::string _mode, unsigned long long _buffer_size);
|
2017-03-24 20:10:43 +08:00
|
|
|
unsigned getHeight() const;
|
2016-09-16 16:56:18 +08:00
|
|
|
void setHeight(unsigned _h);
|
|
|
|
ISNode* getRoot() const;
|
|
|
|
//void setRoot(Node* _root);
|
|
|
|
//insert, search, remove, set
|
2017-03-24 20:10:43 +08:00
|
|
|
bool search(unsigned _key, char*& _str, unsigned& _len);
|
2017-05-17 20:46:02 +08:00
|
|
|
bool insert(unsigned _key, char* _str, unsigned _len);
|
|
|
|
bool modify(unsigned _key, char* _str, unsigned _len);
|
2017-03-24 20:10:43 +08:00
|
|
|
ISNode* find(unsigned _key, int* store, bool ifmodify);
|
|
|
|
bool remove(unsigned _key);
|
2016-09-16 16:56:18 +08:00
|
|
|
const Bstr* getRangeValue();
|
|
|
|
void resetStream();
|
2017-03-24 20:10:43 +08:00
|
|
|
bool range_query(unsigned _key1, unsigned _key2);
|
2017-01-16 14:12:57 +08:00
|
|
|
bool save();
|
2016-09-16 16:56:18 +08:00
|
|
|
~ISTree();
|
|
|
|
void print(std::string s); //DEBUG(print the tree)
|
|
|
|
};
|
|
|
|
//NOTICE: need to save tree manually before delete, otherwise will cause problem.
|
|
|
|
//(problem range between two extremes: not-modified, totally-modified)
|
|
|
|
//After saved, it's ok to continue operations on tree!
|
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
#endif
|
|
|
|
|