sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jso...@apache.org
Subject svn commit: r1807695 - in /sis/branches/JDK8/storage/sis-storage/src: main/java/org/apache/sis/internal/storage/folder/ main/resources/META-INF/services/ test/java/org/apache/sis/internal/storage/folder/ test/java/org/apache/sis/test/suite/
Date Fri, 08 Sep 2017 09:16:31 GMT
Author: jsorel
Date: Fri Sep  8 09:16:31 2017
New Revision: 1807695

URL: http://svn.apache.org/viewvc?rev=1807695&view=rev
Log:
Storage : new FolderStore capable of aggregating multiple smaller file stores

Added:
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderAggregate.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderProvider.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStore.java
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/FolderStoreTest.java
Modified:
    sis/branches/JDK8/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java

Added: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderAggregate.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderAggregate.java?rev=1807695&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderAggregate.java
(added)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderAggregate.java
Fri Sep  8 09:16:31 2017
@@ -0,0 +1,166 @@
+/*
+ * 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.sis.internal.storage.folder;
+
+import java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.sis.internal.storage.AbstractResource;
+import org.apache.sis.internal.storage.MetadataBuilder;
+import org.apache.sis.storage.Aggregate;
+import org.apache.sis.storage.DataStore;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.DataStores;
+import org.apache.sis.storage.ReadOnlyStorageException;
+import org.apache.sis.storage.Resource;
+import org.opengis.metadata.Identifier;
+import org.opengis.metadata.Metadata;
+import org.opengis.metadata.citation.Citation;
+import org.opengis.metadata.identification.Identification;
+
+/**
+ * Folder aggregation of resources.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+class FolderAggregate extends AbstractResource implements Aggregate {
+
+    /**
+     * Used to sort resources by type and name
+     */
+    private static final Comparator<Resource> RESOURCE_COMPARATOR = new Comparator<Resource>()
{
+        @Override
+        public int compare(Resource o1, Resource o2) {
+            if (o1 instanceof FolderAggregate && !(o2 instanceof FolderAggregate))
{
+                return -1;
+            } else if (o2 instanceof FolderAggregate) {
+                return +1;
+            }
+            try {
+                String m1 = getIdentifier(o1.getMetadata());
+                if (m1==null) m1 = "";
+                String m2 = getIdentifier(o2.getMetadata());
+                if (m2==null) m2 = "";
+                return m1.compareTo(m2);
+
+            } catch (DataStoreException ex) {
+                return 0;
+            }
+        }
+    };
+
+    private final FolderStore store;
+    private final FolderAggregate parent;
+    private final Path path;
+    private final Metadata metadata;
+    private List<Resource> resources;
+
+    FolderAggregate(FolderStore store, FolderAggregate parent, Path path) {
+        super(store, null);
+        this.store = store;
+        this.parent = parent;
+        this.path = path;
+
+        final MetadataBuilder mb = new MetadataBuilder();
+        mb.addIdentifier(null, path.getFileName().toString(), MetadataBuilder.Scope.ALL);
+        metadata =  mb.build(true);
+    }
+
+
+    @Override
+    public Metadata getMetadata() throws DataStoreException {
+        return metadata;
+    }
+
+    @Override
+    public synchronized Collection<Resource> components() throws DataStoreException
{
+        if (resources==null) {
+            resources = new ArrayList<>();
+            try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
+                final Iterator<Path> ite = stream.iterator();
+                while (ite.hasNext()) {
+                    final Path candidate = ite.next();
+                    if (Files.isDirectory(candidate)) {
+                        resources.add(new FolderAggregate(store, this, candidate));
+                    } else {
+                        //test it
+                        try {
+                            final DataStore store = DataStores.open(candidate);
+                            resources.add(store);
+                        } catch (DataStoreException ex) {
+                            //not a known type file, skip it
+                        }
+                    }
+                }
+            } catch (IOException ex) {
+                throw new DataStoreException(ex.getMessage(), ex);
+            }
+            Collections.sort(resources, RESOURCE_COMPARATOR);
+            resources = Collections.unmodifiableList(resources);
+        }
+        return resources;
+    }
+
+    @Override
+    public synchronized Resource add(Resource resource) throws DataStoreException, ReadOnlyStorageException
{
+        //TODO : we should try to copy the resource files if it based on a filesystem,
+        //but the API do not provide thoses informations
+        return Aggregate.super.add(resource);
+    }
+
+    @Override
+    public synchronized void remove(Resource resource) throws DataStoreException, ReadOnlyStorageException
{
+        Aggregate.super.remove(resource);
+    }
+
+    synchronized void close() throws DataStoreException {
+        if (resources != null) {
+            //close all children resources
+            for (Resource r : resources) {
+                if (r instanceof DataStore) {
+                    ((DataStore) r).close();
+                } else if (r instanceof FolderAggregate) {
+                    ((FolderAggregate) r).close();
+                }
+            }
+        }
+    }
+
+    private static String getIdentifier(Metadata metadata) {
+        final Collection<? extends Identification> identifications = metadata.getIdentificationInfo();
+        for (Identification identification : identifications) {
+            final Citation citation = identification.getCitation();
+            if (citation != null) {
+                for (Identifier identifier : citation.getIdentifiers()) {
+                    return identifier.getCode();
+                }
+            }
+        }
+        return null;
+    }
+
+}

