Skip to content

Commit ee7aed7

Browse files
authored
Refactor and clean the storage package (#857)
- The DataFrame class becomes the DataTable class - The data.schema package becomes the data.storage package - The method and variable names in the unit tests have been improved - The overall architecture is documented in the README.md of the module
1 parent db7ef92 commit ee7aed7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+539
-476
lines changed

baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataSchema.java baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataStore.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,41 @@
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
2323
import java.util.List;
24-
import org.apache.baremaps.data.schema.DataSchema;
25-
import org.apache.baremaps.data.schema.DataTable;
26-
import org.apache.baremaps.data.schema.DataTableException;
24+
import org.apache.baremaps.data.storage.DataStore;
25+
import org.apache.baremaps.data.storage.DataStoreException;
26+
import org.apache.baremaps.data.storage.DataTable;
2727

2828
/**
29-
* A schema corresponding to the flatgeobuf files of a directory.
29+
* A {@link DataStore} corresponding to the flatgeobuf files of a directory.
3030
*/
31-
public class FlatGeoBufDataSchema implements DataSchema {
31+
public class FlatGeoBufDataStore implements DataStore {
3232

3333
private final Path directory;
3434

35-
public FlatGeoBufDataSchema(Path directory) {
35+
public FlatGeoBufDataStore(Path directory) {
3636
this.directory = directory;
3737
}
3838

3939
/**
4040
* {@inheritDoc}
4141
*/
4242
@Override
43-
public List<String> list() throws DataTableException {
43+
public List<String> list() throws DataStoreException {
4444
try (var files = Files.list(directory)) {
4545
return files
4646
.filter(file -> file.toString().toLowerCase().endsWith(".fgb"))
4747
.map(file -> file.getFileName().toString())
4848
.toList();
4949
} catch (IOException e) {
50-
throw new DataTableException(e);
50+
throw new DataStoreException(e);
5151
}
5252
}
5353

5454
/**
5555
* {@inheritDoc}
5656
*/
5757
@Override
58-
public DataTable get(String name) throws DataTableException {
58+
public DataTable get(String name) throws DataStoreException {
5959
var path = directory.resolve(name);
6060
return new FlatGeoBufDataTable(path);
6161
}
@@ -64,39 +64,39 @@ public DataTable get(String name) throws DataTableException {
6464
* {@inheritDoc}
6565
*/
6666
@Override
67-
public void add(DataTable table) throws DataTableException {
68-
var filename = table.rowType().name();
69-
filename = filename.endsWith(".fgb") ? filename : filename + ".fgb";
70-
add(filename, table);
67+
public void add(DataTable table) throws DataStoreException {
68+
var fileName = table.schema().name();
69+
fileName = fileName.endsWith(".fgb") ? fileName : fileName + ".fgb";
70+
add(fileName, table);
7171
}
7272

7373
@Override
74-
public void add(String name, DataTable table) throws DataTableException {
74+
public void add(String name, DataTable table) throws DataStoreException {
7575
var path = directory.resolve(name);
7676
try {
7777
Files.deleteIfExists(path);
7878
Files.createFile(path);
79-
var flatGeoBufTable = new FlatGeoBufDataTable(path, table.rowType());
79+
var flatGeoBufTable = new FlatGeoBufDataTable(path, table.schema());
8080
flatGeoBufTable.write(table);
8181
} catch (IOException e) {
82-
throw new DataTableException(e);
82+
throw new DataStoreException(e);
8383
}
8484
}
8585

8686
/**
8787
* {@inheritDoc}
8888
*/
8989
@Override
90-
public void remove(String name) throws DataTableException {
90+
public void remove(String name) throws DataStoreException {
9191
var path = directory.resolve(name);
9292
if (name.equals(path.getFileName().toString())) {
9393
try {
9494
Files.delete(path);
9595
} catch (IOException e) {
96-
throw new DataTableException(e);
96+
throw new DataStoreException(e);
9797
}
9898
} else {
99-
throw new DataTableException("Table not found");
99+
throw new DataStoreException("Table not found");
100100
}
101101
}
102102
}

baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufDataTable.java

+32-34
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import java.util.Iterator;
2828
import java.util.NoSuchElementException;
2929
import org.apache.baremaps.data.collection.DataCollection;
30-
import org.apache.baremaps.data.schema.DataRow;
31-
import org.apache.baremaps.data.schema.DataRowType;
32-
import org.apache.baremaps.data.schema.DataTable;
30+
import org.apache.baremaps.data.storage.DataRow;
31+
import org.apache.baremaps.data.storage.DataSchema;
32+
import org.apache.baremaps.data.storage.DataTable;
3333
import org.locationtech.jts.geom.*;
3434
import org.wololo.flatgeobuf.Constants;
3535
import org.wololo.flatgeobuf.GeometryConversions;
@@ -39,13 +39,13 @@
3939
import org.wololo.flatgeobuf.generated.GeometryType;
4040

4141
/**
42-
* A table that stores rows in a flatgeobuf file.
42+
* A {@link DataTable} that stores rows in a flatgeobuf file.
4343
*/
4444
public class FlatGeoBufDataTable implements DataTable {
4545

4646
private final Path file;
4747

48-
private DataRowType rowType;
48+
private DataSchema schema;
4949

5050
/**
5151
* Constructs a table from a flatgeobuf file (used for reading).
@@ -54,40 +54,38 @@ public class FlatGeoBufDataTable implements DataTable {
5454
*/
5555
public FlatGeoBufDataTable(Path file) {
5656
this.file = file;
57-
this.rowType = readRowType(file);
57+
this.schema = readSchema(file);
58+
}
59+
60+
61+
private static DataSchema readSchema(Path file) {
62+
try (var channel = FileChannel.open(file, StandardOpenOption.READ)) {
63+
// try to read the schema from the file
64+
var buffer = ByteBuffer.allocate(1 << 20).order(ByteOrder.LITTLE_ENDIAN);
65+
HeaderMeta headerMeta = readHeaderMeta(channel, buffer);
66+
return FlatGeoBufTypeConversion.asSchema(headerMeta);
67+
} catch (IOException e) {
68+
return null;
69+
}
5870
}
5971

6072
/**
61-
* Constructs a table from a flatgeobuf file and a row type (used for writing).
73+
* Constructs a table from a flatgeobuf file and a schema (used for writing).
6274
*
6375
* @param file the path to the flatgeobuf file
64-
* @param rowType the row type of the table
76+
* @param schema the schema of the table
6577
*/
66-
public FlatGeoBufDataTable(Path file, DataRowType rowType) {
78+
public FlatGeoBufDataTable(Path file, DataSchema schema) {
6779
this.file = file;
68-
this.rowType = rowType;
80+
this.schema = schema;
6981
}
7082

7183
/**
7284
* {@inheritDoc}
7385
*/
7486
@Override
75-
public DataRowType rowType() {
76-
return rowType;
77-
}
78-
79-
/**
80-
* {@inheritDoc}
81-
*/
82-
public static DataRowType readRowType(Path file) {
83-
try (var channel = FileChannel.open(file, StandardOpenOption.READ)) {
84-
// try to read the row type from the file
85-
var buffer = ByteBuffer.allocate(1 << 20).order(ByteOrder.LITTLE_ENDIAN);
86-
HeaderMeta headerMeta = readHeaderMeta(channel, buffer);
87-
return FlatGeoBufTypeConversion.asRowType(headerMeta);
88-
} catch (IOException e) {
89-
return null;
90-
}
87+
public DataSchema schema() {
88+
return schema;
9189
}
9290

9391
/**
@@ -111,7 +109,7 @@ public Iterator<DataRow> iterator() {
111109
buffer.clear();
112110

113111
// create the feature stream
114-
return new RowIterator(channel, headerMeta, rowType, buffer);
112+
return new RowIterator(channel, headerMeta, schema, buffer);
115113
} catch (IOException e) {
116114
throw new RuntimeException(e);
117115
}
@@ -173,8 +171,8 @@ public void write(DataCollection<DataRow> features) throws IOException {
173171
headerMeta.indexNodeSize = 16;
174172
headerMeta.srid = 3857;
175173
headerMeta.featuresCount = features.size();
176-
headerMeta.name = rowType.name();
177-
headerMeta.columns = FlatGeoBufTypeConversion.asColumns(rowType.columns());
174+
headerMeta.name = schema.name();
175+
headerMeta.columns = FlatGeoBufTypeConversion.asColumns(schema.columns());
178176
HeaderMeta.write(headerMeta, outputStream, bufferBuilder);
179177

180178
var indexSize =
@@ -235,7 +233,7 @@ public static class RowIterator implements Iterator<DataRow> {
235233

236234
private final HeaderMeta headerMeta;
237235

238-
private final DataRowType rowType;
236+
private final DataSchema schema;
239237

240238
private final SeekableByteChannel channel;
241239

@@ -248,14 +246,14 @@ public static class RowIterator implements Iterator<DataRow> {
248246
*
249247
* @param channel the channel to read from
250248
* @param headerMeta the header meta
251-
* @param rowType the row type of the table
249+
* @param schema the schema of the table
252250
* @param buffer the buffer to use
253251
*/
254252
public RowIterator(SeekableByteChannel channel, HeaderMeta headerMeta,
255-
DataRowType rowType, ByteBuffer buffer) {
253+
DataSchema schema, ByteBuffer buffer) {
256254
this.channel = channel;
257255
this.headerMeta = headerMeta;
258-
this.rowType = rowType;
256+
this.schema = schema;
259257
this.buffer = buffer;
260258
}
261259

@@ -278,7 +276,7 @@ public DataRow next() {
278276

279277
var featureSize = buffer.getInt();
280278
var row =
281-
FlatGeoBufTypeConversion.asRow(headerMeta, rowType, Feature.getRootAsFeature(buffer));
279+
FlatGeoBufTypeConversion.asRow(headerMeta, schema, Feature.getRootAsFeature(buffer));
282280

283281
buffer.position(Integer.BYTES + featureSize);
284282
buffer.compact();

baremaps-core/src/main/java/org/apache/baremaps/storage/flatgeobuf/FlatGeoBufTypeConversion.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import java.nio.charset.StandardCharsets;
2626
import java.util.*;
2727
import java.util.stream.Collectors;
28-
import org.apache.baremaps.data.schema.*;
29-
import org.apache.baremaps.data.schema.DataColumn.Type;
28+
import org.apache.baremaps.data.storage.*;
29+
import org.apache.baremaps.data.storage.DataColumn.Type;
3030
import org.wololo.flatgeobuf.ColumnMeta;
3131
import org.wololo.flatgeobuf.GeometryConversions;
3232
import org.wololo.flatgeobuf.HeaderMeta;
@@ -50,16 +50,16 @@ public class FlatGeoBufTypeConversion {
5050
types.put(Type.STRING, ColumnType.String);
5151
}
5252

53-
public static DataRowType asRowType(HeaderMeta headerMeta) {
53+
public static DataSchema asSchema(HeaderMeta headerMeta) {
5454
var name = headerMeta.name;
5555
var columns = headerMeta.columns.stream()
5656
.map(column -> new DataColumnImpl(column.name, Type.fromBinding(column.getBinding())))
5757
.map(DataColumn.class::cast)
5858
.toList();
59-
return new DataRowTypeImpl(name, columns);
59+
return new DataSchemaImpl(name, columns);
6060
}
6161

62-
public static DataRow asRow(HeaderMeta headerMeta, DataRowType dataType, Feature feature) {
62+
public static DataRow asRow(HeaderMeta headerMeta, DataSchema dataType, Feature feature) {
6363
var values = new ArrayList();
6464

6565
var geometryBuffer = feature.geometry();

baremaps-core/src/main/java/org/apache/baremaps/storage/geopackage/GeoPackageDataSchema.java baremaps-core/src/main/java/org/apache/baremaps/storage/geopackage/GeoPackageDataStore.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222
import java.util.List;
2323
import mil.nga.geopackage.GeoPackage;
2424
import mil.nga.geopackage.GeoPackageManager;
25-
import org.apache.baremaps.data.schema.DataSchema;
26-
import org.apache.baremaps.data.schema.DataTable;
27-
import org.apache.baremaps.data.schema.DataTableException;
25+
import org.apache.baremaps.data.storage.DataStore;
26+
import org.apache.baremaps.data.storage.DataStoreException;
27+
import org.apache.baremaps.data.storage.DataTable;
2828

2929
/**
30-
* A schema corresponding to a GeoPackage database.
30+
* A {@link DataStore} corresponding to a GeoPackage file.
3131
*/
32-
public class GeoPackageDataSchema implements DataSchema, AutoCloseable {
32+
public class GeoPackageDataStore implements DataStore, AutoCloseable {
3333

3434
private final GeoPackage geoPackage;
3535

3636
/**
37-
* Constructs a schema from a GeoPackage database.
37+
* Constructs a {@link GeoPackageDataStore} from a GeoPackage file.
3838
*
39-
* @param file the path to the GeoPackage database
39+
* @param file the path to the GeoPackage file
4040
*/
41-
public GeoPackageDataSchema(Path file) {
41+
public GeoPackageDataStore(Path file) {
4242
this.geoPackage = GeoPackageManager.open(file.toFile());
4343
}
4444

@@ -54,36 +54,36 @@ public void close() throws Exception {
5454
* {@inheritDoc}
5555
*/
5656
@Override
57-
public List<String> list() throws DataTableException {
57+
public List<String> list() throws DataStoreException {
5858
return geoPackage.getFeatureTables();
5959
}
6060

6161
/**
6262
* {@inheritDoc}
6363
*/
6464
@Override
65-
public DataTable get(String name) throws DataTableException {
65+
public DataTable get(String name) throws DataStoreException {
6666
return new GeoPackageDataTable(geoPackage.getFeatureDao(name));
6767
}
6868

6969
/**
7070
* {@inheritDoc}
7171
*/
7272
@Override
73-
public void add(DataTable table) throws DataTableException {
73+
public void add(DataTable table) throws DataStoreException {
7474
throw new UnsupportedOperationException();
7575
}
7676

7777
@Override
78-
public void add(String name, DataTable table) throws DataTableException {
78+
public void add(String name, DataTable table) throws DataStoreException {
7979
throw new UnsupportedOperationException();
8080
}
8181

8282
/**
8383
* {@inheritDoc}
8484
*/
8585
@Override
86-
public void remove(String name) throws DataTableException {
86+
public void remove(String name) throws DataStoreException {
8787
throw new UnsupportedOperationException();
8888
}
8989
}

0 commit comments

Comments
 (0)