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 
29 static 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 
35 LIBCOUCHSTORE_API
sort_view_kvs_ops_file(const char *file_path, const char *tmp_dir)36 file_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 
48 LIBCOUCHSTORE_API
sort_view_kvs_file(const char *file_path, const char *tmp_dir, file_merger_feed_record_t callback, void *user_ctx)49 file_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 
64 LIBCOUCHSTORE_API
sort_view_ids_ops_file(const char *file_path, const char *tmp_dir)65 file_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 
77 LIBCOUCHSTORE_API
sort_view_ids_file(const char *file_path, const char *tmp_dir, file_merger_feed_record_t callback, void *user_ctx)78 file_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 
93 LIBCOUCHSTORE_API
sort_spatial_kvs_file(const char *file_path, const char *tmp_dir, file_merger_feed_record_t callback, void *user_ctx)94 file_sorter_error_t sort_spatial_kvs_file(const char *file_path,
95                                           const char *tmp_dir,
96                                           file_merger_feed_record_t callback,
97                                           void *user_ctx)
98 {
99     file_sorter_error_t ret;
100     view_file_merge_ctx_t ctx;
101 
102     ctx.key_cmp_fun = spatial_key_cmp;
103     ctx.type = INITIAL_BUILD_SPATIAL_RECORD;
104     ctx.user_ctx = user_ctx;
105 
106     ret = do_sort_file(file_path, tmp_dir, callback, 1, &ctx);
107 
108     return ret;
109 }
110 
111 
do_sort_file(const char *file_path, const char *tmp_dir, file_merger_feed_record_t callback, int skip_writeback, view_file_merge_ctx_t *ctx)112 static file_sorter_error_t do_sort_file(const char *file_path,
113                                         const char *tmp_dir,
114                                         file_merger_feed_record_t callback,
115                                         int skip_writeback,
116                                         view_file_merge_ctx_t *ctx)
117 {
118     return sort_file(file_path,
119                      tmp_dir,
120                      SORT_MAX_NUM_TMP_FILES,
121                      SORT_MAX_BUFFER_SIZE,
122                      read_view_record,
123                      write_view_record,
124                      callback,
125                      compare_view_records,
126                      free_view_record,
127                      skip_writeback,
128                      ctx);
129 }
130