rippled
MultivarJson.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2023 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_JSON_MULTIVARJSON_H_INCLUDED
21 #define RIPPLE_JSON_MULTIVARJSON_H_INCLUDED
22 
23 #include <ripple/json/json_value.h>
24 
25 #include <array>
26 #include <cassert>
27 #include <concepts>
28 #include <cstdlib>
29 
30 namespace ripple {
31 template <std::size_t Size>
33 {
35  constexpr static std::size_t size = Size;
36 
37  Json::Value const&
38  select(auto&& selector) const
39  requires std::same_as<std::size_t, decltype(selector())>
40  {
41  auto const index = selector();
42  assert(index < size);
43  return val[index];
44  }
45 
46  void
47  set(const char* key,
48  auto const&
49  v) requires std::constructible_from<Json::Value, decltype(v)>
50  {
51  for (auto& a : this->val)
52  a[key] = v;
53  }
54 
55  // Intentionally not using class enum here, MultivarJson is scope enough
56  enum IsMemberResult : int { none = 0, some, all };
57 
58  [[nodiscard]] IsMemberResult
59  isMember(const char* key) const
60  {
61  int count = 0;
62  for (auto& a : this->val)
63  if (a.isMember(key))
64  count += 1;
65 
66  return (count == 0 ? none : (count < size ? some : all));
67  }
68 };
69 
70 // Wrapper for Json for all supported API versions.
72 
73 /*
74 
75 NOTE:
76 
77 If a future API version change adds another possible format, change the size of
78 `MultiApiJson`, and update `apiVersionSelector()` to return the appropriate
79 selection value for the new `apiVersion` and higher.
80 
81 e.g. There are 2 formats now, the first, for version one, the second for
82 versions > 1. Hypothetically, if API version 4 adds a new format, `MultiApiJson`
83 would be MultivarJson<3>, and `apiVersionSelector` would return
84 `static_cast<std::size_t>(apiVersion < 2 ? 0u : (apiVersion < 4 ? 1u : 2u))`
85 
86 NOTE:
87 
88 The more different JSON formats we support, the more CPU cycles we need to
89 pre-build JSON for different API versions e.g. when publishing streams to
90 `subscribe` clients. Hence it is desirable to keep MultiApiJson small and
91 instead fully deprecate and remove support for old API versions. For example, if
92 we removed support for API version 1 and added a different format for API
93 version 3, the `apiVersionSelector` would change to
94 `static_cast<std::size_t>(apiVersion > 2)`
95 
96 */
97 
98 // Helper to create appropriate selector for indexing MultiApiJson by apiVersion
99 constexpr auto
100 apiVersionSelector(unsigned int apiVersion) noexcept
101 {
102  return [apiVersion]() constexpr
103  {
104  // apiVersion <= 1 returns 0
105  // apiVersion > 1 returns 1
106  return static_cast<std::size_t>(apiVersion > 1);
107  };
108 }
109 
110 } // namespace ripple
111 
112 #endif
ripple::MultivarJson::isMember
Json::Value const &select(auto &&selector) const requires std IsMemberResult isMember(const char *key) const
Definition: MultivarJson.h:59
ripple::MultivarJson::size
constexpr static std::size_t size
Definition: MultivarJson.h:35
ripple::MultivarJson::val
std::array< Json::Value, Size > val
Definition: MultivarJson.h:34
ripple::TxSearched::all
@ all
ripple::requires
requires(T::ConsequencesFactory==Transactor::Normal) TxConsequences consequences_helper(PreflightContext const &ctx)
Definition: applySteps.cpp:181
concepts
ripple::JsonOptions::none
@ none
ripple::set
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Definition: BasicConfig.h:313
array
ripple::apiVersionSelector
constexpr auto apiVersionSelector(unsigned int apiVersion) noexcept
Definition: MultivarJson.h:100
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
cstdlib
cassert
std::size_t
ripple::MultivarJson
Definition: MultivarJson.h:32
ripple::TxSearched::some
@ some
Json::Value
Represents a JSON value.
Definition: json_value.h:145