Skip to content

Commit

Permalink
Merge pull request e8tools#227 from andreevlex/feature/issue-225
Browse files Browse the repository at this point in the history
Feature/issue 225
  • Loading branch information
dmpas authored Mar 10, 2018
2 parents b8dfd12 + a5b36ab commit 248b8d5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 86 deletions.
112 changes: 59 additions & 53 deletions src/tool1cd/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ static const uint32_t LAST_PAGE = std::numeric_limits<uint32_t>::max();
Index::Index(Table* _base)
{
tbase = _base;
is_primary = false;
num_records = 0;
records = nullptr;
primary = false;
start = 0;
rootblock = 0;
length = 0;
Expand All @@ -34,7 +32,6 @@ Index::Index(Table* _base)
//---------------------------------------------------------------------------
Index::~Index()
{
delete[] records;
}

//---------------------------------------------------------------------------
Expand All @@ -44,19 +41,13 @@ string Index::getname() const
}

//---------------------------------------------------------------------------
bool Index::get_is_primary() const
size_t Index::get_num_records() const
{
return is_primary;
return records.size();
}

//---------------------------------------------------------------------------
int32_t Index::get_num_records() const
{
return num_records;
}

//---------------------------------------------------------------------------
index_record* Index::get_records()
std::vector<IndexRecord>& Index::get_records()
{
return records;
}
Expand Down Expand Up @@ -155,12 +146,12 @@ void Index::dump_recursive(V8Object* file_index, TFileStream* f, int32_t level,

char* curindex;

leaf_page_header* lph;
branch_page_header* bph;
LeafPageHeader* lph;
BranchPageHeader* bph;

buf = new char[pagesize];
lph = (leaf_page_header*)buf;
bph = (branch_page_header*)buf;
lph = (LeafPageHeader*)buf;
bph = (BranchPageHeader*)buf;
file_index->getdata(buf, curblock, pagesize);
curlen = bph->number_indexes;
if(curlen)
Expand Down Expand Up @@ -333,6 +324,16 @@ uint32_t Index::get_length() const
return length;
}

uint64_t Index::get_start_offset() const
{
return start;
}

void Index::start_offset(const uint64_t value)
{
start = value;
}

//---------------------------------------------------------------------------
void Index::dump(const string &_filename)
{
Expand Down Expand Up @@ -387,7 +388,7 @@ char* Index::unpack_leafpage(char* page, uint32_t& number_indexes)
char* rbuf;
char* ibuf;
char* obuf;
leaf_page_header* header;
LeafPageHeader* header;

uint32_t i, j, step;

Expand All @@ -397,7 +398,7 @@ char* Index::unpack_leafpage(char* page, uint32_t& number_indexes)
return nullptr;
}

header = (leaf_page_header*)page;
header = (LeafPageHeader*)page;

