builder.cc 5.9 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  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 62
    uint32_t column_family_id, std::vector<SequenceNumber> snapshots,
    const CompressionType compression,
63
    const CompressionOptions& compression_opts, bool paranoid_file_checks,
64 65
    InternalStats* internal_stats, const Env::IOPriority io_priority,
    TableProperties* table_properties) {
66 67
  // Reports the IOStats for flush for every following bytes.
  const size_t kReportFlushIOStatsEvery = 1048576;
J
jorlow@chromium.org 已提交
68
  Status s;
69
  meta->fd.file_size = 0;
J
jorlow@chromium.org 已提交
70 71
  iter->SeekToFirst();

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

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

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

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

98
    CompactionIterator c_iter(iter, internal_comparator.user_comparator(),
99
                              &merge, kMaxSequenceNumber, &snapshots, env,
100 101 102 103 104 105 106 107 108
                              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 已提交
109 110
      if (io_priority == Env::IO_HIGH &&
          IOSTATS(bytes_written) >= kReportFlushIOStatsEvery) {
111
        ThreadStatusUtil::SetThreadOperationProperty(
I
Igor Canadi 已提交
112
            ThreadStatus::FLUSH_BYTES_WRITTEN, IOSTATS(bytes_written));
113
      }
J
jorlow@chromium.org 已提交
114 115 116
    }

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

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

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

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

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

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

171
}  // namespace rocksdb