You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by pa...@apache.org on 2016/12/03 11:05:57 UTC

[1/8] zest-java git commit: reindexer: rework test so it is more reliable

Repository: zest-java
Updated Branches:
  refs/heads/develop 31c28431c -> 7ac533885


reindexer: rework test so it is more reliable


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/85f2eb58
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/85f2eb58
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/85f2eb58

Branch: refs/heads/develop
Commit: 85f2eb5815cc3eb3adb6079740a5dd97d1226dd5
Parents: 31c2843
Author: Paul Merlin <pa...@apache.org>
Authored: Fri Dec 2 17:56:27 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Fri Dec 2 17:56:27 2016 +0100

----------------------------------------------------------------------
 .../zest/index/reindexer/ReindexerTest.java     | 87 +++++++-------------
 .../jdbm/JdbmEntityStoreService.properties      | 21 -----
 .../rdf/repository/rdf-indexing.properties      | 21 -----
 3 files changed, 29 insertions(+), 100 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/85f2eb58/extensions/reindexer/src/test/java/org/apache/zest/index/reindexer/ReindexerTest.java
----------------------------------------------------------------------
diff --git a/extensions/reindexer/src/test/java/org/apache/zest/index/reindexer/ReindexerTest.java b/extensions/reindexer/src/test/java/org/apache/zest/index/reindexer/ReindexerTest.java
index 9feead5..1786c83 100644
--- a/extensions/reindexer/src/test/java/org/apache/zest/index/reindexer/ReindexerTest.java
+++ b/extensions/reindexer/src/test/java/org/apache/zest/index/reindexer/ReindexerTest.java
@@ -19,11 +19,11 @@
  */
 package org.apache.zest.index.reindexer;
 
-import info.aduna.io.FileUtil;
 import java.io.File;
-import org.apache.tools.ant.util.FileUtils;
-import org.junit.AfterClass;
-import org.junit.Test;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.stream.Stream;
 import org.apache.zest.api.common.Visibility;
 import org.apache.zest.api.entity.EntityBuilder;
 import org.apache.zest.api.entity.EntityComposite;
@@ -40,21 +40,26 @@ import org.apache.zest.index.rdf.assembly.RdfNativeSesameStoreAssembler;
 import org.apache.zest.library.rdf.repository.NativeConfiguration;
 import org.apache.zest.test.AbstractZestTest;
 import org.apache.zest.test.EntityTestAssembler;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
 import static org.apache.zest.api.query.QueryExpressions.eq;
 import static org.apache.zest.api.query.QueryExpressions.templateFor;
+import static org.junit.Assert.assertEquals;
 
-@SuppressWarnings( "PublicInnerClass" )
 public class ReindexerTest
