xref: /6.0.3/couchstore/src/internal.h (revision 2b2ba193)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#ifndef LIBCOUCHSTORE_INTERNAL_H
3#define LIBCOUCHSTORE_INTERNAL_H 1
4
5/*
6 * This file contains datastructures and prototypes for functions only to
7 * be used by the internal workings of libcoucstore. If you for some reason
8 * need access to them from outside the library, you should write a
9 * function to give you what you need.
10 */
11#include <libcouchstore/couch_db.h>
12#include "config.h"
13#include "crc32.h"
14
15#define COUCH_BLOCK_SIZE 4096
16#define COUCH_DISK_VERSION_11 11
17#define COUCH_DISK_VERSION_12 12
18#define COUCH_DISK_VERSION COUCH_DISK_VERSION_12
19#define COUCH_SNAPPY_THRESHOLD 64
20#define MAX_DB_HEADER_SIZE 1024    /* Conservative estimate; just for sanity check */
21
22// Default values for buffered IO
23#define MAX_READ_BUFFERS 16
24#define WRITE_BUFFER_CAPACITY (128*1024)
25#define READ_BUFFER_CAPACITY (4*1024)
26
27#ifdef WIN32
28#define PATH_MAX MAX_PATH
29#endif
30
31#ifndef PATH_MAX
32#define PATH_MAX 1024
33#endif
34
35#define MAX_ERR_STR_LEN 250
36
37typedef struct {
38    uint64_t purge_before_ts;
39    uint64_t purge_before_seq;
40    uint64_t max_purged_seq;
41} time_purge_ctx;
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47    /* Configurations for an open file */
48    struct tree_file_options {
49        tree_file_options() :
50            buf_io_enabled(true),
51            buf_io_read_unit_size(READ_BUFFER_CAPACITY),
52            buf_io_read_buffers(MAX_READ_BUFFERS),
53            kp_nodesize(0),
54            kv_nodesize(0),
55            periodic_sync_bytes(0)
56            { }
57
58        // Flag indicating whether or not buffered IO is enabled.
59        bool buf_io_enabled;
60        // Read buffer capacity, if buffered IO is enabled.
61        // Set to zero for the default value.
62        uint32_t buf_io_read_unit_size;
63        // Max count of read buffers, if buffered IO is enabled.
64        // Set to zero for the default value.
65        uint32_t buf_io_read_buffers;
66        // Threshold of key-pointer (intermediate) node size.
67        uint32_t kp_nodesize;
68        // Threshold of key-value (leaf) node size.
69        uint32_t kv_nodesize;
70        // Automatically issue an sync() operation after every N bytes written.
71        // 0 means don't automatically sync.
72        uint64_t periodic_sync_bytes;
73    };
74
75     /* Structure representing an open file; "superclass" of Db */
76    typedef struct _treefile {
77        uint64_t pos;
78        FileOpsInterface* ops;
79        couch_file_handle handle;
80        const char* path;
81        couchstore_error_info_t lastError;
82        crc_mode_e crc_mode;
83        tree_file_options options;
84    } tree_file;
85
86    typedef struct _nodepointer {
87        sized_buf key;
88        uint64_t pointer;
89        sized_buf reduce_value;
90        uint64_t subtreesize;
91    } node_pointer;
92
93    typedef struct _db_header {
94        uint64_t disk_version;
95        uint64_t update_seq;
96        node_pointer *by_id_root;
97        node_pointer *by_seq_root;
98        node_pointer *local_docs_root;
99        uint64_t purge_seq;
100        uint64_t purge_ptr;
101        uint64_t position;
102    } db_header;
103
104    struct _db {
105        tree_file file;
106        db_header header;
107        int dropped;
108        void *userdata;
109    };
110
111    /**
112     * Returns a newed FileOpsInterface implementation
113     * instance.
114     *
115     * Useful for assuming that a FileOpsInterface object
116     * is heap-allocated.
117     */
118    LIBCOUCHSTORE_API
119    FileOpsInterface* create_default_file_ops(void);
120
121    /** Opens or creates a tree_file.
122        @param file  Pointer to tree_file struct to initialize.
123        @param filename  Path to the file
124        @param flags  POSIX open-mode flags
125        @param crc_mode CRC the file should use.
126        @param ops  File I/O operations to use
127        @param buffered Should the file operations be
128               wrapped by an IO buffer */
129    couchstore_error_t tree_file_open(tree_file* file,
130                                      const char *filename,
131                                      int openflags,
132                                      crc_mode_e crc_mode,
133                                      FileOpsInterface* ops,
134                                      tree_file_options options);
135    /** Closes a tree_file.
136        @param file  Pointer to open tree_file. Does not free this pointer! */
137    couchstore_error_t tree_file_close(tree_file* file);
138
139    /** Reads a chunk from the file at a given position.
140        @param file The tree_file to read from
141        @param pos The byte position to read from
142        @param ret_ptr On success, will be set to a malloced buffer containing the chunk data,
143                or to NULL if the length is zero. Caller is responsible for freeing this buffer!
144                On failure, value pointed to is unaltered.
145        @return The length of the chunk (zero is a valid length!), or a negative error code */
146    int pread_bin(tree_file *file, cs_off_t pos, char **ret_ptr);
147
148    /** Reads a compressed chunk from the file at a given position.
149        Parameters and return value are the same as for pread_bin. */
150    int pread_compressed(tree_file *file, cs_off_t pos, char **ret_ptr);
151
152    /** Reads a file header from the file at a given position.
153        Parameters and return value are the same as for pread_bin. */
154    int pread_header(tree_file *file,
155                     cs_off_t pos,
156                     char **ret_ptr,
157                     uint32_t max_header_size);
158
159    couchstore_error_t write_header(tree_file *file, sized_buf *buf, cs_off_t *pos);
160    int db_write_buf(tree_file *file, const sized_buf *buf, cs_off_t *pos, size_t *disk_size);
161    couchstore_error_t db_write_buf_compressed(tree_file *file, const sized_buf *buf, cs_off_t *pos, size_t *disk_size);
162    struct _os_error *get_os_error_store(void);
163    couchstore_error_t by_seq_read_docinfo(DocInfo **pInfo,
164                                           const sized_buf *k,
165                                           const sized_buf *v);
166
167    couchstore_error_t precommit(Db *db);
168    couchstore_error_t db_write_header(Db *db);
169
170    extern thread_local char internal_error_string[MAX_ERR_STR_LEN];
171
172#ifdef __cplusplus
173}
174#endif
175
176#endif
177