Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 3721388

Browse files
Merge pull request #957 from EOSIO/kv_name_cleanup
2 parents cf793d2 + 826b36b commit 3721388

File tree

6 files changed

+61
-59
lines changed

6 files changed

+61
-59
lines changed

docs/06_how-to-guides/06_key_value/01_key_value_examples.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### kv_table Example:
1+
### kv::table Example:
22
```cpp
33
#include <eosio/eosio.hpp>
44
using namespace eosio;
@@ -15,7 +15,7 @@ struct address {
1515
auto full_name() const { return first_name + " " + last_name; }
1616
};
1717

18-
struct address_table : kv_table<address> {
18+
struct address_table : kv::table<address> {
1919
index<name> account_name{"accname"_n, &address::account_name};
2020
index<std::string> full_name{"fullname"_n, &address::full_name};
2121

@@ -35,7 +35,7 @@ class addressbook : contract {
3535
### KV_NAMED_INDEX Example:
3636
```cpp
3737
// Assume the address definition above
38-
struct address_table : kv_table<address> {
38+
struct address_table : kv::table<address> {
3939
KV_NAMED_INDEX("accname"_n, account_name)
4040
KV_NAMED_INDEX("fullname"_n, full_name)
4141
@@ -53,22 +53,22 @@ struct myrecord {
5353
std::tuple<uint32_t, bool> secondary_2;
5454
}
5555

56-
struct myramtable : kv_table<myrecord> {
56+
struct myramtable : kv::table<myrecord> {
5757
// Assume some indexes
5858
myramtable(eosio::name contract_name) {
5959
init(contract_name, "testtable"_n, "eosio.kvram"_n, ...)
6060
}
6161
}
6262

63-
struct mydisktable : kv_table<myrecord> {
63+
struct mydisktable : kv::table<myrecord> {
6464
// Assume some indexes
6565
mydisktable(eosio::name contract_name) {
6666
init(contract_name, "testtable"_n, "eosio.kvdisk"_n, ...)
6767
}
6868
}
6969
```
7070
71-
### kv_table::put Example:
71+
### kv::table::put Example:
7272
```cpp
7373
// This assumes the code from the class example.
7474
void myaction() {
@@ -84,7 +84,7 @@ void myaction() {
8484
}
8585
```
8686

87-
### kv_table::erase Example:
87+
### kv::table::erase Example:
8888
```cpp
8989
// This assumes the code from the class example.
9090
void myaction() {

libraries/eosiolib/contracts/eosio/key_value.hpp

+49-47
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,15 @@ static constexpr eosio::name kv_disk = "eosio.kvdisk"_n;
187187
template <typename ...Types>
188188
using non_unique = std::tuple<Types...>;
189189

190+
namespace kv {
190191
template<typename T>
191-
class kv_table;
192+
class table;
192193

193-
namespace kv_detail {
194+
namespace internal {
194195

195-
class kv_table_base;
196+
class table_base;
196197

197-
class kv_index {
198+
class index_base {
198199

199200
public:
200201
eosio::name index_name;
@@ -204,10 +205,10 @@ namespace kv_detail {
204205
key_type to_table_key(const key_type& k) const { return prefix + k; }
205206

206207
protected:
207-
kv_index() = default;
208+
index_base() = default;
208209

209210
template <typename KF, typename T>
210-
kv_index(eosio::name index_name, KF&& kf, T*) : index_name{index_name} {
211+
index_base(eosio::name index_name, KF&& kf, T*) : index_name{index_name} {
211212
key_function = [=](const void* t) {
212213
return make_key(std::invoke(kf, static_cast<const T*>(t)));
213214
};
@@ -219,32 +220,32 @@ namespace kv_detail {
219220

220221
void get(const key_type& key, void* ret_val, void (*deserialize)(void*, const void*, std::size_t)) const;
221222

222-
kv_table_base* tbl;
223+
table_base* tbl;
223224
key_type prefix;
224225

225226
private:
226227
template<typename T>
227-
friend class eosio::kv_table;
228-
friend class kv_table_base;
228+
friend class eosio::kv::table;
229+
friend class table_base;
229230
friend class iterator_base;
230231

231232
std::function<key_type(const void*)> key_function;
232233

233234
virtual void setup() = 0;
234235
};
235236

236-
class kv_table_base {
237+
class table_base {
237238
protected:
238-
friend class kv_index;
239+
friend class index_base;
239240
friend class iterator_base;
240241
eosio::name contract_name;
241242
eosio::name table_name;
242243
uint64_t db_name;
243244

244245
eosio::name primary_index_name;
245246

246-
kv_index* primary_index;
247-
std::vector<kv_index*> secondary_indices;
247+
index_base* primary_index;
248+
std::vector<index_base*> secondary_indices;
248249

249250
void put(const void* value, void* old_value,
250251
std::size_t (*get_size)(const void*),
@@ -328,7 +329,7 @@ namespace kv_detail {
328329
}
329330
};
330331

331-
inline void kv_index::get(const key_type& key, void* ret_val, void (*deserialize)(void*, const void*, std::size_t)) const {
332+
inline void index_base::get(const key_type& key, void* ret_val, void (*deserialize)(void*, const void*, std::size_t)) const {
332333
uint32_t value_size;
333334
uint32_t actual_data_size;
334335

@@ -376,7 +377,7 @@ namespace kv_detail {
376377

377378
iterator_base() = default;
378379

379-
iterator_base(uint32_t itr, status itr_stat, const kv_index* index) : itr{itr}, itr_stat{itr_stat}, index{index} {}
380+
iterator_base(uint32_t itr, status itr_stat, const index_base* index) : itr{itr}, itr_stat{itr_stat}, index{index} {}
380381

381382
iterator_base(iterator_base&& other) :
382383
itr(std::exchange(other.itr, 0)),
@@ -469,7 +470,7 @@ namespace kv_detail {
469470
uint32_t itr;
470471
status itr_stat;
471472

472-
const kv_index* index;
473+
const index_base* index;
473474

474475
int compare(const iterator_base& b) const {
475476
bool a_is_end = !itr || itr_stat == status::iterator_end;
@@ -501,15 +502,15 @@ namespace kv_detail {
501502
* @tparam T - the type of the data stored as the value of the table
502503
*/
503504
template<typename T>
504-
class kv_table : kv_detail::kv_table_base {
505+
class table : internal::table_base {
505506
public:
506507
template<typename K>
507508
class index;
508509

509510
private:
510-
using kv_index = kv_detail::kv_index;
511+
using index_base = internal::index_base;
511512

512-
class base_iterator : public kv_detail::iterator_base {
513+
class base_iterator : public internal::iterator_base {
513514
public:
514515
using iterator_base::iterator_base;
515516
/**
@@ -520,7 +521,7 @@ class kv_table : kv_detail::kv_table_base {
520521
*/
521522
T value() const {
522523
T val;
523-
iterator_base::value(&val, &kv_table::deserialize_fun);
524+
iterator_base::value(&val, &table::deserialize_fun);
524525
return val;
525526
}
526527
};
@@ -538,7 +539,7 @@ class kv_table : kv_detail::kv_table_base {
538539

539540
iterator() = default;
540541

541-
iterator(uint32_t itr, status itr_stat, const kv_index* index) : base_iterator{itr, itr_stat, index} {}
542+
iterator(uint32_t itr, status itr_stat, const index_base* index) : base_iterator{itr, itr_stat, index} {}
542543

543544
iterator(iterator&& other) : base_iterator{std::move(other)} {}
544545

@@ -560,7 +561,7 @@ class kv_table : kv_detail::kv_table_base {
560561

561562
iterator& operator--() {
562563
if (!itr) {
563-
itr = internal_use_do_not_use::kv_it_create(static_cast<kv_table*>(index->tbl)->db_name, index->contract_name.value, index->prefix.data(), index->prefix.size());
564+
itr = internal_use_do_not_use::kv_it_create(static_cast<table*>(index->tbl)->db_name, index->contract_name.value, index->prefix.data(), index->prefix.size());
564565
}
565566
itr_stat = static_cast<status>(internal_use_do_not_use::kv_it_prev(itr));
566567
eosio::check(itr_stat != status::iterator_end, "decremented past the beginning");
@@ -602,7 +603,7 @@ class kv_table : kv_detail::kv_table_base {
602603

603604
reverse_iterator() = default;
604605

605-
reverse_iterator(uint32_t itr, status itr_stat, const kv_index* index) : base_iterator{itr, itr_stat, index} {}
606+
reverse_iterator(uint32_t itr, status itr_stat, const index_base* index) : base_iterator{itr, itr_stat, index} {}
606607

607608
reverse_iterator(reverse_iterator&& other) : base_iterator{std::move(other)} {}
608609

@@ -624,7 +625,7 @@ class kv_table : kv_detail::kv_table_base {
624625

625626
reverse_iterator& operator--() {
626627
if (!itr) {
627-
itr = internal_use_do_not_use::kv_it_create(static_cast<kv_table*>(index->tbl)->db_name, index->contract_name.value, index->prefix.data(), index->prefix.size());
628+
itr = internal_use_do_not_use::kv_it_create(static_cast<table*>(index->tbl)->db_name, index->contract_name.value, index->prefix.data(), index->prefix.size());
628629
itr_stat = static_cast<status>(internal_use_do_not_use::kv_it_lower_bound(itr, "", 0));
629630
}
630631
itr_stat = static_cast<status>(internal_use_do_not_use::kv_it_next(itr));
@@ -672,7 +673,7 @@ class kv_table : kv_detail::kv_table_base {
672673
};
673674

674675
public:
675-
using iterator = kv_table::iterator;
676+
using iterator = table::iterator;
676677
using value_type = T;
677678

678679
/**
@@ -686,18 +687,18 @@ class kv_table : kv_detail::kv_table_base {
686687
*
687688
* @tparam K - The type of the key used in the index.
688689
*/
689-
template <typename K>
690-
class index : public kv_index {
690+
template<typename K>
691+
class index : public index_base {
691692
public:
692-
using iterator = kv_table::iterator;
693-
using kv_table<T>::kv_index::tbl;
694-
using kv_table<T>::kv_index::table_name;
695-
using kv_table<T>::kv_index::contract_name;
696-
using kv_table<T>::kv_index::index_name;
697-
using kv_table<T>::kv_index::prefix;
693+
using iterator = table::iterator;
694+
using table<T>::index_base::tbl;
695+
using table<T>::index_base::table_name;
696+
using table<T>::index_base::contract_name;
697+
using table<T>::index_base::index_name;
698+
using table<T>::index_base::prefix;
698699

699700
template <typename KF>
700-
index(eosio::name name, KF&& kf) : kv_index{name, kf, (T*)nullptr} {
701+
index(eosio::name name, KF&& kf) : index_base{name, kf, (T*)nullptr} {
701702
static_assert(std::is_same_v<K, std::remove_cv_t<std::decay_t<decltype(std::invoke(kf, std::declval<const T*>()))>>>,
702703
"Make sure the variable/function passed to the constructor returns the same type as the template parameter.");
703704
}
@@ -712,7 +713,7 @@ class kv_table : kv_detail::kv_table_base {
712713
iterator find(const K& key) const {
713714
auto t_key = prefix + make_key(key);
714715

715-
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<kv_table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
716+
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
716717
int32_t itr_stat = internal_use_do_not_use::kv_it_lower_bound(itr, t_key.data(), t_key.size());
717718

718719
auto cmp = internal_use_do_not_use::kv_it_key_compare(itr, t_key.data(), t_key.size());
@@ -736,7 +737,7 @@ class kv_table : kv_detail::kv_table_base {
736737
uint32_t value_size;
737738
auto t_key = prefix + make_key(key);
738739

739-
return internal_use_do_not_use::kv_get(static_cast<kv_table*>(tbl)->db_name, contract_name.value, t_key.data(), t_key.size(), value_size);
740+
return internal_use_do_not_use::kv_get(static_cast<table*>(tbl)->db_name, contract_name.value, t_key.data(), t_key.size(), value_size);
740741
}
741742

742743
/**
@@ -762,7 +763,7 @@ class kv_table : kv_detail::kv_table_base {
762763
std::optional<T> get(const K& key) const {
763764
std::optional<T> ret_val;
764765
auto k = prefix + make_key(key);
765-
kv_index::get(k, &ret_val, &deserialize_optional_fun);
766+
index_base::get(k, &ret_val, &deserialize_optional_fun);
766767
return ret_val;
767768
}
768769

@@ -773,7 +774,7 @@ class kv_table : kv_detail::kv_table_base {
773774
* @return An iterator to the object with the lowest key (by this index) in the table.
774775
*/
775776
iterator begin() const {
776-
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<kv_table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
777+
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
777778
int32_t itr_stat = internal_use_do_not_use::kv_it_lower_bound(itr, "", 0);
778779

779780
return {itr, static_cast<typename iterator::status>(itr_stat), this};
@@ -796,7 +797,7 @@ class kv_table : kv_detail::kv_table_base {
796797
* @return A reverse iterator to the object with the highest key (by this index) in the table.
797798
*/
798799
reverse_iterator rbegin() const {
799-
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<kv_table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
800+
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
800801
int32_t itr_stat = internal_use_do_not_use::kv_it_prev(itr);
801802

802803
return {itr, static_cast<typename iterator::status>(itr_stat), this};
@@ -821,7 +822,7 @@ class kv_table : kv_detail::kv_table_base {
821822
iterator lower_bound(const K& key) const {
822823
auto t_key = prefix + make_key(key);
823824

824-
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<kv_table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
825+
uint32_t itr = internal_use_do_not_use::kv_it_create(static_cast<table*>(tbl)->db_name, contract_name.value, prefix.data(), prefix.size());
825826
int32_t itr_stat = internal_use_do_not_use::kv_it_lower_bound(itr, t_key.data(), t_key.size());
826827

827828
return {itr, static_cast<typename iterator::status>(itr_stat), this};
@@ -885,7 +886,7 @@ class kv_table : kv_detail::kv_table_base {
885886
*/
886887
void put(const T& value, eosio::name payer) {
887888
T old_value;
888-
kv_table_base::put(&value, &old_value, &get_size_fun, &deserialize_fun, &serialize_fun, payer);
889+
table_base::put(&value, &old_value, &get_size_fun, &deserialize_fun, &serialize_fun, payer);
889890
}
890891

891892
/* @cond PRIVATE */
@@ -911,15 +912,15 @@ class kv_table : kv_detail::kv_table_base {
911912
* @param key - The key of the value to be removed.
912913
*/
913914
void erase(const T& value) {
914-
kv_table_base::erase(&value);
915+
table_base::erase(&value);
915916
}
916917

917918
protected:
918-
kv_table() = default;
919+
table() = default;
919920

920921
template <typename I>
921922
void setup_indices(I& index) {
922-
kv_index* idx = &index;
923+
index_base* idx = &index;
923924
idx->contract_name = contract_name;
924925
idx->table_name = table_name;
925926
idx->tbl = this;
@@ -955,8 +956,9 @@ class kv_table : kv_detail::kv_table_base {
955956

956957
template <typename Type>
957958
constexpr void validate_types(Type& t) {
958-
constexpr bool is_kv_index = std::is_base_of_v<kv_index, std::decay_t<Type>>;
959-
static_assert(is_kv_index, "Incorrect type passed to init. Must be a reference to an index.");
959+
constexpr bool is_index = std::is_base_of_v<index_base, std::decay_t<Type>>;
960+
static_assert(is_index, "Incorrect type passed to init. Must be a reference to an index.");
960961
}
961962
};
962-
} // eosio
963+
} // namespace kv
964+
} // namespace eosio

tests/unit/test_contracts/kv_make_key_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct my_struct {
3434
}
3535
};
3636

37-
struct my_table : eosio::kv_table<my_struct> {
37+
struct my_table : eosio::kv::table<my_struct> {
3838
KV_NAMED_INDEX("t1"_n, tname)
3939
KV_NAMED_INDEX("t2"_n, tstring)
4040
KV_NAMED_INDEX("t3"_n, tui64)

tests/unit/test_contracts/kv_multiple_indices_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct my_struct {
1919
}
2020
};
2121

22-
struct my_table : eosio::kv_table<my_struct> {
22+
struct my_table : eosio::kv::table<my_struct> {
2323
KV_NAMED_INDEX("primarykey"_n, primary_key)
2424
KV_NAMED_INDEX("foo"_n, foo)
2525
KV_NAMED_INDEX("bar"_n, bar)

tests/unit/test_contracts/kv_single_index_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct my_struct {
88
}
99
};
1010

11-
struct my_table : eosio::kv_table<my_struct> {
11+
struct my_table : eosio::kv::table<my_struct> {
1212
KV_NAMED_INDEX("primary"_n, primary_key);
1313

1414
my_table(eosio::name contract_name) {

tests/unit/test_contracts/kv_variant_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct my_struct_v2 {
1111
uint64_t age;
1212
};
1313

14-
struct my_table : eosio::kv_table<my_struct_v> {
14+
struct my_table : eosio::kv::table<my_struct_v> {
1515
KV_NAMED_INDEX("fullname"_n, full_name);
1616
KV_NAMED_INDEX("age"_n, age);
1717

@@ -20,7 +20,7 @@ struct my_table : eosio::kv_table<my_struct_v> {
2020
}
2121
};
2222

23-
struct my_table_v : eosio::kv_table<std::variant<my_struct_v, my_struct_v2>> {
23+
struct my_table_v : eosio::kv::table<std::variant<my_struct_v, my_struct_v2>> {
2424
index<std::string> primary_key{"fullname"_n, [](const auto& obj) {
2525
return std::visit([&](auto&& a) {
2626
using V = std::decay_t<decltype(a)>;

0 commit comments

Comments
 (0)