-        extends AbstractZestTest
+    extends AbstractZestTest
 {
+    private static final String ENTITIES_DIR = "zest-entities";
+    private static final String INDEX_DIR = "zest-index";
+
+    @Rule
+    public final TemporaryFolder tmpDir = new TemporaryFolder();
 
-    @SuppressWarnings( "unchecked" )
+    @Override
     public void assemble( ModuleAssembly module )
-            throws AssemblyException
+        throws AssemblyException
     {
         // JDBM EntityStore
         new JdbmEntityStoreAssembler().assemble( module );
@@ -70,30 +75,29 @@ public class ReindexerTest
         // Configuration
         ModuleAssembly config = module.layer().module( "config" );
         new EntityTestAssembler().assemble( config );
-        config.entities( JdbmConfiguration.class, NativeConfiguration.class, ReindexerConfiguration.class ).visibleIn( Visibility.layer );
+        config.entities( JdbmConfiguration.class, NativeConfiguration.class, ReindexerConfiguration.class )
+              .visibleIn( Visibility.layer );
+        config.forMixin( JdbmConfiguration.class ).declareDefaults()
+              .file().set( new File( tmpDir.getRoot(), ENTITIES_DIR ).getAbsolutePath() );
+        config.forMixin( NativeConfiguration.class ).declareDefaults()
+              .dataDirectory().set( new File( tmpDir.getRoot(), INDEX_DIR ).getAbsolutePath() );
 
         // Test entity
         module.entities( MyEntity.class );
-
     }
 
     private static final String TEST_NAME = "foo";
 
-    public static interface MyEntity
-            extends EntityComposite
+    public interface MyEntity extends EntityComposite
     {
 
         Property<String> name();
-
     }
 
     @Test
     public void createDataWipeIndexReindexAndAssertData()
-            throws UnitOfWorkCompletionException
+        throws UnitOfWorkCompletionException, IOException
     {
-        File rdfDir = new File( System.getProperty( "user.dir" ), "build/testdata/zest-index" ).getAbsoluteFile();
-        rdfDir.mkdirs();
-        assertThat( rdfDir.exists(), is(true) );
 
         // ----> Create data and wipe index
 
@@ -106,8 +110,11 @@ public class ReindexerTest
 
         uow.complete();
 
-        deleteIndexData(); // Wipe the index data on disk
-        rdfDir.mkdirs();
+        // Wipe the index data on disk
+        try( Stream<Path> files = Files.walk( new File( tmpDir.getRoot(), INDEX_DIR ).getAbsoluteFile().toPath() ) )
+        {
+            files.map( Path::toFile ).forEach( File::delete );
+        }
 
 
         // ----> Reindex and assert data
@@ -128,40 +135,4 @@ public class ReindexerTest
 
         uow.complete();
     }
-
-    @AfterClass
-    @SuppressWarnings( "AssignmentReplaceableWithOperatorAssignment" )
-    public static void afterClass()
-            throws Exception
-    {
-        boolean success = true;
-        success = deleteEntitiesData();
-        success = success & deleteIndexData();
-        if ( !success ) {
-            throw new Exception( "Could not delete test data" );
-        }
-
-    }
-
-    private static boolean deleteEntitiesData()
-    {
-        boolean success = true;
-        File esDir = new File( System.getProperty( "user.dir" ), "build/testdata/zest-entities" ).getAbsoluteFile();
-        if ( esDir.exists() ) {
-            success = FileUtil.deltree( esDir );
-        }
-        return success;
-    }
-
-    private static boolean deleteIndexData()
-    {
-        boolean success = true;
-        File rdfDir = new File( System.getProperty( "user.dir" ), "build/testdata/zest-index" ).getAbsoluteFile();
-        if ( rdfDir.exists() ) {
-            FileUtils.delete( rdfDir );
-            success = FileUtil.deltree( rdfDir );
-        }
-        return success;
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/85f2eb58/extensions/reindexer/src/test/resources/org/apache/zest/entitystore/jdbm/JdbmEntityStoreService.properties
----------------------------------------------------------------------
diff --git a/extensions/reindexer/src/test/resources/org/apache/zest/entitystore/jdbm/JdbmEntityStoreService.properties b/extensions/reindexer/src/test/resources/org/apache/zest/entitystore/jdbm/JdbmEntityStoreService.properties
deleted file mode 100644
index b87070c..0000000
--- a/extensions/reindexer/src/test/resources/org/apache/zest/entitystore/jdbm/JdbmEntityStoreService.properties
+++ /dev/null
@@ -1,21 +0,0 @@
-#
-#  Licensed to the Apache Software Foundation (ASF) under one
-#  or more contributor license agreements.  See the NOTICE file
-#  distributed with this work for additional information
-#  regarding copyright ownership.  The ASF licenses this file
-#  to you under the Apache License, Version 2.0 (the
-#  "License"); you may not use this file except in compliance
-#  with the License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-#
-#
-#
-
-file=build/testdata/zest-entities/zest-entities
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/zest-java/blob/85f2eb58/extensions/reindexer/src/test/resources/org/apache/zest/library/rdf/repository/rdf-indexing.properties
----------------------------------------------------------------------
diff --git a/extensions/reindexer/src/test/resources/org/apache/zest/library/rdf/repository/rdf-indexing.properties b/extensions/reindexer/src/test/resources/org/apache/zest/library/rdf/repository/rdf-indexing.properties
deleted file mode 100644
index 07681f9..0000000
--- a/extensions/reindexer/src/test/resources/org/apache/zest/library/rdf/repository/rdf-indexing.properties
+++ /dev/null
@@ -1,21 +0,0 @@
-#
-#  Licensed to the Apache Software Foundation (ASF) under one
-#  or more contributor license agreements.  See the NOTICE file
-#  distributed with this work for additional information
-#  regarding copyright ownership.  The ASF licenses this file
-#  to you under the Apache License, Version 2.0 (the
-#  "License"); you may not use this file except in compliance
-#  with the License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-#
-#
-#
-
-dataDirectory=build/testdata/zest-index
\ No newline at end of file


[2/8] zest-java git commit: redis-entitystore: remove usage of deprecated methods

Posted by pa...@apache.org.
redis-entitystore: remove usage of deprecated methods


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/c574cd02
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/c574cd02
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/c574cd02

Branch: refs/heads/develop
Commit: c574cd02e113dc0ac023d0d684b96035e9669c7b
Parents: 85f2eb5
Author: Paul Merlin <pa...@apache.org>
Authored: Fri Dec 2 17:58:28 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Fri Dec 2 17:58:28 2016 +0100

----------------------------------------------------------------------
 .../entitystore/redis/RedisMapEntityStoreMixin.java   | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/c574cd02/extensions/entitystore-redis/src/main/java/org/apache/zest/entitystore/redis/RedisMapEntityStoreMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-redis/src/main/java/org/apache/zest/entitystore/redis/RedisMapEntityStoreMixin.java b/extensions/entitystore-redis/src/main/java/org/apache/zest/entitystore/redis/RedisMapEntityStoreMixin.java
index dc43d97..4113e39 100644
--- a/extensions/entitystore-redis/src/main/java/org/apache/zest/entitystore/redis/RedisMapEntityStoreMixin.java
+++ b/extensions/entitystore-redis/src/main/java/org/apache/zest/entitystore/redis/RedisMapEntityStoreMixin.java
@@ -89,8 +89,7 @@ public class RedisMapEntityStoreMixin
     public Reader get( EntityReference entityReference )
         throws EntityStoreException
     {
-        Jedis jedis = pool.getResource();
-        try
+        try( Jedis jedis = pool.getResource() )
         {
             String jsonState = jedis.get( entityReference.identity().toString() );
             if( notFound( jsonState ) )
@@ -99,18 +98,13 @@ public class RedisMapEntityStoreMixin
             }
             return new StringReader( jsonState );
         }
-        finally
-        {
-            pool.returnResource( jedis );
-        }
     }
 
     @Override
     public void applyChanges( MapChanges changes )
         throws IOException
     {
-        final Jedis jedis = pool.getResource();
-        try
+        try( Jedis jedis = pool.getResource() )
         {
             changes.visitMap( new MapChanger()
             {
@@ -167,10 +161,6 @@ public class RedisMapEntityStoreMixin
                 }
             } );
         }
-        finally
-        {
-            pool.returnResource( jedis );
-        }
     }
 
     @Override


[4/8] zest-java git commit: jclouds-entitystore: properly isolate test

Posted by pa...@apache.org.
jclouds-entitystore: properly isolate test


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/d8a76c05
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/d8a76c05
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/d8a76c05

Branch: refs/heads/develop
Commit: d8a76c052b33092a2f6ecffef747164baebef825
Parents: d833c4f
Author: Paul Merlin <pa...@apache.org>
Authored: Fri Dec 2 18:00:40 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Fri Dec 2 18:00:40 2016 +0100

----------------------------------------------------------------------
 .../jclouds/JCloudsFilesystemTest.java          | 32 ++++++++------------
 1 file changed, 13 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/d8a76c05/extensions/entitystore-jclouds/src/test/java/org/apache/zest/entitystore/jclouds/JCloudsFilesystemTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-jclouds/src/test/java/org/apache/zest/entitystore/jclouds/JCloudsFilesystemTest.java b/extensions/entitystore-jclouds/src/test/java/org/apache/zest/entitystore/jclouds/JCloudsFilesystemTest.java
index 584cb4a..6bbff6f 100644
--- a/extensions/entitystore-jclouds/src/test/java/org/apache/zest/entitystore/jclouds/JCloudsFilesystemTest.java
+++ b/extensions/entitystore-jclouds/src/test/java/org/apache/zest/entitystore/jclouds/JCloudsFilesystemTest.java
@@ -20,43 +20,37 @@
 
 package org.apache.zest.entitystore.jclouds;
 
