Код: Выделить всё
private: std::fstream f; // std::ios::in | std::ios::out | std::ios::binary
public: void insert(const char* this_key, long this_value) {
char* that_key;
long that_value;
long this_hash = std::hash{}(this_key) % M;
long that_hash; // also block status
long block = this_hash;
long offset = block * BLOCK_SIZE;
while (true) {
this->f.seekg(offset);
this->f.read((char*) &that_hash, sizeof(long));
if (that_hash > -1) { // -1 (by default) indicates a never allocated block
this->f.read(that_key, BLOCK_SIZE);
if (strcmp(this_key, that_key) == 0) {
this->f.seekp(this->f.tellg());
this->f.write((char*) &this_value, sizeof(long));
break;
} else {
block = (block + 1) % M; // linear probing
offset = block * BLOCK_SIZE;
continue;
}
} else {
this->f.seekp(offset);
this->f.write((char*) &this_hash, sizeof(long)); // as block status
this->f.write(this_key, KEY_SIZE);
this->f.write((char*) &this_value, sizeof(long));
break;
}
}
}
< /code>
Тесты до 10 м, пары значений с 50 000 017 блоков были справедливо. (Бинарный размер файла составлял около 3,8 ГБ). < /P>
Однако тест с 50 -метровыми ключами и 250 000 013 блоков чрезвычайно замедляется ... (размер двоичного файла был более чем 19 ГБ в этом случае). 1000 INSERT [*] Что вызывает этот исключительный бинарный файл ввода /вывода?
Код: Выделить всё
seekp[*] Как клавиши, запасы значений и базы данных избегают этого ввода/вывода?
Как я могу решить эту проблему?>
Подробнее здесь: https://stackoverflow.com/questions/594 ... nary-files
Мобильная версия