1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors.
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// TableBuilder provides the interface used to build a Table
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// (an immutable and sorted map from keys to values).
7a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org//
8a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// Multiple threads can invoke const methods on a TableBuilder without
9a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// external synchronization, but if any of the threads may call a
10a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// non-const method, all threads accessing the same TableBuilder must use
11a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// external synchronization.
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stdint.h>
171ca60b12c68a71aac695b15e329b2a76a63cbb0ajorlow@chromium.org#include "leveldb/options.h"
181ca60b12c68a71aac695b15e329b2a76a63cbb0ajorlow@chromium.org#include "leveldb/status.h"
19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass BlockBuilder;
23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass BlockHandle;
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass WritableFile;
25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass TableBuilder {
27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public:
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Create a builder that will store the contents of the table it is
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // building in *file.  Does not close the file.  It is up to the
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // caller to close the file after calling Finish().
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  TableBuilder(const Options& options, WritableFile* file);
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: Either Finish() or Abandon() has been called.
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  ~TableBuilder();
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Change the options used by this builder.  Note: only some of the
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // option fields can be changed after construction.  If a field is
38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // not allowed to change dynamically and its value in the structure
39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // passed to the constructor is different from its value in the
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // structure passed to this method, this method will return an error
41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // without changing any fields.
42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Status ChangeOptions(const Options& options);
43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Add key,value to the table being constructed.
45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: key is after any previously added key according to comparator.
46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: Finish(), Abandon() have not been called
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Add(const Slice& key, const Slice& value);
48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Advanced operation: flush any buffered key/value pairs to file.
50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Can be used to ensure that two adjacent entries never live in
51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // the same data block.  Most clients should not need to use this method.
52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: Finish(), Abandon() have not been called
53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Flush();
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Return non-ok iff some error has been detected.
56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Status status() const;
57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Finish building the table.  Stops using the file passed to the
59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // constructor after this function returns.
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: Finish(), Abandon() have not been called
61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Status Finish();
62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Indicate that the contents of this builder should be abandoned.  Stops
64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // using the file passed to the constructor after this function returns.
65179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If the caller is not going to call Finish(), it must call Abandon()
66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // before destroying this builder.
67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // REQUIRES: Finish(), Abandon() have not been called
68179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Abandon();
69179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
70179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Number of calls to Add() so far.
71179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  uint64_t NumEntries() const;
72179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
73179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Size of the file generated so far.  If invoked after a successful
74179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Finish() call, returns the size of the final generated file.
75179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  uint64_t FileSize() const;
76179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
77179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private:
78179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool ok() const { return status().ok(); }
79179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void WriteBlock(BlockBuilder* block, BlockHandle* handle);
8099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle);
81179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
82179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  struct Rep;
83179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Rep* rep_;
84179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
85179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // No copying allowed
86179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  TableBuilder(const TableBuilder&);
87179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void operator=(const TableBuilder&);
88179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
89179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
9045b9940be332834440bd5299419f396e38085ebehans@chromium.org}  // namespace leveldb
91179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
92179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif  // STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_
93