-import java.util.HashMap;
-import java.util.Map;
-import org.apache.zest.api.time.SystemTime;
-import org.apache.zest.entitystore.jclouds.assembly.JCloudsEntityStoreAssembler;
-import org.jclouds.filesystem.reference.FilesystemConstants;
-import org.junit.AfterClass;
+import java.util.Collections;
 import org.apache.zest.api.common.Visibility;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
+import org.apache.zest.entitystore.jclouds.assembly.JCloudsEntityStoreAssembler;
 import org.apache.zest.test.EntityTestAssembler;
 import org.apache.zest.test.entity.AbstractEntityStoreTest;
 import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+import org.jclouds.filesystem.reference.FilesystemConstants;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
 
 public class JCloudsFilesystemTest
-        extends AbstractEntityStoreTest
+    extends AbstractEntityStoreTest
 {
+    @Rule
+    public TemporaryFolder tmpDir = new TemporaryFolder();
 
     @Override
     public void assemble( ModuleAssembly module )
-            throws AssemblyException
+        throws AssemblyException
     {
         super.assemble( module );
         ModuleAssembly config = module.layer().module( "config" );
         new EntityTestAssembler().assemble( config );
         new OrgJsonValueSerializationAssembler().assemble( module );
         new JCloudsEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
-        JCloudsMapEntityStoreConfiguration defaults = config.forMixin( JCloudsMapEntityStoreConfiguration.class ).declareDefaults();
+        JCloudsMapEntityStoreConfiguration defaults = config.forMixin( JCloudsMapEntityStoreConfiguration.class )
+                                                            .declareDefaults();
         defaults.provider().set( "filesystem" );
-        Map<String, String> props = new HashMap<String, String>();
-        props.put( FilesystemConstants.PROPERTY_BASEDIR, "build/tmp/" + getClass().getPackage().getName() + "/es-jclouds-" + SystemTime.now().toEpochMilli() );
-        defaults.properties().set( props );
+        defaults.properties().set( Collections.singletonMap( FilesystemConstants.PROPERTY_BASEDIR,
+                                                             tmpDir.getRoot().getAbsolutePath() ) );
     }
-
-    @AfterClass
-    public static void filesystemCleanup()
-    {
-        // TODO recursively delete "build/tmp/" + getClass().getPackage().getName()
-    }
-
 }


[8/8] zest-java git commit: mongo-entitystore: remove usage of deprecated mongo client APIs

Posted by pa...@apache.org.
mongo-entitystore: remove usage of deprecated mongo client APIs


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/7ac53388
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/7ac53388
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/7ac53388

Branch: refs/heads/develop
Commit: 7ac5338854990d488d7165d2c2c21fa70b91035b
Parents: d3ae3cd
Author: Paul Merlin <pa...@apache.org>
Authored: Sat Dec 3 12:03:51 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Sat Dec 3 12:03:51 2016 +0100

----------------------------------------------------------------------
 dependencies.gradle                             |   2 +-
 .../entitystore/mongodb/MongoAccessors.java     |   6 +-
 .../mongodb/MongoEntityStoreConfiguration.java  |  46 +++++---
 .../mongodb/MongoMapEntityStoreMixin.java       | 104 ++++++++++---------
 .../mongodb/EmbedMongoMapEntityStoreTest.java   |   2 +-
 .../mongodb/MongoMapEntityStoreTest.java        |   2 +-
 .../MongoMapEntityStoreWithCacheTest.java       |   2 +-
 7 files changed, 94 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/dependencies.gradle
----------------------------------------------------------------------
diff --git a/dependencies.gradle b/dependencies.gradle
index b14783e..a56c160 100644
--- a/dependencies.gradle
+++ b/dependencies.gradle
@@ -63,7 +63,7 @@ def jettyVersion = '9.2.17.v20160517' // 9.3.x Tests fail!
 def leveldbVersion = '0.9'
 def leveldbJniVersion = '1.8'
 def liquibaseVersion = '3.5.3'
-def mongodbVersion = '3.3.0'
+def mongodbVersion = '3.4.0'
 def restletVersion = '2.3.7'
 def rdfVersion = '2.7.16' // 2.8.x change query results!! 4.x exists
 def riakVersion = '2.1.0'

http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java
index a043329..4dea5e8 100644
--- a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java
+++ b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java
@@ -19,16 +19,14 @@
  */
 package org.apache.zest.entitystore.mongodb;
 
-import com.mongodb.DB;
 import com.mongodb.MongoClient;
+import com.mongodb.client.MongoDatabase;
 
 public interface MongoAccessors
 {
-
     MongoClient mongoInstanceUsed();
 
-    DB dbInstanceUsed();
+    MongoDatabase dbInstanceUsed();
 
     String collectionUsed();
-
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java
index 835095f..ee1d32e 100644
--- a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java
+++ b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java
@@ -57,21 +57,39 @@ public interface MongoEntityStoreConfiguration
 
     enum WriteConcern
     {
-
-        /** Exceptions are raised for network issues, but not server errors */
-        NORMAL,
-        /** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */
-        SAFE,
-        /** Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation */
-        MAJORITY,
-        /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk*/
-        FSYNC_SAFE,
-        /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk*/
-        JOURNAL_SAFE,
-        /** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/
-        REPLICAS_SAFE;
+        /**
+         *  Write operations that use this write concern will wait for acknowledgement,
+         *  using the default write concern configured on the server.
+         *  This is the default value.
+         */
+        ACKNOWLEDGED,
+        /**
+         * Write operations that use this write concern will wait for acknowledgement from a single member.
+         */
+        W1,
+        /**
+         * Write operations that use this write concern will wait for acknowledgement from two members.
+         */
+        W2,
+        /**
+         * Write operations that use this write concern will wait for acknowledgement from three members.
+         */
+        W3,
+        /**
+         * Write operations that use this write concern will return as soon as the message is written to the socket.
+         * Exceptions are raised for network issues, but not server errors.
+         */
+        UNACKNOWLEDGED,
+        /**
+         * Write operations wait for the server to group commit to the journal file on disk.
+         */
+        JOURNALED,
+        /**
+         * Exceptions are raised for network issues, and server errors;
+         * waits on a majority of servers for the write operation.
+         */
+        MAJORITY;
 
     }
-
 }
 // END SNIPPET: config
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java
index 93f5af0..9b988f5 100644
--- a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java
+++ b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java
@@ -20,14 +20,15 @@
 package org.apache.zest.entitystore.mongodb;
 
 import com.mongodb.BasicDBObject;
