sst_file_writer.cc 6.3 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 3 4 5 6 7 8 9 10 11
//  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.

#include "rocksdb/sst_file_writer.h"

#include <vector>
#include "db/dbformat.h"
#include "rocksdb/table.h"
#include "table/block_based_table_builder.h"
12
#include "table/sst_file_writer_collectors.h"
13 14 15 16 17 18
#include "util/file_reader_writer.h"

namespace rocksdb {

const std::string ExternalSstFilePropertyNames::kVersion =
    "rocksdb.external_sst_file.version";
19 20
const std::string ExternalSstFilePropertyNames::kGlobalSeqno =
    "rocksdb.external_sst_file.global_seqno";
21 22

struct SstFileWriter::Rep {
A
Aaron Gao 已提交
23
  Rep(const EnvOptions& _env_options, const Options& options,
24
      const Comparator* _user_comparator, ColumnFamilyHandle* _cfh)
25
      : env_options(_env_options),
A
Aaron Gao 已提交
26
        ioptions(options),
Y
Yi Wu 已提交
27
        mutable_cf_options(options),
28 29
        internal_comparator(_user_comparator),
        cfh(_cfh) {}
30 31 32 33 34

  std::unique_ptr<WritableFileWriter> file_writer;
  std::unique_ptr<TableBuilder> builder;
  EnvOptions env_options;
  ImmutableCFOptions ioptions;
A
Aaron Gao 已提交
35
  MutableCFOptions mutable_cf_options;
36 37
  InternalKeyComparator internal_comparator;
  ExternalSstFileInfo file_info;
38
  InternalKey ikey;
39 40
  std::string column_family_name;
  ColumnFamilyHandle* cfh;
41 42 43
};

SstFileWriter::SstFileWriter(const EnvOptions& env_options,
A
Aaron Gao 已提交
44
                             const Options& options,
45 46
                             const Comparator* user_comparator,
                             ColumnFamilyHandle* column_family)
D
Ding Ma 已提交
47 48 49
    : rep_(new Rep(env_options, options, user_comparator, column_family)) {
      rep_->file_info.file_size = 0;
    }
50

I
Islam AbdelRahman 已提交
51 52 53 54 55 56 57 58 59
SstFileWriter::~SstFileWriter() {
  if (rep_->builder) {
    // User did not call Finish() or Finish() failed, we need to
    // abandon the builder.
    rep_->builder->Abandon();
  }

  delete rep_;
}
60 61 62 63 64 65 66 67 68 69

Status SstFileWriter::Open(const std::string& file_path) {
  Rep* r = rep_;
  Status s;
  std::unique_ptr<WritableFile> sst_file;
  s = r->ioptions.env->NewWritableFile(file_path, &sst_file, r->env_options);
  if (!s.ok()) {
    return s;
  }

70 71 72 73
  CompressionType compression_type;
  if (r->ioptions.bottommost_compression != kDisableCompressionOption) {
    compression_type = r->ioptions.bottommost_compression;
  } else if (!r->ioptions.compression_per_level.empty()) {
74 75
    // Use the compression of the last level if we have per level compression
    compression_type = *(r->ioptions.compression_per_level.rbegin());
76 77
  } else {
    compression_type = r->mutable_cf_options.compression;
78 79 80 81
  }

  std::vector<std::unique_ptr<IntTblPropCollectorFactory>>
      int_tbl_prop_collector_factories;
82 83

  // SstFileWriter properties collector to add SstFileWriter version.
84
  int_tbl_prop_collector_factories.emplace_back(
85 86
      new SstFileWriterPropertiesCollectorFactory(2 /* version */,
                                                  0 /* global_seqno*/));
87

88 89 90 91 92 93 94 95
  // User collector factories
  auto user_collector_factories =
      r->ioptions.table_properties_collector_factories;
  for (size_t i = 0; i < user_collector_factories.size(); i++) {
    int_tbl_prop_collector_factories.emplace_back(
        new UserKeyTablePropertiesCollectorFactory(
            user_collector_factories[i]));
  }
96
  int unknown_level = -1;
97 98 99 100 101 102 103 104 105 106 107 108
  uint32_t cf_id;

  if (r->cfh != nullptr) {
    // user explicitly specified that this file will be ingested into cfh,
    // we can persist this information in the file.
    cf_id = r->cfh->GetID();
    r->column_family_name = r->cfh->GetName();
  } else {
    r->column_family_name = "";
    cf_id = TablePropertiesCollectorFactory::Context::kUnknownColumnFamily;
  }

109 110
  TableBuilderOptions table_builder_options(
      r->ioptions, r->internal_comparator, &int_tbl_prop_collector_factories,
111 112
      compression_type, r->ioptions.compression_opts,
      nullptr /* compression_dict */, false /* skip_filters */,
113
      r->column_family_name, unknown_level);
114 115
  r->file_writer.reset(
      new WritableFileWriter(std::move(sst_file), r->env_options));
116 117 118

  // TODO(tec) : If table_factory is using compressed block cache, we will
  // be adding the external sst file blocks into it, which is wasteful.
119
  r->builder.reset(r->ioptions.table_factory->NewTableBuilder(
120
      table_builder_options, cf_id, r->file_writer.get()));
121 122 123 124 125

  r->file_info.file_path = file_path;
  r->file_info.file_size = 0;
  r->file_info.num_entries = 0;
  r->file_info.sequence_number = 0;
126
  r->file_info.version = 2;
127 128 129 130 131 132 133 134 135 136
  return s;
}

Status SstFileWriter::Add(const Slice& user_key, const Slice& value) {
  Rep* r = rep_;
  if (!r->builder) {
    return Status::InvalidArgument("File is not opened");
  }

  if (r->file_info.num_entries == 0) {
137
    r->file_info.smallest_key.assign(user_key.data(), user_key.size());
138 139 140 141 142 143 144 145 146 147
  } else {
    if (r->internal_comparator.user_comparator()->Compare(
            user_key, r->file_info.largest_key) <= 0) {
      // Make sure that keys are added in order
      return Status::InvalidArgument("Keys must be added in order");
    }
  }

  // update file info
  r->file_info.num_entries++;
148
  r->file_info.largest_key.assign(user_key.data(), user_key.size());
149 150
  r->file_info.file_size = r->builder->FileSize();

151
  // TODO(tec) : For external SST files we could omit the seqno and type.
152 153 154
  r->ikey.Set(user_key, 0 /* Sequence Number */,
              ValueType::kTypeValue /* Put */);
  r->builder->Add(r->ikey.Encode(), value);
155 156 157 158 159 160 161 162 163

  return Status::OK();
}

Status SstFileWriter::Finish(ExternalSstFileInfo* file_info) {
  Rep* r = rep_;
  if (!r->builder) {
    return Status::InvalidArgument("File is not opened");
  }
164 165 166
  if (r->file_info.num_entries == 0) {
    return Status::InvalidArgument("Cannot create sst file with no entries");
  }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

  Status s = r->builder->Finish();
  if (s.ok()) {
    if (!r->ioptions.disable_data_sync) {
      s = r->file_writer->Sync(r->ioptions.use_fsync);
    }
    if (s.ok()) {
      s = r->file_writer->Close();
    }
  } else {
    r->builder->Abandon();
  }

  if (!s.ok()) {
    r->ioptions.env->DeleteFile(r->file_info.file_path);
  }

  if (s.ok() && file_info != nullptr) {
    r->file_info.file_size = r->builder->FileSize();
    *file_info = r->file_info;
  }

  r->builder.reset();
  return s;
}
D
Ding Ma 已提交
192 193 194 195

uint64_t SstFileWriter::FileSize() {
  return rep_->file_info.file_size;
}
196
}  // namespace rocksdb