2013-09-19 13:42:22 -07:00
|
|
|
// Copyright (c) 2013 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.
|
|
|
|
|
|
|
|
// Test for issue 200: when iterator switches direction from backward
|
|
|
|
// to forward, the current key can be yielded unexpectedly if a new
|
|
|
|
// mutation has been added just before the current key.
|
|
|
|
|
2019-11-25 09:29:06 -08:00
|
|
|
#include "gtest/gtest.h"
|
2013-09-19 13:42:22 -07:00
|
|
|
#include "leveldb/db.h"
|
2019-11-21 13:09:53 -08:00
|
|
|
#include "util/testutil.h"
|
2013-09-19 13:42:22 -07:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
TEST(Issue200, Test) {
|
|
|
|
// Get rid of any state from an old run.
|
2019-11-21 13:09:53 -08:00
|
|
|
std::string dbpath = testing::TempDir() + "leveldb_issue200_test";
|
2013-09-19 13:42:22 -07:00
|
|
|
DestroyDB(dbpath, Options());
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
DB* db;
|
2013-09-19 13:42:22 -07:00
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(DB::Open(options, dbpath, &db));
|
2013-09-19 13:42:22 -07:00
|
|
|
|
|
|
|
WriteOptions write_options;
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(db->Put(write_options, "1", "b"));
|
|
|
|
ASSERT_LEVELDB_OK(db->Put(write_options, "2", "c"));
|
|
|
|
ASSERT_LEVELDB_OK(db->Put(write_options, "3", "d"));
|
|
|
|
ASSERT_LEVELDB_OK(db->Put(write_options, "4", "e"));
|
|
|
|
ASSERT_LEVELDB_OK(db->Put(write_options, "5", "f"));
|
2013-09-19 13:42:22 -07:00
|
|
|
|
|
|
|
ReadOptions read_options;
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* iter = db->NewIterator(read_options);
|
2013-09-19 13:42:22 -07:00
|
|
|
|
|
|
|
// Add an element that should not be reflected in the iterator.
|
2019-11-21 13:09:53 -08:00
|
|
|
ASSERT_LEVELDB_OK(db->Put(write_options, "25", "cd"));
|
2013-09-19 13:42:22 -07:00
|
|
|
|
|
|
|
iter->Seek("5");
|
|
|
|
ASSERT_EQ(iter->key().ToString(), "5");
|
|
|
|
iter->Prev();
|
|
|
|
ASSERT_EQ(iter->key().ToString(), "4");
|
|
|
|
iter->Prev();
|
|
|
|
ASSERT_EQ(iter->key().ToString(), "3");
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_EQ(iter->key().ToString(), "4");
|
|
|
|
iter->Next();
|
|
|
|
ASSERT_EQ(iter->key().ToString(), "5");
|
|
|
|
|
|
|
|
delete iter;
|
|
|
|
delete db;
|
|
|
|
DestroyDB(dbpath, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace leveldb
|