-import com.mongodb.DB;
-import com.mongodb.DBCollection;
-import com.mongodb.DBCursor;
-import com.mongodb.DBObject;
 import com.mongodb.MongoClient;
+import com.mongodb.MongoClientOptions;
 import com.mongodb.MongoCredential;
 import com.mongodb.ServerAddress;
 import com.mongodb.WriteConcern;
+import com.mongodb.client.FindIterable;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoCursor;
+import com.mongodb.client.MongoDatabase;
 import com.mongodb.util.JSON;
 import java.io.IOException;
 import java.io.Reader;
@@ -37,6 +38,7 @@ import java.io.Writer;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import org.apache.zest.api.configuration.Configuration;
 import org.apache.zest.api.entity.EntityDescriptor;
@@ -50,6 +52,10 @@ import org.apache.zest.io.Sender;
 import org.apache.zest.spi.entitystore.EntityNotFoundException;
 import org.apache.zest.spi.entitystore.EntityStoreException;
 import org.apache.zest.spi.entitystore.helpers.MapEntityStore;
+import org.bson.Document;
+import org.bson.conversions.Bson;
+
+import static com.mongodb.client.model.Filters.eq;
 
 /**
  * MongoDB implementation of MapEntityStore.
@@ -70,7 +76,7 @@ public class MongoMapEntityStoreMixin
     private String username;
     private char[] password;
     private MongoClient mongo;
-    private DB db;
+    private MongoDatabase db;
 
     @Override
     public void activateService()
@@ -79,20 +85,21 @@ public class MongoMapEntityStoreMixin
         loadConfiguration();
 
         // Create Mongo driver and open the database
+        MongoClientOptions options = MongoClientOptions.builder().writeConcern( writeConcern ).build();
         if( username.isEmpty() )
         {
-            mongo = new MongoClient( serverAddresses );
+            mongo = new MongoClient( serverAddresses, options );
         }
         else
         {
             MongoCredential credential = MongoCredential.createMongoCRCredential( username, databaseName, password );
-            mongo = new MongoClient( serverAddresses, Arrays.asList( credential ) );
+            mongo = new MongoClient( serverAddresses, Collections.singletonList( credential ), options );
         }
-        db = mongo.getDB( databaseName );
+        db = mongo.getDatabase( databaseName );
 
         // Create index if needed
-        DBCollection entities = db.getCollection( collectionName );
-        if( entities.getIndexInfo().isEmpty() )
+        MongoCollection<Document> entities = db.getCollection( collectionName );
+        if( !entities.listIndexes().iterator().hasNext() )
         {
             entities.createIndex( new BasicDBObject( IDENTITY_COLUMN, 1 ) );
         }
@@ -139,24 +146,27 @@ public class MongoMapEntityStoreMixin
         // If write concern not configured, set it to normal
         switch( config.writeConcern().get() )
         {
-            case FSYNC_SAFE:
-                writeConcern = WriteConcern.FSYNC_SAFE;
+            case W1:
+                writeConcern = WriteConcern.W1;
                 break;
-            case JOURNAL_SAFE:
-                writeConcern = WriteConcern.JOURNAL_SAFE;
+            case W2:
+                writeConcern = WriteConcern.W2;
                 break;
-            case MAJORITY:
-                writeConcern = WriteConcern.MAJORITY;
+            case W3:
+                writeConcern = WriteConcern.W3;
                 break;
-            case REPLICAS_SAFE:
-                writeConcern = WriteConcern.REPLICAS_SAFE;
+            case UNACKNOWLEDGED:
+                writeConcern = WriteConcern.UNACKNOWLEDGED;
                 break;
-            case SAFE:
-                writeConcern = WriteConcern.SAFE;
+            case JOURNALED:
+                writeConcern = WriteConcern.JOURNALED;
+                break;
+            case MAJORITY:
+                writeConcern = WriteConcern.MAJORITY;
                 break;
-            case NORMAL:
+            case ACKNOWLEDGED:
             default:
-                writeConcern = WriteConcern.NORMAL;
+                writeConcern = WriteConcern.ACKNOWLEDGED;
         }
 
         // Username and password are defaulted to empty strings
@@ -186,7 +196,7 @@ public class MongoMapEntityStoreMixin
     }
 
     @Override
-    public DB dbInstanceUsed()
+    public MongoDatabase dbInstanceUsed()
     {
         return db;
     }
@@ -201,13 +211,14 @@ public class MongoMapEntityStoreMixin
     public Reader get( EntityReference entityReference )
         throws EntityStoreException
     {
-        DBObject entity = db.getCollection( collectionName ).findOne( byIdentity( entityReference ) );
-        if( entity == null )
+        MongoCursor<Document> cursor = db.getCollection( collectionName )
+                                         .find( byIdentity( entityReference ) )
+                                         .limit( 1 ).iterator();
+        if( !cursor.hasNext() )
         {
             throw new EntityNotFoundException( entityReference );
         }
-        DBObject bsonState = (DBObject) entity.get( STATE_COLUMN );
-
+        Document bsonState = (Document) cursor.next().get( STATE_COLUMN );
         String jsonState = JSON.serialize( bsonState );
         return new StringReader( jsonState );
     }
@@ -216,7 +227,7 @@ public class MongoMapEntityStoreMixin
     public void applyChanges( MapChanges changes )
         throws IOException
     {
-        final DBCollection entities = db.getCollection( collectionName );
+        final MongoCollection<Document> entities = db.getCollection( collectionName );
 
         changes.visitMap( new MapChanger()
         {
@@ -231,14 +242,11 @@ public class MongoMapEntityStoreMixin
                         throws IOException
                     {
                         super.close();
-
-                        String jsonState = toString();
-                        DBObject bsonState = (DBObject) JSON.parse( jsonState );
-
-                        BasicDBObject entity = new BasicDBObject();
+                        Document bsonState = Document.parse( toString() );
+                        Document entity = new Document();
                         entity.put( IDENTITY_COLUMN, ref.identity().toString() );
                         entity.put( STATE_COLUMN, bsonState );
-                        entities.insert( entity, writeConcern );
+                        entities.insertOne( entity );
                     }
                 };
             }
@@ -254,13 +262,11 @@ public class MongoMapEntityStoreMixin
                         throws IOException
                     {
                         super.close();
-
-                        DBObject bsonState = (DBObject) JSON.parse( toString() );
-
-                        BasicDBObject entity = new BasicDBObject();
+                        Document bsonState = Document.parse( toString() );
+                        Document entity = new Document();
                         entity.put( IDENTITY_COLUMN, ref.identity().toString() );
                         entity.put( STATE_COLUMN, bsonState );
-                        entities.update( byIdentity( ref ), entity, false, false, writeConcern );
+                        entities.replaceOne( byIdentity( ref ), entity );
                     }
                 };
             }
@@ -269,12 +275,15 @@ public class MongoMapEntityStoreMixin
             public void removeEntity( EntityReference ref, EntityDescriptor entityDescriptor )
                 throws EntityNotFoundException
             {
-                DBObject entity = entities.findOne( byIdentity( ref ) );
-                if( entity == null )
+                Bson byIdFilter = byIdentity( ref );
+                MongoCursor<Document> cursor = db.getCollection( collectionName )
+                                                 .find( byIdFilter )
+                                                 .limit( 1 ).iterator();
+                if( !cursor.hasNext() )
                 {
                     throw new EntityNotFoundException( ref );
                 }
-                entities.remove( entity, writeConcern );
+                entities.deleteOne( byIdFilter );
             }
         } );
     }
@@ -296,11 +305,10 @@ public class MongoMapEntityStoreMixin
                         Receiver<? super Reader, ReceiverThrowableType> receiver )
                         throws ReceiverThrowableType, IOException
                     {
-                        DBCursor cursor = db.getCollection( collectionName ).find();
-                        while( cursor.hasNext() )
+                        FindIterable<Document> cursor = db.getCollection( collectionName ).find();
+                        for( Document eachEntity : cursor )
                         {
-                            DBObject eachEntity = cursor.next();
-                            DBObject bsonState = (DBObject) eachEntity.get( STATE_COLUMN );
+                            Document bsonState = (Document) eachEntity.get( STATE_COLUMN );
                             String jsonState = JSON.serialize( bsonState );
                             receiver.receive( new StringReader( jsonState ) );
                         }
@@ -310,8 +318,8 @@ public class MongoMapEntityStoreMixin
         };
     }
 
-    private DBObject byIdentity( EntityReference entityReference )
+    private Bson byIdentity( EntityReference entityReference )
     {
-        return new BasicDBObject( IDENTITY_COLUMN, entityReference.identity().toString() );
+        return eq( IDENTITY_COLUMN, entityReference.identity().toString() );
     }
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
index b528b54..b5dc6e9 100644
--- a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
+++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
@@ -83,7 +83,7 @@ public class EmbedMongoMapEntityStoreTest extends AbstractEntityStoreTest
 
         MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class )
                                                           .declareDefaults();
-        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE );
+        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.MAJORITY );
         mongoConfig.database().set( "zest:test" );
         mongoConfig.collection().set( testName.getMethodName() );
         mongoConfig.hostname().set( "localhost" );

http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java
index b7cf7b1..65bb810 100644
--- a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java
+++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java
@@ -62,7 +62,7 @@ public class MongoMapEntityStoreTest
         // END SNIPPET: assembly
 
         MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class ).declareDefaults();
-        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE );
+        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.MAJORITY );
         mongoConfig.database().set( "zest:test" );
         mongoConfig.collection().set( "zest:test:entities" );
         // START SNIPPET: assembly

http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java
index b7355a4..a308803 100644
--- a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java
+++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java
@@ -58,7 +58,7 @@ public class MongoMapEntityStoreWithCacheTest
         new MongoDBEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
 
         MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class ).declareDefaults();
-        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE );
+        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.MAJORITY );
         mongoConfig.database().set( "zest:test" );
         mongoConfig.collection().set( "zest:test:entities" );
     }


[3/8] zest-java git commit: preferences-entitystore: properly isolate test

Posted by pa...@apache.org.
preferences-entitystore: properly isolate test


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/d833c4f1
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/d833c4f1
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/d833c4f1

Branch: refs/heads/develop
Commit: d833c4f1b826e66566d72b8f0bd004cbbd03b49b
Parents: c574cd0
Author: Paul Merlin <pa...@apache.org>
Authored: Fri Dec 2 17:59:20 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Fri Dec 2 17:59:20 2016 +0100

----------------------------------------------------------------------
 .../zest/entitystore/PreferencesEntityStoreTest.java      | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/d833c4f1/extensions/entitystore-preferences/src/test/java/org/apache/zest/entitystore/PreferencesEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-preferences/src/test/java/org/apache/zest/entitystore/PreferencesEntityStoreTest.java b/extensions/entitystore-preferences/src/test/java/org/apache/zest/entitystore/PreferencesEntityStoreTest.java
index 369c1ed..c177381 100644
--- a/extensions/entitystore-preferences/src/test/java/org/apache/zest/entitystore/PreferencesEntityStoreTest.java
+++ b/extensions/entitystore-preferences/src/test/java/org/apache/zest/entitystore/PreferencesEntityStoreTest.java
@@ -26,10 +26,14 @@ import org.apache.zest.entitystore.prefs.PreferencesEntityStoreInfo;
 import org.apache.zest.entitystore.prefs.PreferencesEntityStoreService;
 import org.apache.zest.test.entity.AbstractEntityStoreTest;
 import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
 
 public class PreferencesEntityStoreTest
     extends AbstractEntityStoreTest
 {
+    @Rule
+    public TemporaryFolder tmpDir = new TemporaryFolder();
 
     @Override
     public void assemble( ModuleAssembly module )
@@ -40,7 +44,11 @@ public class PreferencesEntityStoreTest
         super.assemble( module );
         ClassLoader cl = Thread.currentThread().getContextClassLoader();
         Thread.currentThread().setContextClassLoader( null );
-        PreferencesEntityStoreInfo metaInfo = new PreferencesEntityStoreInfo( Preferences.userNodeForPackage( getClass() ) );
+        Preferences node = Preferences.userNodeForPackage( getClass() )
+                                      .node( "integtest" )
+                                      .node( tmpDir.getRoot().getName() )
+                                      .node( "PreferencesEntityStoreTest" );
+        PreferencesEntityStoreInfo metaInfo = new PreferencesEntityStoreInfo( node );
         Thread.currentThread().setContextClassLoader( cl );
         module.services( PreferencesEntityStoreService.class ).setMetaInfo( metaInfo ).instantiateOnStartup();
         new OrgJsonValueSerializationAssembler().assemble( module );


[5/8] zest-java git commit: file-entitystore: simplify and fix possible race condition

Posted by pa...@apache.org.
file-entitystore: simplify and fix possible race condition

Simplify using nio, remove quite a bunch of code.

Write temp files in a temporary directory to prevent a possible race
with entityStates() and backup() that list the slices directories.


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/5fee8a2b
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/5fee8a2b
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/5fee8a2b

Branch: refs/heads/develop
Commit: 5fee8a2b8ea06fa75b9d1b798ce9638eae10a1cc
Parents: d8a76c0
Author: Paul Merlin <pa...@apache.org>
Authored: Fri Dec 2 18:29:18 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Fri Dec 2 18:33:12 2016 +0100

----------------------------------------------------------------------
 .../entitystore/file/FileEntityStoreMixin.java  | 204 +++++--------------
 1 file changed, 55 insertions(+), 149 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/5fee8a2b/extensions/entitystore-file/src/main/java/org/apache/zest/entitystore/file/FileEntityStoreMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-file/src/main/java/org/apache/zest/entitystore/file/FileEntityStoreMixin.java b/extensions/entitystore-file/src/main/java/org/apache/zest/entitystore/file/FileEntityStoreMixin.java
index 14eccad..1ab1c53 100644
--- a/extensions/entitystore-file/src/main/java/org/apache/zest/entitystore/file/FileEntityStoreMixin.java
+++ b/extensions/entitystore-file/src/main/java/org/apache/zest/entitystore/file/FileEntityStoreMixin.java
@@ -19,29 +19,22 @@
  */
 package org.apache.zest.entitystore.file;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import org.apache.zest.api.common.Optional;
 import org.apache.zest.api.configuration.Configuration;
 import org.apache.zest.api.entity.EntityDescriptor;
 import org.apache.zest.api.entity.EntityReference;
 import org.apache.zest.api.injection.scope.Service;
 import org.apache.zest.api.injection.scope.This;
