xref: /6.6.0/couchstore/src/views/purgers.cc (revision f798e0b7)
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#include "couchstore_config.h"
21#include "purgers.h"
22#include "bitmap.h"
23#include "values.h"
24#include "reductions.h"
25#include "../couch_btree.h"
26#include <inttypes.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <inttypes.h>
31#include <limits.h>
32
33static int view_purgekv_action(bitmap_t *bm, uint16_t part_id,
34                                             uint64_t valcount,
35                                             view_purger_ctx_t *ctx)
36{
37    int action = PURGE_KEEP;
38    if (is_bit_set(bm, part_id)) {
39        ctx->count += valcount;
40        action = PURGE_ITEM;
41    }
42
43    return action;
44}
45
46static int view_purgekp_action(bitmap_t *clearbm, bitmap_t *redbm,
47                                                  uint64_t kvcount,
48                                                  view_purger_ctx_t *ctx)
49{
50    int action = PURGE_PARTIAL;
51    bitmap_t emptybm, dstbm = *clearbm;
52    memset(&emptybm, 0, sizeof(emptybm));
53
54    if (is_equal_bitmap(redbm, clearbm)) {
55        action = PURGE_ITEM;
56        ctx->count += kvcount;
57    } else {
58        intersect_bitmaps(&dstbm, redbm);
59        if (is_equal_bitmap(&dstbm, &emptybm)) {
60            action = PURGE_KEEP;
61        }
62    }
63
64    return action;
65}
66
67int view_id_btree_purge_kv(const sized_buf *key, const sized_buf *val,
68                                                 void *ctx)
69{
70    view_purger_ctx_t *purge_ctx = (view_purger_ctx_t *) ctx;
71    (void) key;
72
73    return view_purgekv_action(&purge_ctx->cbitmask,
74                               decode_view_btree_partition(val->buf, val->size),
75                               1,
76                               purge_ctx);
77}
78
79int view_id_btree_purge_kp(const node_pointer *ptr, void *ctx)
80{
81    int action;
82    couchstore_error_t errcode = COUCHSTORE_SUCCESS;
83    view_purger_ctx_t *purge_ctx = (view_purger_ctx_t *) ctx;
84    view_id_btree_reduction_t *r = NULL;
85
86    errcode = decode_view_id_btree_reduction(ptr->reduce_value.buf, &r);
87    if (errcode != COUCHSTORE_SUCCESS) {
88        action = (int) errcode;
89        goto cleanup;
90    }
91
92    action = view_purgekp_action(&purge_ctx->cbitmask, &r->partitions_bitmap,
93                                                       r->kv_count,
94                                                       purge_ctx);
95
96cleanup:
97    free_view_id_btree_reduction(r);
98    return action;
99}
100
101int view_btree_purge_kv(const sized_buf *key, const sized_buf *val, void *ctx)
102{
103    view_purger_ctx_t *purge_ctx = (view_purger_ctx_t *) ctx;
104    (void) key;
105
106    auto info = decode_view_btree_partition_and_num_values(val->buf, val->size);
107
108    return view_purgekv_action(
109            &purge_ctx->cbitmask, info.partition, info.num_values, purge_ctx);
110}
111
112int view_btree_purge_kp(const node_pointer *ptr, void *ctx)
113{
114    view_purger_ctx_t *purge_ctx = (view_purger_ctx_t *) ctx;
115    bitmap_t partitions_bitmap;
116    auto kv_count = decode_view_btree_reduction_partitions_bitmap(
117            ptr->reduce_value.buf, ptr->reduce_value.size, partitions_bitmap);
118
119    return view_purgekp_action(
120            &purge_ctx->cbitmask, &partitions_bitmap, kv_count, purge_ctx);
121}
122