1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #include "config.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include "reduces.h"
6 #include "node_types.h"
7 
8 
by_seq_reduce(char *dst, size_t *size_r, const nodelist *leaflist, int count, void *ctx)9 couchstore_error_t by_seq_reduce(char *dst, size_t *size_r, const nodelist *leaflist, int count, void *ctx)
10 {
11     raw_by_seq_reduce *raw = (raw_by_seq_reduce*)dst;
12 
13     (void) leaflist;
14     (void) ctx;
15     encode_raw40(count, &raw->count);
16     *size_r = sizeof(*raw);
17 
18     return COUCHSTORE_SUCCESS;
19 }
20 
by_seq_rereduce(char *dst, size_t *size_r, const nodelist *ptrlist, int count, void *ctx)21 couchstore_error_t by_seq_rereduce(char *dst, size_t *size_r, const nodelist *ptrlist, int count, void *ctx)
22 {
23     uint64_t total = 0;
24     const nodelist *i = ptrlist;
25 
26     (void) ctx;
27 
28     while (i != NULL && count > 0) {
29         const raw_by_seq_reduce *reduce = (const raw_by_seq_reduce*) i->pointer->reduce_value.buf;
30         total += decode_raw40(reduce->count);
31 
32         i = i->next;
33         count--;
34     }
35     raw_by_seq_reduce *raw = (raw_by_seq_reduce*)dst;
36     encode_raw40(total, &raw->count);
37     *size_r = sizeof(*raw);
38 
39     return COUCHSTORE_SUCCESS;
40 }
41 
42 
encode_by_id_reduce(char *dst, uint64_t notdeleted, uint64_t deleted, uint64_t size)43 static size_t encode_by_id_reduce(char *dst, uint64_t notdeleted, uint64_t deleted, uint64_t size)
44 {
45     raw_by_id_reduce *raw = (raw_by_id_reduce*)dst;
46     encode_raw40(notdeleted, &raw->notdeleted);
47     encode_raw40(deleted, &raw->deleted);
48     encode_raw48(size, &raw->size);
49     return sizeof(*raw);
50 }
51 
by_id_reduce(char *dst, size_t *size_r, const nodelist *leaflist, int count, void *ctx)52 couchstore_error_t by_id_reduce(char *dst, size_t *size_r, const nodelist *leaflist, int count, void *ctx)
53 {
54     uint64_t notdeleted = 0, deleted = 0, size = 0;
55     const nodelist *i = leaflist;
56 
57     (void) ctx;
58 
59     while (i != NULL && count > 0) {
60         const raw_id_index_value *raw = (const raw_id_index_value*)i->data.buf;
61         if (decode_raw48(raw->bp) & BP_DELETED_FLAG) {
62             deleted++;
63         } else {
64             notdeleted++;
65         }
66         size += decode_raw32(raw->size);
67 
68         i = i->next;
69         count--;
70     }
71 
72     *size_r = encode_by_id_reduce(dst, notdeleted, deleted, size);
73 
74     return COUCHSTORE_SUCCESS;
75 
76 }
77 
by_id_rereduce(char *dst, size_t *size_r, const nodelist *ptrlist, int count, void *ctx)78 couchstore_error_t by_id_rereduce(char *dst, size_t *size_r, const nodelist *ptrlist, int count, void *ctx)
79 {
80     uint64_t notdeleted = 0, deleted = 0, size = 0;
81     const nodelist *i = ptrlist;
82 
83     (void) ctx;
84 
85     while (i != NULL && count > 0) {
86         const raw_by_id_reduce *reduce = (const raw_by_id_reduce*) i->pointer->reduce_value.buf;
87         notdeleted += decode_raw40(reduce->notdeleted);
88         deleted += decode_raw40(reduce->deleted);
89         size += decode_raw48(reduce->size);
90 
91         i = i->next;
92         count--;
93     }
94 
95     *size_r = encode_by_id_reduce(dst, notdeleted, deleted, size);
96 
97     return COUCHSTORE_SUCCESS;
98 }
99