-import org.apache.zest.io.Files;
 import org.apache.zest.io.Input;
 import org.apache.zest.io.Output;
 import org.apache.zest.io.Receiver;
@@ -53,6 +46,8 @@ import org.apache.zest.spi.entitystore.EntityNotFoundException;
 import org.apache.zest.spi.entitystore.EntityStoreException;
 import org.apache.zest.spi.entitystore.helpers.MapEntityStore;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+
 /**
  * FileEntityStore implementation of MapEntityStore.
  */
@@ -66,19 +61,22 @@ public class FileEntityStoreMixin
     @This
     private Configuration<FileEntityStoreConfiguration> config;
 
+    private String storeId;
     private File dataDirectory;
+    private File tempDirectory;
     private int slices;
 
     @Override
     public void initialize()
         throws Exception
     {
+        config.refresh();
+        storeId = config.get().identity().get().toString();
         String pathName = config.get().directory().get();
         if( pathName == null )
         {
             if( fileConfiguration != null )
             {
-                String storeId = config.get().identity().get().toString();
                 pathName = new File( fileConfiguration.dataDirectory(), storeId ).getAbsolutePath();
             }
             else
@@ -89,10 +87,14 @@ public class FileEntityStoreMixin
         dataDirectory = new File( pathName ).getAbsoluteFile();
         if( !dataDirectory.exists() )
         {
-            if( !dataDirectory.mkdirs() )
-            {
-                throw new IOException( "Unable to create directory " + dataDirectory );
-            }
+            Files.createDirectories( dataDirectory.toPath() );
+        }
+        tempDirectory = fileConfiguration != null
+                        ? new File( fileConfiguration.temporaryDirectory(), storeId )
+                        : new File( new File( System.getProperty( "java.io.tmpdir" ) ), storeId );
+        if( !tempDirectory.exists() )
+        {
+            Files.createDirectories( tempDirectory.toPath() );
         }
         File slicesFile = new File( dataDirectory, "slices" );
         if( slicesFile.exists() )
@@ -117,50 +119,13 @@ public class FileEntityStoreMixin
     private void writeIntegerToFile( File file, int value )
         throws IOException
     {
-        FileWriter fw = null;
-        BufferedWriter bw = null;
-        try
-        {
-            fw = new FileWriter( file );
-            bw = new BufferedWriter( fw );
-            bw.write( "" + value );
-            bw.flush();
-        }
-        finally
-        {
-            if( bw != null )
-            {
-                bw.close();
-            }
-            if( fw != null )
-            {
-                fw.close();
-            }
-        }
+        Files.write( file.toPath(), String.valueOf( value ).getBytes( UTF_8 ) );
     }
 
     private int readIntegerInFile( File file )
         throws IOException
     {
-        FileReader fis = null;
-        BufferedReader br = null;
-        try
-        {
-            fis = new FileReader( file );
-            br = new BufferedReader( fis );
-            return Integer.parseInt( br.readLine() );
-        }
-        finally
-        {
-            if( br != null )
-            {
-                br.close();
-            }
-            if( fis != null )
-            {
-                fis.close();
-            }
-        }
+        return Integer.parseInt( new String( Files.readAllBytes( file.toPath() ), UTF_8 ) );
     }
 
     @Override
@@ -176,10 +141,11 @@ public class FileEntityStoreMixin
                 throw new EntityNotFoundException( entityReference );
             }
 
-            byte[] serializedState = fetch( f );
-            return new StringReader( new String( serializedState, "UTF-8" ) );
+            String serializedState = fetch( f );
+            return new StringReader( serializedState );
         }
