leveldb
Public Member Functions | Public Attributes | Private Types | Private Attributes | List of all members
leveldb::DBTest Class Reference
Collaboration diagram for leveldb::DBTest:
Collaboration graph
[legend]

Public Member Functions

 DBTest ()
 
 ~DBTest ()
 
bool ChangeOptions ()
 
Options CurrentOptions ()
 
DBImpldbfull ()
 
void Reopen (Options *options=NULL)
 
void Close ()
 
void DestroyAndReopen (Options *options=NULL)
 
Status TryReopen (Options *options)
 
Status Put (const std::string &k, const std::string &v)
 
Status Delete (const std::string &k)
 
std::string Get (const std::string &k, const Snapshot *snapshot=NULL)
 
std::string Contents ()
 
std::string AllEntriesFor (const Slice &user_key)
 
int NumTableFilesAtLevel (int level)
 
int TotalTableFiles ()
 
std::string FilesPerLevel ()
 
int CountFiles ()
 
uint64_t Size (const Slice &start, const Slice &limit)
 
void Compact (const Slice &start, const Slice &limit)
 
void MakeTables (int n, const std::string &small, const std::string &large)
 
void FillLevels (const std::string &smallest, const std::string &largest)
 
void DumpFileCounts (const char *label)
 
std::string DumpSSTableList ()
 
std::string IterStatus (Iterator *iter)
 
bool DeleteAnSSTFile ()
 
int RenameLDBToSST ()
 

Public Attributes

std::string dbname_
 
SpecialEnvenv_
 
DBdb_
 
Options last_options_
 

Private Types

enum  OptionConfig {
  kDefault, kReuse, kFilter, kUncompressed,
  kEnd
}
 

Private Attributes

const FilterPolicyfilter_policy_
 
int option_config_
 

Detailed Description

Definition at line 189 of file db_test.cc.

Member Enumeration Documentation

§ OptionConfig

Enumerator
kDefault 
kReuse 
kFilter 
kUncompressed 
kEnd 

Definition at line 194 of file db_test.cc.

Constructor & Destructor Documentation

§ DBTest()

leveldb::DBTest::DBTest ( )
inline

Definition at line 210 of file db_test.cc.

211  env_(new SpecialEnv(Env::Default())) {
213  dbname_ = test::TmpDir() + "/db_test";
214  DestroyDB(dbname_, Options());
215  db_ = NULL;
216  Reopen();
217  }
std::string TmpDir()
Definition: testharness.cc:60
SpecialEnv * env_
Definition: db_test.cc:205
int option_config_
Definition: db_test.cc:201
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1537
void Reopen(Options *options=NULL)
Definition: db_test.cc:262
const FilterPolicy * NewBloomFilterPolicy(int bits_per_key)
Definition: bloom.cc:91
static Env * Default()
Definition: env_posix.cc:613
const FilterPolicy * filter_policy_
Definition: db_test.cc:191
std::string dbname_
Definition: db_test.cc:204
Here is the call graph for this function:

§ ~DBTest()

leveldb::DBTest::~DBTest ( )
inline

Definition at line 219 of file db_test.cc.

219  {
220  delete db_;
221  DestroyDB(dbname_, Options());
222  delete env_;
223  delete filter_policy_;
224  }
SpecialEnv * env_
Definition: db_test.cc:205
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1537
const FilterPolicy * filter_policy_
Definition: db_test.cc:191
std::string dbname_
Definition: db_test.cc:204
Here is the call graph for this function:

Member Function Documentation

§ AllEntriesFor()

std::string leveldb::DBTest::AllEntriesFor ( const Slice user_key)
inline

Definition at line 341 of file db_test.cc.

