xref: /5.5.2/couchstore/src/iobuffer.h (revision ea89a543)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2016 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#ifndef LIBCOUCHSTORE_IOBUFFER_H
19#define LIBCOUCHSTORE_IOBUFFER_H 1
20
21#include <libcouchstore/couch_db.h>
22
23/* Buffered IO parameters */
24struct buffered_file_ops_params {
25    buffered_file_ops_params();
26    buffered_file_ops_params(const buffered_file_ops_params& src);
27    buffered_file_ops_params(const bool _read_only,
28                             const uint32_t _read_buffer_capacity,
29                             const uint32_t _max_read_buffers);
30
31    // Flag indicating whether or not the file is being opened as read only.
32    bool readOnly;
33    // Read buffer capacity.
34    uint32_t read_buffer_capacity;
35    // Max read buffer count.
36    uint32_t max_read_buffers;
37};
38
39/**
40 * Constructs a set of file ops that buffer the I/O provided by an
41 * underlying set of raw ops.
42 *
43 * @param raw_ops the file ops callbacks to use for the underlying I/O
44 * @param handle on output, a constructed (but not opened) couch_file_handle
45 * @param params buffered IO parameters
46 * @return the couch_file_ops to use, or NULL on failure
47 */
48FileOpsInterface* couch_get_buffered_file_ops(couchstore_error_info_t *errinfo,
49                                              FileOpsInterface* raw_ops,
50                                              couch_file_handle* handle,
51                                              buffered_file_ops_params params);
52
53class BufferedFileOps : public FileOpsInterface {
54public:
55    BufferedFileOps() {}
56
57    couch_file_handle constructor(couchstore_error_info_t* errinfo) override ;
58    couchstore_error_t open(couchstore_error_info_t* errinfo,
59                            couch_file_handle* handle, const char* path,
60                            int oflag) override;
61    couchstore_error_t close(couchstore_error_info_t* errinfo,
62                             couch_file_handle handle) override;
63    couchstore_error_t set_periodic_sync(couch_file_handle handle,
64                                         uint64_t period_bytes) override;
65    ssize_t pread(couchstore_error_info_t* errinfo,
66                  couch_file_handle handle, void* buf, size_t nbytes,
67                  cs_off_t offset) override;
68    ssize_t pwrite(couchstore_error_info_t* errinfo,
69                   couch_file_handle handle, const void* buf,
70                   size_t nbytes, cs_off_t offset) override;
71    cs_off_t goto_eof(couchstore_error_info_t* errinfo,
72                      couch_file_handle handle) override;
73    couchstore_error_t sync(couchstore_error_info_t* errinfo,
74                            couch_file_handle handle) override;
75    couchstore_error_t advise(couchstore_error_info_t* errinfo,
76                              couch_file_handle handle, cs_off_t offset,
77                              cs_off_t len,
78                              couchstore_file_advice_t advice) override;
79    void destructor(couch_file_handle handle) override;
80
81    couch_file_handle constructor(couchstore_error_info_t *errinfo,
82                                  FileOpsInterface* raw_ops,
83                                  buffered_file_ops_params params);
84};
85
86#endif // LIBCOUCHSTORE_IOBUFFER_H
87