-        catch( FileNotFoundException e ){
+        catch( FileNotFoundException e )
+        {
             // Can't happen, but it does happen.
             throw new EntityNotFoundException( entityReference );
         }
@@ -189,19 +155,6 @@ public class FileEntityStoreMixin
         }
     }
 
-    private byte[] readDataFromStream( BufferedInputStream in, byte[] buf )
-        throws IOException
-    {
-        int size = in.read( buf );
-        ByteArrayOutputStream baos = new ByteArrayOutputStream( 2000 );
-        while( size > 0 )
-        {
-            baos.write( buf, 0, size );
-            size = in.read( buf );
-        }
-        return baos.toByteArray();
-    }
-
     @Override
     public void applyChanges( MapChanges changes )
         throws IOException
@@ -221,13 +174,13 @@ public class FileEntityStoreMixin
                             throws IOException
                         {
                             super.close();
-                            byte[] stateArray = this.toString().getBytes( "UTF-8" );
+                            String state = this.toString();
                             File dataFile = getDataFile( ref );
                             if( dataFile.exists() )
                             {
                                 throw new EntityAlreadyExistsException( ref );
                             }
-                            store( dataFile, stateArray );
+                            store( dataFile, state );
                         }
                     };
                 }
@@ -243,9 +196,9 @@ public class FileEntityStoreMixin
                             throws IOException
                         {
                             super.close();
-                            byte[] stateArray = this.toString().getBytes( "UTF-8" );
+                            String state = this.toString();
                             File dataFile = getDataFile( ref );
-                            store( dataFile, stateArray );
+                            store( dataFile, state );
                         }
                     };
                 }
