2016-09-25 22:14:36 +08:00
|
|
|
/*=============================================================================
|
|
|
|
# Filename: StringIndex.h
|
|
|
|
# Author: Jiaqi, Chen
|
|
|
|
# Mail: chenjiaqi93@163.com
|
|
|
|
# Last Modified: 2016-09-15
|
|
|
|
# Description:
|
|
|
|
=============================================================================*/
|
|
|
|
|
|
|
|
#ifndef _STRING_INDEX_H
|
|
|
|
#define _STRING_INDEX_H
|
|
|
|
|
|
|
|
#include "../KVstore/KVstore.h"
|
|
|
|
#include "../Util/Util.h"
|
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
//TODO: adjust the type
|
|
|
|
|
2016-09-25 22:14:36 +08:00
|
|
|
class StringIndexFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const long MAX_BLOCK_SIZE = 10000000;
|
|
|
|
enum StringIndexFileType {Entity, Literal, Predicate};
|
|
|
|
private:
|
|
|
|
StringIndexFileType type;
|
|
|
|
std::string loc;
|
2017-06-06 18:56:46 +08:00
|
|
|
unsigned num;
|
2016-09-25 22:14:36 +08:00
|
|
|
long empty_offset;
|
|
|
|
FILE *index_file, *value_file;
|
|
|
|
|
|
|
|
class IndexInfo
|
|
|
|
{
|
|
|
|
public:
|
2017-06-06 18:56:46 +08:00
|
|
|
IndexInfo(long _offset = 0, unsigned _length = 0):offset(_offset), length(_length){}
|
2016-09-25 22:14:36 +08:00
|
|
|
long offset;
|
2017-06-06 18:56:46 +08:00
|
|
|
unsigned length;
|
2016-09-25 22:14:36 +08:00
|
|
|
};
|
|
|
|
std::vector<IndexInfo> index_table;
|
|
|
|
|
2017-06-06 18:56:46 +08:00
|
|
|
unsigned buffer_size;
|
2016-09-25 22:14:36 +08:00
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
class AccessRequest
|
|
|
|
{
|
|
|
|
public:
|
2017-06-06 18:56:46 +08:00
|
|
|
unsigned id;
|
2016-09-25 22:14:36 +08:00
|
|
|
long offset;
|
2017-06-06 18:56:46 +08:00
|
|
|
unsigned length;
|
2016-09-25 22:14:36 +08:00
|
|
|
std::string *str;
|
2017-06-06 18:56:46 +08:00
|
|
|
AccessRequest(unsigned _id, long _offset, unsigned _length, std::string *_str):
|
2016-09-25 22:14:36 +08:00
|
|
|
id(_id), offset(_offset), length(_length), str(_str){};
|
|
|
|
inline bool operator < (const AccessRequest &x) const
|
|
|
|
{
|
|
|
|
return this->offset < x.offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::vector<AccessRequest> request;
|
|
|
|
|
|
|
|
public:
|
2017-06-06 18:56:46 +08:00
|
|
|
StringIndexFile(StringIndexFileType _type, std::string _dir, unsigned _num):type(_type), num(_num), empty_offset(0), index_file(NULL), value_file(NULL), buffer_size(0), buffer(NULL)
|
2016-09-25 22:14:36 +08:00
|
|
|
{
|
|
|
|
if (this->type == Entity)
|
|
|
|
this->loc = _dir + "/entity_";
|
|
|
|
if (this->type == Literal)
|
|
|
|
this->loc = _dir + "/literal_";
|
|
|
|
if (this->type == Predicate)
|
|
|
|
this->loc = _dir + "/predicate_";
|
|
|
|
}
|
|
|
|
~StringIndexFile()
|
|
|
|
{
|
|
|
|
if (this->index_file != NULL)
|
|
|
|
fclose(this->index_file);
|
|
|
|
if (this->value_file != NULL)
|
|
|
|
fclose(this->value_file);
|
|
|
|
if (this->buffer != NULL)
|
|
|
|
delete[] this->buffer;
|
|
|
|
}
|
2017-06-06 18:56:46 +08:00
|
|
|
void setNum(unsigned _num);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
|
|
|
void save(KVstore &kv_store);
|
|
|
|
void load();
|
|
|
|
|
2017-06-06 18:56:46 +08:00
|
|
|
inline void allocBuffer(unsigned length)
|
2016-09-25 22:14:36 +08:00
|
|
|
{
|
|
|
|
if (this->buffer_size <= length)
|
|
|
|
{
|
|
|
|
delete[] this->buffer;
|
|
|
|
this->buffer_size = length + 1;
|
|
|
|
this->buffer = new char[this->buffer_size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:56:46 +08:00
|
|
|
bool randomAccess(unsigned id, std::string *str);
|
|
|
|
void addRequest(unsigned id, std::string *str);
|
2016-09-25 22:14:36 +08:00
|
|
|
void trySequenceAccess();
|
|
|
|
|
2017-06-06 18:56:46 +08:00
|
|
|
void change(unsigned id, KVstore &kv_store);
|
|
|
|
void disable(unsigned id);
|
2016-09-25 22:14:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class StringIndex
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
StringIndexFile entity, literal, predicate;
|
2017-01-16 14:12:57 +08:00
|
|
|
Buffer* entity_buffer;
|
|
|
|
unsigned entity_buffer_size;
|
|
|
|
Buffer* literal_buffer;
|
|
|
|
unsigned literal_buffer_size;
|
2016-09-25 22:14:36 +08:00
|
|
|
public:
|
2017-06-06 18:56:46 +08:00
|
|
|
StringIndex(std::string _dir, unsigned _entity_num = 0, unsigned _literal_num = 0, unsigned _predicate_num = 0) :
|
2016-09-25 22:14:36 +08:00
|
|
|
entity(StringIndexFile::Entity, _dir, _entity_num), literal(StringIndexFile::Literal, _dir, _literal_num), predicate(StringIndexFile::Predicate, _dir, _predicate_num){}
|
|
|
|
|
2017-01-16 14:12:57 +08:00
|
|
|
void setBuffer(Buffer* _ebuf, Buffer* _lbuf)
|
|
|
|
{
|
|
|
|
this->entity_buffer = _ebuf;
|
|
|
|
this->entity_buffer_size = _ebuf->size;
|
|
|
|
this->literal_buffer = _lbuf;
|
|
|
|
this->literal_buffer_size = _lbuf->size;
|
|
|
|
}
|
2017-06-06 18:56:46 +08:00
|
|
|
bool searchBuffer(unsigned _id, std::string* _str);
|
2017-01-16 14:12:57 +08:00
|
|
|
|
2017-06-06 18:56:46 +08:00
|
|
|
void setNum(StringIndexFile::StringIndexFileType _type, unsigned _num);
|
2016-09-25 22:14:36 +08:00
|
|
|
|
|
|
|
void save(KVstore &kv_store);
|
|
|
|
void load();
|
|
|
|
|
2017-06-06 18:56:46 +08:00
|
|
|
bool randomAccess(unsigned id, std::string *str, bool is_entity_or_literal = true);
|
|
|
|
void addRequest(unsigned id, std::string *str, bool is_entity_or_literal = true);
|
2016-09-25 22:14:36 +08:00
|
|
|
void trySequenceAccess();
|
|
|
|
|
2017-03-24 20:10:43 +08:00
|
|
|
void change(std::vector<unsigned> &ids, KVstore &kv_store, bool is_entity_or_literal = true);
|
|
|
|
void disable(std::vector<unsigned> &ids, bool is_entity_or_literal = true);
|
2016-09-25 22:14:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _STRING_INDEX_H
|