Skip to content

Commit fe35750

Browse files
author
a-brandt
committed
changes for query functions
1 parent f413b84 commit fe35750

File tree

4 files changed

+311
-3
lines changed

4 files changed

+311
-3
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5549,7 +5549,7 @@ public QueryTrackingPropertiesEntity setQueryTrackingProperties(QueryTrackingPro
55495549
}
55505550

55515551
/**
5552-
* Returns a list of currently running AQL queries
5552+
* Returns a list of currently running AQL queries of the default database
55535553
*
55545554
* @return a list of currently running AQL queries
55555555
* @throws ArangoException
@@ -5559,7 +5559,19 @@ public QueriesResultEntity getCurrentlyRunningQueries() throws ArangoException {
55595559
}
55605560

55615561
/**
5562-
* Returns a list of slow running AQL queries
5562+
* Returns a list of currently running AQL queries of a database
5563+
*
5564+
* @param database
5565+
* the database name or null
5566+
* @return a list of currently running AQL queries
5567+
* @throws ArangoException
5568+
*/
5569+
public QueriesResultEntity getCurrentlyRunningQueries(String database) throws ArangoException {
5570+
return this.cursorDriver.getCurrentlyRunningQueries(database);
5571+
}
5572+
5573+
/**
5574+
* Returns a list of slow running AQL queries of the default database
55635575
*
55645576
* @return a list of slow running AQL queries
55655577
* @throws ArangoException
@@ -5569,7 +5581,19 @@ public QueriesResultEntity getSlowQueries() throws ArangoException {
55695581
}
55705582

55715583
/**
5572-
* Clears the list of slow AQL queries
5584+
* Returns a list of slow running AQL queries of a database
5585+
*
5586+
* @param database
5587+
* the database name or null
5588+
* @return a list of slow running AQL queries
5589+
* @throws ArangoException
5590+
*/
5591+
public QueriesResultEntity getSlowQueries(String database) throws ArangoException {
5592+
return this.cursorDriver.getSlowQueries(database);
5593+
}
5594+
5595+
/**
5596+
* Clears the list of slow AQL queries of the default database
55735597
*
55745598
* @return
55755599
* @throws ArangoException
@@ -5578,6 +5602,18 @@ public DefaultEntity deleteSlowQueries() throws ArangoException {
55785602
return this.cursorDriver.deleteSlowQueries(getDefaultDatabase());
55795603
}
55805604

5605+
/**
5606+
* Clears the list of slow AQL queries of the default database
5607+
*
5608+
* @param database
5609+
* the database name or null
5610+
* @return
5611+
* @throws ArangoException
5612+
*/
5613+
public DefaultEntity deleteSlowQueries(String database) throws ArangoException {
5614+
return this.cursorDriver.deleteSlowQueries(database);
5615+
}
5616+
55815617
/**
55825618
* Kills an AQL query
55835619
*
@@ -5590,4 +5626,18 @@ public DefaultEntity killQuery(String id) throws ArangoException {
55905626
return this.cursorDriver.killQuery(getDefaultDatabase(), id);
55915627
}
55925628

5629+
/**
5630+
* Kills an AQL query
5631+
*
5632+
* @param id
5633+
* the identifier of a query
5634+
* @param database
5635+
* the database name or null
5636+
* @return
5637+
* @throws ArangoException
5638+
*/
5639+
public DefaultEntity killQuery(String database, String id) throws ArangoException {
5640+
return this.cursorDriver.killQuery(database, id);
5641+
}
5642+
55935643
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,7 @@ public QueriesResultEntity deserialize(JsonElement json, Type typeOfT, JsonDeser
20552055

20562056
if (obj.has("id")) {
20572057
entity.setId(obj.getAsJsonPrimitive("id").getAsString());
2058+
queries.add(entity);
20582059
}
20592060

20602061
if (obj.has("query")) {
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
/*
2+
* Copyright (C) 2012,2013 tamtam180
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import com.arangodb.entity.DefaultEntity;
31+
import com.arangodb.entity.QueriesResultEntity;
32+
import com.arangodb.entity.QueryEntity;
33+
import com.arangodb.entity.QueryTrackingPropertiesEntity;
34+
35+
/**
36+
* @author a-brandt
37+
*
38+
*/
39+
public class ArangoDriverQueryTest extends BaseTest {
40+
41+
public ArangoDriverQueryTest(ArangoConfigure configure, ArangoDriver driver) {
42+
super(configure, driver);
43+
}
44+
45+
@Before
46+
public void setup() throws ArangoException {
47+
}
48+
49+
@Test
50+
public void test_getQueryTrackingProperties() throws ArangoException {
51+
QueryTrackingPropertiesEntity queryTrackingProperties = driver.getQueryTrackingProperties();
52+
assertEquals(200, queryTrackingProperties.getStatusCode());
53+
assertNotNull(queryTrackingProperties.getEnabled());
54+
assertNotNull(queryTrackingProperties.getTrackSlowQueries());
55+
assertNotNull(queryTrackingProperties.getSlowQueryThreshold());
56+
assertNotNull(queryTrackingProperties.getMaxSlowQueries());
57+
assertNotNull(queryTrackingProperties.getMaxQueryStringLength());
58+
}
59+
60+
@Test
61+
public void test_setQueryTrackingProperties() throws ArangoException {
62+
QueryTrackingPropertiesEntity properties1 = driver.getQueryTrackingProperties();
63+
64+
Long maxQueryStringLength = properties1.getMaxQueryStringLength() + 10;
65+
66+
properties1.setMaxQueryStringLength(maxQueryStringLength);
67+
68+
QueryTrackingPropertiesEntity properties2 = driver.setQueryTrackingProperties(properties1);
69+
70+
assertEquals(200, properties2.getStatusCode());
71+
assertNotNull(properties2.getEnabled());
72+
assertNotNull(properties2.getTrackSlowQueries());
73+
assertNotNull(properties2.getSlowQueryThreshold());
74+
assertNotNull(properties2.getMaxSlowQueries());
75+
assertNotNull(properties2.getMaxQueryStringLength());
76+
assertEquals(maxQueryStringLength, properties2.getMaxQueryStringLength());
77+
}
78+
79+
@Test
80+
public void test_getCurrentlyRunningQueries() throws ArangoException, InterruptedException {
81+
82+
String queryString = "return sleep(3)";
83+
84+
Thread thread1 = new Thread(new RunnableThread(driver.getDefaultDatabase(), configure, queryString), "thread1");
85+
thread1.start();
86+
87+
Thread.sleep(1000);
88+
89+
QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries();
90+
91+
// wait for thread
92+
thread1.join();
93+
94+
// check result
95+
assertEquals(200, currentlyRunningQueries.getStatusCode());
96+
List<QueryEntity> queries = currentlyRunningQueries.getQueries();
97+
assertEquals(1, queries.size());
98+
}
99+
100+
@Test
101+
public void test_getCurrentlyRunningQueriesWithoutDatabase() throws ArangoException, InterruptedException {
102+
103+
String queryString = "return sleep(3)";
104+
105+
// create job in _system database
106+
Thread thread1 = new Thread(new RunnableThread(null, configure, queryString), "thread1");
107+
thread1.start();
108+
109+
Thread.sleep(1000);
110+
111+
// search in default database
112+
QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries();
113+
114+
// wait for thread
115+
thread1.join();
116+
117+
// check result
118+
assertEquals(200, currentlyRunningQueries.getStatusCode());
119+
List<QueryEntity> queries = currentlyRunningQueries.getQueries();
120+
assertEquals(0, queries.size());
121+
}
122+
123+
@Test
124+
public void test_getCurrentlyRunningQueriesWithoutDatabase2() throws ArangoException, InterruptedException {
125+
126+
String queryString = "return sleep(3)";
127+
128+
// create job in _system database
129+
Thread thread1 = new Thread(new RunnableThread(null, configure, queryString), "thread1");
130+
thread1.start();
131+
132+
Thread.sleep(1000);
133+
134+
// search in _system database
135+
QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries(null);
136+
137+
// wait for thread
138+
thread1.join();
139+
140+
// check result
141+
assertEquals(200, currentlyRunningQueries.getStatusCode());
142+
List<QueryEntity> queries = currentlyRunningQueries.getQueries();
143+
assertEquals(1, queries.size());
144+
}
145+
146+
@Test
147+
public void test_getSlowQueries() throws ArangoException, InterruptedException {
148+
// set SlowQueryThreshold to 2
149+
QueryTrackingPropertiesEntity properties1 = driver.getQueryTrackingProperties();
150+
properties1.setSlowQueryThreshold(2L);
151+
driver.setQueryTrackingProperties(properties1);
152+
153+
// create a slow query
154+
driver.executeDocumentQuery("return sleep(3)", new HashMap<String, Object>(), null, Map.class);
155+
156+
QueriesResultEntity currentlyRunningQueries = driver.getSlowQueries();
157+
158+
// check result
159+
assertEquals(200, currentlyRunningQueries.getStatusCode());
160+
List<QueryEntity> queries = currentlyRunningQueries.getQueries();
161+
assertTrue(queries.size() > 0);
162+
}
163+
164+
@Test
165+
public void test_deleteSlowQueries() throws ArangoException, InterruptedException {
166+
driver.deleteSlowQueries();
167+
QueriesResultEntity currentlyRunningQueries = driver.getSlowQueries();
168+
169+
// check result
170+
assertEquals(200, currentlyRunningQueries.getStatusCode());
171+
List<QueryEntity> queries = currentlyRunningQueries.getQueries();
172+
assertEquals(0, queries.size());
173+
174+
// set SlowQueryThreshold to 2
175+
QueryTrackingPropertiesEntity properties1 = driver.getQueryTrackingProperties();
176+
properties1.setSlowQueryThreshold(2L);
177+
driver.setQueryTrackingProperties(properties1);
178+
179+
// create a slow query
180+
driver.executeDocumentQuery("return sleep(3)", new HashMap<String, Object>(), null, Map.class);
181+
182+
currentlyRunningQueries = driver.getSlowQueries();
183+
184+
// check result
185+
assertEquals(200, currentlyRunningQueries.getStatusCode());
186+
queries = currentlyRunningQueries.getQueries();
187+
assertEquals(1, queries.size());
188+
}
189+
190+
@Test
191+
public void test_killQuery() throws ArangoException, InterruptedException {
192+
193+
String queryString = "return sleep(3)";
194+
195+
// create job in _system database
196+
Thread thread1 = new Thread(new RunnableThread(driver.getDefaultDatabase(), configure, queryString), "thread1");
197+
thread1.start();
198+
199+
Thread.sleep(1000);
200+
201+
int numKilled = 0;
202+
203+
// search in default database
204+
QueriesResultEntity currentlyRunningQueries = driver.getCurrentlyRunningQueries();
205+
// check result
206+
assertEquals(200, currentlyRunningQueries.getStatusCode());
207+
List<QueryEntity> queries = currentlyRunningQueries.getQueries();
208+
if (queries.size() > 0) {
209+
for (QueryEntity qe : queries) {
210+
try {
211+
DefaultEntity killQuery = driver.killQuery(qe.getId());
212+
assertEquals(200, killQuery.getStatusCode());
213+
numKilled++;
214+
} catch (ArangoException e) {
215+
216+
}
217+
}
218+
}
219+
220+
// wait for thread
221+
thread1.join();
222+
assertTrue(queries.size() > 0);
223+
assertEquals(numKilled, queries.size());
224+
}
225+
226+
class RunnableThread implements Runnable {
227+
228+
Thread runner;
229+
ArangoDriver driver;
230+
String queryString;
231+
232+
public RunnableThread(String database, ArangoConfigure configure, String queryString) {
233+
this.driver = new ArangoDriver(configure);
234+
this.driver.setDefaultDatabase(database);
235+
this.queryString = queryString;
236+
}
237+
238+
public RunnableThread(String threadName) {
239+
runner = new Thread(this, threadName);
240+
runner.start();
241+
}
242+
243+
public void run() {
244+
try {
245+
driver.executeDocumentQuery(queryString, new HashMap<String, Object>(), null, Map.class);
246+
} catch (ArangoException e) {
247+
if (e.getErrorNumber() != ErrorNums.ERROR_QUERY_KILLED) {
248+
e.printStackTrace();
249+
}
250+
}
251+
252+
}
253+
}
254+
}

src/test/java/com/arangodb/ArangoTestSuite.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@
108108

109109
ArangoDriverTraversalTest.class,
110110

111+
ArangoDriverQueryTest.class,
112+
111113
// since ArangoDB 2.7
112114
ArangoDriverQueryCacheTest.class
113115

114116
})
117+
115118
public class ArangoTestSuite {
116119

117120
}

0 commit comments

Comments
 (0)