Skip to content

Commit b1b5db4

Browse files
committed
Fix unit test
1 parent a535dbc commit b1b5db4

File tree

2 files changed

+83
-65
lines changed

2 files changed

+83
-65
lines changed

baremaps-calcite/src/main/java/org/apache/baremaps/calcite/DataTableFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.MappedByteBuffer;
2525
import java.nio.file.Paths;
2626
import java.util.Map;
27+
import org.apache.baremaps.calcite.baremaps.BaremapsDataTable;
2728
import org.apache.baremaps.calcite.csv.CsvDataTable;
2829
import org.apache.baremaps.data.collection.AppendOnlyLog;
2930
import org.apache.baremaps.data.collection.DataCollection;
@@ -78,7 +79,7 @@ private Table createDataTable(SchemaPlus schema, String name, Map<String, Object
7879
.dataType(dataType)
7980
.memory(memory)
8081
.build();
81-
return null; // new BaremapsTable(dataSchema, dataCollection);
82+
return new DataTableAdapter(new BaremapsDataTable(dataSchema, dataCollection));
8283
} catch (IOException e) {
8384
throw new RuntimeException(e);
8485
}

baremaps-calcite/src/test/java/org/apache/baremaps/calcite/DataTableAdapterFactoryTest.java

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,27 @@
1717

1818
package org.apache.baremaps.calcite;
1919

20+
import java.io.ByteArrayOutputStream;
2021
import java.io.File;
2122
import java.io.PrintWriter;
23+
import java.nio.ByteBuffer;
24+
import java.nio.MappedByteBuffer;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
2227
import java.sql.Connection;
2328
import java.sql.DriverManager;
2429
import java.sql.ResultSet;
30+
import java.util.List;
31+
import org.apache.baremaps.calcite.DataColumn.Cardinality;
32+
import org.apache.baremaps.calcite.DataColumn.Type;
33+
import org.apache.baremaps.calcite.baremaps.BaremapsDataTable;
34+
import org.apache.baremaps.data.collection.AppendOnlyLog;
35+
import org.apache.baremaps.data.memory.Memory;
36+
import org.apache.baremaps.data.memory.MemoryMappedDirectory;
37+
import org.apache.baremaps.data.util.FileUtils;
2538
import org.junit.jupiter.api.Test;
39+
import org.locationtech.jts.geom.Coordinate;
40+
import org.locationtech.jts.geom.GeometryFactory;
2641

2742
class DataTableAdapterFactoryTest {
2843

@@ -70,68 +85,70 @@ public void createCsvTable() throws Exception {
7085
}
7186
}
7287

