Skip to content

Commit b0b2036

Browse files
authored
get stream transactions (#301)
1 parent 9bd6b7c commit b0b2036

File tree

8 files changed

+133
-8
lines changed

8 files changed

+133
-8
lines changed

docs/Drivers/Java/Reference/Database/StreamTransactions.md

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ Gets information about a Stream Transaction.
2929
transaction id
3030

3131

32+
## ArangoDatabase.getStreamTransactions
33+
34+
`ArangoDatabase.getStreamTransactions() : Collection<TransactionEntity>`
35+
36+
Gets all the currently running Stream Transactions.
37+
38+
3239
## ArangoDatabase.commitStreamTransaction
3340

3441
`ArangoDatabase.commitStreamTransaction(String id) : StreamTransactionEntity`

src/main/java/com/arangodb/ArangoDatabase.java

+11
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,17 @@ GraphEntity createGraph(String name, Collection<EdgeDefinition> edgeDefinitions,
577577
*/
578578
StreamTransactionEntity getStreamTransaction(String id) throws ArangoDBException;
579579

580+
/**
581+
* Gets all the currently running Stream Transactions.
582+
*
583+
* @return all the currently running Stream Transactions
584+
* @throws ArangoDBException
585+
* @see <a href="https://docs.arangodb.com/current/HTTP/transaction-stream-transaction.html#list-currently-ongoing-transactions">
586+
* API Documentation</a>
587+
* @since ArangoDB 3.5.0
588+
*/
589+
Collection<TransactionEntity> getStreamTransactions() throws ArangoDBException;
590+
580591
/**
581592
* Commits a Stream Transaction.
582593
*

src/main/java/com/arangodb/entity/StreamTransactionEntity.java

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ public class StreamTransactionEntity implements Entity {
3131
private String id;
3232
private StreamTransactionStatus status;
3333

34-
public enum StreamTransactionStatus {
35-
running, committed, aborted
36-
}
37-
3834
public String getId() {
3935
return id;
4036
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
/**
24+
* @author Michele Rastelli
25+
*/
26+
public enum StreamTransactionStatus {
27+
running, committed, aborted
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
/**
24+
* @author Michele Rastelli
25+
* @see <a href=
26+
* "https://docs.arangodb.com/current/HTTP/transaction-stream-transaction.html#list-currently-ongoing-transactions</a>
27+
* @since ArangoDB 3.5.0
28+
*/
29+
public class TransactionEntity implements Entity {
30+
31+
private String id;
32+
private StreamTransactionStatus state;
33+
34+
public String getId() {
35+
return id;
36+
}
37+
38+
public StreamTransactionStatus getStatus() {
39+
return state;
40+
}
41+
42+
}

src/main/java/com/arangodb/internal/ArangoDatabaseImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ public StreamTransactionEntity getStreamTransaction(String id) throws ArangoDBEx
351351
return executor.execute(getStreamTransactionRequest(id), streamTransactionResponseDeserializer());
352352
}
353353

354+
@Override
355+
public Collection<TransactionEntity> getStreamTransactions() throws ArangoDBException {
356+
return executor.execute(getStreamTransactionsRequest(), transactionsResponseDeserializer());
357+
}
358+
354359
@Override
355360
public StreamTransactionEntity commitStreamTransaction(String id) throws ArangoDBException {
356361
return executor.execute(commitStreamTransactionRequest(id), streamTransactionResponseDeserializer());

src/main/java/com/arangodb/internal/InternalArangoDatabase.java

+15
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,25 @@ protected Request abortStreamTransactionRequest(String id) {
387387
return request(name, RequestType.DELETE, PATH_API_TRANSACTION, id);
388388
}
389389

390+
protected Request getStreamTransactionsRequest() {
391+
return request(name, RequestType.GET, PATH_API_TRANSACTION);
392+
}
393+
390394
protected Request getStreamTransactionRequest(String id) {
391395
return request(name, RequestType.GET, PATH_API_TRANSACTION, id);
392396
}
393397

398+
protected ResponseDeserializer<Collection<TransactionEntity>> transactionsResponseDeserializer() {
399+
return new ResponseDeserializer<Collection<TransactionEntity>>() {
400+
@Override
401+
public Collection<TransactionEntity> deserialize(final Response response) throws VPackException {
402+
final VPackSlice result = response.getBody().get("transactions");
403+
return util().deserialize(result, new Type<Collection<TransactionEntity>>() {
404+
}.getType());
405+
}
406+
};
407+
}
408+
394409
protected Request commitStreamTransactionRequest(String id) {
395410
return request(name, RequestType.PUT, PATH_API_TRANSACTION, id);
396411
}

src/test/java/com/arangodb/StreamTransactionTest.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void beginStreamTransaction() {
7373

7474
StreamTransactionEntity tx = db.beginStreamTransaction(null);
7575
assertThat(tx.getId(), is(notNullValue()));
76-
assertThat(tx.getStatus(), is(StreamTransactionEntity.StreamTransactionStatus.running));
76+
assertThat(tx.getStatus(), is(StreamTransactionStatus.running));
7777
db.abortStreamTransaction(tx.getId());
7878
}
7979

@@ -97,7 +97,7 @@ public void abortStreamTransaction() {
9797

9898
assertThat(abortedTx.getId(), is(notNullValue()));
9999
assertThat(abortedTx.getId(), is(begunTx.getId()));
100-
assertThat(abortedTx.getStatus(), is(StreamTransactionEntity.StreamTransactionStatus.aborted));
100+
assertThat(abortedTx.getStatus(), is(StreamTransactionStatus.aborted));
101101
}
102102

103103
@Test
@@ -151,7 +151,7 @@ public void getStreamTransaction() {
151151

152152
assertThat(gotTx.getId(), is(notNullValue()));
153153
assertThat(gotTx.getId(), is(createdTx.getId()));
154-
assertThat(gotTx.getStatus(), is(StreamTransactionEntity.StreamTransactionStatus.running));
154+
assertThat(gotTx.getStatus(), is(StreamTransactionStatus.running));
155155

156156
db.abortStreamTransaction(createdTx.getId());
157157
}
@@ -185,7 +185,7 @@ public void commitStreamTransaction() {
185185

186186
assertThat(committedTx.getId(), is(notNullValue()));
187187
assertThat(committedTx.getId(), is(createdTx.getId()));
188-
assertThat(committedTx.getStatus(), is(StreamTransactionEntity.StreamTransactionStatus.committed));
188+
assertThat(committedTx.getStatus(), is(StreamTransactionStatus.committed));
189189
}
190190

191191
@Test
@@ -705,4 +705,25 @@ public void nextCursor() {
705705
db.abortStreamTransaction(tx.getId());
706706
}
707707

708+
@Test
709+
public void getStreamTransactions() {
710+
assumeTrue(requireSingleServer());
711+
assumeTrue(requireVersion(3, 5));
712+
assumeTrue(requireStorageEngine(ArangoDBEngine.StorageEngineName.rocksdb));
713+
714+
StreamTransactionEntity tx1 = db.beginStreamTransaction(null);
715+
StreamTransactionEntity tx2 = db.beginStreamTransaction(null);
716+
717+
List<String> createdIds = Arrays.asList(tx1.getId(), tx2.getId());
718+
Set<TransactionEntity> gotTxs = db.getStreamTransactions().stream().
719+
filter(it -> createdIds.contains(it.getId())).collect(Collectors.toSet());
720+
721+
assertThat(gotTxs.size(), is(createdIds.size()));
722+
assertThat(gotTxs.stream()
723+
.allMatch(it -> it.getStatus() == StreamTransactionStatus.running), is(true));
724+
725+
db.abortStreamTransaction(tx1.getId());
726+
db.abortStreamTransaction(tx2.getId());
727+
}
728+
708729
}

0 commit comments

Comments
 (0)