if(!(header->flags & indexpage_is_leaf))
{
Expand Down Expand Up @@ -459,7 +460,7 @@ bool Index::pack_leafpage(char* unpack_index, uint32_t number_indexes, char* pag
uint32_t min_bits;
uint32_t max_numrec;

leaf_page_header* header;
LeafPageHeader* header;
uint32_t freebytes;
uint32_t numrecmask;
uint32_t leftmask;
Expand Down Expand Up @@ -511,7 +512,7 @@ bool Index::pack_leafpage(char* unpack_index, uint32_t number_indexes, char* pag

if(left + right > length)
{
if(left < length || is_primary)
if(left < length || primary)
{
throw DetailedException("Ошибка упаковки индексов на странице-листе. Индекс не уникальный либо неверно отсортирован.")
.add_detail("Таблица", tbase->name)
Expand Down Expand Up @@ -559,7 +560,7 @@ bool Index::pack_leafpage(char* unpack_index, uint32_t number_indexes, char* pag
for(i = 0, leftmask = 0; i < leftbits; i++, leftmask = (leftmask << 1) | 1);
for(i = 0, rightmask = 0; i < rightbits; i++, rightmask = (rightmask << 1) | 1);

header = (leaf_page_header*)page_buf;
header = (LeafPageHeader*)page_buf;
header->flags = indexpage_is_leaf;
header->number_indexes = number_indexes;
header->freebytes = freebytes;
Expand All @@ -586,19 +587,24 @@ bool Index::pack_leafpage(char* unpack_index, uint32_t number_indexes, char* pag
return true;
}

bool Index::is_primary() const
{
return primary;
}

//---------------------------------------------------------------------------
void Index::calcRecordIndex(const TableRecord *rec, char *indexBuf)
{
int32_t i, j, k;

j = length;
for(i = 0; i < num_records; i++)
{
k = records[i].field->getSortKey(rec->get_raw(records[i].field), (unsigned char *)indexBuf, j);
int32_t index_buf_size = length;
for( const auto& record : records) {
uint32_t k = record.field->getSortKey(rec->get_raw(record.field), (unsigned char *)indexBuf, index_buf_size);
indexBuf += k;
j -= k;
index_buf_size -= k;
}

if(index_buf_size > 0) {
memset(indexBuf, 0, index_buf_size);
}
if(j) memset(indexBuf, 0, j);
}

//---------------------------------------------------------------------------
Expand All @@ -625,8 +631,8 @@ void Index::delete_index_record(const char* index_buf, const uint32_t phys_numre
void Index::delete_index_record(const char* index_buf, const uint32_t phys_numrec, uint64_t block, bool& is_last_record, bool& page_is_empty, char* new_last_index_buf, uint32_t& new_last_phys_num)
{
char* page;
branch_page_header* bph;
leaf_page_header* lph;
BranchPageHeader* bph;
LeafPageHeader* lph;

bool _is_last_record, _page_is_empty;
uint32_t _new_last_phys_num;
Expand All @@ -647,7 +653,7 @@ void Index::delete_index_record(const char* index_buf, const uint32_t phys_numre
if(*page & indexpage_is_leaf)
{
// страница-лист
lph = (leaf_page_header*)page;
lph = (LeafPageHeader*)page;
flags = lph->flags;
unpack_indexes_buf = unpack_leafpage(page, number_indexes);
cur_index = unpack_indexes_buf;
Expand Down Expand Up @@ -701,9 +707,9 @@ void Index::delete_index_record(const char* index_buf, const uint32_t phys_numre
else
{
// страница-ветка
bph = (branch_page_header*)page;
bph = (BranchPageHeader*)page;

cur_index = page + 12; // 12 = size_of(branch_page_header)
cur_index = page + BranchPageHeader::Size();
delta = length + 8;
for(i = 0; i < bph->number_indexes; i++, cur_index += delta)
{
Expand Down Expand Up @@ -786,7 +792,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
uint64_t new_last_block2;

char* page;
branch_page_header* bph;
BranchPageHeader* bph;
uint64_t block;
uint32_t k;
char* cur_index;
Expand All @@ -797,7 +803,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
{
page = new char[pagesize];
memset(page, 0, pagesize);
bph = (branch_page_header*)page;
bph = (BranchPageHeader*)page;

tbase->file_index->getdata(&k, 0, 4);
if(k)
Expand Down Expand Up @@ -848,8 +854,8 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
// 2 - произошло разбиение на 2 страницы, надо заменить на 2 записи

char* page;
branch_page_header* bph;
leaf_page_header* lph;
BranchPageHeader* bph;
LeafPageHeader* lph;

int32_t _result;
uint32_t _new_last_phys_num;
Expand All @@ -865,8 +871,8 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
uint64_t next_page;


branch_page_header* bph2;
leaf_page_header* lph2;
BranchPageHeader* bph2;
LeafPageHeader* lph2;
uint32_t number_indexes1;
uint32_t number_indexes2;
uint64_t block1;
Expand All @@ -882,7 +888,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
tbase->file_index->getdata(page, block, pagesize);
result = 0;

bph = (branch_page_header*)page;
bph = (BranchPageHeader*)page;
flags = bph->flags;
number_indexes = bph->number_indexes;
prev_page = bph->prev_page;
Expand All @@ -891,7 +897,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
if(flags & indexpage_is_leaf)
{
// страница-лист
lph = (leaf_page_header*)page;
lph = (LeafPageHeader*)page;
unpack_indexes_buf = unpack_leafpage(page, number_indexes);
cur_index = unpack_indexes_buf;
delta = length + 4;
Expand All @@ -903,7 +909,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_

if(j == 0)
{
if(is_primary || *(uint32_t*)cur_index == phys_numrecord)
if(primary || *(uint32_t*)cur_index == phys_numrecord)
{
throw DetailedException("Ошибка записи индекса. Индекс уже существует.")
.add_detail("Таблица", tbase->name)
Expand Down Expand Up @@ -947,7 +953,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
number_indexes2 = number_indexes - number_indexes1;
pack_leafpage(unpack_indexes_buf_new, number_indexes1, page);
pack_leafpage(unpack_indexes_buf_new + number_indexes1 * delta, number_indexes2, page2);
lph2 = (leaf_page_header*)page2;
lph2 = (LeafPageHeader*)page2;

tbase->file_index->getdata(&k, 0, 4);
if(k)
Expand Down Expand Up @@ -989,7 +995,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
{
// страница-ветка

cur_index = page + 12; // 12 = size_of(branch_page_header)
cur_index = page + BranchPageHeader::Size();
delta = length + 8;
max_num_indexes = (pagesize - 12) / delta;

Expand All @@ -999,7 +1005,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
j = memcmp(index_buf, cur_index, length);
if(j == 0)
{
if(is_primary)
if(primary)
{
throw DetailedException("Ошибка записи индекса. Индекс уже существует.")
.add_detail("Таблица", tbase->name)
Expand Down Expand Up @@ -1084,7 +1090,7 @@ void Index::write_index_record(const uint32_t phys_numrecord, const char* index_
number_indexes1 = number_indexes >> 1;
number_indexes2 = number_indexes - number_indexes1;

bph2 = (branch_page_header*)page2;
bph2 = (BranchPageHeader*)page2;
memset(page, 0, pagesize);
memset(page2, 0, pagesize);

Expand Down Expand Up @@ -1182,7 +1188,6 @@ Index *Index::index_from_tree(tree *f, Table *parent)
throw IndexReadError("Ошибка получения очередного индекса таблицы. Нет узлов описаня полей индекса.");
}
Index *ind = new Index(parent);
ind->num_records = numrec;

if (f->get_type() != node_type::nd_list) {
throw IndexReadError("Ошибка получения очередного индекса таблицы. Узел не является деревом.");
Expand All @@ -1199,14 +1204,15 @@ Index *Index::index_from_tree(tree *f, Table *parent)
throw IndexReadError("Ошибка получения типа индекса таблицы. Узел не является числом.");
}
string sIsPrimaryIndex = index_tree->get_value();
if (sIsPrimaryIndex == "0") ind->is_primary = false;
else if(sIsPrimaryIndex == "1") ind->is_primary = true;
if (sIsPrimaryIndex == "0") ind->primary = false;
else if(sIsPrimaryIndex == "1") ind->primary = true;
else {
throw IndexReadError("Неизвестный тип индекса таблицы.", ind->name)
.add_detail("Тип индекса", sIsPrimaryIndex);
}

ind->records = new index_record[numrec];
ind->records.clear();
ind->records.resize(numrec);
for (int j = 0; j < numrec; j++) {
index_tree = index_tree->get_next();
if (index_tree->get_num_subnode() != 2) {
Expand All @@ -1225,7 +1231,7 @@ Index *Index::index_from_tree(tree *f, Table *parent)
string field_name = in->get_value();
ind->records[j].field = parent->find_field(field_name);

if (!ind->records[j].field) {
if (ind->records[j].field == nullptr) {
throw IndexReadError("Ошибка получения индекса таблицы. Не найдено поле таблицы по имени поля индекса.", ind->name)
.add_detail("Номер поля индекса", j + 1)
.add_detail("Поле индекса", field_name);
Expand Down
Loading

0 comments on commit 248b8d5

Please sign in to comment.