uint64add.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2011-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 "utilities/merge_operators/uint64add.h"
  6. #include <memory>
  7. #include "logging/logging.h"
  8. #include "rocksdb/env.h"
  9. #include "rocksdb/merge_operator.h"
  10. #include "rocksdb/slice.h"
  11. #include "util/coding.h"
  12. #include "utilities/merge_operators.h"
  13. namespace ROCKSDB_NAMESPACE { // anonymous namespace
  14. bool UInt64AddOperator::Merge(const Slice& /*key*/, const Slice* existing_value,
  15. const Slice& value, std::string* new_value,
  16. Logger* logger) const {
  17. uint64_t orig_value = 0;
  18. if (existing_value) {
  19. orig_value = DecodeInteger(*existing_value, logger);
  20. }
  21. uint64_t operand = DecodeInteger(value, logger);
  22. assert(new_value);
  23. new_value->clear();
  24. PutFixed64(new_value, orig_value + operand);
  25. return true; // Return true always since corruption will be treated as 0
  26. }
  27. uint64_t UInt64AddOperator::DecodeInteger(const Slice& value,
  28. Logger* logger) const {
  29. uint64_t result = 0;
  30. if (value.size() == sizeof(uint64_t)) {
  31. result = DecodeFixed64(value.data());
  32. } else if (logger != nullptr) {
  33. // If value is corrupted, treat it as 0
  34. ROCKS_LOG_ERROR(logger,
  35. "uint64 value corruption, size: %" ROCKSDB_PRIszt
  36. " > %" ROCKSDB_PRIszt,
  37. value.size(), sizeof(uint64_t));
  38. }
  39. return result;
  40. }
  41. std::shared_ptr<MergeOperator> MergeOperators::CreateUInt64AddOperator() {
  42. return std::make_shared<UInt64AddOperator>();
  43. }
  44. } // namespace ROCKSDB_NAMESPACE