Added: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderProvider.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderProvider.java?rev=1807695&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderProvider.java
(added)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderProvider.java
Fri Sep  8 09:16:31 2017
@@ -0,0 +1,83 @@
+/*
+ * 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.sis.internal.storage.folder;
+
+import java.net.URI;
+import java.nio.file.FileSystemNotFoundException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.apache.sis.internal.storage.URIDataStore;
+import org.apache.sis.storage.DataStore;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.DataStoreProvider;
+import org.apache.sis.storage.ProbeResult;
+import org.apache.sis.storage.StorageConnector;
+import org.opengis.parameter.ParameterDescriptorGroup;
+import org.opengis.parameter.ParameterValueGroup;
+
+/**
+ * The provider of {@link FolderStore} instances.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+public class FolderProvider extends DataStoreProvider {
+
+    public static final String NAME = "folder";
+
+    public static final ParameterDescriptorGroup PARAMETERS_DESCRIPTOR = URIDataStore.Provider.descriptor(NAME);
+
+    @Override
+    public String getShortName() {
+        return NAME;
+    }
+
+    @Override
+    public ParameterDescriptorGroup getOpenParameters() {
+        return PARAMETERS_DESCRIPTOR;
+    }
+
+    @Override
+    public ProbeResult probeContent(StorageConnector connector) throws DataStoreException
{
+        final URI uri = connector.getStorageAs(URI.class);
+        try {
+            //check path is valid
+            final Path path = Paths.get(uri);
+            if (Files.isDirectory(path)) {
+                return new ProbeResult(true, null, null);
+            }
+        } catch (FileSystemNotFoundException ex) {
+            //nothing we can do, may happen a lot
+        }
+       return new ProbeResult(false, null, null);
+    }
+
+    @Override
+    public DataStore open(StorageConnector connector) throws DataStoreException {
+        final URI uri = connector.getStorageAs(URI.class);
+        return new FolderStore(uri);
+    }
+
+    @Override
+    public DataStore open(ParameterValueGroup parameters) throws DataStoreException {
+        return new FolderStore(parameters);
+    }
+
+}

Added: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStore.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStore.java?rev=1807695&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStore.java
(added)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/folder/FolderStore.java
Fri Sep  8 09:16:31 2017
@@ -0,0 +1,147 @@
+/*
+ * 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.sis.internal.storage.folder;
+
+import java.net.URI;
+import java.nio.file.Paths;
+import java.util.Collection;
+import org.apache.sis.parameter.Parameters;
+import org.apache.sis.storage.Aggregate;
+import org.apache.sis.storage.DataStore;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.DataStoreProvider;
+import org.apache.sis.storage.DataStores;
+import org.apache.sis.storage.ReadOnlyStorageException;
+import org.apache.sis.storage.Resource;
+import org.opengis.metadata.Metadata;
+import org.opengis.parameter.ParameterValueGroup;
+
+/**
+ * A Folder store acts as an aggregate of multiple files in a single store.
+ * each file will be tested and possibly opened by another store.
+ * This approach allows to discover the content of a folder or archive without
+ * testing each file one by one.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+public class FolderStore extends DataStore implements Aggregate {
+
+    private final ParameterValueGroup params;
+    private final FolderAggregate root;
+
+    /**
+     * Construct a new FolderStore from an URI.
+     *
+     * @param uri root folder path
+     */
+    public FolderStore(URI uri) {
+        this(toParameters(uri));
+    }
+
+    /**
+     * Construct a new FolderStore from parameters.
+     *
+     * @param params opening parameters, those must comply to the provider parameter
+     *        definition {@link FolderProvider#PARAMETERS_DESCRIPTOR}
+     */
+    public FolderStore(ParameterValueGroup params) {
+        this.params = params;
+        final URI uri = URI.class.cast(params.parameter(DataStoreProvider.LOCATION).getValue());
+        root = new FolderAggregate(this, null, Paths.get(uri));
+    }
+
+    private static ParameterValueGroup toParameters(final URI uri) {
+        final Parameters params = Parameters.castOrWrap(FolderProvider.PARAMETERS_DESCRIPTOR.createValue());
+        params.parameter(DataStoreProvider.LOCATION).setValue(uri);
+        return params;
+    }
+
+    /**
+     * {@inheritDoc }
+     *
+     * @return the factory that created this {@code DataStore} instance
+     */
+    @Override
+    public DataStoreProvider getProvider() {
+        return DataStores.providers().stream().filter(
+                (DataStoreProvider t) -> t.getShortName().equals(FolderProvider.NAME)).findFirst().get();
+    }
+
+    /**
+     * {@inheritDoc }
+     *
+     * @return parameters used for opening this {@code DataStore}, not null.
+     */
+    @Override
+    public ParameterValueGroup getOpenParameters() {
+        return params;
+    }
+
+    /**
+     * @see FolderAggregate#getMetadata()
+     *
+     * @throws org.apache.sis.storage.DataStoreException
+     */
+    @Override
+    public Metadata getMetadata() throws DataStoreException {
+        return root.getMetadata();
+    }
+
+    /**
+     * @see FolderAggregate#components()
+     *
+     * @throws org.apache.sis.storage.DataStoreException
+     */
+    @Override
+    public Collection<Resource> components() throws DataStoreException {
+        return root.components();
+    }
+
+    /**
+     * @see FolderAggregate#add(org.apache.sis.storage.Resource)
+     *
+     * @throws org.apache.sis.storage.DataStoreException
+     */
+    @Override
+    public org.apache.sis.storage.Resource add(org.apache.sis.storage.Resource resource)
throws DataStoreException, ReadOnlyStorageException {
+        return root.add(resource);
+    }
+
+    /**
+     * @see FolderAggregate#remove(org.apache.sis.storage.Resource)
+     *
+     * @throws org.apache.sis.storage.DataStoreException
+     */
+    @Override
+    public void remove(org.apache.sis.storage.Resource resource) throws DataStoreException,
ReadOnlyStorageException {
+        root.remove(resource);
+    }
+
+    /**
+     * @see FolderAggregate#close()
+     *
+     * @throws org.apache.sis.storage.DataStoreException
+     */
+    @Override
+    public void close() throws DataStoreException {
+        root.close();
+    }
+
+}

