|
| 1 | +package com.arangodb; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import org.junit.AfterClass; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.BeforeClass; |
| 8 | +import org.junit.Ignore; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.arangodb.entity.QueryCachePropertiesEntity; |
| 12 | +import com.arangodb.entity.QueryCachePropertiesEntity.CacheMode; |
| 13 | +import com.arangodb.util.AqlQueryOptions; |
| 14 | +import com.arangodb.util.MapBuilder; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Mark - mark@arangodb.com |
| 18 | + * |
| 19 | + */ |
| 20 | +@Ignore |
| 21 | +public class ArangoDriverCacheTest { |
| 22 | + |
| 23 | + private static final String COLLECTION_NAME = "unitTestCollection"; |
| 24 | + private static final String DATABASE_NAME = "unitTestDatabase"; |
| 25 | + private static ArangoConfigure configure; |
| 26 | + private static ArangoDriver driver; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void setup() throws ArangoException { |
| 30 | + |
| 31 | + configure = new ArangoConfigure(); |
| 32 | + configure.init(); |
| 33 | + driver = new ArangoDriver(configure); |
| 34 | + |
| 35 | + // create test database |
| 36 | + try { |
| 37 | + driver.createDatabase(DATABASE_NAME); |
| 38 | + } catch (ArangoException e) { |
| 39 | + } |
| 40 | + driver.setDefaultDatabase(DATABASE_NAME); |
| 41 | + |
| 42 | + // create test collection |
| 43 | + try { |
| 44 | + driver.createCollection(COLLECTION_NAME); |
| 45 | + } catch (final ArangoException e) { |
| 46 | + } |
| 47 | + driver.truncateCollection(COLLECTION_NAME); |
| 48 | + |
| 49 | + // create some test data |
| 50 | + for (int i = 0; i < 1000000; i++) { |
| 51 | + final TestEntity value = new TestEntity("user_" + (i % 10), "desc" + (i % 10), i); |
| 52 | + driver.createDocument(COLLECTION_NAME, value, null); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @AfterClass |
| 58 | + public static void shutdown() { |
| 59 | + try { |
| 60 | + driver.deleteDatabase(DATABASE_NAME); |
| 61 | + } catch (final ArangoException e) { |
| 62 | + } |
| 63 | + configure.shutdown(); |
| 64 | + } |
| 65 | + |
| 66 | + private AqlQueryOptions createAqlQueryOptions( |
| 67 | + final Boolean count, |
| 68 | + final Integer batchSize, |
| 69 | + final Boolean fullCount, |
| 70 | + final Boolean cache) { |
| 71 | + return new AqlQueryOptions().setCount(count).setBatchSize(batchSize).setFullCount(fullCount).setCache(cache); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void test_withoutCache() throws ArangoException { |
| 76 | + // set cache mode off |
| 77 | + final QueryCachePropertiesEntity properties = new QueryCachePropertiesEntity(); |
| 78 | + properties.setMode(CacheMode.off); |
| 79 | + driver.setQueryCacheProperties(properties); |
| 80 | + |
| 81 | + final AqlQueryOptions aqlQueryOptions = createAqlQueryOptions(true, 1000, null, false); |
| 82 | + |
| 83 | + exceuteQuery(aqlQueryOptions); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void test_withCache() throws ArangoException { |
| 88 | + // set cache mode on |
| 89 | + final QueryCachePropertiesEntity properties = new QueryCachePropertiesEntity(); |
| 90 | + properties.setMode(CacheMode.on); |
| 91 | + driver.setQueryCacheProperties(properties); |
| 92 | + |
| 93 | + // set caching to true for the query |
| 94 | + final AqlQueryOptions aqlQueryOptions = createAqlQueryOptions(true, 1000, null, true); |
| 95 | + |
| 96 | + exceuteQuery(aqlQueryOptions); |
| 97 | + } |
| 98 | + |
| 99 | + private void exceuteQuery(AqlQueryOptions aqlQueryOptions) throws ArangoException { |
| 100 | + |
| 101 | + final String query = "FOR t IN " + COLLECTION_NAME + " FILTER t.age >= @age SORT t.age RETURN t"; |
| 102 | + final Map<String, Object> bindVars = new MapBuilder().put("age", 90).get(); |
| 103 | + |
| 104 | + DocumentCursor<TestEntity> rs = driver.executeDocumentQuery(query, bindVars, aqlQueryOptions, TestEntity.class); |
| 105 | + // first time, the query isn't cached |
| 106 | + Assert.assertEquals(false, rs.isCached()); |
| 107 | + |
| 108 | + final long start = System.currentTimeMillis(); |
| 109 | + |
| 110 | + // query the cached value |
| 111 | + rs = driver.executeDocumentQuery(query, bindVars, aqlQueryOptions, TestEntity.class); |
| 112 | + Assert.assertEquals(aqlQueryOptions.getCache(), rs.isCached()); |
| 113 | + |
| 114 | + // load all results |
| 115 | + rs.asEntityList(); |
| 116 | + |
| 117 | + final long time = System.currentTimeMillis() - start; |
| 118 | + System.out.println(time); |
| 119 | + } |
| 120 | + |
| 121 | + private static class TestEntity { |
| 122 | + private String user; |
| 123 | + private String desc; |
| 124 | + private Integer age; |
| 125 | + |
| 126 | + public TestEntity(String user, String desc, Integer age) { |
| 127 | + super(); |
| 128 | + this.user = user; |
| 129 | + this.desc = desc; |
| 130 | + this.age = age; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments