PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/stack/core/src/test/java/org/apache/usergrid/persistence/IndexIT.java

https://gitlab.com/indyaah/incubator-usergrid
Java | 463 lines | 313 code | 119 blank | 31 comment | 24 complexity | 8660f762ebd61fd2c3918f91a749e3ba MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.usergrid.persistence;
  18. import java.nio.ByteBuffer;
  19. import java.util.LinkedHashMap;
  20. import java.util.Map;
  21. import java.util.UUID;
  22. import org.junit.Test;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.apache.usergrid.AbstractCoreIT;
  26. import org.apache.usergrid.CoreITSuite;
  27. import org.apache.usergrid.cassandra.Concurrent;
  28. import org.apache.usergrid.persistence.cassandra.CassandraService;
  29. import org.apache.usergrid.persistence.cassandra.IndexUpdate;
  30. import org.apache.usergrid.persistence.cassandra.IndexUpdate.IndexEntry;
  31. import org.apache.usergrid.persistence.cassandra.RelationManagerImpl;
  32. import org.apache.usergrid.persistence.hector.CountingMutator;
  33. import org.apache.usergrid.utils.JsonUtils;
  34. import org.apache.usergrid.utils.UUIDUtils;
  35. import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
  36. import me.prettyprint.hector.api.Keyspace;
  37. import me.prettyprint.hector.api.mutation.Mutator;
  38. import static org.junit.Assert.assertEquals;
  39. import static org.junit.Assert.assertNotNull;
  40. import static org.junit.Assert.fail;
  41. @Concurrent()
  42. public class IndexIT extends AbstractCoreIT {
  43. private static final Logger LOG = LoggerFactory.getLogger( IndexIT.class );
  44. public static final String[] alphabet = {
  45. "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima",
  46. "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey",
  47. "X-ray", "Yankee", "Zulu"
  48. };
  49. @Test
  50. public void testCollectionOrdering() throws Exception {
  51. LOG.info( "testCollectionOrdering" );
  52. UUID applicationId = setup.createApplication( "testOrganization", "testCollectionOrdering" );
  53. assertNotNull( applicationId );
  54. EntityManager em = setup.getEmf().getEntityManager( applicationId );
  55. assertNotNull( em );
  56. for ( int i = alphabet.length - 1; i >= 0; i-- ) {
  57. String name = alphabet[i];
  58. Map<String, Object> properties = new LinkedHashMap<String, Object>();
  59. properties.put( "name", name );
  60. em.create( "item", properties );
  61. }
  62. int i = 0;
  63. Query query = Query.fromQL( "order by name" );
  64. Results r = em.searchCollection( em.getApplicationRef(), "items", query );
  65. for ( Entity entity : r.getEntities() ) {
  66. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  67. i++;
  68. }
  69. query = Query.fromQL( "order by name" ).withCursor( r.getCursor() );
  70. r = em.searchCollection( em.getApplicationRef(), "items", query );
  71. for ( Entity entity : r.getEntities() ) {
  72. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  73. i++;
  74. }
  75. query = Query.fromQL( "order by name" ).withCursor( r.getCursor() );
  76. r = em.searchCollection( em.getApplicationRef(), "items", query );
  77. for ( Entity entity : r.getEntities() ) {
  78. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  79. i++;
  80. }
  81. assertEquals( alphabet.length, i );
  82. i = alphabet.length;
  83. query = Query.fromQL( "order by name desc" );
  84. r = em.searchCollection( em.getApplicationRef(), "items", query );
  85. for ( Entity entity : r.getEntities() ) {
  86. i--;
  87. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  88. }
  89. query = Query.fromQL( "order by name desc" ).withCursor( r.getCursor() );
  90. r = em.searchCollection( em.getApplicationRef(), "items", query );
  91. // LOG.info(JsonUtils.mapToFormattedJsonString(r.getEntities()));
  92. for ( Entity entity : r.getEntities() ) {
  93. i--;
  94. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  95. }
  96. query = Query.fromQL( "order by name desc" ).withCursor( r.getCursor() );
  97. r = em.searchCollection( em.getApplicationRef(), "items", query );
  98. for ( Entity entity : r.getEntities() ) {
  99. i--;
  100. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  101. }
  102. assertEquals( 0, i );
  103. }
  104. @Test
  105. public void testCollectionFilters() throws Exception {
  106. LOG.info( "testCollectionFilters" );
  107. UUID applicationId = setup.createApplication( "testOrganization", "testCollectionFilters" );
  108. assertNotNull( applicationId );
  109. EntityManager em = setup.getEmf().getEntityManager( applicationId );
  110. assertNotNull( em );
  111. for ( int i = alphabet.length - 1; i >= 0; i-- ) {
  112. String name = alphabet[i];
  113. Map<String, Object> properties = new LinkedHashMap<String, Object>();
  114. properties.put( "name", name );
  115. em.create( "item", properties );
  116. }
  117. Query query = Query.fromQL( "name < 'delta'" );
  118. Results r = em.searchCollection( em.getApplicationRef(), "items", query );
  119. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  120. int i = 0;
  121. for ( Entity entity : r.getEntities() ) {
  122. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  123. i++;
  124. }
  125. assertEquals( 3, i );
  126. query = Query.fromQL( "name <= 'delta'" );
  127. r = em.searchCollection( em.getApplicationRef(), "items", query );
  128. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  129. i = 0;
  130. for ( Entity entity : r.getEntities() ) {
  131. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  132. i++;
  133. }
  134. assertEquals( 4, i );
  135. query = Query.fromQL( "name <= 'foxtrot' and name > 'bravo'" );
  136. r = em.searchCollection( em.getApplicationRef(), "items", query );
  137. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  138. i = 2;
  139. for ( Entity entity : r.getEntities() ) {
  140. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  141. i++;
  142. }
  143. assertEquals( 6, i );
  144. query = Query.fromQL( "name < 'foxtrot' and name > 'bravo'" );
  145. r = em.searchCollection( em.getApplicationRef(), "items", query );
  146. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  147. i = 2;
  148. for ( Entity entity : r.getEntities() ) {
  149. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  150. i++;
  151. }
  152. assertEquals( 5, i );
  153. query = Query.fromQL( "name < 'foxtrot' and name >= 'bravo'" );
  154. r = em.searchCollection( em.getApplicationRef(), "items", query );
  155. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  156. i = 1;
  157. for ( Entity entity : r.getEntities() ) {
  158. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  159. i++;
  160. }
  161. assertEquals( 5, i );
  162. query = Query.fromQL( "name <= 'foxtrot' and name >= 'bravo'" );
  163. r = em.searchCollection( em.getApplicationRef(), "items", query );
  164. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  165. i = 1;
  166. for ( Entity entity : r.getEntities() ) {
  167. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  168. i++;
  169. }
  170. assertEquals( 6, i );
  171. query = Query.fromQL( "name <= 'foxtrot' and name >= 'bravo' order by name desc" );
  172. r = em.searchCollection( em.getApplicationRef(), "items", query );
  173. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  174. i = 6;
  175. for ( Entity entity : r.getEntities() ) {
  176. i--;
  177. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  178. }
  179. assertEquals( 1, i );
  180. query = Query.fromQL( "name < 'foxtrot' and name > 'bravo' order by name desc" );
  181. r = em.searchCollection( em.getApplicationRef(), "items", query );
  182. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  183. i = 5;
  184. for ( Entity entity : r.getEntities() ) {
  185. i--;
  186. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  187. }
  188. assertEquals( 2, i );
  189. query = Query.fromQL( "name < 'foxtrot' and name >= 'bravo' order by name desc" );
  190. r = em.searchCollection( em.getApplicationRef(), "items", query );
  191. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  192. i = 5;
  193. for ( Entity entity : r.getEntities() ) {
  194. i--;
  195. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  196. }
  197. assertEquals( 1, i );
  198. query = Query.fromQL( "name = 'foxtrot'" );
  199. r = em.searchCollection( em.getApplicationRef(), "items", query );
  200. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  201. assertEquals( 1, r.size() );
  202. long created = r.getEntity().getCreated();
  203. UUID entityId = r.getEntity().getUuid();
  204. query = Query.fromQL( "created = " + created );
  205. r = em.searchCollection( em.getApplicationRef(), "items", query );
  206. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  207. assertEquals( 1, r.size() );
  208. assertEquals( entityId, r.getEntity().getUuid() );
  209. }
  210. @Test
  211. public void testSecondarySorts() throws Exception {
  212. LOG.info( "testSecondarySorts" );
  213. UUID applicationId = setup.createApplication( "testOrganization", "testSecondarySorts" );
  214. assertNotNull( applicationId );
  215. EntityManager em = setup.getEmf().getEntityManager( applicationId );
  216. assertNotNull( em );
  217. for ( int i = alphabet.length - 1; i >= 0; i-- ) {
  218. String name = alphabet[i];
  219. Map<String, Object> properties = new LinkedHashMap<String, Object>();
  220. properties.put( "name", name );
  221. properties.put( "group", i / 3 );
  222. properties.put( "reverse_name", alphabet[alphabet.length - 1 - i] );
  223. em.create( "item", properties );
  224. }
  225. Query query = Query.fromQL( "group = 1 order by name desc" );
  226. Results r = em.searchCollection( em.getApplicationRef(), "items", query );
  227. LOG.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) );
  228. int i = 6;
  229. for ( Entity entity : r.getEntities() ) {
  230. i--;
  231. assertEquals( 1L, entity.getProperty( "group" ) );
  232. assertEquals( alphabet[i], entity.getProperty( "name" ) );
  233. }
  234. assertEquals( 3, i );
  235. }
  236. @Test
  237. public void testPropertyUpdateWithConnection() throws Exception {
  238. UUID applicationId = setup.createApplication( "testOrganization", "testPropertyUpdateWithConnection" );
  239. EntityManager em = setup.getEmf().getEntityManager( applicationId );
  240. Map<String, Object> entity1 = new LinkedHashMap<String, Object>();
  241. entity1.put( "name", "name_1" );
  242. entity1.put( "status", "pickled" );
  243. Map<String, Object> entity2 = new LinkedHashMap<String, Object>();
  244. entity2.put( "name", "name_2" );
  245. entity2.put( "status", "foo" );
  246. Entity entity1Ref = em.create( "names", entity1 );
  247. Entity entity2Ref = em.create( "names", entity2 );
  248. em.createConnection( entity2Ref, "connecting", entity1Ref );
  249. //should return valid values
  250. Query query = Query.fromQL( "select * where status = 'pickled'" );
  251. Results r = em.searchCollection( em.getApplicationRef(), "names", query );
  252. assertEquals( 1, r.size() );
  253. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  254. r = em.searchCollection( em.getApplicationRef(), "names", query );
  255. assertEquals( 1, r.size() );
  256. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  257. //now update the first entity, this causes the failure after connections
  258. entity1Ref.setProperty( "status", "herring" );
  259. em.update( entity1Ref );
  260. //query and check the status has been updated, shouldn't return results
  261. query = Query.fromQL( "select * where status = 'pickled'" );
  262. r = em.searchCollection( em.getApplicationRef(), "names", query );
  263. assertEquals( 0, r.size() );
  264. //search connections
  265. r = em.searchCollection( em.getApplicationRef(), "names", query );
  266. assertEquals( 0, r.size() );
  267. //should return results
  268. query = Query.fromQL( "select * where status = 'herring'" );
  269. r = em.searchCollection( em.getApplicationRef(), "names", query );
  270. assertEquals( 1, r.size() );
  271. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  272. //search connections
  273. r = em.searchCollection( em.getApplicationRef(), "names", query );
  274. assertEquals( 1, r.size() );
  275. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  276. }
  277. /** Same as above, but verifies the data in our entity_index_entry CF after the operations have completed */
  278. @Test
  279. public void testPropertyUpdateWithConnectionEntityIndexEntryAudit() throws Exception {
  280. UUID applicationId =
  281. setup.createApplication( "testOrganization", "testPropertyUpdateWithConnectionEntityIndexEntryAudit" );
  282. EntityManager em = setup.getEmf().getEntityManager( applicationId );
  283. Map<String, Object> entity1 = new LinkedHashMap<String, Object>();
  284. entity1.put( "name", "name_1" );
  285. entity1.put( "status", "pickled" );
  286. Map<String, Object> entity2 = new LinkedHashMap<String, Object>();
  287. entity2.put( "name", "name_2" );
  288. entity2.put( "status", "foo" );
  289. Entity entity1Ref = em.create( "names", entity1 );
  290. Entity entity2Ref = em.create( "names", entity2 );
  291. em.createConnection( entity2Ref, "connecting", entity1Ref );
  292. //should return valid values
  293. Query query = Query.fromQL( "select * where status = 'pickled'" );
  294. Results r = em.searchCollection( em.getApplicationRef(), "names", query );
  295. assertEquals( 1, r.size() );
  296. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  297. r = em.searchCollection( em.getApplicationRef(), "names", query );
  298. assertEquals( 1, r.size() );
  299. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  300. //now update the first entity, this causes the failure after connections
  301. entity1Ref.setProperty( "status", "herring" );
  302. em.update( entity1Ref );
  303. //query and check the status has been updated, shouldn't return results
  304. query = Query.fromQL( "select * where status = 'pickled'" );
  305. r = em.searchCollection( em.getApplicationRef(), "names", query );
  306. assertEquals( 0, r.size() );
  307. //search connections
  308. r = em.searchCollection( em.getApplicationRef(), "names", query );
  309. assertEquals( 0, r.size() );
  310. //should return results
  311. query = Query.fromQL( "select * where status = 'herring'" );
  312. r = em.searchCollection( em.getApplicationRef(), "names", query );
  313. assertEquals( 1, r.size() );
  314. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  315. //search connections
  316. r = em.searchCollection( em.getApplicationRef(), "names", query );
  317. assertEquals( 1, r.size() );
  318. assertEquals( entity1Ref.getUuid(), r.getEntity().getUuid() );
  319. RelationManagerImpl impl = ( RelationManagerImpl ) em.getRelationManager( entity2Ref );
  320. //now read the index and see what properties are there
  321. CassandraService cass = CoreITSuite.cassandraResource.getBean( CassandraService.class );
  322. ByteBufferSerializer buf = ByteBufferSerializer.get();
  323. Keyspace ko = cass.getApplicationKeyspace( applicationId );
  324. Mutator<ByteBuffer> m = CountingMutator.createFlushingMutator( ko, buf );
  325. IndexUpdate update =
  326. impl.batchStartIndexUpdate( m, entity1Ref, "status", "ignore", UUIDUtils.newTimeUUID(), false, false,
  327. true, false );
  328. int count = 0;
  329. IndexEntry lastMatch = null;
  330. for ( IndexEntry entry : update.getPrevEntries() ) {
  331. if ( "status".equals( entry.getPath() ) ) {
  332. count++;
  333. lastMatch = entry;
  334. }
  335. }
  336. assertEquals( 1, count );
  337. if ( lastMatch != null ) {
  338. assertEquals( "herring", lastMatch.getValue() );
  339. }
  340. else {
  341. fail( "The last match was null but should have been herring!" );
  342. }
  343. }
  344. }