8000 Merge branch 'devel' of https://github.com/arangodb/arangodb into devel · aa10000/arangodb@9ab9f17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ab9f17

Browse files
committed
Merge branch 'devel' of https://github.com/arangodb/arangodb into devel
2 parents a7f8884 + cf04966 commit 9ab9f17

File tree

12 files changed

+130
-35
lines changed

12 files changed

+130
-35
lines changed

CHANGELOG

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
devel
22
-----
33

4+
* added optional detail flag for db.<collection>.count()
5+
setting the flag to `true` will make the count operation returned the per-shard
6+
counts for the collection:
7+
8+
db._create("test", { numberOfShards: 10 });
9+
for (i = 0; i < 1000; ++i) {
10+
db.test.insert({value: i});
11+
}
12+
db.test.count(true);
13+
14+
{
15+
"s100058" : 99,
16+
"s100057" : 103,
17+
"s100056" : 100,
18+
"s100050" : 94,
19+
"s100055" : 90,
20+
"s100054" : 122,
21+
"s100051" : 109,
22+
"s100059" : 99,
23+
"s100053" : 95,
24+
"s100052" : 89
25+
}
26+
427
* added optional memory limit for AQL queries:
528

629
db._query("FOR i IN 1..100000 SORT i RETURN i", {}, { options: { memoryLimit: 100000 } });

