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#include "file_sorter.h"
22#include "util.h"
23#include "spatial.h"
24
25#define SORT_MAX_BUFFER_SIZE       (64 * 1024 * 1024)
26#define SORT_MAX_NUM_TMP_FILES     16
27
28
29static file_sorter_error_t do_sort_file(const char *file_path,
30                                        const char *tmp_dir,
31                                        file_merger_feed_record_t callback,
32                                        int skip_writeback,
33                                        view_file_merge_ctx_t *ctx);
34
35LIBCOUCHSTORE_API
36file_sorter_error_t sort_view_kvs_ops_file(const char *file_path,
37                                           const char *tmp_dir)
38{
39    view_file_merge_ctx_t ctx;
40
41    ctx.key_cmp_fun = view_key_cmp;
42    ctx.type = INCREMENTAL_UPDATE_VIEW_RECORD;
43
44    return do_sort_file(file_path, tmp_dir, NULL, 0, &ctx);
45}
46
47
48LIBCOUCHSTORE_API
49file_sorter_error_t sort_view_kvs_file(const char *file_path,
50                                       const char *tmp_dir,
51                                       file_merger_feed_record_t callback,
52                                       void *user_ctx)
53{
54    view_file_merge_ctx_t ctx;
55
56    ctx.key_cmp_fun = view_key_cmp;
57    ctx.type = INITIAL_BUILD_VIEW_RECORD;
58    ctx.user_ctx = user_ctx;
59
60    return do_sort_file(file_path, tmp_dir, callback, 1, &ctx);
61}
62
63
64LIBCOUCHSTORE_API
65file_sorter_error_t sort_view_ids_ops_file(const char *file_path,
66                                           const char *tmp_dir)
67{
68    view_file_merge_ctx_t ctx;
69
70    ctx.key_cmp_fun = view_id_cmp;
71    ctx.type = INCREMENTAL_UPDATE_VIEW_RECORD;
72
73    return do_sort_file(file_path, tmp_dir, NULL, 0, &ctx);
74}
75
76
77LIBCOUCHSTORE_API
78file_sorter_error_t sort_view_ids_file(const char *file_path,
79                                       const char *tmp_dir,
80                                       file_merger_feed_record_t callback,
81                                       void *user_ctx)
82{
83    view_file_merge_ctx_t ctx;
84
85    ctx.key_cmp_fun = view_id_cmp;
86    ctx.type = INITIAL_BUILD_VIEW_RECORD;
87    ctx.user_ctx = user_ctx;
88
89    return do_sort_file(file_path, tmp_dir, callback, 1, &ctx);
90}
91
92
93LIBCOUCHSTORE_API
94file_sorter_error_t sort_spatial_kvs_file(const char *file_path,
95                                          const char *tmp_dir,
96                                          const double *mbb,
97                                          const uint16_t dimension)
98{
99    file_sorter_error_t ret;
100    view_file_merge_ctx_t ctx;
101    scale_factor_t *user_ctx = spatial_scale_factor(mbb, dimension,
102                                                    ZCODE_MAX_VALUE);
103
104    ctx.key_cmp_fun = spatial_key_cmp;
105    ctx.type = INITIAL_BUILD_SPATIAL_RECORD;
106    ctx.user_ctx = (void *)user_ctx;
107
108    ret = do_sort_file(file_path, tmp_dir, NULL, 0, &ctx);
109
110    free_spatial_scale_factor(user_ctx);
111    return ret;
112}
113
114
115static file_sorter_error_t do_sort_file(const char *file_path,
116                                        const char *tmp_dir,
117                                        file_merger_feed_record_t callback,
118                                        int skip_writeback,
119                                        view_file_merge_ctx_t *ctx)
120{
121    return sort_file(file_path,
122                     tmp_dir,
123                     SORT_MAX_NUM_TMP_FILES,
124                     SORT_MAX_BUFFER_SIZE,
125                     read_view_record,
126                     write_view_record,
127                     callback,
128                     compare_view_records,
129                     free_view_record,
130                     skip_writeback,
131                     ctx);
132}
133