341  {
342  Iterator* iter = dbfull()->TEST_NewInternalIterator();
343  InternalKey target(user_key, kMaxSequenceNumber, kTypeValue);
344  iter->Seek(target.Encode());
345  std::string result;
346  if (!iter->status().ok()) {
347  result = iter->status().ToString();
348  } else {
349  result = "[ ";
350  bool first = true;
351  while (iter->Valid()) {
352  ParsedInternalKey ikey;
353  if (!ParseInternalKey(iter->key(), &ikey)) {
354  result += "CORRUPTED";
355  } else {
356  if (last_options_.comparator->Compare(ikey.user_key, user_key) != 0) {
357  break;
358  }
359  if (!first) {
360  result += ", ";
361  }
362  first = false;
363  switch (ikey.type) {
364  case kTypeValue:
365  result += iter->value().ToString();
366  break;
367  case kTypeDeletion:
368  result += "DEL";
369  break;
370  }
371  }
372  iter->Next();
373  }
374  if (!first) {
375  result += " ";
376  }
377  result += "]";
378  }
379  delete iter;
380  return result;
381  }
static const SequenceNumber kMaxSequenceNumber
Definition: dbformat.h:67
bool ParseInternalKey(const Slice &internal_key, ParsedInternalKey *result)
Definition: dbformat.h:176
DBImpl * dbfull()
Definition: db_test.cc:258
Iterator * TEST_NewInternalIterator()
Definition: db_impl.cc:1098
const Comparator * comparator
Definition: options.h:41
virtual int Compare(const Slice &a, const Slice &b) const =0
Options last_options_
Definition: db_test.cc:208
Here is the call graph for this function:

§ ChangeOptions()

bool leveldb::DBTest::ChangeOptions ( )
inline

Definition at line 228 of file db_test.cc.

228  {
229  option_config_++;
230  if (option_config_ >= kEnd) {
231  return false;
232  } else {
234  return true;
235  }
236  }
int option_config_
Definition: db_test.cc:201
void DestroyAndReopen(Options *options=NULL)
Definition: db_test.cc:271

§ Close()

void leveldb::DBTest::Close ( )
inline

Definition at line 266 of file db_test.cc.

266  {
267  delete db_;
268  db_ = NULL;
269  }

§ Compact()

void leveldb::DBTest::Compact ( const Slice start,
const Slice limit 
)
inline

Definition at line 429 of file db_test.cc.

429  {
430  db_->CompactRange(&start, &limit);
431  }
virtual void CompactRange(const Slice *begin, const Slice *end)=0
Here is the call graph for this function:

§ Contents()

std::string leveldb::DBTest::Contents ( )
inline

Definition at line 316 of file db_test.cc.

316  {
317  std::vector<std::string> forward;
318  std::string result;
319  Iterator* iter = db_->NewIterator(ReadOptions());
320  for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
321  std::string s = IterStatus(iter);
322  result.push_back('(');
323  result.append(s);
324  result.push_back(')');
325  forward.push_back(s);
326  }
327 
328  // Check reverse iteration results are the reverse of forward results
329  size_t matched = 0;
330  for (iter->SeekToLast(); iter->Valid(); iter->Prev()) {
331  ASSERT_LT(matched, forward.size());
332  ASSERT_EQ(IterStatus(iter), forward[forward.size() - matched - 1]);
333  matched++;
334  }
335  ASSERT_EQ(matched, forward.size());
336 
337  delete iter;
338  return result;
339  }
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
#define ASSERT_LT(a, b)
Definition: testharness.h:112
std::string IterStatus(Iterator *iter)
Definition: db_test.cc:468
virtual Iterator * NewIterator(const ReadOptions &options)=0
Here is the call graph for this function:

§ CountFiles()

int leveldb::DBTest::CountFiles ( )
inline

Definition at line 416 of file db_test.cc.

416  {
417  std::vector<std::string> files;
418  env_->GetChildren(dbname_, &files);
419  return static_cast<int>(files.size());
420  }
SpecialEnv * env_
Definition: db_test.cc:205
Status GetChildren(const std::string &dir, std::vector< std::string > *r)
Definition: env.h:311
std::string dbname_
Definition: db_test.cc:204
Here is the call graph for this function:

§ CurrentOptions()

Options leveldb::DBTest::CurrentOptions ( )
inline

Definition at line 239 of file db_test.cc.