@@ -296,8 +249,7 @@ public class FileEntityStoreMixin
                         {
                             for( File file : sliceDirectory.listFiles() )
                             {
-                                byte[] stateArray = fetch( file );
-                                receiver.receive( new String( stateArray, "UTF-8" ) );
+                                receiver.receive( fetch( file ) );
                             }
                         }
                     }
@@ -323,8 +275,7 @@ public class FileEntityStoreMixin
                     {
                         String id = item.substring( "{\"reference\":\"".length() );
                         id = id.substring( 0, id.indexOf( '"' ) );
-                        byte[] stateArray = item.getBytes( "UTF-8" );
-                        store( getDataFile( id ), stateArray );
+                        store( getDataFile( id ), item );
                     }
                 } );
             }
@@ -350,8 +301,8 @@ public class FileEntityStoreMixin
                         {
                             for( File file : sliceDirectory.listFiles() )
                             {
-                                byte[] serializedState = fetch( file );
-                                receiver.receive( new StringReader( new String( serializedState, "UTF-8" ) ) );
+                                String state = fetch( file );
+                                receiver.receive( new StringReader( state ) );
                             }
                         }
                     }
@@ -402,7 +353,6 @@ public class FileEntityStoreMixin
                 b.append( '~' );
                 b.append( toHex( value ) );
             }
-
         }
         return b.toString();
     }
@@ -418,89 +368,45 @@ public class FileEntityStoreMixin
         return getDataFile( ref.identity().toString() );
     }
 
-    private byte[] fetch( File dataFile )
-        throws IOException
+    private String uncheckedFetch( File dataFile )
     {
-        byte[] buf = new byte[1000];
-        BufferedInputStream in = null;
-        FileInputStream fis = null;
         try
         {
-            fis = new FileInputStream( dataFile );
-            in = new BufferedInputStream( fis );
-            return readDataFromStream( in, buf );
+            return fetch( dataFile );
         }
-        finally
+        catch( IOException e )
         {
-            if( in != null )
-            {
-                try
-                {
-                    in.close();
-                }
-                catch( IOException e )
-                {
-                    // Ignore ??
-                }
-            }
-            if( fis != null )
-            {
-                try
-                {
-                    fis.close();
-                }
-                catch( IOException e )
-                {
-                    // ignore??
-                }
-            }
+            throw new EntityStoreException( e );
         }
     }
 
-    private void store( File dataFile, byte[] stateArray )
-        throws IOException
+    private void uncheckedStore( File dataFile, String state )
     {
-        FileOutputStream fos = null;
-        BufferedOutputStream bos = null;
-
-        // Write to tempfile first
-        File tempFile = Files.createTemporayFileOf( dataFile );
-        tempFile.deleteOnExit();
-
         try
         {
-            fos = new FileOutputStream( tempFile, false );
-            bos = new BufferedOutputStream( fos );
-            bos.write( stateArray );
+            store( dataFile, state );
         }
-        finally
+        catch( IOException e )
         {
-            if( bos != null )
-            {
-                try
-                {
-                    bos.close();
-                }
-                catch( IOException e )
-                {
-                    // ignore??
-                }
-            }
-            if( fos != null )
-            {
-                try
-                {
-                    fos.close();
-                }
-                catch( IOException e )
-                {
-                    // ignore??
-                }
-            }
+            throw new EntityStoreException( e );
         }
+    }
+
+    private String fetch( File dataFile )
+        throws IOException
+    {
+        return new String( Files.readAllBytes( dataFile.toPath() ), UTF_8 );
+    }
+
+    private void store( File dataFile, String state )
+        throws IOException
+    {
+        // Write to temporary file first
+        Path tempFile = Files.createTempFile( tempDirectory.toPath(), storeId, "write" );
+        tempFile.toFile().deleteOnExit();
+        Files.write( tempFile, state.getBytes( UTF_8 ) );
 
         // Replace old file
-        dataFile.delete();
-        tempFile.renameTo( dataFile );
+        Files.move( tempFile, dataFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
     }
 }
\ No newline at end of file


[6/8] zest-java git commit: free-port-finder: remove range in which one port was seen busy on CI

Posted by pa...@apache.org.
free-port-finder: remove range in which one port was seen busy on CI


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/de4731b7
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/de4731b7
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/de4731b7

