xref: /3.0.2-MP2/couchstore/src/tree_writer.h (revision afa48171)
1#ifndef LIBCOUCHSTORE_TREE_WRITER_H
2#define LIBCOUCHSTORE_TREE_WRITER_H
3
4#include <libcouchstore/couch_db.h>
5#include "couch_btree.h"
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11typedef struct TreeWriter TreeWriter;
12
13
14/**
15 * Creates a new TreeWriter.
16 * @param unsortedFilePath If non-NULL, the path to an existing file containing a series of unsorted
17 * key/value pairs in TreeWriter format. If NULL, an empty TreeWriter will be created (using a
18 * temporary file for the external sorting.)
19 * @param key_compare Callback function that compares two keys.
20 * @param out_writer The new TreeWriter pointer will be stored here.
21 * @return Error code or COUCHSTORE_SUCCESS.
22 */
23couchstore_error_t TreeWriterOpen(char* unsortedFilePath,
24                                  compare_callback key_compare,
25                                  reduce_fn reduce,
26                                  reduce_fn rereduce,
27                                  void *user_reduce_ctx,
28                                  TreeWriter** out_writer);
29
30/**
31 * Frees a TreeWriter instance. It is safe to pass a NULL pointer.
32 */
33void TreeWriterFree(TreeWriter* writer);
34
35/**
36 * Adds a key/value pair to a TreeWriter. These can be added in any order.
37 */
38couchstore_error_t TreeWriterAddItem(TreeWriter* writer, sized_buf key, sized_buf value);
39
40/**
41 * Sorts the key/value pairs already added.
42 * The keys are sorted by ebin_cmp (basic lexicographic order by byte values).
43 * If this TreeWriter was opened on an existing data file, the contents of the file will be sorted.
44 */
45couchstore_error_t TreeWriterSort(TreeWriter* writer);
46
47/**
48 * Writes the key/value pairs to a tree file, returning a pointer to the new root.
49 * The items should first have been sorted.
50 */
51couchstore_error_t TreeWriterWrite(TreeWriter* writer,
52                                   tree_file* to_file,
53                                   node_pointer** out_root);
54
55
56/*
57 * The input file format for TreeWriterOpen is as follows:
58 * The file is binary and consists of nothing more than a series of records.
59 * Each record looks like this:
60 *     2 bytes: Key length (big-endian)
61 *     4 bytes: Value length (big-endian)
62 *     <key bytes>
63 *     <value bytes>
64 */
65
66#ifdef __cplusplus
67}
68#endif
69
70
71#endif // LIBCOUCHSTORE_TREE_WRITER_H
72