version_edit.cc 19.9 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  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).
5
//
J
jorlow@chromium.org 已提交
6 7 8 9 10 11 12
// Copyright (c) 2011 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.

#include "db/version_edit.h"

#include "db/version_set.h"
13
#include "logging/event_logger.h"
14
#include "rocksdb/slice.h"
15
#include "test_util/sync_point.h"
J
jorlow@chromium.org 已提交
16
#include "util/coding.h"
17
#include "util/string_util.h"
J
jorlow@chromium.org 已提交
18

19
namespace rocksdb {
J
jorlow@chromium.org 已提交
20 21 22

// Tag numbers for serialized VersionEdit.  These numbers are written to
// disk and should not be changed.
23
enum Tag : uint32_t {
24 25 26 27 28 29 30
  kComparator = 1,
  kLogNumber = 2,
  kNextFileNumber = 3,
  kLastSequence = 4,
  kCompactPointer = 5,
  kDeletedFile = 6,
  kNewFile = 7,
D
dgrogan@chromium.org 已提交
31
  // 8 was used for large value refs
32
  kPrevLogNumber = 9,
S
Siying Dong 已提交
33
  kMinLogNumberToKeep = 10,
34 35

  // these are new formats divergent from open source leveldb
36 37
  kNewFile2 = 100,
  kNewFile3 = 102,
38
  kNewFile4 = 103,      // 4th (the latest) format version of adding files
39 40 41 42
  kColumnFamily = 200,  // specify column family for version edit
  kColumnFamilyAdd = 201,
  kColumnFamilyDrop = 202,
  kMaxColumnFamily = 203,
43 44

  kInAtomicGroup = 300,
J
jorlow@chromium.org 已提交
45 46
};

47 48 49
// Mask for an identified tag from the future which can be safely ignored.
uint32_t kTagSafeIgnoreMask = 1 << 13;

50
enum CustomTag : uint32_t {
51 52
  kTerminate = 1,  // The end of customized fields
  kNeedCompaction = 2,
S
Siying Dong 已提交
53
  // Since Manifest is not entirely currently forward-compatible, and the only
54
  // forward-compatible part is the CutsomtTag of kNewFile, we currently encode
S
Siying Dong 已提交
55 56 57
  // kMinLogNumberToKeep as part of a CustomTag as a hack. This should be
  // removed when manifest becomes forward-comptabile.
  kMinLogNumberToKeepHack = 3,
58 59 60 61 62 63
  kPathId = 65,
};
// If this bit for the custom tag is set, opening DB should fail if
// we don't know this field.
uint32_t kCustomTagNonSafeIgnoreMask = 1 << 6;

64 65 66 67 68
uint64_t PackFileNumberAndPathId(uint64_t number, uint64_t path_id) {
  assert(number <= kFileNumberMask);
  return number | (path_id * (kFileNumberMask + 1));
}

J
jorlow@chromium.org 已提交
69 70
void VersionEdit::Clear() {
  comparator_.clear();
71
  max_level_ = 0;
J
jorlow@chromium.org 已提交
72
  log_number_ = 0;
73
  prev_log_number_ = 0;
J
jorlow@chromium.org 已提交
74 75
  last_sequence_ = 0;
  next_file_number_ = 0;
76
  max_column_family_ = 0;
S
Siying Dong 已提交
77
  min_log_number_to_keep_ = 0;
J
jorlow@chromium.org 已提交
78 79
  has_comparator_ = false;
  has_log_number_ = false;
80
  has_prev_log_number_ = false;
J
jorlow@chromium.org 已提交
81 82
  has_next_file_number_ = false;
  has_last_sequence_ = false;
83
  has_max_column_family_ = false;
S
Siying Dong 已提交
84
  has_min_log_number_to_keep_ = false;
J
jorlow@chromium.org 已提交
85 86
  deleted_files_.clear();
  new_files_.clear();
87 88 89 90
  column_family_ = 0;
  is_column_family_add_ = 0;
  is_column_family_drop_ = 0;
  column_family_name_.clear();
91 92
  is_in_atomic_group_ = false;
  remaining_entries_ = 0;
J
jorlow@chromium.org 已提交
93 94
}

95
bool VersionEdit::EncodeTo(std::string* dst) const {
J
jorlow@chromium.org 已提交
96 97 98 99 100
  if (has_comparator_) {
    PutVarint32(dst, kComparator);
    PutLengthPrefixedSlice(dst, comparator_);
  }
  if (has_log_number_) {
101
    PutVarint32Varint64(dst, kLogNumber, log_number_);
J
jorlow@chromium.org 已提交
102
  }
103
  if (has_prev_log_number_) {
104
    PutVarint32Varint64(dst, kPrevLogNumber, prev_log_number_);
105
  }
J
jorlow@chromium.org 已提交
106
  if (has_next_file_number_) {
107
    PutVarint32Varint64(dst, kNextFileNumber, next_file_number_);
J
jorlow@chromium.org 已提交
108 109
  }
  if (has_last_sequence_) {
110
    PutVarint32Varint64(dst, kLastSequence, last_sequence_);
J
jorlow@chromium.org 已提交
111
  }
112
  if (has_max_column_family_) {
113
    PutVarint32Varint32(dst, kMaxColumnFamily, max_column_family_);
114
  }
K
kailiu 已提交
115
  for (const auto& deleted : deleted_files_) {
116 117
    PutVarint32Varint32Varint64(dst, kDeletedFile, deleted.first /* level */,
                                deleted.second /* file number */);
J
jorlow@chromium.org 已提交
118 119
  }

S
Siying Dong 已提交
120
  bool min_log_num_written = false;
D
dgrogan@chromium.org 已提交
121
  for (size_t i = 0; i < new_files_.size(); i++) {
J
jorlow@chromium.org 已提交
122
    const FileMetaData& f = new_files_[i].second;
123 124 125
    if (!f.smallest.Valid() || !f.largest.Valid()) {
      return false;
    }
126
    bool has_customized_fields = false;
S
Siying Dong 已提交
127
    if (f.marked_for_compaction || has_min_log_number_to_keep_) {
128 129 130
      PutVarint32(dst, kNewFile4);
      has_customized_fields = true;
    } else if (f.fd.GetPathId() == 0) {
131 132 133 134 135 136
      // Use older format to make sure user can roll back the build if they
      // don't config multiple DB paths.
      PutVarint32(dst, kNewFile2);
    } else {
      PutVarint32(dst, kNewFile3);
    }
137
    PutVarint32Varint64(dst, new_files_[i].first /* level */, f.fd.GetNumber());
138 139
    if (f.fd.GetPathId() != 0 && !has_customized_fields) {
      // kNewFile3
140 141
      PutVarint32(dst, f.fd.GetPathId());
    }
142
    PutVarint64(dst, f.fd.GetFileSize());
J
jorlow@chromium.org 已提交
143 144
    PutLengthPrefixedSlice(dst, f.smallest.Encode());
    PutLengthPrefixedSlice(dst, f.largest.Encode());
145
    PutVarint64Varint64(dst, f.fd.smallest_seqno, f.fd.largest_seqno);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    if (has_customized_fields) {
      // Customized fields' format:
      // +-----------------------------+
      // | 1st field's tag (varint32)  |
      // +-----------------------------+
      // | 1st field's size (varint32) |
      // +-----------------------------+
      // |    bytes for 1st field      |
      // |  (based on size decoded)    |
      // +-----------------------------+
      // |                             |
      // |          ......             |
      // |                             |
      // +-----------------------------+
      // | last field's size (varint32)|
      // +-----------------------------+
      // |    bytes for last field     |
      // |  (based on size decoded)    |
      // +-----------------------------+
      // | terminating tag (varint32)  |
      // +-----------------------------+
      //
      // Customized encoding for fields:
      //   tag kPathId: 1 byte as path_id
      //   tag kNeedCompaction:
      //        now only can take one char value 1 indicating need-compaction
      //
      if (f.fd.GetPathId() != 0) {
        PutVarint32(dst, CustomTag::kPathId);
        char p = static_cast<char>(f.fd.GetPathId());
        PutLengthPrefixedSlice(dst, Slice(&p, 1));
      }
      if (f.marked_for_compaction) {
        PutVarint32(dst, CustomTag::kNeedCompaction);
        char p = static_cast<char>(1);
        PutLengthPrefixedSlice(dst, Slice(&p, 1));
      }
S
Siying Dong 已提交
183 184 185 186 187 188 189
      if (has_min_log_number_to_keep_ && !min_log_num_written) {
        PutVarint32(dst, CustomTag::kMinLogNumberToKeepHack);
        std::string varint_log_number;
        PutFixed64(&varint_log_number, min_log_number_to_keep_);
        PutLengthPrefixedSlice(dst, Slice(varint_log_number));
        min_log_num_written = true;
      }
190 191 192 193 194
      TEST_SYNC_POINT_CALLBACK("VersionEdit::EncodeTo:NewFile4:CustomizeFields",
                               dst);

      PutVarint32(dst, CustomTag::kTerminate);
    }
J
jorlow@chromium.org 已提交
195
  }
196 197 198

  // 0 is default and does not need to be explicitly written
  if (column_family_ != 0) {
199
    PutVarint32Varint32(dst, kColumnFamily, column_family_);
200 201 202 203 204 205 206 207 208 209
  }

  if (is_column_family_add_) {
    PutVarint32(dst, kColumnFamilyAdd);
    PutLengthPrefixedSlice(dst, Slice(column_family_name_));
  }

  if (is_column_family_drop_) {
    PutVarint32(dst, kColumnFamilyDrop);
  }
210 211 212 213 214

  if (is_in_atomic_group_) {
    PutVarint32(dst, kInAtomicGroup);
    PutVarint32(dst, remaining_entries_);
  }
215
  return true;
J
jorlow@chromium.org 已提交
216 217 218 219 220 221
}

static bool GetInternalKey(Slice* input, InternalKey* dst) {
  Slice str;
  if (GetLengthPrefixedSlice(input, &str)) {
    dst->DecodeFrom(str);
222
    return dst->Valid();
J
jorlow@chromium.org 已提交
223 224 225 226 227
  } else {
    return false;
  }
}

A
Andrew Kryczka 已提交
228
bool VersionEdit::GetLevel(Slice* input, int* level, const char** /*msg*/) {
J
jorlow@chromium.org 已提交
229
  uint32_t v;
230
  if (GetVarint32(input, &v)) {
J
jorlow@chromium.org 已提交
231
    *level = v;
232 233 234
    if (max_level_ < *level) {
      max_level_ = *level;
    }
J
jorlow@chromium.org 已提交
235 236 237 238 239 240
    return true;
  } else {
    return false;
  }
}

241 242 243 244 245 246 247
const char* VersionEdit::DecodeNewFile4From(Slice* input) {
  const char* msg = nullptr;
  int level;
  FileMetaData f;
  uint64_t number;
  uint32_t path_id = 0;
  uint64_t file_size;
248 249
  SequenceNumber smallest_seqno;
  SequenceNumber largest_seqno;
S
Siying Dong 已提交
250 251 252
  // Since this is the only forward-compatible part of the code, we hack new
  // extension into this record. When we do, we set this boolean to distinguish
  // the record from the normal NewFile records.
253 254 255
  if (GetLevel(input, &level, &msg) && GetVarint64(input, &number) &&
      GetVarint64(input, &file_size) && GetInternalKey(input, &f.smallest) &&
      GetInternalKey(input, &f.largest) &&
256 257
      GetVarint64(input, &smallest_seqno) &&
      GetVarint64(input, &largest_seqno)) {
258 259 260 261 262 263 264 265 266 267 268
    // See comments in VersionEdit::EncodeTo() for format of customized fields
    while (true) {
      uint32_t custom_tag;
      Slice field;
      if (!GetVarint32(input, &custom_tag)) {
        return "new-file4 custom field";
      }
      if (custom_tag == kTerminate) {
        break;
      }
      if (!GetLengthPrefixedSlice(input, &field)) {
F
Faustin Lammler 已提交
269
        return "new-file4 custom field length prefixed slice error";
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
      }
      switch (custom_tag) {
        case kPathId:
          if (field.size() != 1) {
            return "path_id field wrong size";
          }
          path_id = field[0];
          if (path_id > 3) {
            return "path_id wrong vaue";
          }
          break;
        case kNeedCompaction:
          if (field.size() != 1) {
            return "need_compaction field wrong size";
          }
          f.marked_for_compaction = (field[0] == 1);
          break;
S
Siying Dong 已提交
287 288
        case kMinLogNumberToKeepHack:
          // This is a hack to encode kMinLogNumberToKeep in a
289
          // forward-compatible fashion.
S
Siying Dong 已提交
290 291 292 293 294
          if (!GetFixed64(&field, &min_log_number_to_keep_)) {
            return "deleted log number malformatted";
          }
          has_min_log_number_to_keep_ = true;
          break;
295 296 297 298 299 300 301 302 303 304 305
        default:
          if ((custom_tag & kCustomTagNonSafeIgnoreMask) != 0) {
            // Should not proceed if cannot understand it
            return "new-file4 custom field not supported";
          }
          break;
      }
    }
  } else {
    return "new-file4 entry";
  }
306 307
  f.fd =
      FileDescriptor(number, path_id, file_size, smallest_seqno, largest_seqno);
308 309 310 311
  new_files_.push_back(std::make_pair(level, f));
  return nullptr;
}

J
jorlow@chromium.org 已提交
312 313 314
Status VersionEdit::DecodeFrom(const Slice& src) {
  Clear();
  Slice input = src;
A
Abhishek Kona 已提交
315
  const char* msg = nullptr;
J
jorlow@chromium.org 已提交
316 317 318 319 320 321 322 323
  uint32_t tag;

  // Temporary storage for parsing
  int level;
  FileMetaData f;
  Slice str;
  InternalKey key;

A
Abhishek Kona 已提交
324
  while (msg == nullptr && GetVarint32(&input, &tag)) {
J
jorlow@chromium.org 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    switch (tag) {
      case kComparator:
        if (GetLengthPrefixedSlice(&input, &str)) {
          comparator_ = str.ToString();
          has_comparator_ = true;
        } else {
          msg = "comparator name";
        }
        break;

      case kLogNumber:
        if (GetVarint64(&input, &log_number_)) {
          has_log_number_ = true;
        } else {
          msg = "log number";
        }
        break;

343 344 345 346 347 348 349 350
      case kPrevLogNumber:
        if (GetVarint64(&input, &prev_log_number_)) {
          has_prev_log_number_ = true;
        } else {
          msg = "previous log number";
        }
        break;

J
jorlow@chromium.org 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
      case kNextFileNumber:
        if (GetVarint64(&input, &next_file_number_)) {
          has_next_file_number_ = true;
        } else {
          msg = "next file number";
        }
        break;

      case kLastSequence:
        if (GetVarint64(&input, &last_sequence_)) {
          has_last_sequence_ = true;
        } else {
          msg = "last sequence number";
        }
        break;

367 368 369 370 371 372 373 374
      case kMaxColumnFamily:
        if (GetVarint32(&input, &max_column_family_)) {
          has_max_column_family_ = true;
        } else {
          msg = "max column family";
        }
        break;

S
Siying Dong 已提交
375 376 377 378 379 380 381 382
      case kMinLogNumberToKeep:
        if (GetVarint64(&input, &min_log_number_to_keep_)) {
          has_min_log_number_to_keep_ = true;
        } else {
          msg = "min log number to kee";
        }
        break;

J
jorlow@chromium.org 已提交
383
      case kCompactPointer:
384
        if (GetLevel(&input, &level, &msg) &&
J
jorlow@chromium.org 已提交
385
            GetInternalKey(&input, &key)) {
I
Igor Canadi 已提交
386 387 388
          // we don't use compact pointers anymore,
          // but we should not fail if they are still
          // in manifest
J
jorlow@chromium.org 已提交
389
        } else {
390 391 392
          if (!msg) {
            msg = "compaction pointer";
          }
J
jorlow@chromium.org 已提交
393 394 395
        }
        break;

I
Igor Canadi 已提交
396 397 398
      case kDeletedFile: {
        uint64_t number;
        if (GetLevel(&input, &level, &msg) && GetVarint64(&input, &number)) {
J
jorlow@chromium.org 已提交
399 400
          deleted_files_.insert(std::make_pair(level, number));
        } else {
401 402 403
          if (!msg) {
            msg = "deleted file";
          }
J
jorlow@chromium.org 已提交
404 405
        }
        break;
I
Igor Canadi 已提交
406
      }
J
jorlow@chromium.org 已提交
407

408 409 410 411 412
      case kNewFile: {
        uint64_t number;
        uint64_t file_size;
        if (GetLevel(&input, &level, &msg) && GetVarint64(&input, &number) &&
            GetVarint64(&input, &file_size) &&
J
jorlow@chromium.org 已提交
413 414
            GetInternalKey(&input, &f.smallest) &&
            GetInternalKey(&input, &f.largest)) {
415
          f.fd = FileDescriptor(number, 0, file_size);
J
jorlow@chromium.org 已提交
416 417
          new_files_.push_back(std::make_pair(level, f));
        } else {
418 419 420
          if (!msg) {
            msg = "new-file entry";
          }
J
jorlow@chromium.org 已提交
421 422
        }
        break;
423 424 425 426
      }
      case kNewFile2: {
        uint64_t number;
        uint64_t file_size;
427 428
        SequenceNumber smallest_seqno;
        SequenceNumber largest_seqno;
429 430
        if (GetLevel(&input, &level, &msg) && GetVarint64(&input, &number) &&
            GetVarint64(&input, &file_size) &&
431 432
            GetInternalKey(&input, &f.smallest) &&
            GetInternalKey(&input, &f.largest) &&
433 434 435 436
            GetVarint64(&input, &smallest_seqno) &&
            GetVarint64(&input, &largest_seqno)) {
          f.fd = FileDescriptor(number, 0, file_size, smallest_seqno,
                                largest_seqno);
437 438 439 440 441 442 443 444 445 446 447 448 449
          new_files_.push_back(std::make_pair(level, f));
        } else {
          if (!msg) {
            msg = "new-file2 entry";
          }
        }
        break;
      }

      case kNewFile3: {
        uint64_t number;
        uint32_t path_id;
        uint64_t file_size;
450 451
        SequenceNumber smallest_seqno;
        SequenceNumber largest_seqno;
452 453 454 455
        if (GetLevel(&input, &level, &msg) && GetVarint64(&input, &number) &&
            GetVarint32(&input, &path_id) && GetVarint64(&input, &file_size) &&
            GetInternalKey(&input, &f.smallest) &&
            GetInternalKey(&input, &f.largest) &&
456 457 458 459
            GetVarint64(&input, &smallest_seqno) &&
            GetVarint64(&input, &largest_seqno)) {
          f.fd = FileDescriptor(number, path_id, file_size, smallest_seqno,
                                largest_seqno);
460 461 462
          new_files_.push_back(std::make_pair(level, f));
        } else {
          if (!msg) {
463
            msg = "new-file3 entry";
464 465 466
          }
        }
        break;
467
      }
468

469 470 471 472 473
      case kNewFile4: {
        msg = DecodeNewFile4From(&input);
        break;
      }

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
      case kColumnFamily:
        if (!GetVarint32(&input, &column_family_)) {
          if (!msg) {
            msg = "set column family id";
          }
        }
        break;

      case kColumnFamilyAdd:
        if (GetLengthPrefixedSlice(&input, &str)) {
          is_column_family_add_ = true;
          column_family_name_ = str.ToString();
        } else {
          if (!msg) {
            msg = "column family add";
          }
        }
        break;

      case kColumnFamilyDrop:
        is_column_family_drop_ = true;
        break;

497 498 499 500 501 502 503 504 505
      case kInAtomicGroup:
        is_in_atomic_group_ = true;
        if (!GetVarint32(&input, &remaining_entries_)) {
          if (!msg) {
            msg = "remaining entries";
          }
        }
        break;

J
jorlow@chromium.org 已提交
506
      default:
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
        if (tag & kTagSafeIgnoreMask) {
          // Tag from future which can be safely ignored.
          // The next field must be the length of the entry.
          uint32_t field_len;
          if (!GetVarint32(&input, &field_len) ||
              static_cast<size_t>(field_len) > input.size()) {
            if (!msg) {
              msg = "safely ignoreable tag length error";
            }
          } else {
            input.remove_prefix(static_cast<size_t>(field_len));
          }
        } else {
          msg = "unknown tag";
        }
J
jorlow@chromium.org 已提交
522 523 524 525
        break;
    }
  }

A
Abhishek Kona 已提交
526
  if (msg == nullptr && !input.empty()) {
J
jorlow@chromium.org 已提交
527 528 529 530
    msg = "invalid tag";
  }

  Status result;
A
Abhishek Kona 已提交
531
  if (msg != nullptr) {
J
jorlow@chromium.org 已提交
532 533 534 535 536
    result = Status::Corruption("VersionEdit", msg);
  }
  return result;
}

537
std::string VersionEdit::DebugString(bool hex_key) const {
J
jorlow@chromium.org 已提交
538 539 540 541 542 543 544 545 546 547
  std::string r;
  r.append("VersionEdit {");
  if (has_comparator_) {
    r.append("\n  Comparator: ");
    r.append(comparator_);
  }
  if (has_log_number_) {
    r.append("\n  LogNumber: ");
    AppendNumberTo(&r, log_number_);
  }
548 549 550 551
  if (has_prev_log_number_) {
    r.append("\n  PrevLogNumber: ");
    AppendNumberTo(&r, prev_log_number_);
  }
J
jorlow@chromium.org 已提交
552
  if (has_next_file_number_) {
553
    r.append("\n  NextFileNumber: ");
J
jorlow@chromium.org 已提交
554 555
    AppendNumberTo(&r, next_file_number_);
  }
S
Siying Dong 已提交
556 557 558 559
  if (has_min_log_number_to_keep_) {
    r.append("\n  MinLogNumberToKeep: ");
    AppendNumberTo(&r, min_log_number_to_keep_);
  }
J
jorlow@chromium.org 已提交
560 561 562 563 564 565 566 567 568 569 570 571
  if (has_last_sequence_) {
    r.append("\n  LastSeq: ");
    AppendNumberTo(&r, last_sequence_);
  }
  for (DeletedFileSet::const_iterator iter = deleted_files_.begin();
       iter != deleted_files_.end();
       ++iter) {
    r.append("\n  DeleteFile: ");
    AppendNumberTo(&r, iter->first);
    r.append(" ");
    AppendNumberTo(&r, iter->second);
  }
D
dgrogan@chromium.org 已提交
572
  for (size_t i = 0; i < new_files_.size(); i++) {
J
jorlow@chromium.org 已提交
573 574 575 576
    const FileMetaData& f = new_files_[i].second;
    r.append("\n  AddFile: ");
    AppendNumberTo(&r, new_files_[i].first);
    r.append(" ");
577
    AppendNumberTo(&r, f.fd.GetNumber());
J
jorlow@chromium.org 已提交
578
    r.append(" ");
579
    AppendNumberTo(&r, f.fd.GetFileSize());
G
Gabor Cselle 已提交
580
    r.append(" ");
581
    r.append(f.smallest.DebugString(hex_key));
G
Gabor Cselle 已提交
582
    r.append(" .. ");
583
    r.append(f.largest.DebugString(hex_key));
J
jorlow@chromium.org 已提交
584
  }
585 586 587 588 589 590 591 592 593
  r.append("\n  ColumnFamily: ");
  AppendNumberTo(&r, column_family_);
  if (is_column_family_add_) {
    r.append("\n  ColumnFamilyAdd: ");
    r.append(column_family_name_);
  }
  if (is_column_family_drop_) {
    r.append("\n  ColumnFamilyDrop");
  }
594 595
  if (has_max_column_family_) {
    r.append("\n  MaxColumnFamily: ");
596
    AppendNumberTo(&r, max_column_family_);
597
  }
598
  if (is_in_atomic_group_) {
599
    r.append("\n  AtomicGroup: ");
600 601 602
    AppendNumberTo(&r, remaining_entries_);
    r.append(" entries remains");
  }
J
jorlow@chromium.org 已提交
603 604 605 606
  r.append("\n}\n");
  return r;
}

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
std::string VersionEdit::DebugJSON(int edit_num, bool hex_key) const {
  JSONWriter jw;
  jw << "EditNumber" << edit_num;

  if (has_comparator_) {
    jw << "Comparator" << comparator_;
  }
  if (has_log_number_) {
    jw << "LogNumber" << log_number_;
  }
  if (has_prev_log_number_) {
    jw << "PrevLogNumber" << prev_log_number_;
  }
  if (has_next_file_number_) {
    jw << "NextFileNumber" << next_file_number_;
  }
  if (has_last_sequence_) {
    jw << "LastSeq" << last_sequence_;
  }

  if (!deleted_files_.empty()) {
    jw << "DeletedFiles";
    jw.StartArray();

    for (DeletedFileSet::const_iterator iter = deleted_files_.begin();
         iter != deleted_files_.end();
         ++iter) {
      jw.StartArrayedObject();
      jw << "Level" << iter->first;
      jw << "FileNumber" << iter->second;
      jw.EndArrayedObject();
    }

    jw.EndArray();
  }

  if (!new_files_.empty()) {
    jw << "AddedFiles";
    jw.StartArray();

    for (size_t i = 0; i < new_files_.size(); i++) {
      jw.StartArrayedObject();
      jw << "Level" << new_files_[i].first;
      const FileMetaData& f = new_files_[i].second;
      jw << "FileNumber" << f.fd.GetNumber();
      jw << "FileSize" << f.fd.GetFileSize();
      jw << "SmallestIKey" << f.smallest.DebugString(hex_key);
      jw << "LargestIKey" << f.largest.DebugString(hex_key);
      jw.EndArrayedObject();
    }

    jw.EndArray();
  }

  jw << "ColumnFamily" << column_family_;

  if (is_column_family_add_) {
    jw << "ColumnFamilyAdd" << column_family_name_;
  }
  if (is_column_family_drop_) {
    jw << "ColumnFamilyDrop" << column_family_name_;
  }
  if (has_max_column_family_) {
    jw << "MaxColumnFamily" << max_column_family_;
  }
S
Siying Dong 已提交
672 673 674
  if (has_min_log_number_to_keep_) {
    jw << "MinLogNumberToKeep" << min_log_number_to_keep_;
  }
675 676 677
  if (is_in_atomic_group_) {
    jw << "AtomicGroup" << remaining_entries_;
  }
678 679 680 681 682 683

  jw.EndObject();

  return jw.Get();
}

684
}  // namespace rocksdb