compacted_db_impl.cc 5.4 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
L
Lei Jin 已提交
2 3 4 5 6
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.

#ifndef ROCKSDB_LITE
7
#include "db/compacted_db_impl.h"
L
Lei Jin 已提交
8 9
#include "db/db_impl.h"
#include "db/version_set.h"
10
#include "table/get_context.h"
L
Lei Jin 已提交
11 12 13 14 15

namespace rocksdb {

extern void MarkKeyMayExist(void* arg);
extern bool SaveValue(void* arg, const ParsedInternalKey& parsed_key,
16
                      const Slice& v, bool hit_and_return);
L
Lei Jin 已提交
17 18 19 20 21 22 23 24 25

CompactedDBImpl::CompactedDBImpl(
  const DBOptions& options, const std::string& dbname)
  : DBImpl(options, dbname) {
}

CompactedDBImpl::~CompactedDBImpl() {
}

26
size_t CompactedDBImpl::FindFile(const Slice& key) {
L
Lei Jin 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  size_t left = 0;
  size_t right = files_.num_files - 1;
  while (left < right) {
    size_t mid = (left + right) >> 1;
    const FdWithKeyRange& f = files_.files[mid];
    if (user_comparator_->Compare(ExtractUserKey(f.largest_key), key) < 0) {
      // Key at "mid.largest" is < "target".  Therefore all
      // files at or before "mid" are uninteresting.
      left = mid + 1;
    } else {
      // Key at "mid.largest" is >= "target".  Therefore all files
      // after "mid" are uninteresting.
      right = mid;
    }
  }
42 43 44 45 46
  return right;
}

Status CompactedDBImpl::Get(const ReadOptions& options,
     ColumnFamilyHandle*, const Slice& key, std::string* value) {
47
  GetContext get_context(user_comparator_, nullptr, nullptr, nullptr,
48 49
                         GetContext::kNotFound, key, value, nullptr, nullptr,
                         nullptr);
L
Lei Jin 已提交
50
  LookupKey lkey(key, kMaxSequenceNumber);
51 52 53
  files_.files[FindFile(key)].fd.table_reader->Get(
      options, lkey.internal_key(), &get_context);
  if (get_context.State() == GetContext::kFound) {
L
Lei Jin 已提交
54 55 56 57 58
    return Status::OK();
  }
  return Status::NotFound();
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
std::vector<Status> CompactedDBImpl::MultiGet(const ReadOptions& options,
    const std::vector<ColumnFamilyHandle*>&,
    const std::vector<Slice>& keys, std::vector<std::string>* values) {
  autovector<TableReader*, 16> reader_list;
  for (const auto& key : keys) {
    const FdWithKeyRange& f = files_.files[FindFile(key)];
    if (user_comparator_->Compare(key, ExtractUserKey(f.smallest_key)) < 0) {
      reader_list.push_back(nullptr);
    } else {
      LookupKey lkey(key, kMaxSequenceNumber);
      f.fd.table_reader->Prepare(lkey.internal_key());
      reader_list.push_back(f.fd.table_reader);
    }
  }
  std::vector<Status> statuses(keys.size(), Status::NotFound());
  values->resize(keys.size());
  int idx = 0;
  for (auto* r : reader_list) {
    if (r != nullptr) {
78 79
      GetContext get_context(user_comparator_, nullptr, nullptr, nullptr,
                             GetContext::kNotFound, keys[idx], &(*values)[idx],
80
                             nullptr, nullptr, nullptr);
81
      LookupKey lkey(keys[idx], kMaxSequenceNumber);
82 83
      r->Get(options, lkey.internal_key(), &get_context);
      if (get_context.State() == GetContext::kFound) {
84 85 86 87 88 89 90 91
        statuses[idx] = Status::OK();
      }
    }
    ++idx;
  }
  return statuses;
}

L
Lei Jin 已提交
92 93 94 95
Status CompactedDBImpl::Init(const Options& options) {
  mutex_.Lock();
  ColumnFamilyDescriptor cf(kDefaultColumnFamilyName,
                            ColumnFamilyOptions(options));
96
  Status s = Recover({cf}, true /* read only */, false, true);
L
Lei Jin 已提交
97 98 99 100 101 102 103 104 105
  if (s.ok()) {
    cfd_ = reinterpret_cast<ColumnFamilyHandleImpl*>(
              DefaultColumnFamily())->cfd();
    delete cfd_->InstallSuperVersion(new SuperVersion(), &mutex_);
  }
  mutex_.Unlock();
  if (!s.ok()) {
    return s;
  }
Y
Yueh-Hsuan Chiang 已提交
106
  NewThreadStatusCfInfo(cfd_);
L
Lei Jin 已提交
107 108
  version_ = cfd_->GetSuperVersion()->current;
  user_comparator_ = cfd_->user_comparator();
S
sdong 已提交
109
  auto* vstorage = version_->storage_info();
110 111 112
  if (vstorage->num_non_empty_levels() == 0) {
    return Status::NotSupported("no file exists");
  }
S
sdong 已提交
113
  const LevelFilesBrief& l0 = vstorage->LevelFilesBrief(0);
L
Lei Jin 已提交
114
  // L0 should not have files
115
  if (l0.num_files > 1) {
L
Lei Jin 已提交
116 117
    return Status::NotSupported("L0 contain more than 1 file");
  }
118
  if (l0.num_files == 1) {
119
    if (vstorage->num_non_empty_levels() > 1) {
L
Lei Jin 已提交
120 121
      return Status::NotSupported("Both L0 and other level contain files");
    }
122
    files_ = l0;
L
Lei Jin 已提交
123 124 125
    return Status::OK();
  }

126
  for (int i = 1; i < vstorage->num_non_empty_levels() - 1; ++i) {
S
sdong 已提交
127
    if (vstorage->LevelFilesBrief(i).num_files > 0) {
L
Lei Jin 已提交
128 129 130 131
      return Status::NotSupported("Other levels also contain files");
    }
  }

132
  int level = vstorage->num_non_empty_levels() - 1;
S
sdong 已提交
133 134
  if (vstorage->LevelFilesBrief(level).num_files > 0) {
    files_ = vstorage->LevelFilesBrief(level);
L
Lei Jin 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    return Status::OK();
  }
  return Status::NotSupported("no file exists");
}

Status CompactedDBImpl::Open(const Options& options,
                             const std::string& dbname, DB** dbptr) {
  *dbptr = nullptr;

  if (options.max_open_files != -1) {
    return Status::InvalidArgument("require max_open_files = -1");
  }
  if (options.merge_operator.get() != nullptr) {
    return Status::InvalidArgument("merge operator is not supported");
  }
  DBOptions db_options(options);
  std::unique_ptr<CompactedDBImpl> db(new CompactedDBImpl(db_options, dbname));
  Status s = db->Init(options);
  if (s.ok()) {
154 155 156
    Log(INFO_LEVEL, db->db_options_.info_log,
        "Opened the db as fully compacted mode");
    LogFlush(db->db_options_.info_log);
L
Lei Jin 已提交
157 158 159 160 161 162 163
    *dbptr = db.release();
  }
  return s;
}

}   // namespace rocksdb
#endif  // ROCKSDB_LITE