2011-03-18 22:37:00 +00:00
|
|
|
// 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"
|
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
2019-05-02 11:01:00 -07:00
|
|
|
#include "db/filename.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/table_cache.h"
|
|
|
|
#include "db/version_edit.h"
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/iterator.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Status BuildTable(const std::string& dbname, Env* env, const Options& options,
|
|
|
|
TableCache* table_cache, Iterator* iter, FileMetaData* meta) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status s;
|
|
|
|
meta->file_size = 0;
|
|
|
|
iter->SeekToFirst();
|
|
|
|
|
|
|
|
std::string fname = TableFileName(dbname, meta->number);
|
|
|
|
if (iter->Valid()) {
|
|
|
|
WritableFile* file;
|
|
|
|
s = env->NewWritableFile(fname, &file);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableBuilder* builder = new TableBuilder(options, file);
|
|
|
|
meta->smallest.DecodeFrom(iter->key());
|
|
|
|
for (; iter->Valid(); iter->Next()) {
|
|
|
|
Slice key = iter->key();
|
|
|
|
meta->largest.DecodeFrom(key);
|
|
|
|
builder->Add(key, iter->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish and check for builder errors
|
2017-09-01 14:36:20 -07:00
|
|
|
s = builder->Finish();
|
2011-03-18 22:37:00 +00:00
|
|
|
if (s.ok()) {
|
2017-09-01 14:36:20 -07:00
|
|
|
meta->file_size = builder->FileSize();
|
|
|
|
assert(meta->file_size > 0);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
delete builder;
|
|
|
|
|
|
|
|
// Finish and check for file errors
|
|
|
|
if (s.ok()) {
|
|
|
|
s = file->Sync();
|
|
|
|
}
|
|
|
|
if (s.ok()) {
|
|
|
|
s = file->Close();
|
|
|
|
}
|
|
|
|
delete file;
|
2018-04-10 16:18:06 -07:00
|
|
|
file = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
// Verify that the table is usable
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number,
|
2011-03-28 20:43:44 +00:00
|
|
|
meta->file_size);
|
2011-03-18 22:37:00 +00:00
|
|
|
s = it->status();
|
|
|
|
delete it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for input iterator errors
|
|
|
|
if (!iter->status().ok()) {
|
|
|
|
s = iter->status();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok() && meta->file_size > 0) {
|
2011-06-22 02:36:45 +00:00
|
|
|
// Keep it
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
|
|
|
env->DeleteFile(fname);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|