Skip to content

Instantly share code, notes, and snippets.

@evanmcc
Created February 13, 2019 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanmcc/da9769290f32f0861f5f99b5caa9ede2 to your computer and use it in GitHub Desktop.
Save evanmcc/da9769290f32f0861f5f99b5caa9ede2 to your computer and use it in GitHub Desktop.
test program to show that iterators within transactions on an optimistic transaction db don't honor iterator bounds
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"
#include "rocksdb/utilities/optimistic_transaction_db.h"
#include "rocksdb/utilities/transaction.h"
using namespace rocksdb;
std::string kDBPath = "/tmp/rocksdb_column_families_example";
int main() {
// open DB
Options options;
options.create_if_missing = true;
OptimisticTransactionDB* db;
DB *base;
Status s = OptimisticTransactionDB::Open(options, kDBPath, &db);
assert(s.ok());
base = db->GetBaseDB();
s = base->Put(WriteOptions(), Slice("a"), Slice("x"));
assert(s.ok());
s = base->Put(WriteOptions(), Slice("b"), Slice("y"));
assert(s.ok());
WriteOptions write_options;
OptimisticTransactionOptions txn_options;
Transaction* txn = db->BeginTransaction(write_options, txn_options);
s = txn->Put(Slice("d"), Slice("v2"));
s = txn->Put(Slice("e"), Slice("v3"));
ReadOptions read;
read.iterate_upper_bound = new Slice("e");
auto it = txn->GetIterator(read);
it->SeekToFirst();
assert(it->key() == Slice("a"));
it->Next();
assert(it->key() == Slice("b"));
it->Next();
assert(it->key() == Slice("d"));
it->Next();
// this should not work but does (should still be "d")
assert(it->key() == Slice("e"));
// nor should this
assert(it->Valid());
delete it;
// write everything to disk so we can use a normal iterator
txn->Commit();
auto it2 = base->NewIterator(read);
it2->SeekToFirst();
assert(it2->key() == Slice("a"));
it2->Next();
assert(it2->key() == Slice("b"));
it2->Next();
assert(it2->key() == Slice("d"));
it2->Next();
std::cout << "failing: " << it2->key().ToString() << std::endl;
// this fails as it should
assert(it2->Valid());
delete it2;
delete db;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment