xref: /4.0.0/couchstore/src/file_merger.h (revision d80f7621)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3/**
4 * @copyright 2013 Couchbase, Inc.
5 *
6 * @author Filipe Manana  <filipe@couchbase.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 * use this file except in compliance with the License. You may obtain a copy of
10 * the License at
11 *
12 *  http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 **/
20
21#ifndef _FILE_MERGER_H
22#define _FILE_MERGER_H
23
24#include "config.h"
25#include <stdio.h>
26#include <libcouchstore/couch_db.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32
33    typedef enum {
34        FILE_MERGER_SUCCESS          = 0,
35        FILE_MERGER_ERROR_OPEN_FILE  = -1,
36        FILE_MERGER_ERROR_FILE_READ  = -2,
37        FILE_MERGER_ERROR_FILE_WRITE = -3,
38        FILE_MERGER_ERROR_BAD_ARG    = -4,
39        FILE_MERGER_ERROR_ALLOC      = -5,
40        FILE_MERGER_SORT_ERROR       = -6
41    } file_merger_error_t;
42
43
44    typedef struct {
45        void *record;
46        unsigned filenum;
47    } file_merger_record_t;
48
49    /*
50     * Returns the length of the record read from the file on success.
51     * Returns 0 when the file's EOF is reached, and a negative value
52     * on failure (a file_merger_error_t value).
53     */
54    typedef int (*file_merger_read_record_t)(FILE *f,
55                                             void **record_buffer,
56                                             void *user_ctx);
57
58    /*
59     * Returns FILE_MERGER_SUCCESS on success, another file_merger_error_t
60     * value otherwise.
61     */
62    typedef file_merger_error_t (*file_merger_write_record_t)(FILE *f,
63                                                              void *record_buffer,
64                                                              void *user_ctx);
65
66    /*
67     * Returns 0 if both records compare equal, a negative value if the first record
68     * is smaller than the second record, a positive value if the first record is
69     * greater than the second record.
70     */
71    typedef int (*file_merger_compare_records_t)(const void *record_buffer1,
72                                                 const void *record_buffer2,
73                                                 void *user_ctx);
74
75    typedef void  (*file_merger_record_free_t)(void *record,
76                                               void *user_ctx);
77
78    /* Callback which gets called for resulting merge records */
79    typedef file_merger_error_t (*file_merger_feed_record_t)(void *record_buffer,
80                                                             void *user_ctx);
81
82    /* Among the provided records, return the winner record index */
83    typedef size_t (*file_merger_deduplicate_records_t)(
84                                    file_merger_record_t **records,
85                                    size_t len,
86                                    void *user_ctx);
87
88    file_merger_error_t merge_files(const char *source_files[],
89                                    unsigned num_files,
90                                    const char *dest_file,
91                                    file_merger_read_record_t read_record,
92                                    file_merger_write_record_t write_record,
93                                    file_merger_feed_record_t feed_record,
94                                    file_merger_compare_records_t compare_records,
95                                    file_merger_deduplicate_records_t dedup_records,
96                                    file_merger_record_free_t free_record,
97                                    int skip_writeback,
98                                    void *user_ctx);
99
100
101#ifdef __cplusplus
102}
103#endif
104
105#endif
106