Modified: sis/branches/JDK8/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider?rev=1807695&r1=1807694&r2=1807695&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider
[UTF-8] Fri Sep  8 09:16:31 2017
@@ -1,3 +1,4 @@
 org.apache.sis.internal.storage.xml.StoreProvider
 org.apache.sis.internal.storage.wkt.StoreProvider
 org.apache.sis.internal.storage.csv.StoreProvider
+org.apache.sis.internal.storage.folder.FolderProvider

Added: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/FolderStoreTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/FolderStoreTest.java?rev=1807695&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/FolderStoreTest.java
(added)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/folder/FolderStoreTest.java
Fri Sep  8 09:16:31 2017
@@ -0,0 +1,64 @@
+/*
+ * 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.sis.internal.storage.folder;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.sis.internal.storage.io.IOUtilities;
+import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.storage.DataStore;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.Resource;
+import org.apache.sis.test.TestCase;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+/**
+ * Tests {@link FolderStore}.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+public class FolderStoreTest extends TestCase {
+
+    /**
+     * Test if sub stores are correctly detected.
+     */
+    @Test
+    public void testOpeningFolder() throws IOException, DataStoreException {
+
+        //create a folder with a prj file
+        final Path directory = Files.createTempDirectory("folder");
+        final Path prjFile = directory.resolve("crs.prj");
+        try (BufferedWriter writer = Files.newBufferedWriter(prjFile)) {
+            writer.write(CommonCRS.WGS84.normalizedGeographic().toWKT());
+        }
+
+        final FolderStore store = new FolderStore(directory.toUri());
+        assertEquals(1,store.components().size());
+        final Resource prjResource = store.components().iterator().next();
+        assertTrue(prjResource instanceof DataStore);
+
+
+
+    }
+
+}

Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java?rev=1807695&r1=1807694&r2=1807695&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] Fri Sep  8 09:16:31 2017
@@ -48,6 +48,7 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.storage.wkt.StoreTest.class,
     org.apache.sis.internal.storage.csv.StoreProviderTest.class,
     org.apache.sis.internal.storage.csv.StoreTest.class,
+    org.apache.sis.internal.storage.folder.FolderStoreTest.class,
     org.apache.sis.storage.DataStoresTest.class
 })
 public final strictfp class StorageTestSuite extends TestSuite {



Mime
View raw message