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

Public Member Functions

 CorruptionTest ()
 
 ~CorruptionTest ()
 
Status TryReopen ()
 
void Reopen ()
 
void RepairDB ()
 
void Build (int n)
 
void Check (int min_expected, int max_expected)
 
void Corrupt (FileType filetype, int offset, int bytes_to_corrupt)
 
int Property (const std::string &name)
 
Slice Key (int i, std::string *storage)
 
Slice Value (int k, std::string *storage)
 

Public Attributes

test::ErrorEnv env_
 
std::string dbname_
 
Cachetiny_cache_
 
Options options_
 
DBdb_
 

Detailed Description

Definition at line 27 of file corruption_test.cc.

Constructor & Destructor Documentation

§ CorruptionTest()

leveldb::CorruptionTest::CorruptionTest ( )
inline

Definition at line 35 of file corruption_test.cc.

35  {
36  tiny_cache_ = NewLRUCache(100);
37  options_.env = &env_;
39  dbname_ = test::TmpDir() + "/corruption_test";
41 
42  db_ = NULL;
44  Reopen();
46  }
std::string TmpDir()
Definition: testharness.cc:60
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1537
bool create_if_missing
Definition: options.h:45
Cache * block_cache
Definition: options.h:98
Cache * NewLRUCache(size_t capacity)
Definition: cache.cc:401
Here is the call graph for this function:

§ ~CorruptionTest()

leveldb::CorruptionTest::~CorruptionTest ( )
inline

Definition at line 48 of file corruption_test.cc.

48  {
49  delete db_;
50  DestroyDB(dbname_, Options());
51  delete tiny_cache_;
52  }
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1537
Here is the call graph for this function:

Member Function Documentation

§ Build()

void leveldb::CorruptionTest::Build ( int  n)
inline

Definition at line 70 of file corruption_test.cc.

70  {
71  std::string key_space, value_space;
72  WriteBatch batch;
73  for (int i = 0; i < n; i++) {
74  //if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
75  Slice key = Key(i, &key_space);
76  batch.Clear();
77  batch.Put(key, Value(i, &value_space));
78  WriteOptions options;
79  // Corrupt() doesn't work without this sync on windows; stat reports 0 for
80  // the file size.
81  if (i == n - 1) {
82  options.sync = true;
83  }
84  ASSERT_OK(db_->Write(options, &batch));
85  }
86  }
#define ASSERT_OK(s)
Definition: testharness.h:106
Slice Key(int i, std::string *storage)
Slice Value(int k, std::string *storage)
virtual Status Write(const WriteOptions &options, WriteBatch *updates)=0
Here is the call graph for this function:
Here is the caller graph for this function:

§ Check()

void leveldb::CorruptionTest::Check ( int  min_expected,
int  max_expected 
)
inline

Definition at line 88 of file corruption_test.cc.

88  {
89  int next_expected = 0;
90  int missed = 0;
91  int bad_keys = 0;
92  int bad_values = 0;
93  int correct = 0;
94  std::string value_space;
95  Iterator* iter = db_->NewIterator(ReadOptions());
96  for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
97  uint64_t key;
98  Slice in(iter->key());
99  if (in == "" || in == "~") {
100  // Ignore boundary keys.
101  continue;
102  }
103  if (!ConsumeDecimalNumber(&in, &key) ||
104  !in.empty() ||
105  key < next_expected) {
106  bad_keys++;
107  continue;
108  }
109  missed += (key - next_expected);
110  next_expected = key + 1;
111  if (iter->value() != Value(key, &value_space)) {
112  bad_values++;
113  } else {
114  correct++;
115  }
116  }
117  delete iter;
118 
119  fprintf(stderr,
120  "expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%d\n",
121  min_expected, max_expected, correct, bad_keys, bad_values, missed);
122  ASSERT_LE(min_expected, correct);
123  ASSERT_GE(max_expected, correct);
124  }
#define ASSERT_LE(a, b)
Definition: testharness.h:111
bool ConsumeDecimalNumber(Slice *in, uint64_t *val)
Definition: logging.cc:48
Slice Value(int k, std::string *storage)
#define ASSERT_GE(a, b)
Definition: testharness.h:109
virtual Iterator * NewIterator(const ReadOptions &options)=0
Here is the call graph for this function:
Here is the caller graph for this function:

§ Corrupt()

void leveldb::CorruptionTest::Corrupt ( FileType  filetype,
int  offset,
int  bytes_to_corrupt 
)
inline

Definition at line 126 of file corruption_test.cc.

