xref: /4.0.0/couchstore/tests/views/cleanup.c (revision 81a8545e)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3/**
4 * @copyright 2013 Couchbase, Inc.
5 *
6 * @author Sarath Lakshman  <sarath@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 "view_tests.h"
22#include "../src/couch_btree.h"
23
24static void test_view_id_btree_cleanup()
25{
26    sized_buf valbuf;
27    view_purger_ctx_t purge_ctx;
28    view_id_btree_value_t data1;
29    char *data_bin1 = NULL;
30    size_t data_bin1_size = 0;
31    view_id_btree_reduction_t reduction1;
32    char reduction_bin1[512];
33    size_t reduction_bin1_size = 0;
34    node_pointer np;
35
36    memset(&purge_ctx, 0, sizeof(purge_ctx));
37    set_bit(&purge_ctx.cbitmask, 64);
38
39    /* Purge kv tests */
40    data1.partition = 64;
41    data1.num_view_keys_map = 1;
42    data1.view_keys_map = (view_keys_mapping_t *) malloc(sizeof(view_keys_mapping_t));
43    cb_assert(data1.view_keys_map != NULL);
44    data1.view_keys_map[0].view_id = 0;
45    data1.view_keys_map[0].num_keys = 1;
46    data1.view_keys_map[0].json_keys = (sized_buf *) malloc(sizeof(sized_buf));
47    data1.view_keys_map[0].json_keys[0].buf = "100";
48    data1.view_keys_map[0].json_keys[0].size = sizeof("100") - 1;
49    cb_assert(encode_view_id_btree_value(&data1, &data_bin1, &data_bin1_size) == COUCHSTORE_SUCCESS);
50    valbuf.buf = data_bin1;
51    valbuf.size = data_bin1_size;
52
53    cb_assert(view_id_btree_purge_kv(NULL, &valbuf, &purge_ctx) == PURGE_ITEM);
54    cb_assert(purge_ctx.count == 1);
55    free(data_bin1);
56    data1.partition = 32;
57    cb_assert(encode_view_id_btree_value(&data1, &data_bin1, &data_bin1_size) == COUCHSTORE_SUCCESS);
58    valbuf.buf = data_bin1;
59    valbuf.size = data_bin1_size;
60    purge_ctx.count = 0;
61    cb_assert(view_id_btree_purge_kv(NULL, &valbuf, &purge_ctx) == PURGE_KEEP);
62    cb_assert(purge_ctx.count == 0);
63    free(data_bin1);
64    free(data1.view_keys_map[0].json_keys);
65    free(data1.view_keys_map);
66
67    /* Purge kp tests */
68    reduction1.kv_count = 11;
69    memset(&reduction1.partitions_bitmap, 0, sizeof(reduction1.partitions_bitmap));
70
71    cb_assert(encode_view_id_btree_reduction(&reduction1, reduction_bin1, &reduction_bin1_size) == COUCHSTORE_SUCCESS);
72    np.reduce_value.buf = reduction_bin1;
73    np.reduce_value.size = reduction_bin1_size;
74
75    cb_assert(view_id_btree_purge_kp(&np, &purge_ctx) == PURGE_KEEP);
76    cb_assert(purge_ctx.count == 0);
77
78    set_bit(&reduction1.partitions_bitmap, 64);
79    cb_assert(encode_view_id_btree_reduction(&reduction1, reduction_bin1, &reduction_bin1_size) == COUCHSTORE_SUCCESS);
80    np.reduce_value.buf = reduction_bin1;
81    np.reduce_value.size = reduction_bin1_size;
82
83    cb_assert(view_id_btree_purge_kp(&np, &purge_ctx) == PURGE_ITEM);
84    cb_assert(purge_ctx.count == 11);
85    purge_ctx.count = 0;
86
87    set_bit(&reduction1.partitions_bitmap, 100);
88    cb_assert(encode_view_id_btree_reduction(&reduction1, reduction_bin1, &reduction_bin1_size) == COUCHSTORE_SUCCESS);
89    np.reduce_value.buf = reduction_bin1;
90    np.reduce_value.size = reduction_bin1_size;
91
92    cb_assert(view_id_btree_purge_kp(&np, &purge_ctx) == PURGE_PARTIAL);
93    cb_assert(purge_ctx.count == 0);
94}
95
96static void test_view_btree_cleanup()
97{
98    sized_buf valbuf;
99    view_purger_ctx_t purge_ctx;
100    view_btree_value_t value1;
101    char *value_bin1 = NULL;
102    size_t value_bin1_size = 0;
103    view_btree_reduction_t reduction1;
104    char reduction_bin1[512];
105    size_t reduction_bin1_size = 0;
106    node_pointer np;
107
108    memset(&purge_ctx, 0, sizeof(purge_ctx));
109    set_bit(&purge_ctx.cbitmask, 64);
110
111    /* Purge KV tests */
112    value1.partition = 64;
113    value1.num_values = 2;
114    value1.values = (sized_buf *) malloc(sizeof(sized_buf) * 2);
115    value1.values[0].buf = "100";
116    value1.values[0].size = sizeof("100") - 1;
117    value1.values[1].buf = "1";
118    value1.values[1].size = sizeof("1") - 1;
119    cb_assert(encode_view_btree_value(&value1, &value_bin1, &value_bin1_size) == COUCHSTORE_SUCCESS);
120
121    valbuf.buf = value_bin1;
122    valbuf.size = value_bin1_size;
123
124    cb_assert(view_btree_purge_kv(NULL, &valbuf, &purge_ctx) == PURGE_ITEM);
125    cb_assert(purge_ctx.count == 2);
126    purge_ctx.count = 0;
127    free(value_bin1);
128
129    value1.partition = 100;
130    cb_assert(encode_view_btree_value(&value1, &value_bin1, &value_bin1_size) == COUCHSTORE_SUCCESS);
131    valbuf.buf = value_bin1;
132    valbuf.size = value_bin1_size;
133
134    cb_assert(view_btree_purge_kv(NULL, &valbuf, &purge_ctx) == PURGE_KEEP);
135    cb_assert(purge_ctx.count == 0);
136    free(value_bin1);
137    free(value1.values);
138
139    /* Purge KP tests */
140    reduction1.kv_count = 11;
141    reduction1.num_values = 1;
142    reduction1.reduce_values = (sized_buf *) malloc(sizeof(sized_buf));
143    reduction1.reduce_values[0].buf = "value";
144    reduction1.reduce_values[0].size = sizeof("value") - 1;
145    memset(&reduction1.partitions_bitmap, 0, sizeof(reduction1.partitions_bitmap));
146
147    cb_assert(encode_view_btree_reduction(&reduction1, reduction_bin1, &reduction_bin1_size) == COUCHSTORE_SUCCESS);
148    np.reduce_value.buf = reduction_bin1;
149    np.reduce_value.size = reduction_bin1_size;
150
151    cb_assert(view_btree_purge_kp(&np, &purge_ctx) == PURGE_KEEP);
152    cb_assert(purge_ctx.count == 0);
153
154    set_bit(&reduction1.partitions_bitmap, 64);
155    cb_assert(encode_view_btree_reduction(&reduction1, reduction_bin1, &reduction_bin1_size) == COUCHSTORE_SUCCESS);
156    np.reduce_value.buf = reduction_bin1;
157    np.reduce_value.size = reduction_bin1_size;
158
159    cb_assert(view_btree_purge_kp(&np, &purge_ctx) == PURGE_ITEM);
160    cb_assert(purge_ctx.count == 11);
161    purge_ctx.count = 0;
162
163    set_bit(&reduction1.partitions_bitmap, 100);
164    cb_assert(encode_view_btree_reduction(&reduction1, reduction_bin1, &reduction_bin1_size) == COUCHSTORE_SUCCESS);
165    np.reduce_value.buf = reduction_bin1;
166    np.reduce_value.size = reduction_bin1_size;
167
168    cb_assert(view_btree_purge_kp(&np, &purge_ctx) == PURGE_PARTIAL);
169    cb_assert(purge_ctx.count == 0);
170    free(reduction1.reduce_values);
171}
172
173void cleanup_tests(void)
174{
175    fprintf(stderr, "Running view id_btree cleanup tests ... \n");
176    test_view_id_btree_cleanup();
177    fprintf(stderr, "End of view id_btree cleanup tests ... \n");
178    fprintf(stderr, "Running view btree cleanup tests ... \n");
179    test_view_btree_cleanup();
180    fprintf(stderr, "End of view btree cleanup tests ... \n");
181}
182