wide_columns_helper.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  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. #pragma once
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <ostream>
  9. #include "rocksdb/rocksdb_namespace.h"
  10. #include "rocksdb/wide_columns.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class WideColumnsHelper {
  13. public:
  14. static void DumpWideColumns(const WideColumns& columns, std::ostream& os,
  15. bool hex);
  16. static Status DumpSliceAsWideColumns(const Slice& value, std::ostream& os,
  17. bool hex);
  18. static bool HasDefaultColumn(const WideColumns& columns) {
  19. return !columns.empty() && columns.front().name() == kDefaultWideColumnName;
  20. }
  21. static bool HasDefaultColumnOnly(const WideColumns& columns) {
  22. return columns.size() == 1 &&
  23. columns.front().name() == kDefaultWideColumnName;
  24. }
  25. static const Slice& GetDefaultColumn(const WideColumns& columns) {
  26. assert(HasDefaultColumn(columns));
  27. return columns.front().value();
  28. }
  29. static void SortColumns(WideColumns& columns) {
  30. std::sort(columns.begin(), columns.end(),
  31. [](const WideColumn& lhs, const WideColumn& rhs) {
  32. return lhs.name().compare(rhs.name()) < 0;
  33. });
  34. }
  35. template <typename Iterator>
  36. static Iterator Find(Iterator begin, Iterator end, const Slice& column_name) {
  37. assert(std::is_sorted(begin, end,
  38. [](const WideColumn& lhs, const WideColumn& rhs) {
  39. return lhs.name().compare(rhs.name()) < 0;
  40. }));
  41. auto it = std::lower_bound(begin, end, column_name,
  42. [](const WideColumn& lhs, const Slice& rhs) {
  43. return lhs.name().compare(rhs) < 0;
  44. });
  45. if (it == end || it->name() != column_name) {
  46. return end;
  47. }
  48. return it;
  49. }
  50. };
  51. } // namespace ROCKSDB_NAMESPACE