73-
// @Test
74-
// public void createMMapTable() throws Exception {
75-
// Path path = Files.createTempDirectory("temp");
76-
//
77-
// DataSchema dataSchema = new DataSchema("test", List.of(
78-
// new DataColumnFixed("id", Cardinality.REQUIRED, Type.INTEGER),
79-
// new DataColumnFixed("name", Cardinality.REQUIRED, Type.STRING),
80-
// new DataColumnFixed("geom", Cardinality.REQUIRED, Type.GEOMETRY)));
81-
//
82-
// // Serialize the schema
83-
// ByteArrayOutputStream output = new ByteArrayOutputStream();
84-
// DataSchema.write(output, dataSchema);
85-
// byte[] bytes = output.toByteArray();
86-
//
87-
// // Write the schema to the header of the memory mapped file
88-
// Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(path);
89-
// ByteBuffer header = memory.header();
90-
// header.position(Long.BYTES);
91-
// header.putInt(bytes.length);
92-
// header.put(bytes);
93-
//
94-
// DataTable dataTable =
95-
// new BaremapsDataTable(dataSchema,
96-
// new AppendOnlyLog<>(new DataRowType(dataSchema), memory));
97-
// dataTable.add(new DataRow(dataSchema,
98-
// List.of(1, "a", new GeometryFactory().createPoint(new Coordinate(1, 1)))));
99-
// dataTable.add(new DataRow(dataSchema,
100-
// List.of(2, "b", new GeometryFactory().createPoint(new Coordinate(2, 2)))));
101-
//
102-
// dataTable.close();
103-
//
104-
//
105-
// String model = """
106-
// {
107-
// version: '1.0',
108-
// defaultSchema: 'TEST',
109-
// schemas: [
110-
// {
111-
// name: 'TEST',
112-
// tables: [
113-
// {
114-
// name: 'TEST',
115-
// factory: 'org.apache.baremaps.calcite.DataTableFactory',
116-
// operand: {
117-
// format: 'baremaps',
118-
// directory: '%s'
119-
// }
120-
// }
121-
// ]
122-
// }
123-
// ]
124-
// }
125-
// """.formatted(path.toAbsolutePath());
126-
// try (Connection connection =
127-
// DriverManager.getConnection("jdbc:calcite:model=inline:" + model)) {
128-
//
129-
// ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM TEST.TEST");
130-
// while (resultSet.next()) {
131-
// System.out.println(resultSet.getString("ID") + " " + resultSet.getString("GEOM"));
132-
// }
133-
// } finally {
134-
// FileUtils.deleteRecursively(path);
135-
// }
136-
// }
88+
@Test
89+
public void createMMapTable() throws Exception {
90+
Path path = Files.createTempDirectory("temp");
91+
92+
DataSchema dataSchema = new DataSchema("test", List.of(
93+
new DataColumnFixed("id", Cardinality.REQUIRED, Type.INTEGER),
94+
new DataColumnFixed("name", Cardinality.REQUIRED, Type.STRING),
95+
new DataColumnFixed("geom", Cardinality.REQUIRED, Type.GEOMETRY)));
96+
97+
// Serialize the schema
98+
ByteArrayOutputStream output = new ByteArrayOutputStream();
99+
DataSchema.write(output, dataSchema);
100+
byte[] bytes = output.toByteArray();
101+
102+
// Write the schema to the header of the memory mapped file
103+
Memory<MappedByteBuffer> memory = new MemoryMappedDirectory(path);
104+
ByteBuffer header = memory.header();
105+
header.position(Long.BYTES);
106+
header.putInt(bytes.length);
107+
header.put(bytes);
108+
109+
DataTable dataTable =
110+
new BaremapsDataTable(dataSchema,
111+
AppendOnlyLog.<DataRow>builder()
112+
.dataType(new DataRowType(dataSchema))
113+
.memory(memory)
114+
.build());
115+
dataTable.add(new DataRow(dataSchema,
116+
List.of(1, "a", new GeometryFactory().createPoint(new Coordinate(1, 1)))));
117+
dataTable.add(new DataRow(dataSchema,
118+
List.of(2, "b", new GeometryFactory().createPoint(new Coordinate(2, 2)))));
119+
120+
dataTable.close();
121+
122+
String model = """
123+
{
124+
version: '1.0',
125+
defaultSchema: 'TEST',
126+
schemas: [
127+
{
128+
name: 'TEST',
129+
tables: [
130+
{
131+
name: 'TEST',
132+
factory: 'org.apache.baremaps.calcite.DataTableFactory',
133+
operand: {
134+
format: 'baremaps',
135+
directory: '%s'
136+
}
137+
}
138+
]
139+
}
140+
]
141+
}
142+
""".formatted(path.toAbsolutePath());
143+
try (Connection connection =
144+
DriverManager.getConnection("jdbc:calcite:model=inline:" + model)) {
145+
146+
ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM TEST.TEST");
147+
while (resultSet.next()) {
148+
System.out.println(resultSet.getString("ID") + " " + resultSet.getString("GEOM"));
149+
}
150+
} finally {
151+
FileUtils.deleteRecursively(path);
152+
}
153+
}
137154
}

0 commit comments

Comments
 (0)