126  {
127  // Pick file to corrupt
128  std::vector<std::string> filenames;
129  ASSERT_OK(env_.GetChildren(dbname_, &filenames));
130  uint64_t number;
131  FileType type;
132  std::string fname;
133  int picked_number = -1;
134  for (size_t i = 0; i < filenames.size(); i++) {
135  if (ParseFileName(filenames[i], &number, &type) &&
136  type == filetype &&
137  int(number) > picked_number) { // Pick latest file
138  fname = dbname_ + "/" + filenames[i];
139  picked_number = number;
140  }
141  }
142  ASSERT_TRUE(!fname.empty()) << filetype;
143 
144  struct stat sbuf;
145  if (stat(fname.c_str(), &sbuf) != 0) {
146  const char* msg = strerror(errno);
147  ASSERT_TRUE(false) << fname << ": " << msg;
148  }
149 
150  if (offset < 0) {
151  // Relative to end of file; make it absolute
152  if (-offset > sbuf.st_size) {
153  offset = 0;
154  } else {
155  offset = sbuf.st_size + offset;
156  }
157  }
158  if (offset > sbuf.st_size) {
159  offset = sbuf.st_size;
160  }
161  if (offset + bytes_to_corrupt > sbuf.st_size) {
162  bytes_to_corrupt = sbuf.st_size - offset;
163  }
164 
165  // Do it
166  std::string contents;
167  Status s = ReadFileToString(Env::Default(), fname, &contents);
168  ASSERT_TRUE(s.ok()) << s.ToString();
169  for (int i = 0; i < bytes_to_corrupt; i++) {
170  contents[i + offset] ^= 0x80;
171  }
172  s = WriteStringToFile(Env::Default(), contents, fname);
173  ASSERT_TRUE(s.ok()) << s.ToString();
174  }
Status WriteStringToFile(Env *env, const Slice &data, const std::string &fname)
Definition: env.cc:62
bool ParseFileName(const std::string &fname, uint64_t *number, FileType *type)
Definition: filename.cc:80
#define ASSERT_OK(s)
Definition: testharness.h:106
#define ASSERT_TRUE(c)
Definition: testharness.h:105
FileType
Definition: filename.h:20
Status GetChildren(const std::string &dir, std::vector< std::string > *r)
Definition: env.h:311
static Env * Default()
Definition: env_posix.cc:613
Status ReadFileToString(Env *env, const std::string &fname, std::string *data)
Definition: env.cc:72
Here is the call graph for this function:
Here is the caller graph for this function:

§ Key()

Slice leveldb::CorruptionTest::Key ( int  i,
std::string *  storage 
)
inline

Definition at line 188 of file corruption_test.cc.

188  {
189  char buf[100];
190  snprintf(buf, sizeof(buf), "%016d", i);
191  storage->assign(buf, strlen(buf));
192  return Slice(*storage);
193  }
Here is the caller graph for this function:

§ Property()

int leveldb::CorruptionTest::Property ( const std::string &  name)
inline

Definition at line 176 of file corruption_test.cc.

176  {
177  std::string property;
178  int result;
179  if (db_->GetProperty(name, &property) &&
180  sscanf(property.c_str(), "%d", &result) == 1) {
181  return result;
182  } else {
183  return -1;
184  }
185  }
virtual bool GetProperty(const Slice &property, std::string *value)=0
Here is the call graph for this function:
Here is the caller graph for this function:

§ Reopen()

void leveldb::CorruptionTest::Reopen ( )
inline

Definition at line 60 of file corruption_test.cc.

60  {
62  }
#define ASSERT_OK(s)
Definition: testharness.h:106
Here is the call graph for this function:
Here is the caller graph for this function:

§ RepairDB()

void leveldb::CorruptionTest::RepairDB ( )
inline

Definition at line 64 of file corruption_test.cc.

64  {
65  delete db_;
66  db_ = NULL;
68  }
#define ASSERT_OK(s)
Definition: testharness.h:106
Status RepairDB(const std::string &dbname, const Options &options)
Definition: repair.cc:456
Here is the call graph for this function:
Here is the caller graph for this function:

§ TryReopen()

Status leveldb::CorruptionTest::TryReopen ( )
inline

Definition at line 54 of file corruption_test.cc.

54  {
55  delete db_;
56  db_ = NULL;
57  return DB::Open(options_, dbname_, &db_);
58  }
static Status Open(const Options &options, const std::string &name, DB **dbptr)
Definition: db_impl.cc:1490
Here is the call graph for this function:
Here is the caller graph for this function:

§ Value()

Slice leveldb::CorruptionTest::Value ( int  k,
std::string *  storage 
)
inline

Definition at line 196 of file corruption_test.cc.

196  {
197  Random r(k);
198  return test::RandomString(&r, kValueSize, storage);
199  }
static const int kValueSize
Slice RandomString(Random *rnd, int len, std::string *dst)
Definition: testutil.cc:12
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

§ db_

DB* leveldb::CorruptionTest::db_

Definition at line 33 of file corruption_test.cc.

§ dbname_

std::string leveldb::CorruptionTest::dbname_

Definition at line 30 of file corruption_test.cc.

§ env_

test::ErrorEnv leveldb::CorruptionTest::env_

Definition at line 29 of file corruption_test.cc.

§ options_

Options leveldb::CorruptionTest::options_

Definition at line 32 of file corruption_test.cc.

§ tiny_cache_

Cache* leveldb::CorruptionTest::tiny_cache_

Definition at line 31 of file corruption_test.cc.


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