239  {
240  Options options;
241  options.reuse_logs = false;
242  switch (option_config_) {
243  case kReuse:
244  options.reuse_logs = true;
245  break;
246  case kFilter:
247  options.filter_policy = filter_policy_;
248  break;
249  case kUncompressed:
250  options.compression = kNoCompression;
251  break;
252  default:
253  break;
254  }
255  return options;
256  }
int option_config_
Definition: db_test.cc:201
const FilterPolicy * filter_policy_
Definition: db_test.cc:191

§ dbfull()

DBImpl* leveldb::DBTest::dbfull ( )
inline

Definition at line 258 of file db_test.cc.

258  {
259  return reinterpret_cast<DBImpl*>(db_);
260  }

§ Delete()

Status leveldb::DBTest::Delete ( const std::string &  k)
inline

Definition at line 297 of file db_test.cc.

297  {
298  return db_->Delete(WriteOptions(), k);
299  }
virtual Status Delete(const WriteOptions &options, const Slice &key)=0
Definition: db_impl.cc:1482
Here is the call graph for this function:

§ DeleteAnSSTFile()

bool leveldb::DBTest::DeleteAnSSTFile ( )
inline

Definition at line 478 of file db_test.cc.

478  {
479  std::vector<std::string> filenames;
480  ASSERT_OK(env_->GetChildren(dbname_, &filenames));
481  uint64_t number;
482  FileType type;
483  for (size_t i = 0; i < filenames.size(); i++) {
484  if (ParseFileName(filenames[i], &number, &type) && type == kTableFile) {
486  return true;
487  }
488  }
489  return false;
490  }
bool ParseFileName(const std::string &fname, uint64_t *number, FileType *type)
Definition: filename.cc:80
#define ASSERT_OK(s)
Definition: testharness.h:106
SpecialEnv * env_
Definition: db_test.cc:205
std::string TableFileName(const std::string &name, uint64_t number)
Definition: filename.cc:32
Status DeleteFile(const std::string &f)
Definition: env.h:314
FileType
Definition: filename.h:20
Status GetChildren(const std::string &dir, std::vector< std::string > *r)
Definition: env.h:311
std::string dbname_
Definition: db_test.cc:204
Here is the call graph for this function:

§ DestroyAndReopen()

void leveldb::DBTest::DestroyAndReopen ( Options options = NULL)
inline

Definition at line 271 of file db_test.cc.

271  {
272  delete db_;
273  db_ = NULL;
274  DestroyDB(dbname_, Options());
275  ASSERT_OK(TryReopen(options));
276  }
#define ASSERT_OK(s)
Definition: testharness.h:106
Status TryReopen(Options *options)
Definition: db_test.cc:278
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1537
std::string dbname_
Definition: db_test.cc:204
Here is the call graph for this function:

§ DumpFileCounts()

void leveldb::DBTest::DumpFileCounts ( const char *  label)
inline

Definition at line 449 of file db_test.cc.

449  {
450  fprintf(stderr, "---\n%s:\n", label);
451  fprintf(stderr, "maxoverlap: %lld\n",
452  static_cast<long long>(
453  dbfull()->TEST_MaxNextLevelOverlappingBytes()));
454  for (int level = 0; level < config::kNumLevels; level++) {
455  int num = NumTableFilesAtLevel(level);
456  if (num > 0) {
457  fprintf(stderr, " level %3d : %d files\n", level, num);
458  }
459  }
460  }
static const int kNumLevels
Definition: dbformat.h:22
int NumTableFilesAtLevel(int level)
Definition: db_test.cc:383
DBImpl * dbfull()
Definition: db_test.cc:258

§ DumpSSTableList()

std::string leveldb::DBTest::DumpSSTableList ( )
inline

Definition at line 462 of file db_test.cc.

462  {
463  std::string property;
464  db_->GetProperty("leveldb.sstables", &property);
465  return property;
466  }
virtual bool GetProperty(const Slice &property, std::string *value)=0
Here is the call graph for this function:

§ FilesPerLevel()

