Skip to content

Commit

Permalink
Format code and cleanup issue320_test.
Browse files Browse the repository at this point in the history
Changes to issue320_test:

1. Releasing snapshots during test cleanup to avoid
   memory leak warnings.
2. Refactored test to use testutil.h to be in line
   with other issue tests and to create the test
   database in the correct temporary location.
3. Added copyright banner.

Otherwise, just minor formatting and limiting character
width to 80 characters.
  • Loading branch information
cmumford committed Mar 18, 2019
1 parent 6a3656b commit 4b72cb1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 74 deletions.
17 changes: 9 additions & 8 deletions db/version_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1397,26 +1397,27 @@ FileMetaData* FindSmallestBoundaryFile(const InternalKeyComparator & icmp,
// parameters:
// in levelFiles: list of files to search for boundary files
// in/out compactionFiles: list of files to extend by adding boundary files
void AddBoundaryInputs(const InternalKeyComparator & icmp,
const std::vector<FileMetaData*> & levelFiles,
std::vector<FileMetaData*> * compactionFiles) {
void AddBoundaryInputs(const InternalKeyComparator& icmp,
const std::vector<FileMetaData*>& levelFiles,
std::vector<FileMetaData*>* compactionFiles) {
InternalKey largestKey;

// find largestKey in compactionFiles, quick return if compactionFiles is empty
if (! FindLargestKey(icmp, *compactionFiles, &largestKey)) {
// find largestKey in compactionFiles, quick return if compactionFiles is
// empty
if (!FindLargestKey(icmp, *compactionFiles, &largestKey)) {
return;
}

bool continueSearching = true;
while (continueSearching) {
FileMetaData* smallestBoundaryFile = FindSmallestBoundaryFile(icmp, levelFiles, largestKey);
FileMetaData* smallestBoundaryFile =
FindSmallestBoundaryFile(icmp, levelFiles, largestKey);

// if a boundary file was found advance largestKey, otherwise we're done
if (smallestBoundaryFile != NULL) {
compactionFiles->push_back(smallestBoundaryFile);
largestKey = smallestBoundaryFile->largest;
}
else {
} else {
continueSearching = false;
}
}
Expand Down
92 changes: 64 additions & 28 deletions db/version_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,28 @@ TEST(FindFileTest, OverlappingFiles) {
ASSERT_TRUE(Overlaps("600", "700"));
}

void AddBoundaryInputs(const InternalKeyComparator & icmp, const std::vector<FileMetaData*> & levelFiles, std::vector<FileMetaData*> * compactionFiles);
void AddBoundaryInputs(const InternalKeyComparator &icmp,
const std::vector<FileMetaData *> &levelFiles,
std::vector<FileMetaData *> *compactionFiles);

class AddBoundaryInputsTest {
public:
std::vector<FileMetaData*> levelFiles_;
std::vector<FileMetaData*> compactionFiles_;
std::vector<FileMetaData*> allFiles_;
std::vector<FileMetaData *> levelFiles_;
std::vector<FileMetaData *> compactionFiles_;
std::vector<FileMetaData *> allFiles_;
InternalKeyComparator icmp_;

AddBoundaryInputsTest() :
icmp_(BytewiseComparator())
{};
AddBoundaryInputsTest() : icmp_(BytewiseComparator()){};

~AddBoundaryInputsTest()
{
~AddBoundaryInputsTest() {
for (size_t i = 0; i < allFiles_.size(); ++i) {
delete allFiles_[i];
}
allFiles_.clear();
};

FileMetaData *CreateFileMetaData(uint64_t number, InternalKey smallest, InternalKey largest) {
FileMetaData *CreateFileMetaData(uint64_t number, InternalKey smallest,
InternalKey largest) {
FileMetaData *f = new FileMetaData();
f->number = number;
f->smallest = smallest;
Expand All @@ -210,7 +210,9 @@ TEST(AddBoundaryInputsTest, TestEmptyFileSets) {
}

TEST(AddBoundaryInputsTest, TestEmptyLevelFiles) {
FileMetaData *f1 = CreateFileMetaData(1, InternalKey("100", 2, kTypeValue), InternalKey(InternalKey("100", 1, kTypeValue)));
FileMetaData *f1 =
CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
InternalKey(InternalKey("100", 1, kTypeValue)));
compactionFiles_.push_back(f1);

AddBoundaryInputs(icmp_, levelFiles_, &compactionFiles_);
Expand All @@ -220,7 +222,9 @@ TEST(AddBoundaryInputsTest, TestEmptyLevelFiles) {
}

TEST(AddBoundaryInputsTest, TestEmptyCompactionFiles) {
FileMetaData *f1 = CreateFileMetaData(1, InternalKey("100", 2, kTypeValue), InternalKey(InternalKey("100", 1, kTypeValue)));
FileMetaData *f1 =
CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
InternalKey(InternalKey("100", 1, kTypeValue)));
levelFiles_.push_back(f1);

AddBoundaryInputs(icmp_, levelFiles_, &compactionFiles_);
Expand All @@ -230,9 +234,15 @@ TEST(AddBoundaryInputsTest, TestEmptyCompactionFiles) {
}

TEST(AddBoundaryInputsTest, TestNoBoundaryFiles) {
FileMetaData *f1 = CreateFileMetaData(1, InternalKey("100", 2, kTypeValue), InternalKey(InternalKey("100", 1, kTypeValue)));
FileMetaData *f2 = CreateFileMetaData(1, InternalKey("200", 2, kTypeValue), InternalKey(InternalKey("200", 1, kTypeValue)));
FileMetaData *f3 = CreateFileMetaData(1, InternalKey("300", 2, kTypeValue), InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f1 =
CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
InternalKey(InternalKey("100", 1, kTypeValue)));
FileMetaData *f2 =
CreateFileMetaData(1, InternalKey("200", 2, kTypeValue),
InternalKey(InternalKey("200", 1, kTypeValue)));
FileMetaData *f3 =
CreateFileMetaData(1, InternalKey("300", 2, kTypeValue),
InternalKey(InternalKey("300", 1, kTypeValue)));

levelFiles_.push_back(f3);
levelFiles_.push_back(f2);
Expand All @@ -245,9 +255,15 @@ TEST(AddBoundaryInputsTest, TestNoBoundaryFiles) {
}

TEST(AddBoundaryInputsTest, TestOneBoundaryFiles) {
FileMetaData *f1 = CreateFileMetaData(1, InternalKey("100", 3, kTypeValue), InternalKey(InternalKey("100", 2, kTypeValue)));
FileMetaData *f2 = CreateFileMetaData(1, InternalKey("100", 1, kTypeValue), InternalKey(InternalKey("200", 3, kTypeValue)));
FileMetaData *f3 = CreateFileMetaData(1, InternalKey("300", 2, kTypeValue), InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f1 =
CreateFileMetaData(1, InternalKey("100", 3, kTypeValue),
InternalKey(InternalKey("100", 2, kTypeValue)));
FileMetaData *f2 =
CreateFileMetaData(1, InternalKey("100", 1, kTypeValue),
InternalKey(InternalKey("200", 3, kTypeValue)));
FileMetaData *f3 =
CreateFileMetaData(1, InternalKey("300", 2, kTypeValue),
InternalKey(InternalKey("300", 1, kTypeValue)));

levelFiles_.push_back(f3);
levelFiles_.push_back(f2);
Expand All @@ -261,9 +277,15 @@ TEST(AddBoundaryInputsTest, TestOneBoundaryFiles) {
}

TEST(AddBoundaryInputsTest, TestTwoBoundaryFiles) {
FileMetaData *f1 = CreateFileMetaData(1, InternalKey("100", 6, kTypeValue), InternalKey(InternalKey("100", 5, kTypeValue)));
FileMetaData *f2 = CreateFileMetaData(1, InternalKey("100", 2, kTypeValue), InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f3 = CreateFileMetaData(1, InternalKey("100", 4, kTypeValue), InternalKey(InternalKey("100", 3, kTypeValue)));
FileMetaData *f1 =
CreateFileMetaData(1, InternalKey("100", 6, kTypeValue),
InternalKey(InternalKey("100", 5, kTypeValue)));
FileMetaData *f2 =
CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f3 =
CreateFileMetaData(1, InternalKey("100", 4, kTypeValue),
InternalKey(InternalKey("100", 3, kTypeValue)));

levelFiles_.push_back(f2);
levelFiles_.push_back(f3);
Expand All @@ -278,13 +300,27 @@ TEST(AddBoundaryInputsTest, TestTwoBoundaryFiles) {
}

TEST(AddBoundaryInputsTest, TestDisjoinFilePointers) {
FileMetaData *f1 = CreateFileMetaData(1, InternalKey("100", 6, kTypeValue), InternalKey(InternalKey("100", 5, kTypeValue)));
FileMetaData *f2 = CreateFileMetaData(1, InternalKey("100", 2, kTypeValue), InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f3 = CreateFileMetaData(1, InternalKey("100", 4, kTypeValue), InternalKey(InternalKey("100", 3, kTypeValue)));

FileMetaData *f4 = CreateFileMetaData(1, InternalKey("100", 6, kTypeValue), InternalKey(InternalKey("100", 5, kTypeValue)));
FileMetaData *f5 = CreateFileMetaData(1, InternalKey("100", 2, kTypeValue), InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f6 = CreateFileMetaData(1, InternalKey("100", 4, kTypeValue), InternalKey(InternalKey("100", 3, kTypeValue)));
FileMetaData *f1 =
CreateFileMetaData(1, InternalKey("100", 6, kTypeValue),
InternalKey(InternalKey("100", 5, kTypeValue)));
#if 0
FileMetaData *f2 =
CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f3 =
CreateFileMetaData(1, InternalKey("100", 4, kTypeValue),
InternalKey(InternalKey("100", 3, kTypeValue)));
#endif

FileMetaData *f4 =
CreateFileMetaData(1, InternalKey("100", 6, kTypeValue),
InternalKey(InternalKey("100", 5, kTypeValue)));
FileMetaData *f5 =
CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
InternalKey(InternalKey("300", 1, kTypeValue)));
FileMetaData *f6 =
CreateFileMetaData(1, InternalKey("100", 4, kTypeValue),
InternalKey(InternalKey("100", 3, kTypeValue)));

levelFiles_.push_back(f4);
levelFiles_.push_back(f5);
Expand Down
85 changes: 47 additions & 38 deletions issues/issue320_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// 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.
#include <iostream>
#include <sstream>
#include <string>
Expand All @@ -9,9 +12,14 @@

#include <leveldb/db.h>
#include <leveldb/write_batch.h>
#include <util/testharness.h>

using namespace std;

namespace leveldb {

namespace {

unsigned int random(unsigned int max) {
return rand() % max;
}
Expand All @@ -31,100 +39,101 @@ string newString(int32_t index) {
return string(bytes, sizeof(bytes));
}

int main(int argc, char** argv) {
} // namespace

class Issue320 { };

TEST(Issue320, Test) {
srandom(0);

bool delete_before_put = false;
bool keep_snapshots = true;

vector<pair<string, string> *> test_map(10000, NULL);
vector<leveldb::Snapshot const*> snapshots(100, NULL);
vector<pair<string, string>*> test_map(10000, nullptr);
vector<Snapshot const*> snapshots(100, nullptr);

leveldb::DB* db;
leveldb::Options options;
DB* db;
Options options;
options.create_if_missing = true;
int system_status;

system_status = system("rm -rf ./issue320_test_db");
leveldb::Status status = leveldb::DB::Open(options, "./issue320_test_db",
&db);

if (false == status.ok()) {
cerr << "Unable to open/create test database './issue320_test_db'" << endl;
cerr << status.ToString() << endl;
return -1;
}
std::string dbpath = test::TmpDir() + "/leveldb_issue320_test";
ASSERT_OK(DB::Open(options, dbpath, &db));

unsigned int target_size = 10000;
unsigned int num_items = 0;
unsigned long count = 0;
string key;
string value, old_value;

leveldb::WriteOptions writeOptions;
leveldb::ReadOptions readOptions;
WriteOptions writeOptions;
ReadOptions readOptions;
while (count < 200000) {
if ((++count % 1000) == 0) {
cout << "count: " << count << endl;
}

unsigned int index = random(test_map.size());
leveldb::WriteBatch batch;
WriteBatch batch;

if (test_map[index] == NULL) {
if (test_map[index] == nullptr) {
num_items++;
test_map[index] = new pair<string, string>(newString(index),
newString(index));
test_map[index] =
new pair<string, string>(newString(index), newString(index));
batch.Put(test_map[index]->first, test_map[index]->second);
} else {
if (!db->Get(readOptions, test_map[index]->first, &old_value).ok()) {
cout << "read failed" << endl;
abort();
}
ASSERT_OK(db->Get(readOptions, test_map[index]->first, &old_value));
if (old_value != test_map[index]->second) {
cout << "ERROR incorrect value returned by Get" << endl;
cout << " count=" << count << endl;
cout << " old value=" << old_value << endl;
cout << " test_map[index]->second=" << test_map[index]->second << endl;
cout << " test_map[index]->first=" << test_map[index]->first << endl;
cout << " index=" << index << endl;
exit(-1);
ASSERT_NE(old_value, test_map[index]->second);
}

if (num_items >= target_size && random(100) > 30) {
batch.Delete(test_map[index]->first);
delete test_map[index];
test_map[index] = NULL;
test_map[index] = nullptr;
--num_items;
} else {
test_map[index]->second = newString(index);
if (delete_before_put)
batch.Delete(test_map[index]->first);
if (delete_before_put) batch.Delete(test_map[index]->first);
batch.Put(test_map[index]->first, test_map[index]->second);
}
}

if (!db->Write(writeOptions, &batch).ok()) {
cout << "write failed" << endl;
abort();
}
ASSERT_OK(db->Write(writeOptions, &batch));

if (keep_snapshots && random(10) == 0) {
unsigned int i = random(snapshots.size());
if (snapshots[i] != NULL) {
if (snapshots[i] != nullptr) {
db->ReleaseSnapshot(snapshots[i]);
}
snapshots[i] = db->GetSnapshot();
}
}

for (Snapshot const* snapshot : snapshots) {
if (snapshot) {
db->ReleaseSnapshot(snapshot);
}
}

for (size_t i = 0; i < test_map.size(); ++i) {
if (test_map[i] != NULL) {
if (test_map[i] != nullptr) {
delete test_map[i];
test_map[i] = NULL;
test_map[i] = nullptr;
}
}

system_status = system("rm -rf ./issue320_test_db");
return 0;
delete db;
DestroyDB(dbpath, options);
}

} // namespace leveldb

int main(int argc, char** argv) {
return leveldb::test::RunAllTests();
}

0 comments on commit 4b72cb1

Please sign in to comment.