use of org.apache.sis.storage.StorageConnector in project sis by apache.
the class StoreTest method testMovingFeatures.
/**
* Tests reading the data as a moving features. In the following data:
*
* {@preformat text
* a, 10, 150, 11.0 2.0 12.0 3.0, walking, 1
* b, 10, 190, 10.0 2.0 11.0 3.0, walking, 2
* a, 150, 190, 12.0 3.0 10.0 3.0
* c, 10, 190, 12.0 1.0 10.0 2.0 11.0 3.0, vehicle, 1
* }
*
* the two rows for the "a" features shall be merged in a single trajectory.
*
* @throws DataStoreException if an error occurred while parsing the data.
*/
@Test
public void testMovingFeatures() throws DataStoreException {
isMovingFeature = true;
try (Store store = new Store(null, new StorageConnector(testData()))) {
verifyFeatureType(store.featureType, Polyline.class, Integer.MAX_VALUE);
assertEquals("foliation", Foliation.TIME, store.foliation);
final Iterator<AbstractFeature> it = store.features(false).iterator();
assertPropertyEquals(it.next(), "a", "12:33:51", "12:36:51", new double[] { 11, 2, 12, 3, 10, 3 }, singletonList("walking"), Arrays.asList(1, 2));
assertPropertyEquals(it.next(), "b", "12:33:51", "12:36:51", new double[] { 10, 2, 11, 3 }, singletonList("walking"), singletonList(2));
assertPropertyEquals(it.next(), "c", "12:33:51", "12:36:51", new double[] { 12, 1, 10, 2, 11, 3 }, singletonList("vehicle"), singletonList(1));
assertFalse(it.hasNext());
}
}
use of org.apache.sis.storage.StorageConnector in project sis by apache.
the class ReaderTest method createFromURL.
/**
* Creates a data store for the {@code "1.1/route.xml"} test files using its URL instead than the input stream.
* Using the URL makes easier for the data store to read the same data more than once.
*/
private static Store createFromURL() throws DataStoreException {
final StorageConnector connector = new StorageConnector(ReaderTest.class.getResource("1.1/route.xml"));
connector.setOption(OptionKey.URL_ENCODING, "UTF-8");
return new Store(provider, connector);
}
use of org.apache.sis.storage.StorageConnector in project sis by apache.
the class WriterTest method testInputReplacement.
/**
* Tests coexistence of read and write operations by first reading part of a file,
* then switching in write mode.
*
* @throws Exception if an error occurred while creating the temporary test file,
* the test data or performing data store operation.
*/
@Test
@DependsOnMethod("testRoutes110")
public void testInputReplacement() throws Exception {
try (Store store = new Store(provider, new StorageConnector(TestUtilities.createTemporaryFile(ReaderTest.class, "1.1/metadata.xml")))) {
/*
* Read part of the file. We verify its content as a matter of principle,
* but the main purpose of following code is to advance in the stream.
*/
ReaderTest.verifyMetadata((Metadata) store.getMetadata(), 3);
assertEquals("version", StoreProvider.V1_1, store.getVersion());
/*
* Replace the metadata content by route content. The data store should rewind
* to the begining of the file and replace the input stream by an output stream.
*/
testFeatures(store, Type.ROUTE);
/*
* Following should revert the output stream back to an input stream and rewind again.
*/
ReaderTest.verifyRoute110(store);
}
}
use of org.apache.sis.storage.StorageConnector in project sis by apache.
the class WritableStore method add.
/**
* Create a new file for the given resource.
* This implementation uses the provider specified at creation time.
*/
@Override
public synchronized Resource add(final Resource resource) throws DataStoreException {
ArgumentChecks.ensureNonNull("resource", resource);
if (!(resource instanceof FeatureSet)) {
throw new DataStoreException(message(Resources.Keys.CanNotStoreResourceType_2, new Object[] { FolderStoreProvider.NAME, StoreUtilities.getInterface(resource.getClass()) }));
}
/*
* If we determined in a previous method invocation that the given provider can not write feature set,
* we are better to fail now instead than polluting the directory with files that we can not use.
*/
if (isReadOnly) {
throw new ReadOnlyStorageException(messages().getString(Resources.Keys.StoreIsReadOnly));
}
/*
* Infer a filename from the resource identifier, if one can be found.
* A suffix is added to the filename if available (some formats may have no suffix at all).
*/
String filename = StoreUtilities.getIdentifier(resource.getMetadata());
if (filename == null) {
throw new DataStoreException(message(Resources.Keys.MissingResourceIdentifier_1, StoreUtilities.getLabel(resource)));
}
final String[] suffixes = StoreUtilities.getFileSuffixes(componentProvider.getClass());
if (suffixes.length != 0) {
filename += '.' + suffixes[0];
}
/*
* Create new store/resource for write access, provided that no store already exist for the path.
* We use the CREATE_NEW option in order to intentionally fail if the resource already exists.
*/
final Path path = location.resolve(filename);
if (!children.containsKey(path)) {
final StorageConnector connector = new StorageConnector(path);
connector.setOption(OptionKey.LOCALE, locale);
connector.setOption(OptionKey.TIMEZONE, timezone);
connector.setOption(OptionKey.ENCODING, encoding);
connector.setOption(OptionKey.OPEN_OPTIONS, new StandardOpenOption[] { StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE });
final DataStore store = componentProvider.open(connector);
if (children.putIfAbsent(path, store) == null) {
// TODO: handle transactional case.
if (store instanceof WritableFeatureSet) {
StoreUtilities.copy((FeatureSet) resource, (WritableFeatureSet) store);
// Clear cache. TODO: we should do something more efficient.
components = null;
return store;
}
/*
* If the data store is not a WritableFeatureSet, current implementation can not use it.
* Files created by this failed attempt may remain; instead of trying to delete them with
* uncertain consequences, we set a flag for avoiding to pollute further the directory.
*/
isReadOnly = true;
children.remove(path, store);
final String name = store.getDisplayName();
store.close();
throw new DataStoreException(message(Resources.Keys.NotAWritableFeatureSet_1, name));
}
store.close();
}
throw new DataStoreException(message(Resources.Keys.ResourceAlreadyExists_1, path));
}
use of org.apache.sis.storage.StorageConnector in project sis by apache.
the class StoreProvider method open.
/**
* Returns a CSV {@link Store} implementation from the given parameters.
*
* @return a data store implementation associated with this provider for the given parameters.
* @throws DataStoreException if an error occurred while creating the data store instance.
*/
@Override
public DataStore open(final ParameterValueGroup parameters) throws DataStoreException {
ArgumentChecks.ensureNonNull("parameter", parameters);
final StorageConnector connector = connector(this, parameters);
final Parameters pg = Parameters.castOrWrap(parameters);
connector.setOption(DataOptionKey.ENCODING, pg.getValue(ENCODING));
connector.setOption(DataOptionKey.FOLIATION_REPRESENTATION, pg.getValue(FOLIATION));
return new Store(this, connector);
}
Aggregations