Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 614CE200BD2 for ; Sat, 3 Dec 2016 12:20:13 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 5D2C2160B28; Sat, 3 Dec 2016 11:20:13 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B3E98160B0E for ; Sat, 3 Dec 2016 12:20:11 +0100 (CET) Received: (qmail 60883 invoked by uid 500); 3 Dec 2016 11:20:11 -0000 Mailing-List: contact commits-help@zest.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zest.apache.org Delivered-To: mailing list commits@zest.apache.org Received: (qmail 60874 invoked by uid 99); 3 Dec 2016 11:20:10 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Dec 2016 11:20:10 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A6C46E02E4; Sat, 3 Dec 2016 11:20:10 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulmerlin@apache.org To: commits@zest.apache.org Date: Sat, 03 Dec 2016 11:20:10 -0000 Message-Id: <193b665ea33a4f918304b35fe9dfdecf@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] zest-java git commit: entitystores: replace usage of core/io with streams archived-at: Sat, 03 Dec 2016 11:20:13 -0000 Repository: zest-java Updated Branches: refs/heads/replace-io-by-streams [created] eb4e31a97 http://git-wip-us.apache.org/repos/asf/zest-java/blob/eb4e31a9/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java b/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java index a52b526..fa66da2 100644 --- a/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java +++ b/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java @@ -34,8 +34,13 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.apache.zest.api.common.Optional; import org.apache.zest.api.common.QualifiedName; import org.apache.zest.api.entity.EntityDescriptor; @@ -57,11 +62,6 @@ import org.apache.zest.entitystore.sql.internal.DatabaseSQLService; import org.apache.zest.entitystore.sql.internal.DatabaseSQLService.EntityValueResult; import org.apache.zest.entitystore.sql.internal.SQLEntityState; import org.apache.zest.entitystore.sql.internal.SQLEntityState.DefaultSQLEntityState; -import org.apache.zest.functional.Visitor; -import org.apache.zest.io.Input; -import org.apache.zest.io.Output; -import org.apache.zest.io.Receiver; -import org.apache.zest.io.Sender; import org.apache.zest.library.sql.common.SQLUtil; import org.apache.zest.spi.ZestSPI; import org.apache.zest.spi.entity.EntityState; @@ -272,7 +272,8 @@ public class SQLEntityStoreMixin EntityDescriptor entityDescriptor ) { - return new DefaultSQLEntityState( new DefaultEntityState( unitOfWork.currentTime(), entityRef, entityDescriptor ) ); + return new DefaultSQLEntityState( + new DefaultEntityState( unitOfWork.currentTime(), entityRef, entityDescriptor ) ); } @Override @@ -282,77 +283,56 @@ public class SQLEntityStoreMixin } @Override - public Input entityStates( final ModuleDescriptor module ) + public Stream entityStates( final ModuleDescriptor module ) { - return new Input() + try { - @Override - public void transferTo( Output output ) - throws EntityStoreException, ReceiverThrowableType - { - output.receiveFrom( new Sender() + Connection connection = database.getConnection(); + PreparedStatement ps = database.prepareGetAllEntitiesStatement( connection ); + database.populateGetAllEntitiesStatement( ps ); + ResultSet rs = ps.executeQuery(); + return StreamSupport.stream( + new Spliterators.AbstractSpliterator( Long.MAX_VALUE, Spliterator.ORDERED ) { @Override - public void sendTo( final Receiver receiver ) - throws RecThrowableType, EntityStoreException + public boolean tryAdvance( final Consumer action ) { - queryAllEntities( module, visited -> { - try - { - receiver.receive( visited ); - } - catch( Throwable receiverThrowableType ) - { - throw new SQLException( receiverThrowableType ); - } + try + { + if( !rs.next() ) { return false; } + EntityState entityState = readEntityState( module, + database.getEntityValue( rs ).getReader() ); + action.accept( entityState ); return true; - } ); + } + catch( SQLException ex ) + { + SQLUtil.closeQuietly( rs, ex ); + SQLUtil.closeQuietly( ps, ex ); + SQLUtil.closeQuietly( connection, ex ); + throw new EntityStoreException( ex ); + } } - } ); - } - }; - } - - private void queryAllEntities( ModuleDescriptor module, EntityStatesVisitor entityStatesVisitor ) - { - Connection connection = null; - PreparedStatement ps = null; - ResultSet rs = null; - try - { - connection = database.getConnection(); - ps = database.prepareGetAllEntitiesStatement( connection ); - database.populateGetAllEntitiesStatement( ps ); - rs = ps.executeQuery(); - while( rs.next() ) - { - DefaultEntityState entityState = readEntityState( module, database.getEntityValue( rs ).getReader() ); - if( !entityStatesVisitor.visit( entityState ) ) + }, + false + ).onClose( + () -> { - return; + SQLUtil.closeQuietly( rs ); + SQLUtil.closeQuietly( ps ); + SQLUtil.closeQuietly( connection ); } - } + ); } catch( SQLException ex ) { throw new EntityStoreException( ex ); } - finally - { - SQLUtil.closeQuietly( rs ); - SQLUtil.closeQuietly( ps ); - SQLUtil.closeQuietly( connection ); - } - } - - private interface EntityStatesVisitor - extends Visitor - { } protected Identity newUnitOfWorkId() { - return identityGenerator.generate(EntityStore.class); + return identityGenerator.generate( EntityStore.class ); } protected DefaultEntityState readEntityState( ModuleDescriptor module, Reader entityState ) @@ -364,7 +344,7 @@ public class SQLEntityStoreMixin final EntityStatus[] status = { EntityStatus.LOADED }; String version = jsonObject.getString( JSONKeys.VERSION ); - Instant modified = Instant.ofEpochMilli(jsonObject.getLong( JSONKeys.MODIFIED )); + Instant modified = Instant.ofEpochMilli( jsonObject.getLong( JSONKeys.MODIFIED ) ); String identity = jsonObject.getString( JSONKeys.IDENTITY ); // Check if version is correct @@ -398,102 +378,117 @@ public class SQLEntityStoreMixin Map properties = new HashMap<>(); JSONObject props = jsonObject.getJSONObject( JSONKeys.PROPERTIES ); - entityDescriptor.state().properties().forEach( propertyDescriptor -> { - Object jsonValue; - try + entityDescriptor.state().properties().forEach( + propertyDescriptor -> { - jsonValue = props.get( propertyDescriptor.qualifiedName().name() ); - if( JSONObject.NULL.equals( jsonValue ) ) + Object jsonValue; + try { - properties.put( propertyDescriptor.qualifiedName(), null ); + jsonValue = props.get( + propertyDescriptor.qualifiedName().name() ); + if( JSONObject.NULL.equals( jsonValue ) ) + { + properties.put( propertyDescriptor.qualifiedName(), null ); + } + else + { + Object value = valueSerialization.deserialize( module, + propertyDescriptor.valueType(), + jsonValue.toString() ); + properties.put( propertyDescriptor.qualifiedName(), value ); + } } - else + catch( JSONException e ) { - Object value = valueSerialization.deserialize( module, propertyDescriptor.valueType(), jsonValue - .toString() ); - properties.put( propertyDescriptor.qualifiedName(), value ); + // Value not found, default it + Object initialValue = propertyDescriptor.initialValue( module ); + properties.put( propertyDescriptor.qualifiedName(), initialValue ); + status[ 0 ] = EntityStatus.UPDATED; } } - catch( JSONException e ) - { - // Value not found, default it - Object initialValue = propertyDescriptor.initialValue( module ); - properties.put( propertyDescriptor.qualifiedName(), initialValue ); - status[ 0 ] = EntityStatus.UPDATED; - } - } ); + ); Map associations = new HashMap<>(); JSONObject assocs = jsonObject.getJSONObject( JSONKeys.ASSOCIATIONS ); - entityDescriptor.state().associations().forEach( associationType -> { - try + entityDescriptor.state().associations().forEach( + associationType -> { - Object jsonValue = assocs.get( associationType.qualifiedName().name() ); - EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference( - (String) jsonValue ); - associations.put( associationType.qualifiedName(), value ); - } - catch( JSONException e ) - { - // Association not found, default it to null - associations.put( associationType.qualifiedName(), null ); - status[ 0 ] = EntityStatus.UPDATED; + try + { + Object jsonValue = assocs.get( associationType.qualifiedName().name() ); + EntityReference value = jsonValue == JSONObject.NULL + ? null + : EntityReference.parseEntityReference( (String) jsonValue ); + associations.put( associationType.qualifiedName(), value ); + } + catch( JSONException e ) + { + // Association not found, default it to null + associations.put( associationType.qualifiedName(), null ); + status[ 0 ] = EntityStatus.UPDATED; + } } - } ); + ); JSONObject manyAssocs = jsonObject.getJSONObject( JSONKeys.MANY_ASSOCIATIONS ); Map> manyAssociations = new HashMap<>(); - entityDescriptor.state().manyAssociations().forEach( manyAssociationType -> { - List references = new ArrayList<>(); - try + entityDescriptor.state().manyAssociations().forEach( + manyAssociationType -> { - JSONArray jsonValues = manyAssocs.getJSONArray( manyAssociationType.qualifiedName().name() ); - for( int i = 0; i < jsonValues.length(); i++ ) + List references = new ArrayList<>(); + try + { + JSONArray jsonValues = manyAssocs.getJSONArray( manyAssociationType.qualifiedName().name() ); + for( int i = 0; i < jsonValues.length(); i++ ) + { + Object jsonValue = jsonValues.getString( i ); + EntityReference value = jsonValue == JSONObject.NULL + ? null + : EntityReference.parseEntityReference( (String) jsonValue ); + references.add( value ); + } + manyAssociations.put( manyAssociationType.qualifiedName(), references ); + } + catch( JSONException e ) { - Object jsonValue = jsonValues.getString( i ); - EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference( - (String) jsonValue ); - references.add( value ); + // ManyAssociation not found, default to empty one + manyAssociations.put( manyAssociationType.qualifiedName(), references ); } - manyAssociations.put( manyAssociationType.qualifiedName(), references ); - } - catch( JSONException e ) - { - // ManyAssociation not found, default to empty one - manyAssociations.put( manyAssociationType.qualifiedName(), references ); - } - } ); + } ); JSONObject namedAssocs = jsonObject.has( JSONKeys.NAMED_ASSOCIATIONS ) ? jsonObject.getJSONObject( JSONKeys.NAMED_ASSOCIATIONS ) : new JSONObject(); Map> namedAssociations = new HashMap<>(); - entityDescriptor.state().namedAssociations().forEach( namedAssociationType -> { - Map references = new LinkedHashMap<>(); - try + entityDescriptor.state().namedAssociations().forEach( + namedAssociationType -> { - JSONObject jsonValues = namedAssocs.getJSONObject( namedAssociationType.qualifiedName().name() ); - JSONArray names = jsonValues.names(); - if( names != null ) + Map references = new LinkedHashMap<>(); + try { - for( int idx = 0; idx < names.length(); idx++ ) + JSONObject jsonValues = namedAssocs.getJSONObject( namedAssociationType.qualifiedName().name() ); + JSONArray names = jsonValues.names(); + if( names != null ) { - String name = names.getString( idx ); - String jsonValue = jsonValues.getString( name ); - references.put( name, EntityReference.parseEntityReference( jsonValue ) ); + for( int idx = 0; idx < names.length(); idx++ ) + { + String name = names.getString( idx ); + String jsonValue = jsonValues.getString( name ); + references.put( name, EntityReference.parseEntityReference( jsonValue ) ); + } } + namedAssociations.put( namedAssociationType.qualifiedName(), references ); } - namedAssociations.put( namedAssociationType.qualifiedName(), references ); - } - catch( JSONException e ) - { - // NamedAssociation not found, default to empty one - namedAssociations.put( namedAssociationType.qualifiedName(), references ); - } - } ); + catch( JSONException e ) + { + // NamedAssociation not found, default to empty one + namedAssociations.put( namedAssociationType.qualifiedName(), references ); + } + } ); return new DefaultEntityState( version, modified, - EntityReference.parseEntityReference( identity ), status[ 0 ], entityDescriptor, + EntityReference.parseEntityReference( identity ), status[ 0 ], + entityDescriptor, properties, associations, manyAssociations, namedAssociations ); } catch( JSONException e ) @@ -507,7 +502,7 @@ public class SQLEntityStoreMixin throws IOException { JSONObject jsonObject; - try (Reader reader = getValue( EntityReference.parseEntityReference( id ) ).getReader()) + try( Reader reader = getValue( EntityReference.parseEntityReference( id ) ).getReader() ) { jsonObject = new JSONObject( new JSONTokener( reader ) ); } @@ -553,58 +548,65 @@ public class SQLEntityStoreMixin try { JSONWriter json = new JSONWriter( writer ); - JSONWriter properties = json.object(). - key( JSONKeys.IDENTITY ).value( state.entityReference().identity().toString() ). - key( JSONKeys.APPLICATION_VERSION ).value( application.version() ). - key( JSONKeys.TYPE ).value( state.entityDescriptor().types().findFirst().get().getName() ). - key( JSONKeys.VERSION ).value( version ). - key( JSONKeys.MODIFIED ).value( state.lastModified().toEpochMilli() ). - key( JSONKeys.PROPERTIES ).object(); - - state.entityDescriptor().state().properties().forEach( persistentProperty -> { - try + JSONWriter properties = json.object() + .key( JSONKeys.IDENTITY ) + .value( state.entityReference().identity().toString() ) + .key( JSONKeys.APPLICATION_VERSION ) + .value( application.version() ) + .key( JSONKeys.TYPE ) + .value( state.entityDescriptor().types().findFirst().get().getName() ) + .key( JSONKeys.VERSION ) + .value( version ) + .key( JSONKeys.MODIFIED ) + .value( state.lastModified().toEpochMilli() ) + .key( JSONKeys.PROPERTIES ) + .object(); + + state.entityDescriptor().state().properties().forEach( + persistentProperty -> { - Object value = state.properties().get( persistentProperty.qualifiedName() ); - json.key( persistentProperty.qualifiedName().name() ); - if( value == null || ValueType.isPrimitiveValue( value ) ) + try { - json.value( value ); - } - else - { - String serialized = valueSerialization.serialize( value ); - if( serialized.startsWith( "{" ) ) + Object value = state.properties().get( persistentProperty.qualifiedName() ); + json.key( persistentProperty.qualifiedName().name() ); + if( value == null || ValueType.isPrimitiveValue( value ) ) { - json.value( new JSONObject( serialized ) ); - } - else if( serialized.startsWith( "[" ) ) - { - json.value( new JSONArray( serialized ) ); + json.value( value ); } else { - json.value( serialized ); + String serialized = valueSerialization.serialize( value ); + if( serialized.startsWith( "{" ) ) + { + json.value( new JSONObject( serialized ) ); + } + else if( serialized.startsWith( "[" ) ) + { + json.value( new JSONArray( serialized ) ); + } + else + { + json.value( serialized ); + } } } - } - catch( JSONException e ) - { - throw new EntityStoreException( "Could not store EntityState", e ); - } - } ); + catch( JSONException e ) + { + throw new EntityStoreException( + "Could not store EntityState", e ); + } + } ); JSONWriter associations = properties.endObject().key( JSONKeys.ASSOCIATIONS ).object(); - for( Map.Entry stateNameEntityReferenceEntry : state.associations() - .entrySet() ) + for( Map.Entry stateNameEntityRefEntry : state.associations().entrySet() ) { - EntityReference value = stateNameEntityReferenceEntry.getValue(); - associations.key( stateNameEntityReferenceEntry.getKey().name() ). - value( value != null ? value.identity().toString() : null ); + EntityReference value = stateNameEntityRefEntry.getValue(); + associations.key( stateNameEntityRefEntry.getKey().name() ) + .value( value != null ? value.identity().toString() : null ); } JSONWriter manyAssociations = associations.endObject().key( JSONKeys.MANY_ASSOCIATIONS ).object(); - for( Map.Entry> stateNameListEntry : state.manyAssociations() - .entrySet() ) + for( Map.Entry> stateNameListEntry : state.manyAssociations().entrySet() ) { JSONWriter assocs = manyAssociations.key( stateNameListEntry.getKey().name() ).array(); for( EntityReference entityReference : stateNameListEntry.getValue() ) @@ -615,8 +617,7 @@ public class SQLEntityStoreMixin } JSONWriter namedAssociations = manyAssociations.endObject().key( JSONKeys.NAMED_ASSOCIATIONS ).object(); - for( Map.Entry> stateNameMapEntry : state.namedAssociations() - .entrySet() ) + for( Map.Entry> stateNameMapEntry : state.namedAssociations().entrySet() ) { JSONWriter assocs = namedAssociations.key( stateNameMapEntry.getKey().name() ).object(); for( Map.Entry entry : stateNameMapEntry.getValue().entrySet() ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/eb4e31a9/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java ---------------------------------------------------------------------- diff --git a/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java b/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java index 1da1f7b..7f8cb54 100644 --- a/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java +++ b/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java @@ -19,26 +19,18 @@ */ package org.apache.zest.migration; -import java.io.BufferedReader; import java.io.IOException; -import java.io.StringReader; -import org.apache.zest.api.identity.Identity; -import org.apache.zest.bootstrap.unitofwork.DefaultUnitOfWorkAssembler; -import org.hamcrest.CoreMatchers; -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.Test; +import java.util.List; +import java.util.stream.Stream; import org.apache.zest.api.activation.ActivationException; +import org.apache.zest.api.identity.Identity; import org.apache.zest.api.service.importer.NewObjectImporter; import org.apache.zest.api.unitofwork.UnitOfWork; import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; import org.apache.zest.bootstrap.SingletonAssembler; -import org.apache.zest.io.Input; -import org.apache.zest.io.Output; -import org.apache.zest.io.Receiver; -import org.apache.zest.io.Sender; +import org.apache.zest.bootstrap.unitofwork.DefaultUnitOfWorkAssembler; import org.apache.zest.migration.assembly.EntityMigrationOperation; import org.apache.zest.migration.assembly.MigrationBuilder; import org.apache.zest.migration.assembly.MigrationOperation; @@ -47,7 +39,12 @@ import org.apache.zest.spi.entitystore.helpers.JSONKeys; import org.apache.zest.spi.entitystore.helpers.StateStore; import org.apache.zest.test.AbstractZestTest; import org.apache.zest.test.EntityTestAssembler; +import org.hamcrest.CoreMatchers; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Test; +import static java.util.stream.Collectors.toList; import static org.junit.Assert.assertThat; /** @@ -106,7 +103,7 @@ public class MigrationTest { Identity id; // Set up version 1 - StringInputOutput data_v1 = new StringInputOutput(); + List data_v1; { SingletonAssembler v1 = new SingletonAssembler() { @@ -130,11 +127,14 @@ public class MigrationTest BackupRestore backupRestore = v1.module() .findService( BackupRestore.class ) .get(); - backupRestore.backup().transferTo( data_v1 ); + try( Stream backup = backupRestore.backup() ) + { + data_v1 = backup.collect( toList() ); + } } // Set up version 1.1 - StringInputOutput data_v1_1 = new StringInputOutput(); + List data_v1_1; { SingletonAssembler v1_1 = new SingletonAssembler() { @@ -148,7 +148,7 @@ public class MigrationTest }; BackupRestore testData = v1_1.module().findService( BackupRestore.class ).get(); - data_v1.transferTo( testData.restore() ); + testData.restore( data_v1.stream() ); UnitOfWork uow = v1_1.module().unitOfWorkFactory().newUnitOfWork(); TestEntity1_1 entity = uow.get( TestEntity1_1.class, id ); @@ -157,7 +157,10 @@ public class MigrationTest assertThat( "Association has been renamed", entity.newFooAssoc().get(), CoreMatchers.equalTo( entity ) ); uow.complete(); - testData.backup().transferTo( data_v1_1 ); + try( Stream backup = testData.backup() ) + { + data_v1_1 = backup.collect( toList() ); + } } // Set up version 2.0 @@ -177,7 +180,7 @@ public class MigrationTest // Test migration from 1.0 -> 2.0 { - data_v1.transferTo( testData.restore() ); + testData.restore( data_v1.stream() ); UnitOfWork uow = v2_0.module().unitOfWorkFactory().newUnitOfWork(); TestEntity2_0 entity = uow.get( TestEntity2_0.class, id ); assertThat( "Property has been created", entity.bar().get(), CoreMatchers.equalTo( "Some value" ) ); @@ -202,11 +205,11 @@ public class MigrationTest }; BackupRestore testData = v3_0.module().findService( BackupRestore.class ).get(); - data_v1_1.transferTo( testData.restore() ); + testData.restore( data_v1_1.stream() ); // Test migration from 1.0 -> 3.0 { - data_v1.transferTo( testData.restore() ); + testData.restore( data_v1.stream() ); UnitOfWork uow = v3_0.module().unitOfWorkFactory().newUnitOfWork(); org.apache.zest.migration.moved.TestEntity2_0 entity = uow.get( org.apache.zest.migration.moved.TestEntity2_0.class, id ); uow.complete(); @@ -258,51 +261,4 @@ public class MigrationTest System.out.println( msg ); } } - - private static class StringInputOutput - implements Output, Input - { - final StringBuilder builder = new StringBuilder(); - - @Override - public void receiveFrom( Sender sender ) - throws IOException, SenderThrowableType - { - sender.sendTo((Receiver) item -> builder.append( item ).append( "\n" )); - } - - @Override - public void transferTo( Output output ) - throws IOException, ReceiverThrowableType - { - output.receiveFrom( new Sender() - { - @Override - public void sendTo( Receiver receiver ) - throws ReceiverThrowableType, IOException - { - BufferedReader reader = new BufferedReader( new StringReader( builder.toString() ) ); - String line; - try - { - while( ( line = reader.readLine() ) != null ) - { - receiver.receive( line ); - } - } - finally - { - reader.close(); - } - } - } ); - } - - @Override - public String toString() - { - return builder.toString(); - } - } - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/eb4e31a9/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java ---------------------------------------------------------------------- diff --git a/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java b/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java index c3a3af4..f11dc77 100644 --- a/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java +++ b/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java @@ -21,7 +21,7 @@ package org.apache.zest.index.reindexer.internal; import java.util.ArrayList; -import org.apache.zest.api.common.QualifiedName; +import java.util.stream.Stream; import org.apache.zest.api.configuration.Configuration; import org.apache.zest.api.identity.HasIdentity; import org.apache.zest.api.injection.scope.Service; @@ -31,9 +31,6 @@ import org.apache.zest.api.service.ServiceReference; import org.apache.zest.api.structure.ModuleDescriptor; import org.apache.zest.index.reindexer.Reindexer; import org.apache.zest.index.reindexer.ReindexerConfiguration; -import org.apache.zest.io.Output; -import org.apache.zest.io.Receiver; -import org.apache.zest.io.Sender; import org.apache.zest.spi.entity.EntityState; import org.apache.zest.spi.entitystore.EntityStore; import org.apache.zest.spi.entitystore.StateChangeListener; @@ -67,52 +64,43 @@ public class ReindexerMixin { loadValue = 50; } - new ReindexerOutput( loadValue ).reindex( store ); + ReindexerHelper helper = new ReindexerHelper( loadValue ); + helper.reindex( store ); } - private class ReindexerOutput - implements Output, Receiver + private class ReindexerHelper { private int count; private int loadValue; private ArrayList states; - public ReindexerOutput( Integer loadValue ) + private ReindexerHelper( int loadValue ) { this.loadValue = loadValue; states = new ArrayList<>(); } - public void reindex( EntityStore store ) + private void reindex( EntityStore store ) { - - store.entityStates( module ).transferTo( this ); - reindexState(); - } - - @Override - public void receiveFrom( Sender sender ) - throws RuntimeException, SenderThrowableType - { - sender.sendTo( this ); - reindexState(); - } - - @Override - public void receive( EntityState item ) - throws RuntimeException - { - count++; - item.setPropertyValue( HasIdentity.IDENTITY_STATE_NAME, item.entityReference().identity() ); - states.add( item ); - - if( states.size() >= loadValue ) + try( Stream entityStates = store.entityStates( module ) ) { - reindexState(); + entityStates + .forEach( entityState -> + { + count++; + entityState.setPropertyValue( HasIdentity.IDENTITY_STATE_NAME, + entityState.entityReference().identity() ); + states.add( entityState ); + if( states.size() >= loadValue ) + { + reindexState(); + } + } ); } + reindexState(); } - public void reindexState() + private void reindexState() { for( ServiceReference listener : listeners ) { http://git-wip-us.apache.org/repos/asf/zest-java/blob/eb4e31a9/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java ---------------------------------------------------------------------- diff --git a/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java b/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java index ae75fb4..1fccdde 100644 --- a/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java +++ b/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java @@ -20,9 +20,8 @@ package org.apache.zest.library.logging; -import java.util.function.Function; +import java.util.stream.Stream; import org.apache.zest.api.identity.Identity; -import org.junit.Test; import org.apache.zest.api.injection.scope.This; import org.apache.zest.api.mixin.Mixins; import org.apache.zest.api.service.ServiceComposite; @@ -31,8 +30,6 @@ import org.apache.zest.api.unitofwork.UnitOfWork; import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; -import org.apache.zest.io.Outputs; -import org.apache.zest.io.Transforms; import org.apache.zest.library.logging.debug.Debug; import org.apache.zest.library.logging.debug.DebugConcern; import org.apache.zest.library.logging.debug.records.ServiceDebugRecordEntity; @@ -42,6 +39,7 @@ import org.apache.zest.spi.entity.EntityState; import org.apache.zest.spi.entitystore.EntityStore; import org.apache.zest.test.AbstractZestTest; import org.apache.zest.test.EntityTestAssembler; +import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -76,19 +74,18 @@ public class DebuggingTest assertEquals( message, "Hello!" ); EntityStore es = serviceFinder.findService( EntityStore.class ).get(); final Identity[] result = new Identity[1]; - es.entityStates( module ).transferTo( Transforms.map( new Function() - { - public EntityState apply( EntityState entityState ) - { - if( ServiceDebugRecordEntity.class.getName() - .equals( entityState.entityDescriptor().types().findFirst().get().getName() ) ) - { - result[0] = entityState.entityReference().identity(); - } - - return entityState; - } - }, Outputs.noop() )); + try( Stream entityStates = es.entityStates( module ) ) + { + entityStates + .forEach( entityState -> + { + if( ServiceDebugRecordEntity.class.getName().equals( + entityState.entityDescriptor().types().findFirst().get().getName() ) ) + { + result[ 0 ] = entityState.entityReference().identity(); + } + } ); + } ServiceDebugRecordEntity debugEntry = uow.get( ServiceDebugRecordEntity.class, result[ 0 ] ); String mess = debugEntry.message().get(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/eb4e31a9/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java ---------------------------------------------------------------------- diff --git a/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java b/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java index 12a2bd4..e87c9c6 100644 --- a/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java +++ b/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java @@ -26,43 +26,94 @@ import java.sql.Statement; public class SQLUtil { - public static void closeQuietly( ResultSet resultSet ) { - if ( resultSet != null ) { - try { + closeQuietly( resultSet, null ); + } + + public static void closeQuietly( ResultSet resultSet, Throwable originalException ) + { + if( resultSet != null ) + { + try + { resultSet.close(); - } catch ( SQLException ignored ) { + } + catch( SQLException ignored ) + { + if( originalException != null ) + { + originalException.addSuppressed( ignored ); + } } } } public static void closeQuietly( Statement select ) { - if ( select != null ) { - try { + closeQuietly( select, null ); + } + + public static void closeQuietly( Statement select, Throwable originalException ) + { + if( select != null ) + { + try + { select.close(); - } catch ( SQLException ignored ) { + } + catch( SQLException ignored ) + { + if( originalException != null ) + { + originalException.addSuppressed( ignored ); + } } } } public static void closeQuietly( Connection connection ) { - if ( connection != null ) { - try { + closeQuietly( connection, null ); + } + + public static void closeQuietly( Connection connection, Throwable originalException ) + { + if( connection != null ) + { + try + { connection.close(); - } catch ( SQLException ignored ) { + } + catch( SQLException ignored ) + { + if( originalException != null ) + { + originalException.addSuppressed( ignored ); + } } } } public static void rollbackQuietly( Connection connection ) { - if ( connection != null ) { - try { + rollbackQuietly( connection, null ); + } + + public static void rollbackQuietly( Connection connection, Throwable originalException ) + { + if( connection != null ) + { + try + { connection.rollback(); - } catch ( SQLException ignored ) { + } + catch( SQLException ignored ) + { + if( originalException != null ) + { + originalException.addSuppressed( ignored ); + } } } } @@ -70,5 +121,4 @@ public class SQLUtil private SQLUtil() { } - }