8000 Avoid copying of metadata in r.db() and r.table() (and a few others) · jchjava/rethinkdb@e2e1e80 · GitHub
[go: up one dir, main page]

Skip to content

Commit e2e1e80

Browse files
author
Daniel Mewes
committed
Avoid copying of metadata in r.db() and r.table() (and a few others)
1 parent 2fcd724 commit e2e1e80

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

src/clustering/administration/metadata.hpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,19 @@ enum metadata_search_status_t {
203203
/* A helper class to search through metadata in various ways. Can be
204204
constructed from a pointer to the internal map of the metadata,
205205
e.g. `metadata.databases.databases`. Look in rdb_protocol/query_language.cc
206-
for examples on how to use. */
207-
template<class T>
208-
class metadata_searcher_t {
206+
for examples on how to use.
207+ `generic_metadata_searcher_t` should not be directly used. Instead there
208+
are two variants defined below:
209+
`const_metadata_searcher_t` for const maps
210+
and `metadata_searcher_t` for non-const maps. */
211+
template<class T, class metamap_t, class iterator_t>
212+
class generic_metadata_searcher_t {
209213
public:
210-
typedef std::map<uuid_u, deletable_t<T> > metamap_t;
211-
typedef typename metamap_t::iterator iterator;
214+
typedef iterator_t iterator;
212215
iterator begin() {return map->begin();}
213216
iterator end() {return map->end();}
214217

215-
explicit metadata_searcher_t(metamap_t *_map): map(_map) { }
218+
explicit generic_metadata_searcher_t(metamap_t *_map): map(_map) { }
216219

217220
template<class callable_t>
218221
/* Find the next iterator >= [start] matching [predicate]. */
@@ -262,6 +265,28 @@ class metadata_searcher_t {
262265
private:
263266
metamap_t *map;
264267
};
268+
template<class T>
269+
class metadata_searcher_t :
270+
public generic_metadata_searcher_t<T,
271+
typename std::map<uuid_u, deletable_t<T> >,
272+
typename std::map<uuid_u, deletable_t<T> >::iterator> {
273+
public:
274+
typedef typename std::map<uuid_u, deletable_t<T> >::iterator iterator;
275+
typedef typename std::map<uuid_u, deletable_t<T> > metamap_t;
276+
explicit metadata_searcher_t(metamap_t *_map) :
277+
generic_metadata_searcher_t<T, metamap_t, iterator>(_map) { }
278+
};
279+
template<class T>
280+
class const_metadata_searcher_t :
281+
public generic_metadata_searcher_t<T,
282+
const typename std::map<uuid_u, deletable_t<T> >,
283+
typename std::map<uuid_u, deletable_t<T> >::const_iterator> {
284+
public:
285+
typedef typename std::map<uuid_u, deletable_t<T> >::const_iterator iterator;
286+
typedef const typename std::map<uuid_u, deletable_t<T> > metamap_t;
287+
explicit const_metadata_searcher_t(metamap_t *_map) :
288+
generic_metadata_searcher_t<T, metamap_t, iterator>(_map) { }
289+
};
265290

266291
class namespace_predicate_t {
267292
public:

src/rdb_protocol/terms/db_table.cc

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class meta_op_term_t : public op_term_t {
4040
virtual bool is_deterministic() const { return false; }
4141
};
4242

43+
// If you don't have to modify any of the data, use
44+
// `const_rethreading_metadata_accessor_t` instead which is more efficient.
4345
struct rethreading_metadata_accessor_t : public on_thread_t {
4446
explicit rethreading_metadata_accessor_t(scope_env_t *env)
4547
: on_thread_t(env->env->cluster_access.semilattice_metadata->home_thread()),
@@ -51,13 +53,28 @@ struct rethreading_metadata_accessor_t : public on_thread_t {
5153
{ }
5254
cluster_semilattice_metadata_t metadata;
5355
cow_ptr_t<namespaces_semilattice_metadata_t<rdb_protocol_t> >::change_t
54-
ns_change;
56+
ns_change;
5557
metadata_searcher_t<namespace_semilattice_metadata_t<rdb_protocol_t> >
56-
ns_searcher;
58+
ns_searcher;
5759
metadata_searcher_t<database_semilattice_metadata_t> db_searcher;
5860
metadata_searcher_t<datacenter_semilattice_metadata_t> dc_searcher;
5961
};
6062

63+
struct const_rethreading_metadata_accessor_t : public on_thread_t {
64+
explicit const_rethreading_metadata_accessor_t(scope_env_t *env)
65+
: on_thread_t(env->env->cluster_access.semilattice_metadata->home_thread()),
66+
metadata(env->env->cluster_access.semilattice_metadata->get()),
67+
ns_searcher(&metadata.rdb_namespaces.get()->namespaces),
68+
db_searcher(&metadata.databases.databases),
69+
dc_searcher(&metadata.datacenters.datacenters)
70+
{ }
71+
cluster_semilattice_metadata_t metadata;
72+
const_metadata_searcher_t<namespace_semilattice_metadata_t<rdb_protocol_t> >
73+
ns_searcher;
74+
const_metadata_searcher_t<database_semilattice_metadata_t> db_searcher;
75+
const_metadata_searcher_t<datacenter_semilattice_metadata_t> dc_searcher;
76+
};
77+
6178

6279
class meta_write_op_t : public meta_op_term_t {
6380
public:
@@ -94,7 +111,7 @@ class db_term_t : public meta_op_term_t {
94111
name_string_t db_name = get_name(arg(env, 0), this, "Database");
95112
uuid_u uuid;
96113
{
97-
rethreading_metadata_accessor_t meta(env);
114+
const_rethreading_metadata_accessor_t meta(env);
98115
uuid = meta_get_uuid(&meta.db_searcher, db_name,
99116
strprintf("Database `%s` does not exist.",
100117
db_name.c_str()), this);
@@ -165,7 +182,7 @@ class table_create_term_t : public meta_write_op_t {
165182
if (counted_t<val_t> v = optarg(env, "datacenter")) {
166183
name_string_t name = get_name(v, this, "Table");
167184
{
168-
rethreading_metadata_accessor_t meta(env);
185+
const_rethreading_metadata_accessor_t meta(env);
169186
dc_id = meta_get_uuid(&meta.dc_searcher, name,
170187
strprintf("Datacenter `%s` does not exist.",
171188
name.str().c_str()),
@@ -355,7 +372,7 @@ class db_list_term_t : public meta_op_term_t {
355372
virtual counted_t<val_t> eval_impl(scope_env_t *env, UNUSED eval_flags_t flags) {
356373
std::vector<std::string> dbs;
357374
{
358-
rethreading_metadata_accessor_t meta(env);
375+
const_rethreading_metadata_accessor_t meta(env);
359376
for (auto it = meta.db_searcher.find_next(meta.db_searcher.begin());
360377
it != meta.db_searcher.end();
361378
it = meta.db_searcher.find_next(++it)) {
@@ -393,7 +410,7 @@ class table_list_term_t : public meta_op_term_t {
393410
std::vector<std::string> tables;
394411
namespace_predicate_t pred(&db_id);
395412
{
396-
rethreading_metadata_accessor_t meta(env);
413+
const_rethreading_metadata_accessor_t meta(env);
397414
for (auto it = meta.ns_searcher.find_next(meta.ns_searcher.begin(), pred);
398415
it != meta.ns_searcher.end();
399416
it = meta.ns_searcher.find_next(++it, pred)) {

src/rdb_protocol/val.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ table_t::table_t(env_t *env,
2828
name.c_str(), name_string_t::valid_char_msg));
2929
cow_ptr_t<namespaces_semilattice_metadata_t<rdb_protocol_t> > namespaces_metadata
3030
= env->cluster_access.namespaces_semilattice_metadata->get();
31-
cow_ptr_t<namespaces_semilattice_metadata_t<rdb_protocol_t> >::change_t
32-
namespaces_metadata_change(&namespaces_metadata);
33-
metadata_searcher_t<namespace_semilattice_metadata_t<rdb_protocol_t> >
34-
ns_searcher(&namespaces_metadata_change.get()->namespaces);
31+
const_metadata_searcher_t<namespace_semilattice_metadata_t<rdb_protocol_t> >
32+
ns_searcher(&namespaces_metadata.get()->namespaces);
3533
// TODO: fold into iteration below
3634
namespace_predicate_t pred(&table_name, &db_id);
3735
uuid_u id = meta_get_uuid(&ns_searcher, pred,
@@ -41,7 +39,7 @@ table_t::table_t(env_t *env,
4139
access.init(new rdb_namespace_access_t(id, env));
4240

4341
metadata_search_status_t status;
44-
metadata_searcher_t<namespace_semilattice_metadata_t<rdb_protocol_t> >::iterator
42+
const_metadata_searcher_t<namespace_semilattice_metadata_t<rdb_protocol_t> >::iterator
4543
ns_metadata_it = ns_searcher.find_uniq(pred, &status);
4644
rcheck(status == METADATA_SUCCESS,
4745
base_exc_t::GENERIC,

0 commit comments

Comments
 (0)
0