Branch: refs/heads/develop
Commit: de4731b7fc2d4a74236640ae049be612fa6492cf
Parents: 5fee8a2
Author: Paul Merlin <pa...@apache.org>
Authored: Fri Dec 2 18:37:15 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Fri Dec 2 18:37:15 2016 +0100

----------------------------------------------------------------------
 .../src/main/java/org/apache/zest/test/util/FreePortFinder.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/de4731b7/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java b/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java
index 3791ea1..1ff33f8 100644
--- a/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java
+++ b/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java
@@ -59,7 +59,7 @@ public class FreePortFinder
         new Range( 43442, 44122 ),
         new Range( 46337, 46997 ),
         new Range( 35358, 36000 ),
-        new Range( 36866, 37474 ),
+        // new Range( 36866, 37474 ), // 37334 seen busy
         new Range( 38204, 38799 ),
         new Range( 33657, 34248 ),
         new Range( 30261, 30831 ),


[7/8] zest-java git commit: mongo-entitystore: test using embedded mongodb process

Posted by pa...@apache.org.
mongo-entitystore: test using embedded mongodb process


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/d3ae3cdc
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/d3ae3cdc
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/d3ae3cdc

Branch: refs/heads/develop
Commit: d3ae3cdce0a49b5a25b1758ae7e8682e51680969
Parents: de4731b
Author: Paul Merlin <pa...@apache.org>
Authored: Sat Dec 3 11:30:54 2016 +0100
Committer: Paul Merlin <pa...@apache.org>
Committed: Sat Dec 3 11:30:54 2016 +0100

----------------------------------------------------------------------
 dependencies.gradle                             |  2 +
 extensions/entitystore-mongodb/build.gradle     |  1 +
 .../mongodb/EmbedMongoMapEntityStoreTest.java   | 92 ++++++++++++++++++++
 3 files changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/d3ae3cdc/dependencies.gradle
----------------------------------------------------------------------
diff --git a/dependencies.gradle b/dependencies.gradle
index b3c0aa3..b14783e 100644
--- a/dependencies.gradle
+++ b/dependencies.gradle
@@ -171,6 +171,7 @@ def antVersion = '1.9.7'
 def awaitilityVersion = '2.0.0'
 def dnsJavaVersion = '2.1.7'
 def easyMockVersion = '3.4'
+def embedMongoVersion = '1.50.5'
 def h2Version = '1.4.193'
 def hamcrestVersion = '1.3'
 def junitVersion = '4.12'
@@ -186,6 +187,7 @@ dependencies.libraries << [
   derby             : "org.apache.derby:derby:$derbyVersion",
   dnsjava           : "dnsjava:dnsjava:$dnsJavaVersion",
   easymock          : "org.easymock:easymock:$easyMockVersion",
+  embed_mongo       : "de.flapdoodle.embed:de.flapdoodle.embed.mongo:$embedMongoVersion",
   h2                : "com.h2database:h2:$h2Version",
   hamcrest          : [ "org.hamcrest:hamcrest-core:$hamcrestVersion",
                         "org.hamcrest:hamcrest-library:$hamcrestVersion" ],

http://git-wip-us.apache.org/repos/asf/zest-java/blob/d3ae3cdc/extensions/entitystore-mongodb/build.gradle
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/build.gradle b/extensions/entitystore-mongodb/build.gradle
index d7ae1ec..55f77f6 100644
--- a/extensions/entitystore-mongodb/build.gradle
+++ b/extensions/entitystore-mongodb/build.gradle
@@ -31,6 +31,7 @@ dependencies {
 
   testCompile zest.core.testsupport
   testCompile zest.extension( 'valueserialization-orgjson' )
+  testCompile libraries.embed_mongo
 
   testRuntime libraries.logback
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/d3ae3cdc/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
new file mode 100644
index 0000000..b528b54
--- /dev/null
+++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java
@@ -0,0 +1,92 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.zest.entitystore.mongodb;
+
+import de.flapdoodle.embed.mongo.MongodExecutable;
+import de.flapdoodle.embed.mongo.MongodStarter;
+import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
+import de.flapdoodle.embed.mongo.config.Net;
+import de.flapdoodle.embed.mongo.distribution.Version;
+import de.flapdoodle.embed.process.runtime.Network;
+import java.io.IOException;
+import org.apache.zest.api.common.Visibility;
+import org.apache.zest.bootstrap.AssemblyException;
+import org.apache.zest.bootstrap.ModuleAssembly;
+import org.apache.zest.entitystore.mongodb.assembly.MongoDBEntityStoreAssembler;
+import org.apache.zest.test.EntityTestAssembler;
+import org.apache.zest.test.entity.AbstractEntityStoreTest;
+import org.apache.zest.test.util.FreePortFinder;
+import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.rules.TestName;
+
+public class EmbedMongoMapEntityStoreTest extends AbstractEntityStoreTest
+{
+    private static final MongodStarter MONGO_STARTER = MongodStarter.getDefaultInstance();
+
+    @Rule
+    public TestName testName = new TestName();
+    private static int port;
+    private static MongodExecutable mongod;
+
+
+    @BeforeClass
+    public static void startEmbedMongo() throws IOException
+    {
+        port = FreePortFinder.findFreePortOnLoopback();
+        mongod = MONGO_STARTER.prepare( new MongodConfigBuilder()
+                                            .version( Version.Main.PRODUCTION )
+                                            .net( new Net( port, Network.localhostIsIPv6() ) )
+                                            .build() );
+        mongod.start();
+    }
+
+    @AfterClass
+    public static void stopEmbedMongo()
+    {
+        if( mongod != null )
+        {
+            mongod.stop();
+        }
+    }
+
+    @Override
+    public void assemble( ModuleAssembly module )
+        throws AssemblyException
+    {
+        super.assemble( module );
+
+        ModuleAssembly config = module.layer().module( "config" );
+        new EntityTestAssembler().assemble( config );
+
+        new OrgJsonValueSerializationAssembler().assemble( module );
+
+        new MongoDBEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module );
+
+
+        MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class )
+                                                          .declareDefaults();
+        mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE );
+        mongoConfig.database().set( "zest:test" );
+        mongoConfig.collection().set( testName.getMethodName() );
+        mongoConfig.hostname().set( "localhost" );
+        mongoConfig.port().set( port );
+    }
+}