|
17 | 17 |
|
18 | 18 | package org.apache.baremaps.calcite;
|
19 | 19 |
|
| 20 | +import java.io.ByteArrayOutputStream; |
20 | 21 | import java.io.File;
|
21 | 22 | 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; |
22 | 27 | import java.sql.Connection;
|
23 | 28 | import java.sql.DriverManager;
|
24 | 29 | 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; |
25 | 38 | import org.junit.jupiter.api.Test;
|
| 39 | +import org.locationtech.jts.geom.Coordinate; |
| 40 | +import org.locationtech.jts.geom.GeometryFactory; |
26 | 41 |
|
27 | 42 | class DataTableAdapterFactoryTest {
|
28 | 43 |
|
@@ -70,68 +85,70 @@ public void createCsvTable() throws Exception {
|
70 | 85 | }
|
71 | 86 | }
|
72 | 87 |
|
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 | + } |
137 | 154 | }
|
0 commit comments