std::string leveldb::DBTest::FilesPerLevel ( )
inline

Definition at line 400 of file db_test.cc.

400  {
401  std::string result;
402  int last_non_zero_offset = 0;
403  for (int level = 0; level < config::kNumLevels; level++) {
404  int f = NumTableFilesAtLevel(level);
405  char buf[100];
406  snprintf(buf, sizeof(buf), "%s%d", (level ? "," : ""), f);
407  result += buf;
408  if (f > 0) {
409  last_non_zero_offset = result.size();
410  }
411  }
412  result.resize(last_non_zero_offset);
413  return result;
414  }
static const int kNumLevels
Definition: dbformat.h:22
int NumTableFilesAtLevel(int level)
Definition: db_test.cc:383

§ FillLevels()

void leveldb::DBTest::FillLevels ( const std::string &  smallest,
const std::string &  largest 
)
inline

Definition at line 445 of file db_test.cc.

445  {
446  MakeTables(config::kNumLevels, smallest, largest);
447  }
static const int kNumLevels
Definition: dbformat.h:22
void MakeTables(int n, const std::string &small, const std::string &large)
Definition: db_test.cc:435

§ Get()

std::string leveldb::DBTest::Get ( const std::string &  k,
const Snapshot snapshot = NULL 
)
inline

Definition at line 301 of file db_test.cc.

301  {
302  ReadOptions options;
303  options.snapshot = snapshot;
304  std::string result;
305  Status s = db_->Get(options, k, &result);
306  if (s.IsNotFound()) {
307  result = "NOT_FOUND";
308  } else if (!s.ok()) {
309  result = s.ToString();
310  }
311  return result;
312  }
std::string ToString() const
Definition: status.cc:36
virtual Status Get(const ReadOptions &options, const Slice &key, std::string *value)=0
Here is the call graph for this function:

§ IterStatus()

std::string leveldb::DBTest::IterStatus ( Iterator iter)
inline

Definition at line 468 of file db_test.cc.

468  {
469  std::string result;
470  if (iter->Valid()) {
471  result = iter->key().ToString() + "->" + iter->value().ToString();
472  } else {
473  result = "(invalid)";
474  }
475  return result;
476  }
Here is the call graph for this function:

§ MakeTables()

void leveldb::DBTest::MakeTables ( int  n,
const std::string &  small,
const std::string &  large 
)
inline

Definition at line 435 of file db_test.cc.

435  {
436  for (int i = 0; i < n; i++) {
437  Put(small, "begin");
438  Put(large, "end");
440  }
441  }
DBImpl * dbfull()
Definition: db_test.cc:258
Status TEST_CompactMemTable()
Definition: db_impl.cc:621
Status Put(const std::string &k, const std::string &v)
Definition: db_test.cc:293

§ NumTableFilesAtLevel()

int leveldb::DBTest::NumTableFilesAtLevel ( int  level)
inline

Definition at line 383 of file db_test.cc.

383  {
384  std::string property;
385  ASSERT_TRUE(
386  db_->GetProperty("leveldb.num-files-at-level" + NumberToString(level),
387  &property));
388  return atoi(property.c_str());
389  }
virtual bool GetProperty(const Slice &property, std::string *value)=0
#define ASSERT_TRUE(c)
Definition: testharness.h:105
std::string NumberToString(uint64_t num)
Definition: logging.cc:36
Here is the call graph for this function:

§ Put()

Status leveldb::DBTest::Put ( const std::string &  k,
const std::string &  v 
)
inline

Definition at line 293 of file db_test.cc.

293  {
294  return db_->Put(WriteOptions(), k, v);
295  }
virtual Status Put(const WriteOptions &options, const Slice &key, const Slice &value)=0
Definition: db_impl.cc:1476
Here is the call graph for this function:

§ RenameLDBToSST()

int leveldb::DBTest::RenameLDBToSST ( )
inline

Definition at line 493 of file db_test.cc.