arangod/Aql/Collection.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ size_t Collection::count() const {
6666
if (numDocuments == UNINITIALIZED) {
6767
if (arangodb::ServerState::instance()->isCoordinator()) {
6868
// cluster case
69-
uint64_t result;
69+
std::vector<std::pair<std::string, uint64_t>> result;
7070
int res = arangodb::countOnCoordinator(vocbase->name(), name, result);
7171
if (res != TRI_ERROR_NO_ERROR) {
7272
THROW_ARANGO_EXCEPTION_MESSAGE(
7373
res, "could not determine number of documents in collection");
7474
}
75-
numDocuments = static_cast<int64_t>(result);
75+
uint64_t count = 0;
76+
for (auto const& it : result) {
77+
count += it.second;
78+
}
79+
numDocuments = static_cast<int64_t>(count);
7680
} else {
7781
// local case
7882
// cache the result

arangod/Cluster/ClusterMethods.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,16 +686,16 @@ int figuresOnCoordinator(std::string const& dbname, std::string const& collname,
686686
}
687687

688688
////////////////////////////////////////////////////////////////////////////////
689-
/// @brief counts number of documents in a coordinator
689+
/// @brief counts number of documents in a coordinator, by shard
690690
////////////////////////////////////////////////////////////////////////////////
691691

692692
int countOnCoordinator(std::string const& dbname, std::string const& collname,
693-
uint64_t& result) {
693+
std::vector<std::pair<std::string, uint64_t>>& result) {
694694
// Set a few variables needed for our work:
695695
ClusterInfo* ci = ClusterInfo::instance();
696696
ClusterComm* cc = ClusterComm::instance();
697697

698-
result = 0;
698+
result.clear();
699699

700700
// First determine the collection ID from the name:
701701
std::shared_ptr<LogicalCollection> collinfo;
@@ -727,9 +727,8 @@ int countOnCoordinator(std::string const& dbname, std::string const& collname,
727727

728728
if (answer.isObject()) {
729729
// add to the total
730-
result +=
731-
arangodb::basics::VelocyPackHelper::getNumericValue<uint64_t>(
732-
answer, "count", 0);
730+
result.emplace_back(res.shardID, arangodb::basics::VelocyPackHelper::getNumericValue<uint64_t>(
731+
answer, "count", 0));
733732
} else {
734733
return TRI_ERROR_INTERNAL;
735734
}

arangod/Cluster/ClusterMethods.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ int figuresOnCoordinator(std::string const& dbname, std::string const& collname,
7676
std::shared_ptr<arangodb::velocypack::Builder>&);
7777

7878
////////////////////////////////////////////////////////////////////////////////
79-
/// @brief counts number of documents in a coordinator
79+
/// @brief counts number of documents in a coordinator, by shard
8080
////////////////////////////////////////////////////////////////////////////////
8181

8282
int countOnCoordinator(std::string const& dbname, std::string const& collname,
83-
uint64_t& result);
83+
std::vector<std::pair<std::string, uint64_t>>& result);
8484

8585
////////////////////////////////////////////////////////////////////////////////
8686
/// @brief creates a document in a coordinator

arangod/Utils/Transaction.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,11 +2797,11 @@ OperationResult Transaction::truncateLocal(std::string const& collectionName,
27972797
/// @brief count the number of documents in a collection
27982798
////////////////////////////////////////////////////////////////////////////////
27992799

2800-
OperationResult Transaction::count(std::string const& collectionName) {
2800+
OperationResult Transaction::count(std::string const& collectionName, bool aggregate) {
28012801
TRI_ASSERT(getStatus() == TRI_TRANSACTION_RUNNING);
28022802

28032803
if ( 741A ServerState::isCoordinator(_serverRole)) {
2804-
return countCoordinator(collectionName);
2804+
return countCoordinator(collectionName, aggregate);
28052805
}
28062806

28072807
return countLocal(collectionName);
@@ -2812,18 +2812,16 @@ OperationResult Transaction::count(std::string const& collectionName) {
28122812
//////////////////////////////////////////////////////////////////////////////
28132813

28142814
#ifndef USE_ENTERPRISE
2815-
OperationResult Transaction::countCoordinator(std::string const& collectionName) {
2816-
uint64_t count = 0;
2815+
OperationResult Transaction::countCoordinator(std::string const& collectionName,
2816+
bool aggregate) {
2817+
std::vector<std::pair<std::string, uint64_t>> count;
28172818
int res = arangodb::countOnCoordinator(_vocbase->name(), collectionName, count);
28182819

28192820
if (res != TRI_ERROR_NO_ERROR) {
28202821
return OperationResult(res);
28212822
}
28222823

2823-
VPackBuilder resultBuilder;
2824-
resultBuilder.add(VPackValue(count));
2825-
2826-
return OperationResult(resultBuilder.steal(), nullptr, "", TRI_ERROR_NO_ERROR, false);
2824+
return buildCountResult(count, aggregate);
28272825
}
28282826
#endif
28292827

@@ -3598,6 +3596,25 @@ int Transaction::resolveId(char const* handle, size_t length,
35983596

35993597
return TRI_ERROR_NO_ERROR;
36003598
}
3599+
3600+
OperationResult Transaction::buildCountResult(std::vector<std::pair<std::string, uint64_t>> const& count, bool aggregate) {
3601+
VPackBuilder resultBuilder;
3602+
3603+
if (aggregate) {
3604+
uint64_t result = 0;
3605+
for (auto const& it : count) {
3606+
result += it.second;
3607+
}
3608+
resultBuilder.add(VPackValue(result));
3609+
} else {
3610+
resultBuilder.openObject();
3611+
for (auto const& it : count) {
3612+
resultBuilder.add(it.first, VPackValue(it.second));
3613+
}
3614+
resultBuilder.close();
3615+
}
3616+
return OperationResult(resultBuilder.steal(), nullptr, "", TRI_ERROR_NO_ERROR, false);
3617+
}
36013618

36023619
//////////////////////////////////////////////////////////////////////////////
36033620
/// @brief constructor, leases a StringBuffer

arangod/Utils/Transaction.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ class Transaction {
550550
/// @brief count the number of documents in a collection
551551
//////////////////////////////////////////////////////////////////////////////
552552

553-
OperationResult count(std::string const& collectionName);
553+
OperationResult count(std::string const& collectionName, bool aggregate);
554554

555555
//////////////////////////////////////////////////////////////////////////////
556556
/// @brief Gets the best fitting index for an AQL condition.
@@ -749,11 +749,13 @@ class Transaction {
749749
OperationResult truncateLocal(std::string const& collectionName,
750750
OperationOptions& options);
751751

752-
OperationResult countCoordinator(std::string const& collectionName);
752+
OperationResult countCoordinator(std::string const& collectionName, bool aggregate);
753753
OperationResult countLocal(std::string const& collectionName);
754754

755755
protected:
756756

757+
static OperationResult buildCountResult(std::vector<std::pair<std::string, uint64_t>> const& count, bool aggregate);
758+
757759
//////////////////////////////////////////////////////////////////////////////
758760
/// @brief return the transaction collection for a document collection
759761
//////////////////////////////////////////////////////////////////////////////

arangod/V8Server/v8-collection.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,9 +2771,14 @@ static void JS_CountVocbaseCol(
27712771
TRI_V8_THROW_EXCEPTION_INTERNAL("cannot extract collection");
27722772
}
27732773

2774-
if (args.Length() != 0) {
2774+
if (args.Length() > 1) {
27752775
TRI_V8_THROW_EXCEPTION_USAGE("count()");
27762776
}
2777+
2778+
bool details = false;
2779+
if (args.Length() == 1 && ServerState::instance()->isCoordinator()) {
2780+
details = TRI_ObjectToBoolean(args[0]);
2781+
}
27772782

27782783
TRI_vocbase_t* vocbase = col->vocbase();
27792784

@@ -2791,18 +2796,22 @@ static void JS_CountVocbaseCol(
27912796
TRI_V8_THROW_EXCEPTION(res);
27922797
}
27932798

2794-
OperationResult opResult = trx.count(collectionName);
2795-
2799+
OperationResult opResult = trx.count(collectionName, !details);
27962800
res = trx.finish(opResult.code);
27972801

27982802
if (res != TRI_ERROR_NO_ERROR) {
27992803
TRI_V8_THROW_EXCEPTION(res);
28002804
}
28012805

28022806
VPackSlice s = opResult.slice();
2803-
TRI_ASSERT(s.isNumber());
2804-
2805-
TRI_V8_RETURN(v8::Number::New(isolate, static_cast<double>(s.getNumber<double>())));
2807+
if (details) {
2808+
TRI_ASSERT(s.isObject());
2809+
v8::Handle<v8::Value> result = TRI_VPackToV8(isolate, s);
2810+
TRI_V8_RETURN(result);
2811+
} else {
2812+
TRI_ASSERT(s.isNumber());
2813+
TRI_V8_RETURN(v8::Number::New(isolate, static_cast<double>(s.getNumber<double>())));
2814+
}
28062815
TRI_V8_TRY_CATCH_END
28072816
}
28082817

arangod/V8Server/v8-query.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static void JS_AllQuery(v8::FunctionCallbackInfo<v8::Value> const& args) {
231231
TRI_V8_THROW_EXCEPTION(opCursor->code);
232232
}
233233

234-
OperationResult countResult = trx.count(collectionName);
234+
OperationResult countResult = trx.count(collectionName, true);
235235
res = trx.finish(countResult.code);
236236

237237
if (countResult.failed()) {

js/actions/_api/collection/app.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function databasePrefix (req, url) {
4545
// / @brief collection representation
4646
// //////////////////////////////////////////////////////////////////////////////
4747

48-
function collectionRepresentation (collection, showProperties, showCount, showFigures) {
48+
function collectionRepresentation(collection, showProperties, showCount, showFigures) {
4949
var result = {};
5050

5151
result.id = collection._id;
@@ -70,7 +70,8 @@ function collectionRepresentation (collection, showProperties, showCount, showFi
7070
}
7171

7272
if (showCount) {
73-
result.count = collection.count();
73+
// show either the count value as a number or the detailed shard counts
74+
result.count = collection.count(showCount === 'details');
7475
}
7576

7677
if (showFigures) {
@@ -378,7 +379,12 @@ function get_api_collection (req, res) {
378379
// /_api/collection/<identifier>/count
379380
// .............................................................................
380381
else if (sub === 'count') {
381-
result = collectionRepresentation(collection, true, true, false);
382+
// show either the count value as a number or the detailed shard counts
383+
if (req.parameters.details === 'true') {
384+
result = collectionRepresentation(collection, true, 'details', false);
385+
} else {
386+
result = collectionRepresentation(collection, true, true, false);
387+
}
382388
headers = {
383389
location: databasePrefix(req, '/_api/collection/' + collection.name() + '/count')
384390
};

js/client/modules/@arangodb/arango-collection.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,13 @@ ArangoCollection.prototype.ensureIndex = function (data) {
605605
// / @brief gets the number of documents
606606
// //////////////////////////////////////////////////////////////////////////////
607607

608-
ArangoCollection.prototype.count = function () {
609-
var requestResult = this._database._connection.GET(this._baseurl('count'));
610-
608+
ArangoCollection.prototype.count = function (details) {
609+
var requestResult;
610+
if (details) {
611+
requestResult = this._database._connection.GET(this._baseurl('count') + '?details=true');
612+
} else {
613+
requestResult = this._database._connection.GET(this._baseurl('count'));
614+
}
611615
arangosh.checkRequestResult(requestResult);
612616

613617
return requestResult.count;

0 commit comments

Comments
 (0)
0