10 #include <sys/types.h>
25 static const int kValueSize = 1000;
57 return DB::Open(options_, dbname_, &db_);
71 std::string key_space, value_space;
73 for (
int i = 0; i < n; i++) {
77 batch.
Put(key,
Value(i, &value_space));
82 void Check(
int min_expected,
int max_expected) {
83 int next_expected = 0;
88 std::string value_space;
93 if (in ==
"" || in ==
"~") {
99 key < next_expected) {
103 missed += (key - next_expected);
104 next_expected = key + 1;
105 if (iter->
value() !=
Value(key, &value_space)) {
114 "expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%d\n",
115 min_expected, max_expected, correct, bad_keys, bad_values, missed);
122 std::vector<std::string> filenames;
127 int picked_number = -1;
128 for (
int i = 0; i < filenames.size(); i++) {
131 int(number) > picked_number) {
132 fname = dbname_ +
"/" + filenames[i];
133 picked_number = number;
139 if (stat(fname.c_str(), &sbuf) != 0) {
140 const char* msg = strerror(errno);
146 if (-offset > sbuf.st_size) {
149 offset = sbuf.st_size + offset;
152 if (offset > sbuf.st_size) {
153 offset = sbuf.st_size;
155 if (offset + bytes_to_corrupt > sbuf.st_size) {
156 bytes_to_corrupt = sbuf.st_size - offset;
160 std::string contents;
163 for (
int i = 0; i < bytes_to_corrupt; i++) {
164 contents[i + offset] ^= 0x80;
171 std::string property;
174 sscanf(property.c_str(),
"%d", &result) == 1) {
184 snprintf(buf,
sizeof(buf),
"%016d", i);
185 storage->assign(buf, strlen(buf));
186 return Slice(*storage);
200 Corrupt(
kLogFile, log::kBlockSize + 1000, 1);
208 env_.writable_file_error_ =
true;
215 env_.writable_file_error_ =
true;
217 std::string value_storage;
219 for (
int i = 0; s.
ok() && i < num; i++) {
221 batch.
Put(
"a",
Value(100, &value_storage));
226 env_.writable_file_error_ =
false;
283 dbi->TEST_CompactRange(0, NULL, NULL);
300 const int last = config::kMaxMemCompactLevel;
318 for (
int i = 0; i < 2; i++) {
327 std::string tmp1, tmp2;
329 ASSERT_TRUE(!s.
ok()) <<
"write did not fail in corrupted paranoid db";
338 std::string tmp1, tmp2;
350 int main(
int argc,
char** argv) {
virtual Status Write(const WriteOptions &options, WriteBatch *updates)
Slice Value(int k, std::string *storage)
virtual void CompactRange(const Slice *begin, const Slice *end)
virtual Status Write(const WriteOptions &options, WriteBatch *updates)=0
Status GetChildren(const std::string &dir, std::vector< std::string > *r)
Status WriteStringToFile(Env *env, const Slice &data, const std::string &fname)
Slice Key(int i, std::string *storage)
bool ParseFileName(const std::string &fname, uint64_t *number, FileType *type)
int main(int argc, char **argv)
Status TEST_CompactMemTable()
void TEST_CompactRange(int level, const Slice *begin, const Slice *end)
virtual void SleepForMicroseconds(int micros)=0
virtual Iterator * NewIterator(const ReadOptions &options)=0
virtual Slice value() const =0
Status RepairDB(const std::string &dbname, const Options &options)
Status DestroyDB(const std::string &dbname, const Options &options)
bool ConsumeDecimalNumber(Slice *in, uint64_t *val)
unsigned long long uint64_t
static Status Open(const Options &options, const std::string &name, DB **dbptr)
TEST(CorruptionTest, Recovery)
virtual void SeekToFirst()=0
virtual Status Put(const WriteOptions &, const Slice &key, const Slice &value)
virtual Status Get(const ReadOptions &options, const Slice &key, std::string *value)
void Corrupt(FileType filetype, int offset, int bytes_to_corrupt)
Cache * NewLRUCache(size_t capacity)
virtual bool Valid() const =0
int Property(const std::string &name)
virtual Slice key() const =0
Slice RandomString(Random *rnd, int len, std::string *dst)
virtual bool GetProperty(const Slice &property, std::string *value)=0
void Check(int min_expected, int max_expected)
void Put(const Slice &key, const Slice &value)
std::string NumberToString(uint64_t num)
Status ReadFileToString(Env *env, const std::string &fname, std::string *data)
std::string ToString() const