xref: /6.0.3/couchstore/src/internal.h (revision c2c458ff)
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              tracing_enabled(false),
57              write_validation_enabled(false),
58              mprotect_enabled(false) {
59        }
60
61        // Flag indicating whether or not buffered IO is enabled.
62        bool buf_io_enabled;
63        // Read buffer capacity, if buffered IO is enabled.
64        // Set to zero for the default value.
65        uint32_t buf_io_read_unit_size;
66        // Max count of read buffers, if buffered IO is enabled.
67        // Set to zero for the default value.
68        uint32_t buf_io_read_buffers;
69        // Threshold of key-pointer (intermediate) node size.
70        uint32_t kp_nodesize;
71        // Threshold of key-value (leaf) node size.
72        uint32_t kv_nodesize;
73        // Automatically issue an sync() operation after every N bytes written.
74        // 0 means don't automatically sync.
75        uint64_t periodic_sync_bytes;
76        /* tracing and validation options  */
77        bool tracing_enabled;
78        bool write_validation_enabled;
79        bool mprotect_enabled;
80    };
81
82     /* Structure representing an open file; "superclass" of Db */
83    typedef struct _treefile {
84        uint64_t pos;
85        FileOpsInterface* ops;
86        couch_file_handle handle;
87        const char* path;
88        couchstore_error_info_t lastError;
89        crc_mode_e crc_mode;
90        tree_file_options options;
91    } tree_file;
92
93    typedef struct _nodepointer {
94        sized_buf key;
95        uint64_t pointer;
96        sized_buf reduce_value;
97        uint64_t subtreesize;
98    } node_pointer;
99
100    typedef struct _db_header {
101        uint64_t disk_version;
102        uint64_t update_seq;
103        node_pointer *by_id_root;
104        node_pointer *by_seq_root;
105        node_pointer *local_docs_root;
106        uint64_t purge_seq;
107        uint64_t purge_ptr;
108        uint64_t position;
109    } db_header;
110
111    struct _db {
112        tree_file file;
113        db_header header;
114        int dropped;
115        void *userdata;
116    };
117
118    /**
119     * Returns a newed FileOpsInterface implementation
120     * instance.
121     *
122     * Useful for assuming that a FileOpsInterface object
123     * is heap-allocated.
124     */
125    LIBCOUCHSTORE_API
126    FileOpsInterface* create_default_file_ops(void);
127
128    /** Opens or creates a tree_file.
129        @param file  Pointer to tree_file struct to initialize.
130        @param filename  Path to the file
131        @param flags  POSIX open-mode flags
132        @param crc_mode CRC the file should use.
133        @param ops  File I/O operations to use
134        @param buffered Should the file operations be
135               wrapped by an IO buffer */
136    couchstore_error_t tree_file_open(tree_file* file,
137                                      const char *filename,
138                                      int openflags,
139                                      crc_mode_e crc_mode,
140                                      FileOpsInterface* ops,
141                                      tree_file_options options);
142    /** Closes a tree_file.
143        @param file  Pointer to open tree_file. Does not free this pointer! */
144    couchstore_error_t tree_file_close(tree_file* file);
145
146    /** Reads a chunk from the file at a given position.
147        @param file The tree_file to read from
148        @param pos The byte position to read from
149        @param ret_ptr On success, will be set to a malloced buffer containing the chunk data,
150                or to NULL if the length is zero. Caller is responsible for freeing this buffer!
151                On failure, value pointed to is unaltered.
152        @return The length of the chunk (zero is a valid length!), or a negative error code */
153    int pread_bin(tree_file *file, cs_off_t pos, char **ret_ptr);
154
155    /** Reads a compressed chunk from the file at a given position.
156        Parameters and return value are the same as for pread_bin. */
157    int pread_compressed(tree_file *file, cs_off_t pos, char **ret_ptr);
158
159    /** Reads a file header from the file at a given position.
160        Parameters and return value are the same as for pread_bin. */
161    int pread_header(tree_file *file,
162                     cs_off_t pos,
163                     char **ret_ptr,
164                     uint32_t max_header_size);
165
166    couchstore_error_t write_header(tree_file *file, sized_buf *buf, cs_off_t *pos);
167    int db_write_buf(tree_file *file, const sized_buf *buf, cs_off_t *pos, size_t *disk_size);
168    couchstore_error_t db_write_buf_compressed(tree_file *file, const sized_buf *buf, cs_off_t *pos, size_t *disk_size);
169    struct _os_error *get_os_error_store(void);
170    couchstore_error_t by_seq_read_docinfo(DocInfo **pInfo,
171                                           const sized_buf *k,
172                                           const sized_buf *v);
173
174    couchstore_error_t precommit(Db *db);
175    couchstore_error_t db_write_header(Db *db);
176
177#if defined __APPLE__
178    /*
179     * Apple's clang disables thread_local keyword support in older versions
180     */
181    extern __thread char internal_error_string[MAX_ERR_STR_LEN];
182#else
183    extern thread_local char internal_error_string[MAX_ERR_STR_LEN];
184#endif
185
186#ifdef __cplusplus
187}
188#endif
189
190#endif
191