summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/utilities/agg_merge/test_agg_merge.cc
blob: 06e5b5697a3194f65c8db3489cac35abd8cd696e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//  Copyright (c) 2017-present, Facebook, Inc.  All rights reserved.
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).

#include "test_agg_merge.h"

#include <assert.h>

#include <deque>
#include <vector>

#include "util/coding.h"
#include "utilities/agg_merge/agg_merge.h"

namespace ROCKSDB_NAMESPACE {

std::string EncodeHelper::EncodeFuncAndInt(const Slice& function_name,
                                           int64_t value) {
  std::string encoded_value;
  PutVarsignedint64(&encoded_value, value);
  std::string ret;
  Status s = EncodeAggFuncAndPayload(function_name, encoded_value, ret);
  assert(s.ok());
  return ret;
}

std::string EncodeHelper::EncodeInt(int64_t value) {
  std::string encoded_value;
  PutVarsignedint64(&encoded_value, value);
  return encoded_value;
}

std::string EncodeHelper::EncodeFuncAndList(const Slice& function_name,
                                            const std::vector<Slice>& list) {
  std::string ret;
  Status s = EncodeAggFuncAndPayload(function_name, EncodeList(list), ret);
  assert(s.ok());
  return ret;
}

std::string EncodeHelper::EncodeList(const std::vector<Slice>& list) {
  std::string result;
  for (const Slice& entity : list) {
    PutLengthPrefixedSlice(&result, entity);
  }
  return result;
}

bool SumAggregator::Aggregate(const std::vector<Slice>& item_list,
                              std::string& result) const {
  int64_t sum = 0;
  for (const Slice& item : item_list) {
    int64_t ivalue;
    Slice v = item;
    if (!GetVarsignedint64(&v, &ivalue) || !v.empty()) {
      return false;
    }
    sum += ivalue;
  }
  result = EncodeHelper::EncodeInt(sum);
  return true;
}

bool MultipleAggregator::Aggregate(const std::vector<Slice>& item_list,
                                   std::string& result) const {
  int64_t mresult = 1;
  for (const Slice& item : item_list) {
    int64_t ivalue;
    Slice v = item;
    if (!GetVarsignedint64(&v, &ivalue) || !v.empty()) {
      return false;
    }
    mresult *= ivalue;
  }
  result = EncodeHelper::EncodeInt(mresult);
  return true;
}

bool Last3Aggregator::Aggregate(const std::vector<Slice>& item_list,
                                std::string& result) const {
  std::vector<Slice> last3;
  last3.reserve(3);
  for (auto it = item_list.rbegin(); it != item_list.rend(); ++it) {
    Slice input = *it;
    Slice entity;
    bool ret;
    while ((ret = GetLengthPrefixedSlice(&input, &entity)) == true) {
      last3.push_back(entity);
      if (last3.size() >= 3) {
        break;
      }
    }
    if (last3.size() >= 3) {
      break;
    }
    if (!ret) {
      continue;
    }
  }
  result = EncodeHelper::EncodeList(last3);
  return true;
}
}  // namespace ROCKSDB_NAMESPACE