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

I
Igor Canadi 已提交
12
#include <algorithm>
13
#include <deque>
14
#include <vector>
15

16
#include "db/compaction_iterator.h"
J
jorlow@chromium.org 已提交
17
#include "db/dbformat.h"
K
kailiu 已提交
18
#include "db/filename.h"
19
#include "db/internal_stats.h"
20
#include "db/merge_helper.h"
J
jorlow@chromium.org 已提交
21 22
#include "db/table_cache.h"
#include "db/version_edit.h"
23 24 25
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "rocksdb/iterator.h"
S
Siying Dong 已提交
26
#include "rocksdb/options.h"
K
kailiu 已提交
27
#include "rocksdb/table.h"
S
Siying Dong 已提交
28
#include "table/block_based_table_builder.h"
S
sdong 已提交
29
#include "table/internal_iterator.h"
30
#include "util/file_reader_writer.h"
31
#include "util/iostats_context_imp.h"
32
#include "util/stop_watch.h"
33
#include "util/thread_status_util.h"
J
jorlow@chromium.org 已提交
34

35
namespace rocksdb {
J
jorlow@chromium.org 已提交
36

S
Siying Dong 已提交
37 38
class TableFactory;

39 40 41 42 43
TableBuilder* NewTableBuilder(
    const ImmutableCFOptions& ioptions,
    const InternalKeyComparator& internal_comparator,
    const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
        int_tbl_prop_collector_factories,
44 45
    uint32_t column_family_id, WritableFileWriter* file,
    const CompressionType compression_type,
46 47 48 49 50
    const CompressionOptions& compression_opts, const bool skip_filters) {
  return ioptions.table_factory->NewTableBuilder(
      TableBuilderOptions(ioptions, internal_comparator,
                          int_tbl_prop_collector_factories, compression_type,
                          compression_opts, skip_filters),
51
      column_family_id, file);
S
Siying Dong 已提交
52 53
}

54 55
Status BuildTable(
    const std::string& dbname, Env* env, const ImmutableCFOptions& ioptions,
S
sdong 已提交
56 57 58
    const EnvOptions& env_options, TableCache* table_cache,
    InternalIterator* iter, FileMetaData* meta,
    const InternalKeyComparator& internal_comparator,
59 60
    const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
        int_tbl_prop_collector_factories,
61
    uint32_t column_family_id, std::vector<SequenceNumber> snapshots,
62
    SequenceNumber earliest_write_conflict_snapshot,
63
    const CompressionType compression,
64
    const CompressionOptions& compression_opts, bool paranoid_file_checks,
65
    InternalStats* internal_stats, const Env::IOPriority io_priority,
66
    TableProperties* table_properties) {
67 68
  // Reports the IOStats for flush for every following bytes.
  const size_t kReportFlushIOStatsEvery = 1048576;
J
jorlow@chromium.org 已提交
69
  Status s;
70
  meta->fd.file_size = 0;
J
jorlow@chromium.org 已提交
71 72
  iter->SeekToFirst();

L
Lei Jin 已提交
73
  std::string fname = TableFileName(ioptions.db_paths, meta->fd.GetNumber(),
74
                                    meta->fd.GetPathId());
J
jorlow@chromium.org 已提交
75
  if (iter->Valid()) {
76 77 78 79
    TableBuilder* builder;
    unique_ptr<WritableFileWriter> file_writer;
    {
      unique_ptr<WritableFile> file;
S
sdong 已提交
80
      s = NewWritableFile(env, fname, &file, env_options);
81 82 83 84
      if (!s.ok()) {
        return s;
      }
      file->SetIOPriority(io_priority);
S
Siying Dong 已提交
85

86 87 88 89
      file_writer.reset(new WritableFileWriter(std::move(file), env_options));

      builder = NewTableBuilder(
          ioptions, internal_comparator, int_tbl_prop_collector_factories,
90
          column_family_id, file_writer.get(), compression, compression_opts);
91
    }
92

I
Igor Canadi 已提交
93 94
    MergeHelper merge(env, internal_comparator.user_comparator(),
                      ioptions.merge_operator, nullptr, ioptions.info_log,
L
Lei Jin 已提交
95
                      ioptions.min_partial_merge_operands,
I
Igor Canadi 已提交
96 97
                      true /* internal key corruption is not ok */,
                      snapshots.empty() ? 0 : snapshots.back());
98

99
    CompactionIterator c_iter(iter, internal_comparator.user_comparator(),
100
                              &merge, kMaxSequenceNumber, &snapshots,
101
                              earliest_write_conflict_snapshot, env,
102 103 104 105 106 107 108 109 110
                              true /* internal key corruption is not ok */);
    c_iter.SeekToFirst();
    for (; c_iter.Valid(); c_iter.Next()) {
      const Slice& key = c_iter.key();
      const Slice& value = c_iter.value();
      builder->Add(key, value);
      meta->UpdateBoundaries(key, c_iter.ikey().sequence);

      // TODO(noetzli): Update stats after flush, too.
I
Igor Canadi 已提交
111 112
      if (io_priority == Env::IO_HIGH &&
          IOSTATS(bytes_written) >= kReportFlushIOStatsEvery) {
113
        ThreadStatusUtil::SetThreadOperationProperty(
I
Igor Canadi 已提交
114
            ThreadStatus::FLUSH_BYTES_WRITTEN, IOSTATS(bytes_written));
115
      }
J
jorlow@chromium.org 已提交
116 117 118
    }

    // Finish and check for builder errors
A
Andres Noetzli 已提交
119
    bool empty = builder->NumEntries() == 0;
120
    s = c_iter.status();
A
Andres Noetzli 已提交
121
    if (!s.ok() || empty) {
J
jorlow@chromium.org 已提交
122
      builder->Abandon();
A
Andres Noetzli 已提交
123 124
    } else {
      s = builder->Finish();
J
jorlow@chromium.org 已提交
125
    }
A
Andres Noetzli 已提交
126 127

    if (s.ok() && !empty) {
128
      meta->fd.file_size = builder->FileSize();
129
      meta->marked_for_compaction = builder->NeedCompact();
130 131 132 133 134
      assert(meta->fd.GetFileSize() > 0);
      if (table_properties) {
        *table_properties = builder->GetTableProperties();
      }
    }
J
jorlow@chromium.org 已提交
135 136 137
    delete builder;

    // Finish and check for file errors
A
Andres Noetzli 已提交
138
    if (s.ok() && !empty && !ioptions.disable_data_sync) {
139 140
      StopWatch sw(env, ioptions.statistics, TABLE_SYNC_MICROS);
      file_writer->Sync(ioptions.use_fsync);
J
jorlow@chromium.org 已提交
141
    }
A
Andres Noetzli 已提交
142
    if (s.ok() && !empty) {
143
      s = file_writer->Close();
J
jorlow@chromium.org 已提交
144 145
    }

A
Andres Noetzli 已提交
146
    if (s.ok() && !empty) {
J
jorlow@chromium.org 已提交
147
      // Verify that the table is usable
S
sdong 已提交
148
      std::unique_ptr<InternalIterator> it(table_cache->NewIterator(
149 150 151
          ReadOptions(), env_options, internal_comparator, meta->fd, nullptr,
          (internal_stats == nullptr) ? nullptr
                                      : internal_stats->GetFileReadHist(0),
152
          false));
J
jorlow@chromium.org 已提交
153
      s = it->status();
154
      if (s.ok() && paranoid_file_checks) {
A
Andres Noetzli 已提交
155 156
        for (it->SeekToFirst(); it->Valid(); it->Next()) {
        }
157 158
        s = it->status();
      }
J
jorlow@chromium.org 已提交
159 160 161 162 163 164 165 166
    }
  }

  // Check for input iterator errors
  if (!iter->status().ok()) {
    s = iter->status();
  }

A
Andres Noetzli 已提交
167
  if (!s.ok() || meta->fd.GetFileSize() == 0) {
J
jorlow@chromium.org 已提交
168 169 170 171 172
    env->DeleteFile(fname);
  }
  return s;
}

173
}  // namespace rocksdb