493  {
494  std::vector<std::string> filenames;
495  ASSERT_OK(env_->GetChildren(dbname_, &filenames));
496  uint64_t number;
497  FileType type;
498  int files_renamed = 0;
499  for (size_t i = 0; i < filenames.size(); i++) {
500  if (ParseFileName(filenames[i], &number, &type) && type == kTableFile) {
501  const std::string from = TableFileName(dbname_, number);
502  const std::string to = SSTTableFileName(dbname_, number);
503  ASSERT_OK(env_->RenameFile(from, to));
504  files_renamed++;
505  }
506  }
507  return files_renamed;
508  }
bool ParseFileName(const std::string &fname, uint64_t *number, FileType *type)
Definition: filename.cc:80
#define ASSERT_OK(s)
Definition: testharness.h:106
SpecialEnv * env_
Definition: db_test.cc:205
std::string TableFileName(const std::string &name, uint64_t number)
Definition: filename.cc:32
Status RenameFile(const std::string &s, const std::string &t)
Definition: env.h:320
std::string SSTTableFileName(const std::string &name, uint64_t number)
Definition: filename.cc:37
FileType
Definition: filename.h:20
Status GetChildren(const std::string &dir, std::vector< std::string > *r)
Definition: env.h:311
std::string dbname_
Definition: db_test.cc:204
Here is the call graph for this function:

§ Reopen()

void leveldb::DBTest::Reopen ( Options options = NULL)
inline

Definition at line 262 of file db_test.cc.

262  {
263  ASSERT_OK(TryReopen(options));
264  }
#define ASSERT_OK(s)
Definition: testharness.h:106
Status TryReopen(Options *options)
Definition: db_test.cc:278

§ Size()

uint64_t leveldb::DBTest::Size ( const Slice start,
const Slice limit 
)
inline

Definition at line 422 of file db_test.cc.

422  {
423  Range r(start, limit);
424  uint64_t size;
425  db_->GetApproximateSizes(&r, 1, &size);
426  return size;
427  }
virtual void GetApproximateSizes(const Range *range, int n, uint64_t *sizes)=0
Here is the call graph for this function:

§ TotalTableFiles()

int leveldb::DBTest::TotalTableFiles ( )
inline

Definition at line 391 of file db_test.cc.

391  {
392  int result = 0;
393  for (int level = 0; level < config::kNumLevels; level++) {
394  result += NumTableFilesAtLevel(level);
395  }
396  return result;
397  }
static const int kNumLevels
Definition: dbformat.h:22
int NumTableFilesAtLevel(int level)
Definition: db_test.cc:383

§ TryReopen()

Status leveldb::DBTest::TryReopen ( Options options)
inline

Definition at line 278 of file db_test.cc.

278  {
279  delete db_;
280  db_ = NULL;
281  Options opts;
282  if (options != NULL) {
283  opts = *options;
284  } else {
285  opts = CurrentOptions();
286  opts.create_if_missing = true;
287  }
288  last_options_ = opts;
289 
290  return DB::Open(opts, dbname_, &db_);
291  }
static Status Open(const Options &options, const std::string &name, DB **dbptr)
Definition: db_impl.cc:1490
Options last_options_
Definition: db_test.cc:208
std::string dbname_
Definition: db_test.cc:204
Options CurrentOptions()
Definition: db_test.cc:239
Here is the call graph for this function:

Member Data Documentation

§ db_

DB* leveldb::DBTest::db_

Definition at line 206 of file db_test.cc.

§ dbname_

std::string leveldb::DBTest::dbname_

Definition at line 204 of file db_test.cc.

§ env_

SpecialEnv* leveldb::DBTest::env_

Definition at line 205 of file db_test.cc.

§ filter_policy_

const FilterPolicy* leveldb::DBTest::filter_policy_
private

Definition at line 191 of file db_test.cc.

§ last_options_

Options leveldb::DBTest::last_options_

Definition at line 208 of file db_test.cc.

§ option_config_

int leveldb::DBTest::option_config_
private

Definition at line 201 of file db_test.cc.


The documentation for this class was generated from the following file: