2016-01-24 18:10:16 -08:00
|
|
|
// Copyright (c) 2019 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2019-04-11 19:10:37 -07:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
2016-01-24 18:10:16 -08:00
|
|
|
#include <iostream>
|
2019-04-12 00:16:52 -07:00
|
|
|
#include <memory>
|
2016-01-24 18:10:16 -08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-11-25 09:29:06 -08:00
|
|
|
#include "gtest/gtest.h"
|
2019-04-12 00:16:52 -07:00
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/write_batch.h"
|
2019-11-21 13:09:53 -08:00
|
|
|
#include "util/testutil.h"
|
2016-01-24 18:10:16 -08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-04-11 19:10:37 -07:00
|
|
|
// Creates a random number in the range of [0, max).
|
|
|
|
int GenerateRandomNumber(int max) { return std::rand() % max; }
|
2016-01-24 18:10:16 -08:00
|
|
|
|
2019-04-11 19:10:37 -07:00
|
|
|
std::string CreateRandomString(int32_t index) {
|
|
|
|
static const size_t len = 1024;
|
2016-01-24 18:10:16 -08:00
|
|
|
char bytes[len];
|
2019-04-11 19:10:37 -07:00
|
|
|
size_t i = 0;
|
2016-01-24 18:10:16 -08:00
|
|
|
while (i < 8) {
|
|
|
|
bytes[i] = 'a' + ((index >> (4 * i)) & 0xf);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
while (i < sizeof(bytes)) {
|
2019-04-11 19:10:37 -07:00
|
|
|
bytes[i] = 'a' + GenerateRandomNumber(26);
|
2016-01-24 18:10:16 -08:00
|
|
|
++i;
|
|
|
|
}
|
2019-04-11 19:10:37 -07:00
|
|
|
return std::string(bytes, sizeof(bytes));
|
2016-01-24 18:10:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(Issue320, Test) {
|
|
|
|
std::srand(0);
|
|
|
|
|
|
|
|
bool delete_before_put = false;
|
|
|
|
bool keep_snapshots = true;
|
|
|
|
|
2019-04-11 19:47:16 -07:00
|
|
|
std::vector<std::unique_ptr<std::pair<std::string, std::string>>> test_map(
|
|
|
|
10000);
|
2019-04-11 19:10:37 -07:00
|
|
|
std::vector<Snapshot const*> snapshots(100, nullptr);
|
2016-01-24 18:10:16 -08:00
|
|
|
|
|
|
|
DB* db;
|
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
std::string dbpath = testing::TempDir() + "leveldb_issue320_test";
|
|
|
|
ASSERT_LEVELDB_OK(DB::Open(options, dbpath, &db));
|
2016-01-24 18:10:16 -08:00
|
|
|
|
2019-04-11 19:10:37 -07:00
|
|
|
uint32_t target_size = 10000;
|
|
|
|
uint32_t num_items = 0;
|
|
|
|
uint32_t count = 0;
|
|
|
|
std::string key;
|
|
|
|
std::string value, old_value;
|
2016-01-24 18:10:16 -08:00
|
|
|
|
|
|
|
WriteOptions writeOptions;
|
|
|
|
ReadOptions readOptions;
|
|
|
|
while (count < 200000) {
|
|
|
|
if ((++count % 1000) == 0) {
|
2019-04-12 01:09:39 -07:00
|
|
|
std::cout << "count: " << count << std::endl;
|
2016-01-24 18:10:16 -08:00
|
|
|
}
|
|
|
|
|
2019-04-11 19:10:37 -07:00
|
|
|
int index = GenerateRandomNumber(test_map.size());
|
2016-01-24 18:10:16 -08:00
|
|
|
WriteBatch batch;
|
|
|
|
|
|
|
|
if (test_map[index] == nullptr) {
|
|
|
|
num_items++;
|
2019-04-12 01:09:39 -07:00
|
|
|
test_map[index].reset(new std::pair<std::string, std::string>(
|
|
|
|
CreateRandomString(index), CreateRandomString(index)));
|
2016-01-24 18:10:16 -08:00
|
|
|
batch.Put(test_map[index]->first, test_map[index]->second);
|
|
|
|
} else {
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(
|
|
|
|
db->Get(readOptions, test_map[index]->first, &old_value));
|
2016-01-24 18:10:16 -08:00
|
|
|
if (old_value != test_map[index]->second) {
|
2019-04-12 01:09:39 -07:00
|
|
|
std::cout << "ERROR incorrect value returned by Get" << std::endl;
|
|
|
|
std::cout << " count=" << count << std::endl;
|
|
|
|
std::cout << " old value=" << old_value << std::endl;
|
|
|
|
std::cout << " test_map[index]->second=" << test_map[index]->second
|
|
|
|
<< std::endl;
|
|
|
|
std::cout << " test_map[index]->first=" << test_map[index]->first
|
|
|
|
<< std::endl;
|
|
|
|
std::cout << " index=" << index << std::endl;
|
2016-01-24 18:10:16 -08:00
|
|
|
ASSERT_EQ(old_value, test_map[index]->second);
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:10:37 -07:00
|
|
|
if (num_items >= target_size && GenerateRandomNumber(100) > 30) {
|
2016-01-24 18:10:16 -08:00
|
|
|
batch.Delete(test_map[index]->first);
|
|
|
|
test_map[index] = nullptr;
|
|
|
|
--num_items;
|
|
|
|
} else {
|
2019-04-11 19:10:37 -07:00
|
|
|
test_map[index]->second = CreateRandomString(index);
|
2016-01-24 18:10:16 -08:00
|
|
|
if (delete_before_put) batch.Delete(test_map[index]->first);
|
|
|
|
batch.Put(test_map[index]->first, test_map[index]->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(db->Write(writeOptions, &batch));
|
2016-01-24 18:10:16 -08:00
|
|
|
|
2019-04-11 19:10:37 -07:00
|
|
|
if (keep_snapshots && GenerateRandomNumber(10) == 0) {
|
|
|
|
int i = GenerateRandomNumber(snapshots.size());
|
2016-01-24 18:10:16 -08:00
|
|
|
if (snapshots[i] != nullptr) {
|
|
|
|
db->ReleaseSnapshot(snapshots[i]);
|
|
|
|
}
|
|
|
|
snapshots[i] = db->GetSnapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Snapshot const* snapshot : snapshots) {
|
|
|
|
if (snapshot) {
|
|
|
|
db->ReleaseSnapshot(snapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete db;
|
|
|
|
DestroyDB(dbpath, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace leveldb
|