test_agg_merge.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "test_agg_merge.h"
  6. #include <cassert>
  7. #include <deque>
  8. #include <vector>
  9. #include "util/coding.h"
  10. #include "utilities/agg_merge/agg_merge_impl.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. std::string EncodeHelper::EncodeFuncAndInt(const Slice& function_name,
  13. int64_t value) {
  14. std::string encoded_value;
  15. PutVarsignedint64(&encoded_value, value);
  16. std::string ret;
  17. Status s = EncodeAggFuncAndPayload(function_name, encoded_value, ret);
  18. assert(s.ok());
  19. return ret;
  20. }
  21. std::string EncodeHelper::EncodeInt(int64_t value) {
  22. std::string encoded_value;
  23. PutVarsignedint64(&encoded_value, value);
  24. return encoded_value;
  25. }
  26. std::string EncodeHelper::EncodeFuncAndList(const Slice& function_name,
  27. const std::vector<Slice>& list) {
  28. std::string ret;
  29. Status s = EncodeAggFuncAndPayload(function_name, EncodeList(list), ret);
  30. assert(s.ok());
  31. return ret;
  32. }
  33. std::string EncodeHelper::EncodeList(const std::vector<Slice>& list) {
  34. std::string result;
  35. for (const Slice& entity : list) {
  36. PutLengthPrefixedSlice(&result, entity);
  37. }
  38. return result;
  39. }
  40. bool SumAggregator::Aggregate(const std::vector<Slice>& item_list,
  41. std::string& result) const {
  42. int64_t sum = 0;
  43. for (const Slice& item : item_list) {
  44. int64_t ivalue;
  45. Slice v = item;
  46. if (!GetVarsignedint64(&v, &ivalue) || !v.empty()) {
  47. return false;
  48. }
  49. sum += ivalue;
  50. }
  51. result = EncodeHelper::EncodeInt(sum);
  52. return true;
  53. }
  54. bool MultipleAggregator::Aggregate(const std::vector<Slice>& item_list,
  55. std::string& result) const {
  56. int64_t mresult = 1;
  57. for (const Slice& item : item_list) {
  58. int64_t ivalue;
  59. Slice v = item;
  60. if (!GetVarsignedint64(&v, &ivalue) || !v.empty()) {
  61. return false;
  62. }
  63. mresult *= ivalue;
  64. }
  65. result = EncodeHelper::EncodeInt(mresult);
  66. return true;
  67. }
  68. bool Last3Aggregator::Aggregate(const std::vector<Slice>& item_list,
  69. std::string& result) const {
  70. std::vector<Slice> last3;
  71. last3.reserve(3);
  72. for (auto it = item_list.rbegin(); it != item_list.rend(); ++it) {
  73. Slice input = *it;
  74. Slice entity;
  75. bool ret;
  76. while ((ret = GetLengthPrefixedSlice(&input, &entity)) == true) {
  77. last3.push_back(entity);
  78. if (last3.size() >= 3) {
  79. break;
  80. }
  81. }
  82. if (last3.size() >= 3) {
  83. break;
  84. }
  85. if (!ret) {
  86. continue;
  87. }
  88. }
  89. result = EncodeHelper::EncodeList(last3);
  90. return true;
  91. }
  92. } // namespace ROCKSDB_NAMESPACE