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());
|
2020-07-11 13:44:11 +08:00
|
|
|
Slice key;
|
2011-03-18 22:37:00 +00:00
|
|
|
for (; iter->Valid(); iter->Next()) {
|
2020-07-11 13:44:11 +08:00
|
|
|
key = iter->key();
|
2011-03-18 22:37:00 +00:00
|
|
|
builder->Add(key, iter->value());
|
|
|
|
}
|
2020-07-13 19:14:04 +00:00
|
|
|
if (!key.empty()) {
|
2020-07-11 13:44:11 +08:00
|
|
|
meta->largest.DecodeFrom(key);
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// 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 {
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-08 09:14:53 -08:00
|
|
|
env->RemoveFile(fname);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|