public boolean hasVersion(){ return getVersion() != null; }
/** * Create the index doc from the given entity */ public static String createIndexDocId( final ApplicationScope applicationScope, final Entity entity, final IndexEdge indexEdge ) { return createIndexDocId( applicationScope, entity.getId(), entity.getVersion(), indexEdge ); }
@Override public EntityIndexBatch deindex( final SearchEdge searchEdge, final Entity entity ) { return deindex( searchEdge, entity.getId(), entity.getVersion() ); }
private void deIndexOldVersionsOfEntity(org.apache.usergrid.persistence.model.entity.Entity cpEntity) { // queue up an event to clean-up older versions than this one from the index if (entityManagerFig.getDeindexOnUpdate()) { indexService.queueDeIndexOldVersion( applicationScope, cpEntity.getId(), cpEntity.getVersion()); } }
if ( entity.getVersion() == null ) { return; List<MvccLogEntry> versions = logEntryStrat.load( applicationScope, entity.getId(), entity.getVersion(), 2 ); entity.getVersion().toString() );
@Override public EntityIndexBatch index( final IndexEdge indexEdge, final Entity entity, final Optional<Set<String>> fieldsToIndex ) { IndexValidationUtils.validateIndexEdge(indexEdge); ValidationUtils.verifyEntityWrite(entity); ValidationUtils.verifyVersion( entity.getVersion() ); final String writeAlias = alias.getWriteAlias(); if ( logger.isDebugEnabled() ) { logger.debug( "Indexing to alias {} with scope {} on edge {} with entity data {}", writeAlias, applicationScope, indexEdge, entity.getFieldMap().keySet() ); } //add app id for indexing container.addIndexRequest(new IndexOperation(writeAlias, applicationScope, indexEdge, entity,fieldsToIndex)); return this; }
@Override public List<PotentialAssignment> getValueSources( final ParameterSignature sig ) { final List<PotentialAssignment> result = new ArrayList<PotentialAssignment>(); final Entity entity = mock( Entity.class ); when( entity.getId() ).thenReturn( null ); when( entity.getVersion() ).thenReturn( null ); result.add( PotentialAssignment.forValue( "nullEntity", null ) ); result.add( PotentialAssignment.forValue( "nullIdsEntity", entity ) ); return result; } }
/** * Return an MvccEntityMock with valid inputs from the supplied entity * * @param entity * @return */ public static MvccEntity fromEntity(Entity entity) { final MvccEntity mvccEntity = mock(MvccEntity.class); when(mvccEntity.getId()).thenReturn(entity.getId()); when(mvccEntity.getVersion()).thenReturn(entity.getVersion()); when(mvccEntity.getEntity()).thenReturn(Optional.of(entity)); return mvccEntity; }
/** * Return an MvccEntityMock with valid inputs from the supplied entity * * @param entity * @return */ public static MvccEntity fromEntityStatus(Entity entity, MvccEntity.Status status) { final MvccEntity mvccEntity = mock(MvccEntity.class); when(mvccEntity.getId()).thenReturn(entity.getId()); when(mvccEntity.getVersion()).thenReturn(entity.getVersion()); when(mvccEntity.getEntity()).thenReturn(Optional.of(entity)); when(mvccEntity.getStatus()).thenReturn(status); return mvccEntity; }
@Test public void testNoConflict() throws Exception { final ApplicationScope collectionScope = mock( ApplicationScope.class ); when( collectionScope.getApplication() ) .thenReturn( new SimpleId( UUIDGenerator.newTimeUUID(), "organization" ) ); final Entity entity = generateEntity(); entity.setField(new StringField("name", "FOO", true)); entity.setField(new StringField("identifier", "BAR", true)); final MvccEntity mvccEntity = fromEntity( entity ); List<MvccLogEntry> logEntries = new ArrayList<MvccLogEntry>(); logEntries.add( new MvccLogEntryImpl( entity.getId(), UUIDGenerator.newTimeUUID(), Stage.ACTIVE, MvccLogEntry.State.COMPLETE )); logEntries.add( new MvccLogEntryImpl( entity.getId(), UUIDGenerator.newTimeUUID(), Stage.COMMITTED, MvccLogEntry.State.COMPLETE )); MvccLogEntrySerializationStrategy noConflictLog = mock( MvccLogEntrySerializationStrategy.class ); when( noConflictLog.load( collectionScope, entity.getId(), entity.getVersion(), 2) ) .thenReturn( logEntries ); UniqueValueSerializationStrategy uvstrat = mock( UniqueValueSerializationStrategy.class); // Run the stage WriteOptimisticVerify newStage = new WriteOptimisticVerify( noConflictLog ); newStage.call( new CollectionIoEvent<>( collectionScope, mvccEntity ) ); }
@Test public void write() { ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); Entity newEntity = new Entity( new SimpleId( "test" ) ); EntityCollectionManager manager = factory.createCollectionManager( context ); Observable<Entity> observable = manager.write( newEntity, null ); Entity returned = observable.toBlocking().lastOrDefault( null ); assertNotNull( "Returned has a uuid", returned.getId() ); assertNotNull( "Version exists", returned.getVersion() ); }
@Test public void writeAndLoad() { ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); Entity newEntity = new Entity( new SimpleId( "test" ) ); EntityCollectionManager manager = factory.createCollectionManager( context ); Observable<Entity> observable = manager.write( newEntity, null ); Entity createReturned = observable.toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", createReturned.getId() ); assertNotNull( "Version was assigned", createReturned.getVersion() ); Observable<Entity> loadObservable = manager.load( createReturned.getId() ); Entity loadReturned = loadObservable.toBlocking().lastOrDefault( null ); assertEquals( "Same value", createReturned, loadReturned ); }
@Test public void updateVersioning() { // create entity Entity origEntity = new Entity( new SimpleId( "testUpdate" ) ); origEntity.setField( new StringField( "testField", "value" ) ); ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); EntityCollectionManager manager = factory.createCollectionManager( context ); Entity returned = manager.write( origEntity, null ).toBlocking().lastOrDefault( null ); // note its version UUID oldVersion = returned.getVersion(); // partial update entity but with new entity that has version = null assertNotNull( "A version must be assigned", oldVersion ); // partial update entity but we don't have version number Entity updateEntity = new Entity( origEntity.getId() ); updateEntity.setField( new StringField( "addedField", "other value" ) ); manager.write( updateEntity, null ).toBlocking().lastOrDefault( null ); // get entity now, it must have a new version returned = manager.load( origEntity.getId() ).toBlocking().lastOrDefault( null ); UUID newVersion = returned.getVersion(); assertNotNull( "A new version must be assigned", newVersion ); // new Version should be > old version assertTrue( UUIDComparator.staticCompare( newVersion, oldVersion ) > 0 ); }
@Test public void testVersionLogWrite() { ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); final EntityCollectionManager manager = factory.createCollectionManager( context ); final Entity newEntity = new Entity( new SimpleId( "test" ) ); final Entity v1Created = manager.write( newEntity, null ).toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", v1Created.getId() ); assertNotNull( "Version was assigned", v1Created.getVersion() ); final UUID v1Version = v1Created.getVersion(); final VersionSet resultsV1 = manager.getLatestVersion( Arrays.asList( v1Created.getId() ) ).toBlocking().last(); final MvccLogEntry version1Log = resultsV1.getMaxVersion( v1Created.getId() ); assertEquals( v1Created.getId(), version1Log.getEntityId() ); assertEquals( v1Version, version1Log.getVersion() ); assertEquals( MvccLogEntry.State.COMPLETE, version1Log.getState() ); assertEquals( Stage.COMMITTED, version1Log.getStage() ); final Entity v2Created = manager.write( v1Created, null ).toBlocking().last(); final UUID v2Version = v2Created.getVersion(); assertTrue( "Newer version in v2", UUIDComparator.staticCompare( v2Version, v1Version ) > 0 ); final VersionSet resultsV2 = manager.getLatestVersion( Arrays.asList( v1Created.getId() ) ).toBlocking().last(); final MvccLogEntry version2Log = resultsV2.getMaxVersion( v1Created.getId() ); assertEquals( v1Created.getId(), version2Log.getEntityId() ); assertEquals( v2Version, version2Log.getVersion() ); assertEquals( MvccLogEntry.State.COMPLETE, version2Log.getState() ); assertEquals( Stage.COMMITTED, version2Log.getStage() ); }
UUID oldVersion = cpEntity.getVersion(); UUID newVersion = cpUpdated.getVersion();
@Test public void testGetVersion() { ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); final EntityCollectionManager manager = factory.createCollectionManager( context ); final Entity newEntity = new Entity( new SimpleId( "test" ) ); Entity created1 = manager.write( newEntity, null ).toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", created1.getId() ); assertNotNull( "Version was assigned", created1.getVersion() ); Entity secondEntity = new Entity( new SimpleId( "test" ) ); Entity created2 = manager.write( secondEntity, null ).toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", created2.getId() ); assertNotNull( "Version was assigned", created2.getVersion() ); VersionSet results = manager.getLatestVersion( Arrays.asList( created1.getId(), created2.getId() ) ).toBlocking().last(); final MvccLogEntry version1Log = results.getMaxVersion( created1.getId() ); assertEquals( created1.getId(), version1Log.getEntityId() ); assertEquals( created1.getVersion(), version1Log.getVersion() ); assertEquals( MvccLogEntry.State.COMPLETE, version1Log.getState() ); assertEquals( Stage.COMMITTED, version1Log.getStage() ); final MvccLogEntry version2Log = results.getMaxVersion( created2.getId() ); assertEquals( created2.getId(), version2Log.getEntityId() ); assertEquals( created2.getVersion(), version2Log.getVersion() ); assertEquals( MvccLogEntry.State.COMPLETE, version2Log.getState() ); assertEquals( Stage.COMMITTED, version2Log.getStage() ); }
@Test public void testVersionLogUpdate() { ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); final EntityCollectionManager manager = factory.createCollectionManager( context ); final Entity newEntity = new Entity( new SimpleId( "test" ) ); final Entity v1Created = manager.write( newEntity, null ).toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", v1Created.getId() ); assertNotNull( "Version was assigned", v1Created.getVersion() ); final UUID v1Version = v1Created.getVersion(); final VersionSet resultsV1 = manager.getLatestVersion( Arrays.asList( v1Created.getId() ) ).toBlocking().last(); final MvccLogEntry version1Log = resultsV1.getMaxVersion( v1Created.getId() ); assertEquals( v1Created.getId(), version1Log.getEntityId() ); assertEquals( v1Version, version1Log.getVersion() ); assertEquals( MvccLogEntry.State.COMPLETE, version1Log.getState() ); assertEquals( Stage.COMMITTED, version1Log.getStage() ); final Entity v2Created = manager.write( v1Created, null ).toBlocking().last(); final UUID v2Version = v2Created.getVersion(); assertEquals( "Same entityId", v1Created.getId(), v2Created.getId() ); assertTrue( "Newer version in v2", UUIDComparator.staticCompare( v2Version, v1Version ) > 0 ); final VersionSet resultsV2 = manager.getLatestVersion( Arrays.asList( v1Created.getId() ) ).toBlocking().last(); final MvccLogEntry version2Log = resultsV2.getMaxVersion( v1Created.getId() ); assertEquals( v2Created.getId(), version2Log.getEntityId() ); assertEquals( v2Version, version2Log.getVersion() ); assertEquals( MvccLogEntry.State.COMPLETE, version2Log.getState() ); assertEquals( Stage.COMMITTED, version2Log.getStage() ); }
@Test public void writeAndLoadScopeClosure() { ApplicationScope collectionScope1 = new ApplicationScopeImpl( new SimpleId( "organization" ) ); Entity newEntity = new Entity( new SimpleId( "test" ) ); EntityCollectionManager manager = factory.createCollectionManager( collectionScope1 ); Observable<Entity> observable = manager.write( newEntity, null ); Entity createReturned = observable.toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", createReturned.getId() ); assertNotNull( "Version was assigned", createReturned.getVersion() ); Observable<Entity> loadObservable = manager.load( createReturned.getId() ); Entity loadReturned = loadObservable.toBlocking().lastOrDefault( null ); assertEquals( "Same value", createReturned, loadReturned ); ApplicationScope collectionScope2 = new ApplicationScopeImpl( new SimpleId( "organization" ) ); //now make sure we can't load it from another scope, using the same org EntityCollectionManager manager2 = factory.createCollectionManager( collectionScope2 ); Entity loaded = manager2.load( createReturned.getId() ).toBlocking().lastOrDefault( null ); assertNull( "CollectionScope works correctly", loaded ); }
@Test public void writeAndGetField() { ApplicationScope collectionScope1 = new ApplicationScopeImpl( new SimpleId( "organization" ) ); Entity newEntity = new Entity( new SimpleId( "test" ) ); Field field = new StringField( "testField", "unique", true ); newEntity.setField( field ); EntityCollectionManager manager = factory.createCollectionManager( collectionScope1 ); Observable<Entity> observable = manager.write( newEntity, null ); Entity createReturned = observable.toBlocking().lastOrDefault( null ); assertNotNull( "Id was assigned", createReturned.getId() ); assertNotNull( "Version was assigned", createReturned.getVersion() ); Id id = manager.getIdField( newEntity.getId().getType(), field ).toBlocking().lastOrDefault( null ); assertNotNull( id ); assertEquals( newEntity.getId(), id ); Field fieldNull = new StringField( "testFieldNotThere", "uniquely", true ); id = manager.getIdField( newEntity.getId().getType(), fieldNull ).toBlocking().lastOrDefault( null ); assertNull( id ); }
@Test public void deleteVerification() throws Throwable { Id ownerId = new SimpleId( "owner" ); IndexEdge indexSCope = new IndexEdgeImpl( ownerId, "user", SearchEdge.NodeType.SOURCE, 10 ); final String middleName = "middleName" + UUIDUtils.newTimeUUID(); Map entityMap = new HashMap() {{ put( "username", "edanuff" ); put( "email", "ed@anuff.com" ); put( "middlename", middleName ); }}; Entity user = EntityIndexMapUtils.fromMap( entityMap ); EntityUtils.setId( user, new SimpleId( "edanuff" ) ); EntityUtils.setVersion( user, UUIDGenerator.newTimeUUID() ); EntityIndexBatch batch = entityIndex.createBatch(); batch.index( indexSCope, user ); indexProducer.put(batch.build()).subscribe();; entityIndex.refreshAsync().toBlocking().first(); final String query = "where username = 'edanuff'"; CandidateResults r = entityIndex.search( indexSCope, SearchTypes.fromTypes( "edanuff" ), query, 10, 0, false); assertEquals( user.getId(), r.get( 0 ).getId()); batch.deindex( indexSCope, user.getId(), user.getVersion() ); indexProducer.put(batch.build()).subscribe();; entityIndex.refreshAsync().toBlocking().first(); // EntityRef r = entityIndex.search( indexSCope, SearchTypes.fromTypes( "edanuff" ), query, 10, 0, false ); assertFalse( r.iterator().hasNext() ); }