10000 Rework planned collection loading · lethalbrains/arangodb@04ba515 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04ba515

Browse files
author
Andreas Streichardt
committed
Rework planned collection loading
1 parent a80d208 commit 04ba515

File tree

8 files changed

+252
-359
lines changed

8 files changed

+252
-359
lines changed

arangod/Cluster/ClusterInfo.cpp

Lines changed: 142 additions & 249 deletions
Large diffs are not rendered by default.

arangod/Cluster/ClusterInfo.h

Lines changed: 92 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "Basics/Common.h"
2929
#include "Basics/JsonHelper.h"
30+
#include "Basics/VelocyPackHelper.h"
3031
#include "Basics/Mutex.h"
3132
#include "Basics/ReadWriteLock.h"
3233
#include "Cluster/AgencyComm.h"
@@ -35,6 +36,8 @@
3536
#include "VocBase/vocbase.h"
3637

3738
#include <velocypack/Slice.h>
39+
#include <velocypack/Iterator.h>
40+
#include <velocypack/velocypack-aliases.h>
3841

3942
struct TRI_json_t;
4043

@@ -55,7 +58,7 @@ class CollectionInfo {
5558
public:
5659
CollectionInfo();
5760

58-
explicit CollectionInfo(struct TRI_json_t*);
61+
CollectionInfo(std::shared_ptr<VPackBuilder>, VPackSlice);
5962

6063
CollectionInfo(CollectionInfo const&);
6164

@@ -72,70 +75,65 @@ class CollectionInfo {
7275
//////////////////////////////////////////////////////////////////////////////
7376

7477
int replicationFactor () const {
75-
TRI_json_t* const node
76-
= arangodb::basics::JsonHelper::getObjectElement(_json,
77-
"replicationFactor");
78-
79-
if (TRI_IsNumberJson(node)) {
80-
return (int) (node->_value._number);
81-
}
82-
return 1;
78+
return arangodb::basics::VelocyPackHelper::getNumericValue<TRI_voc_size_t>(
79+
_slice, "replicationFactor", 1);
8380
}
8481

8582
//////////////////////////////////////////////////////////////////////////////
8683
/// @brief returns the replication quorum
8784
//////////////////////////////////////////////////////////////////////////////
8885

8986
int replicationQuorum () const {
90-
TRI_json_t* const node
91-
= arangodb::basics::JsonHelper::getObjectElement(_json,
92-
"replicationQuorum");
93-
94-
if (TRI_IsNumberJson(node)) {
95-
return (int) (node->_value._number);
96-
}
97-
return 1;
87+
return arangodb::basics::VelocyPackHelper::getNumericValue<TRI_voc_size_t>(
88+
_slice, "replicationQuorum", 1);
9889
}
9990

10091
//////////////////////////////////////////////////////////////////////////////
10192
/// @brief checks whether there is no info contained
10293
//////////////////////////////////////////////////////////////////////////////
10394

10495
bool empty() const {
105-
return (nullptr == _json); //|| (id() == 0);
96+
return _slice.isNone();
10697
}
10798

10899
//////////////////////////////////////////////////////////////////////////////
109100
/// @brief returns the collection id
110101
//////////////////////////////////////////////////////////////////////////////
111102

112103
TRI_voc_cid_t id() const {
113-
return arangodb::basics::JsonHelper::stringUInt64(_json, "id");
104+
if (!_slice.isObject()) {
105+
return 0;
106+
}
107+
VPackSlice idSlice = _slice.get("id");
108+
if (idSlice.isString()) {
109+
return arangodb::basics::VelocyPackHelper::stringUInt64(idSlice);
110+
}
111+
return 0;
114112
}
115113

116114
//////////////////////////////////////////////////////////////////////////////
117115
/// @brief returns the collection id as a string
118116
//////////////////////////////////////////////////////////////////////////////
119117

120118
std::string id_as_string() const {
121-
return arangodb::basics::JsonHelper::getStringValue(_json, "id", "");
119+
return arangodb::basics::VelocyPackHelper::getStringValue(_slice, "id", "");
122120
}
123121

124122
//////////////////////////////////////////////////////////////////////////////
125123
/// @brief returns the collection name
126124
//////////////////////////////////////////////////////////////////////////////
127125

128126
std::string name() const {
129-
return arangodb::basics::JsonHelper::getStringValue(_json, "name", "");
127+
return arangodb::basics::VelocyPackHelper::getStringValue(_slice, "name", "");
130128
}
131129

132130
//////////////////////////////////////////////////////////////////////////////
133131
/// @brief returns the collection type
134132
//////////////////////////////////////////////////////////////////////////////
135133

136134
TRI_col_type_e type() const {
137-
return (TRI_col_type_e)arangodb::basics::JsonHelper::getNumericValue<int>(
138-
_json, "type", (int)TRI_COL_TYPE_UNKNOWN);
135+
return (TRI_col_type_e)arangodb::basics::VelocyPackHelper::getNumericValue<int>(
136+
_slice, "type", (int)TRI_COL_TYPE_UNKNOWN);
139137
}
140138

141139
//////////////////////////////////////////////////////////////////////////////
@@ -144,8 +142,8 @@ class CollectionInfo {
144142

145143
TRI_vocbase_col_status_e status() const {
146144
return (TRI_vocbase_col_status_e)
147-
arangodb::basics::JsonHelper::getNumericValue<int>(
148-
_json, "status", (int)TRI_VOC_COL_STATUS_CORRUPTED);
145+
arangodb::basics::VelocyPackHelper::getNumericValue<int>(
146+
_slice, "status", (int)TRI_VOC_COL_STATUS_CORRUPTED);
149147
}
150148

151149
//////////////////////////////////////////////////////////////////////////////
@@ -161,7 +159,7 @@ class CollectionInfo {
161159
//////////////////////////////////////////////////////////////////////////////
162160

163161
bool deleted() const {
164-
return arangodb::basics::JsonHelper::getBooleanValue(_json, "deleted",
162+
return arangodb::basics::VelocyPackHelper::getBooleanValue(_slice, "deleted",
165163
false);
166164
}
167165

@@ -170,7 +168,7 @@ class CollectionInfo {
170168
//////////////////////////////////////////////////////////////////////////////
171169

172170
bool doCompact() const {
173-
return arangodb::basics::JsonHelper::getBooleanValue(_json, "doCompact",
171+
return arangodb::basics::VelocyPackHelper::getBooleanValue(_slice, "doCompact",
174172
false);
175173
}
176174

@@ -179,7 +177,7 @@ class CollectionInfo {
179177
//////////////////////////////////////////////////////////////////////////////
180178

181179
bool isSystem() const {
182-
return arangodb::basics::JsonHelper::getBooleanValue(_json, "isSystem",
180+
return arangodb::basics::VelocyPackHelper::getBooleanValue(_slice, "isSystem",
183181
false);
184182
}
185183

@@ -188,45 +186,41 @@ class CollectionInfo {
188186
//////////////////////////////////////////////////////////////////////////////
189187

190188
bool isVolatile() const {
191-
return arangodb::basics::JsonHelper::getBooleanValue(_json, "isVolatile",
189+
return arangodb::basics::VelocyPackHelper::getBooleanValue(_slice, "isVolatile",
192190
false);
193191
}
194192

195193
//////////////////////////////////////////////////////////////////////////////
196194
/// @brief returns the indexes
197195
//////////////////////////////////////////////////////////////////////////////
198196

199-
TRI_json_t const* getIndexes() const {
200-
return arangodb::basics::JsonHelper::getObjectElement(_json, "indexes");
197+
VPackSlice const getIndexes() const {
198+
if (_slice.isNone()) {
199+
return VPackSlice();
200+
}
201+
return _slice.get("indexes");
201202
}
202203

203204
//////////////////////////////////////////////////////////////////////////////
204205
/// @brief returns a copy of the key options
205-
/// the caller is responsible for freeing it
206206
//////////////////////////////////////////////////////////////////////////////
207207

208-
TRI_json_t* keyOptions() const {
209-
TRI_json_t const* keyOptions =
210-
arangodb::basics::JsonHelper::getObjectElement(_json, "keyOptions");
211-
212-
if (keyOptions != nullptr) {
213-
return TRI_CopyJson(TRI_UNKNOWN_MEM_ZONE, keyOptions);
208+
VPackSlice const keyOptions() const {
209+
if (_slice.isNone()) {
210+
return VPackSlice();
214211
}
215-
216-
return nullptr;
212+
return _slice.get("keyOptions");
217213
}
218214

219215
//////////////////////////////////////////////////////////////////////////////
220216
/// @brief whether or not a collection allows user-defined keys
221217
//////////////////////////////////////////////////////////////////////////////
222218

223219
bool allowUserKeys() const {
224-
TRI_json_t const* keyOptions =
225-
arangodb::basics::JsonHelper::getObjectElement(_json, "keyOptions");
226-
227-
if (keyOptions != nullptr) {
228-
return arangodb::basics::JsonHelper::getBooleanValue(
229-
keyOptions, "allowUserKeys", true);
220+
VPackSlice keyOptionsSlice = keyOptions();
221+
if (!keyOptionsSlice.isNone()) {
222+
return arangodb::basics::VelocyPackHelper::getBooleanValue(
223+
keyOptionsSlice, "allowUserKeys", true);
230224
}
231225

232226
return true; // the default value
@@ -237,7 +231,7 @@ class CollectionInfo {
237231
//////////////////////////////////////////////////////////////////////////////
238232

239233
bool waitForSync() const {
240-
return arangodb::basics::JsonHelper::getBooleanValue(_json, "waitForSync",
234+
return arangodb::basics::VelocyPackHelper::getBooleanValue(_slice, "waitForSync",
241235
false);
242236
}
243237

@@ -246,43 +240,56 @@ class CollectionInfo {
246240
//////////////////////////////////////////////////////////////////////////////
247241

248242
TRI_voc_size_t journalSize() const {
249-
return arangodb::basics::JsonHelper::getNumericValue<TRI_voc_size_t>(
250-
_json, "journalSize", 0);
243+
return arangodb::basics::VelocyPackHelper::getNumericValue<TRI_voc_size_t>(
244+
_slice, "journalSize", 0);
251245
}
252246

253247
//////////////////////////////////////////////////////////////////////////////
254248
/// @brief returns the number of buckets for indexes
255249
//////////////////////////////////////////////////////////////////////////////
256250

257251
uint32_t indexBuckets() const {
258-
return arangodb::basics::JsonHelper::getNumericValue<uint32_t>(
259-
_json, "indexBuckets", 1);
252+
return arangodb::basics::VelocyPackHelper::getNumericValue<uint32_t>(
253+
_slice, "indexBuckets", 1);
260254
}
261255

262256
//////////////////////////////////////////////////////////////////////////////
263257
/// @brief returns the shard keys
264258
//////////////////////////////////////////////////////////////////////////////
265259

266260
std::vector<std::string> shardKeys() const {
267-
TRI_json_t* const node =
268-
arangodb::basics::JsonHelper::getObjectElement(_json, "shardKeys");
269-
return arangodb::basics::JsonHelper::stringArray(node);
261+
std::vector<std::string> shardKeys;
262+
263+
if (_slice.isNone()) {
264+
return shardKeys;
265+
}
266+
auto shardKeysSlice = _slice.get("shardKeys");
267+
if (shardKeysSlice.isArray()) {
268+
for (auto const& shardKey: VPackArrayIterator(shardKeysSlice)) {
269+
shardKeys.push_back(shardKey.copyString());
270+
}
271+
}
272+
273+
return shardKeys;
270274
}
271275

272276
//////////////////////////////////////////////////////////////////////////////
273277
/// @brief returns true if the default shard key is used
274278
//////////////////////////////////////////////////////////////////////////////
275279

276280
bool usesDefaultShardKeys() const {
277-
TRI_json_t* const node =
278-
arangodb::basics::JsonHelper::getObjectElement(_json, "shardKeys");
279-
if (TRI_LengthArrayJson(node) != 1) {
281+
if (_slice.isNone()) {
282+
return false;
283+
}
284+
auto shardKeysSlice = _slice.get("shardKeys");
285+
if (!shardKeysSlice.isArray() || shardKeysSlice.length() != 1) {
280286
return false;
281287
}
282-
TRI_json_t* firstKey = TRI_LookupArrayJson(node, 0);
283-
TRI_ASSERT(TRI_IsStringJson(firstKey));
288+
289+
auto firstElement = shardKeysSlice.get(0);
290+
TRI_ASSERT(firstElement.isString());
284291
std::string shardKey =
285-
arangodb::basics::JsonHelper::getStringValue(firstKey, "");
292+
arangodb::basics::VelocyPackHelper::getStringValue(firstElement, "");
286293
return shardKey == TRI_VOC_ATTRIBUTE_KEY;
287294
}
288295

@@ -302,22 +309,18 @@ class CollectionInfo {
302309
return res;
303310
}
304311
res.reset(new ShardMap());
305-
TRI_json_t* const node =
306-
arangodb::basics::JsonHelper::getObjectElement(_json, "shards");
307-
if (node != nullptr && TRI_IsObjectJson(node)) {
308-
size_t len = TRI_LengthVector(&node->_value._objects);
309-
for (size_t i = 0; i < len; i += 2) {
310-
auto key =
311-
static_cast<TRI_json_t*>(TRI_AtVector(&node->_value._objects, i));
312-
auto value = static_cast<TRI_json_t*>(
313-
TRI_AtVector(&node->_value._objects, i + 1));
314-
if (TRI_IsStringJson(key) && TRI_IsArrayJson(value)) {
315-
ShardID shard = arangodb::basics::JsonHelper::getStringValue(key, "");
316-
std::vector<ServerID> servers =
317-
arangodb::basics::JsonHelper::stringArray(value);
318-
if (shard != "") {
319-
(*res).insert(make_pair(shard, servers));
312+
313+
auto shardsSlice = _slice.get("shards");
314+
if (shardsSlice.isObject()) {
315+
for (auto const& shardSlice: VPackObjectIterator(shardsSlice)) {
316+
if (shardSlice.key.isString() && shardSlice.value.isArray()) {
317+
ShardID shard = shardSlice.key.copyString();
318+
319+
std::vector<ServerID> servers;
320+
for (auto const& serverSlice: VPackArrayIterator(shardSlice.value)) {
321+
servers.push_back(serverSlice.copyString());
320322
}
323+
(*res).insert(make_pair(shardSlice.key.copyString(), servers));
321324
}
322325
}
323326
}
@@ -333,11 +336,13 @@ class CollectionInfo {
333336
//////////////////////////////////////////////////////////////////////////////
334337

335338
int numberOfShards() const {
336-
TRI_json_t* const node =
337-
arangodb::basics::JsonHelper::getObjectElement(_json, "shards");
339+
if (_slice.isNone()) {
340+
return 0;
341+
}
342+
auto shardsSlice = _slice.get("shards");
338343

339-
if (TRI_IsObjectJson(node)) {
340-
return (int)(TRI_LengthVector(&node->_value._objects) / 2);
344+
if (shardsSlice.isObject()) {
345+
return shardsSlice.length();
341346
}
342347
return 0;
343348
}
@@ -346,10 +351,16 @@ class CollectionInfo {
346351
/// @brief returns the json
347352
//////////////////////////////////////////////////////////////////////////////
348353

349-
TRI_json_t const* getJson() const { return _json; }
354+
std::shared_ptr<VPackBuilder> const getVPack() const { return _vpack; }
355+
356+
//////////////////////////////////////////////////////////////////////////////
357+
/// @brief returns the slice
358+
//////////////////////////////////////////////////////////////////////////////
359+
VPackSlice const getSlice() const { return _slice; }
350360

351361
private:
352-
TRI_json_t* _json;
362+
std::shared_ptr<VPackBuilder> _vpack;
363+
VPackSlice _slice;
353364

354365
// Only to protect the cache:
355366
mutable Mutex _mutex;
@@ -583,13 +594,6 @@ class ClusterInfo {
583594

584595
std::vector<DatabaseID> listDatabases(bool = false);
585596

586-
//////////////////////////////////////////////////////////////////////////////
587-
/// @brief (re-)load the information about planned collections from the agency
588-
/// Usually one does not have to call this directly.
589-
//////////////////////////////////////////////////////////////////////////////
590-
591-
void loadPlannedCollections();
592-
593597
//////////////////////////////////////////////////////////////////////////////
594598
/// @brief (re-)load the information about our plan
595599
/// Usually one does not have to call this directly.
@@ -933,7 +937,6 @@ class ClusterInfo {
933937

934938
// The Plan state:
935939
AllCollections _plannedCollections; // from Plan/Collections/
936-
ProtectionData _plannedCollectionsProt;
937940
std::unordered_map<CollectionID,
938941
std::shared_ptr<std::vector<std::string>>>
939942
_shards; // from Plan/Collections/

arangod/Cluster/v8-cluster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ static void JS_GetCollectionInfoClusterInfo(
824824
}
825825
result->Set(TRI_V8_ASCII_STRING("shards"), shardIds);
826826

827-
v8::Handle<v8::Value> indexes = TRI_ObjectJson(isolate, ci->getIndexes());
827+
v8::Handle<v8::Value> indexes = TRI_VPackToV8(isolate, ci->getIndexes());
828828
result->Set(TRI_V8_ASCII_STRING("indexes"), indexes);
829829

830830
TRI_V8_RETURN(result);

0 commit comments

Comments
 (0)
0