/
GeoGigFeatureStoreIndexedTest.java
384 lines (328 loc) · 15.6 KB
/
GeoGigFeatureStoreIndexedTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright (c) 2017 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.geotools.data;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.factory.Hints;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.junit.Test;
import org.locationtech.geogig.model.Node;
import org.locationtech.geogig.model.NodeRef;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.RevFeature;
import org.locationtech.geogig.model.RevObject.TYPE;
import org.locationtech.geogig.model.RevObjectFactory;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.model.impl.CanonicalTreeBuilder;
import org.locationtech.geogig.model.internal.QuadTreeTestSupport;
import org.locationtech.geogig.plumbing.index.IndexTestSupport;
import org.locationtech.geogig.porcelain.index.CreateQuadTree;
import org.locationtech.geogig.porcelain.index.Index;
import org.locationtech.geogig.repository.IndexInfo;
import org.locationtech.geogig.repository.Repository;
import org.locationtech.geogig.repository.WorkingTree;
import org.locationtech.geogig.repository.impl.SpatialOps;
import org.locationtech.geogig.storage.ObjectStore;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
/**
* Run all {@link GeoGigFeatureStoreTest} test cases and make sure the {@link IndexInfo index} is
* correctly updated
*
*/
public class GeoGigFeatureStoreIndexedTest extends GeoGigFeatureStoreTest {
private Index createIndex(NodeRef typeRef, String... extraAttributes) {
List<String> extraAtts = extraAttributes == null ? null : Arrays.asList(extraAttributes);
return createIndex(typeRef, extraAtts);
}
private Index createIndex(NodeRef typeRef, List<String> extraAttributes) {
Index index = repo.command(CreateQuadTree.class)//
.setBounds(QuadTreeTestSupport.wgs84Bounds())//
.setTypeTreeRef(typeRef)//
.setExtraAttributes(extraAttributes)//
.call();
return index;
}
public @Test void testMoveGeometriesFromUnpromotablesToQuad() throws IOException {
final Index polygonsInitialIndex;
{
GeogigFeatureStore polys = (GeogigFeatureStore) dataStore.getFeatureSource(polyName);
NodeRef typeRef = polys.delegate.getTypeRef();
polygonsInitialIndex = createIndex(typeRef, "sp", "ip");
ObjectId indexTreeId = polygonsInitialIndex.indexTreeId();
ObjectId canonicalTreeId = typeRef.getObjectId();
IndexTestSupport.verifyIndex(geogig, indexTreeId, canonicalTreeId, "sp", "ip");
}
final Geometry unpromotable = geom("POLYGON((-1 -1,-1 1,1 1,1 -1,-1 -1))");
final Geometry promotable = geom("POLYGON((0.1 0.1,0.1 0.2,0.2 0.2,0.2 0.1,0.1 0.1))");
final List<SimpleFeature> features;
features = createClones(130, feature(polyType, "0", "sval", 1, unpromotable));
{
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(polyName);
Transaction tx = new DefaultTransaction();
store.setTransaction(tx);
store.addFeatures(DataUtilities.collection(features));
tx.commit();
tx.close();
}
verify(polygonsInitialIndex.info(), features);
final List<SimpleFeature> expectedFeatures;
expectedFeatures = createClones(130, feature(polyType, "0", "sval", 1, promotable));
{
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(polyName);
Transaction tx = new DefaultTransaction();
store.setTransaction(tx);
store.modifyFeatures("pp", promotable, Filter.INCLUDE);
tx.commit();
tx.close();
}
verify(polygonsInitialIndex.info(), expectedFeatures);
}
public @Test void testVerySmallLineStrings() throws Exception {
Geometry smallGeom = geom(
"LINESTRING(1.401298464324817E-45 1.401298464324817E-45,2.802596928649634E-45 2.802596928649634E-45)");
Geometry updateGeom = geom("LINESTRING(1 1, 1.000001 1.000001)");
testVerySmallGeometries(linesType, 130, smallGeom, updateGeom);
}
private void testVerySmallGeometries(SimpleFeatureType type, final int count,
final Geometry initialGeom, final Geometry updateGeom, final String... extraAttributes)
throws Exception {
final String typeName = type.getTypeName();
final Index index;
{
GeogigFeatureStore store = (GeogigFeatureStore) dataStore.getFeatureSource(typeName);
NodeRef typeRef = store.delegate.getTypeRef();
index = createIndex(typeRef);
}
final String geometryAttribute = type.getGeometryDescriptor().getLocalName();
final List<SimpleFeature> features;
features = createClones(count, feature(type, "0", "sval", 1, initialGeom));
{
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
Transaction tx = new DefaultTransaction();
store.setTransaction(tx);
store.addFeatures(DataUtilities.collection(features));
assertEquals(count, store.getCount(Query.ALL));
tx.commit();
tx.close();
store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
assertEquals(count, store.getCount(Query.ALL));
}
verify(index.info(), features);
final List<SimpleFeature> expectedFeatures;
expectedFeatures = createClones(count, feature(type, "1", "sval-updated", 2, updateGeom));
{
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
Transaction tx;
tx = new DefaultTransaction();
store.setTransaction(tx);
store.modifyFeatures(geometryAttribute, updateGeom, Filter.INCLUDE);
tx.commit();
tx.close();
tx = new DefaultTransaction();
store.setTransaction(tx);
store.modifyFeatures(type.getDescriptor("ip").getName(), 2, Filter.INCLUDE);
tx.commit();
tx.close();
tx = new DefaultTransaction();
store.setTransaction(tx);
store.modifyFeatures(type.getDescriptor("sp").getName(), "sval-updated",
Filter.INCLUDE);
tx.commit();
tx.close();
store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
assertEquals(count, store.getCount(Query.ALL));
}
verify(index.info(), expectedFeatures);
}
private void verify(IndexInfo indexInfo, List<SimpleFeature> expectedFeatures)
throws IOException {
String typeName = indexInfo.getTreeName();
GeogigFeatureStore source = (GeogigFeatureStore) dataStore.getFeatureSource(typeName);
assertEquals(expectedFeatures.size(), source.getCount(Query.ALL));
NodeRef typeRef = source.delegate.getTypeRef();
ObjectId canonicalTreeId = typeRef.getObjectId();
Optional<ObjectId> resolveIndexedTree = repo.indexDatabase().resolveIndexedTree(indexInfo,
canonicalTreeId);
assertTrue(resolveIndexedTree.isPresent());
ObjectId indexTreeId = resolveIndexedTree.get();
Set<String> extraAttributes = IndexInfo.getMaterializedAttributeNames(indexInfo);
String[] extraAtts = new ArrayList<>(extraAttributes).toArray(new String[0]);
IndexTestSupport.verifyIndex(geogig, indexTreeId, canonicalTreeId, extraAtts);
List<SimpleFeature> contents = DataUtilities.list(source.getFeatures());
Map<String, SimpleFeature> expected = Maps.uniqueIndex(expectedFeatures, (f) -> f.getID());
Map<String, SimpleFeature> actual = Maps.uniqueIndex(contents, (f) -> f.getID());
assertEquals(expected.size(), actual.size());
assertEquals(expected.keySet(), actual.keySet());
for (String fid : expected.keySet()) {
SimpleFeature e = expected.get(fid);
SimpleFeature a = actual.get(fid);
List<Object> eatts = e.getAttributes();
List<Object> aatts = a.getAttributes();
assertEquals(eatts, aatts);
}
}
private List<SimpleFeature> createClones(int count, SimpleFeature proto) {
List<SimpleFeature> list = new ArrayList<>(count);
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(proto.getFeatureType());
for (int i = 0; i < count; i++) {
builder.init(proto);
String fid = String.valueOf(i);
SimpleFeature f = builder.buildFeature(fid);
f.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
f.getUserData().put(Hints.PROVIDED_FID, fid);
list.add(f);
}
return list;
}
@Test
public void testRemoveFeatures2() throws Exception {
final NodeRef layerNode = IndexTestSupport.createWorldPointsLayer(repo);
final String typeName = layerNode.getNode().getName();
add();
commit("created layer " + typeName);
final Index initialIndex = createIndex(layerNode);
final Filter filter = ff.bbox("geom", 0, 0, 180, 90, "EPSG:4326");
GeogigFeatureStore store = (GeogigFeatureStore) dataStore.getFeatureSource(typeName);
final int total = store.getCount(Query.ALL);
final int matching = store.getCount(new Query(typeName, filter));
assertTrue(matching > 0);
final int expected = total - matching;
List<SimpleFeature> expectedResult = DataUtilities.list(store.getFeatures());
for (Iterator<SimpleFeature> it = expectedResult.iterator(); it.hasNext();) {
SimpleFeature f = it.next();
if (filter.evaluate(f)) {
it.remove();
}
}
Transaction tx = new DefaultTransaction();
store.setTransaction(tx);
try {
// initial # of features
assertEquals(total, store.getCount(Query.ALL));
// remove feature
store.removeFeatures(filter);
// #of features before commit on the same store
assertEquals(expected, store.getCount(Query.ALL));
// #of features before commit on a different store instance
assertEquals(total, dataStore.getFeatureSource(typeName).getCount(Query.ALL));
tx.commit();
// #of features after commit on a different store instance
assertEquals(expected, dataStore.getFeatureSource(typeName).getCount(Query.ALL));
} catch (Exception e) {
tx.rollback();
throw e;
} finally {
tx.close();
}
store.setTransaction(Transaction.AUTO_COMMIT);
assertEquals(expected, store.getFeatures().size());
assertEquals(0, store.getFeatures(filter).size());
verify(initialIndex.info(), expectedResult);
}
@Test
public void testRemoveFeaturesUnpromotables() throws Exception {
final NodeRef layerNode = IndexTestSupport.createWorldPointsLayer(repo);
final String typeName = layerNode.getNode().getName();
add();
commit("created layer " + typeName);
createIndex(layerNode);
final Filter filter = ff.bbox("pp", 0, 0, 180, 90, "EPSG:4326");
GeogigFeatureStore store = (GeogigFeatureStore) dataStore.getFeatureSource(typeName);
final int total = store.getCount(Query.ALL);
final int matching = store.getCount(new Query(typeName, filter));
assertTrue(matching > 0);
final int expected = total - matching;
Transaction tx = new DefaultTransaction();
store.setTransaction(tx);
try {
// initial # of features
assertEquals(total, store.getCount(Query.ALL));
// remove feature
store.removeFeatures(filter);
// #of features before commit on the same store
assertEquals(expected, store.getCount(Query.ALL));
// #of features before commit on a different store instance
assertEquals(total, dataStore.getFeatureSource(typeName).getCount(Query.ALL));
tx.commit();
// #of features after commit on a different store instance
assertEquals(expected, dataStore.getFeatureSource(typeName).getCount(Query.ALL));
} catch (Exception e) {
tx.rollback();
throw e;
} finally {
tx.close();
}
store.setTransaction(Transaction.AUTO_COMMIT);
assertEquals(expected, store.getFeatures().size());
assertEquals(0, store.getFeatures(filter).size());
// ObjectId indexTreeId;
// ObjectId canonicalTreeId;
// String extraAttributes;
// IndexTestSupport.verifyIndex(geogig, indexTreeId, canonicalTreeId, extraAttributes);
}
public static NodeRef createOddLayer(String typeName, Repository repository) {
String typeSpec = "geom:Point:srid=4326,x:Double,y:Double,xystr:String";
SimpleFeatureType type;
try {
type = DataUtilities.createType(typeName, typeSpec);
} catch (SchemaException e) {
throw new RuntimeException(e);
}
RevTree tree = IndexTestSupport.createWorldPointsTree(repository);
WorkingTree workingTree = repository.workingTree();
NodeRef typeTreeRef = workingTree.createTypeTree(type.getTypeName(), type);
ObjectStore store = repository.objectDatabase();
CanonicalTreeBuilder newRootBuilder = CanonicalTreeBuilder.create(store,
workingTree.getTree());
NodeRef newTypeTreeRef = typeTreeRef.update(tree.getId(), SpatialOps.boundsOf(tree));
newRootBuilder.put(newTypeTreeRef.getNode());
RevTree newWorkTree = newRootBuilder.build();
workingTree.updateWorkHead(newWorkTree.getId());
return newTypeTreeRef;
}
public static RevTree createTree_AllFeaturesSameGeometry(int numFeatues,
Repository repository) {
double x = 1.000000001;
double y = 2.000000002;
ObjectStore store = repository.objectDatabase();
CanonicalTreeBuilder builder = CanonicalTreeBuilder.create(store);
for (int i = 0; i < numFeatues; i++) {
String fid = String.valueOf(i);
RevFeature feature;
feature = IndexTestSupport.createPointFeature(x, y, Double.valueOf(i),
Double.valueOf(i), fid);
Envelope env = SpatialOps.boundsOf(feature);
ObjectId oid = feature.getId();
Node node = RevObjectFactory.defaultInstance().createNode(fid, oid, ObjectId.NULL,
TYPE.FEATURE, env, null);
store.put(feature);
builder.put(node);
}
RevTree tree = builder.build();
return tree;
}
}