Skip to content

Commit

Permalink
added spatial transaction tests + implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 6, 2015
1 parent 910ee5b commit 0250e10
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,27 @@ private int calculateDeletedMatch() {
for (Document doc : deletedDocs) {
memoryIndex.reset();
for (IndexableField field : doc.getFields()) {
memoryIndex.addField(field.name(), field.stringValue(), manager.analyzer(field.name()));

if (field.stringValue() != null) {
memoryIndex.addField(field.name(), field.stringValue(), manager.analyzer(field.name()));
} else {
try {
memoryIndex.addField(field.name(), field.tokenStream(manager.analyzer(field.name()), null));
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (queryContext.filter != null) {
try {
TopDocs topDocs1 = memoryIndex.createSearcher().search(query, queryContext.filter, 1);
match += topDocs1.totalHits;
} catch (IOException e) {
e.printStackTrace();
}
} else {
match += (memoryIndex.search(query) > 0.0f) ? 1 : 0;
}
match += (memoryIndex.search(query) > 0.0f) ? 1 : 0;
}
return match;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package com.orientechnologies.lucene.engine;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.lucene.collections.LuceneResultSet;
import com.orientechnologies.lucene.collections.LuceneResultSetFactory;
import com.orientechnologies.lucene.query.QueryContext;
import com.orientechnologies.lucene.query.SpatialQueryContext;
import com.orientechnologies.lucene.tx.OLuceneTxChanges;
Expand All @@ -33,7 +33,6 @@
import org.apache.lucene.document.Document;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.spatial.SpatialStrategy;
import org.apache.lucene.spatial.bbox.BBoxStrategy;

import java.util.Collection;
import java.util.HashMap;
Expand All @@ -47,8 +46,9 @@ public OLuceneGeoSpatialIndexEngine(String name, OShapeBuilder factory) {

@Override
protected SpatialStrategy createSpatialStrategy(OIndexDefinition indexDefinition, ODocument metadata) {
return new BBoxStrategy(ctx, "location");
// return new RecursivePrefixTreeStrategy(new PackedQuadPrefixTree(ctx, 11), "location");

return strategyFactory.createStrategy(ctx, getDatabase(), indexDefinition, metadata);

}

@Override
Expand All @@ -60,7 +60,7 @@ public Object get(Object key) {
public Object getInTx(Object key, OLuceneTxChanges changes) {
try {
if (key instanceof Map) {
return newGeoSearch((Map<String, Object>) key);
return newGeoSearch((Map<String, Object>) key, changes);

} else {
// TODO HANDLE EXCEPTION
Expand All @@ -72,8 +72,10 @@ public Object getInTx(Object key, OLuceneTxChanges changes) {
return null;
}

private Object newGeoSearch(Map<String, Object> key) throws Exception {
return new LuceneResultSet(this, queryStrategy.build(key));
private Object newGeoSearch(Map<String, Object> key, OLuceneTxChanges changes) throws Exception {

QueryContext queryContext = queryStrategy.build(key).setChanges(changes);
return LuceneResultSetFactory.INSTANCE.create(this, queryContext);

}

Expand Down Expand Up @@ -108,4 +110,9 @@ public void put(Object key, Object value) {
}
}

@Override
public Document buildDocument(Object key, OIdentifiable value) {
ODocument location = ((OIdentifiable) key).getRecord();
return newGeoDocument(value, factory.fromDoc(location));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ public void sendLookupTime(OCommandContext context, final TopDocs docs, final In
put("totalTime", finalTime);
put("totalHits", docs.totalHits);
put("returnedHits", docs.scoreDocs.length);
put("maxScore", docs.getMaxScore());
if (!Float.isNaN(docs.getMaxScore())) {
put("maxScore", docs.getMaxScore());
}

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.orientechnologies.lucene.engine;

import com.orientechnologies.lucene.OLuceneIndexType;
import com.orientechnologies.lucene.factory.OSpatialStrategyFactory;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.index.OIndexCursor;
import com.orientechnologies.orient.core.index.OIndexDefinition;
Expand Down Expand Up @@ -48,16 +49,18 @@
*/
public abstract class OLuceneSpatialIndexEngineAbstract extends OLuceneIndexEngineAbstract implements OLuceneSpatialIndexContainer {

protected final OShapeBuilder factory;
protected SpatialContext ctx;
protected SpatialStrategy strategy;
protected final OShapeBuilder factory;
protected SpatialContext ctx;
protected SpatialStrategy strategy;

protected SpatialQueryBuilder queryStrategy;
protected OSpatialStrategyFactory strategyFactory;
protected SpatialQueryBuilder queryStrategy;

public OLuceneSpatialIndexEngineAbstract(String indexName, OShapeBuilder factory) {
super(indexName);
this.ctx = factory.getSpatialContext();
this.ctx = factory.context();
this.factory = factory;
strategyFactory = new OSpatialStrategyFactory(factory);
this.queryStrategy = new SpatialQueryBuilder(this, factory);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
*
* * Copyright 2014 Orient Technologies.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.orientechnologies.lucene.factory;

import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.index.OIndexDefinition;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.spatial.shape.OShapeBuilder;
import com.spatial4j.core.context.SpatialContext;
import org.apache.lucene.spatial.SpatialStrategy;
import org.apache.lucene.spatial.bbox.BBoxStrategy;
import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;

/**
* Created by Enrico Risa on 02/10/15.
*/
public class OSpatialStrategyFactory {

private OShapeBuilder factory;

public OSpatialStrategyFactory(OShapeBuilder factory) {
this.factory = factory;
}

public SpatialStrategy createStrategy(SpatialContext ctx, ODatabaseDocumentInternal db, OIndexDefinition indexDefinition,
ODocument metadata) {

SpatialStrategy strategy;

OClass aClass = db.getMetadata().getSchema().getClass(indexDefinition.getClassName());

OProperty property = aClass.getProperty(indexDefinition.getFields().get(0));

OClass linkedClass = property.getLinkedClass();

if ("OPoint".equalsIgnoreCase(linkedClass.getName())) {
strategy = new RecursivePrefixTreeStrategy(new GeohashPrefixTree(ctx, 11), "location");
} else {
strategy = new BBoxStrategy(ctx, "location");
}
return strategy;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ public Object callEngine(OIndexEngine engine) {
});
}

protected Object encodeKey(Object key) {
return key;
}

protected Object decodeKey(Object key) {
return key;
}

@Override
public OLuceneIndexNotUnique put(final Object key, final OIdentifiable singleValue) {

OTransaction transaction = getDatabase().getTransaction();
if (transaction.isActive()) {
OLuceneTxChanges transactionChanges = getTransactionChanges(transaction);
transaction.addIndexEntry(this, super.getName(), OTransactionIndexChanges.OPERATION.PUT, key, singleValue);
transaction.addIndexEntry(this, super.getName(), OTransactionIndexChanges.OPERATION.PUT, encodeKey(key), singleValue);

Document luceneDoc = storage.callIndexEngine(false, false, indexId, new OIndexEngineCallback<Document>() {
@Override
Expand Down Expand Up @@ -144,7 +152,7 @@ public boolean remove(final Object key, final OIdentifiable value) {
OTransaction transaction = getDatabase().getTransaction();
if (transaction.isActive()) {

transaction.addIndexEntry(this, super.getName(), OTransactionIndexChanges.OPERATION.REMOVE, key, value);
transaction.addIndexEntry(this, super.getName(), OTransactionIndexChanges.OPERATION.REMOVE, encodeKey(key), value);
OLuceneTxChanges transactionChanges = getTransactionChanges(transaction);
try {
transactionChanges.remove(key, value);
Expand Down Expand Up @@ -243,7 +251,7 @@ public Boolean callEngine(OIndexEngine engine) {
checkForKeyType(key);

for (OIdentifiable oIdentifiable : operations.removed) {
indexEngine.remove(key, oIdentifiable);
indexEngine.remove(decodeKey(key), oIdentifiable);
}

}
Expand All @@ -252,7 +260,7 @@ public Boolean callEngine(OIndexEngine engine) {
LuceneTxOperations operations = (LuceneTxOperations) snapshotEntry.getValue();
checkForKeyType(key);

indexEngine.put(key, operations.added);
indexEngine.put(decodeKey(key), operations.added);

}
OTransaction transaction = getDatabase().getTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@

import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage;
import com.orientechnologies.orient.spatial.shape.OShapeFactory;
import com.spatial4j.core.shape.Shape;
import com.vividsolutions.jts.geom.Geometry;

public class OLuceneSpatialIndex extends OLuceneIndexNotUnique {

public OLuceneSpatialIndex(String name, String typeId, String algorithm,int version, OAbstractPaginatedStorage storage,
String valueContainerAlgorithm, ODocument metadata) {
super(name, typeId, algorithm,version, storage, valueContainerAlgorithm, metadata);
}


OShapeFactory shapeFactory = OShapeFactory.INSTANCE;

public OLuceneSpatialIndex(String name, String typeId, String algorithm, int version, OAbstractPaginatedStorage storage,
String valueContainerAlgorithm, ODocument metadata) {
super(name, typeId, algorithm, version, storage, valueContainerAlgorithm, metadata);

}

@Override
protected Object encodeKey(Object key) {

Shape shape = shapeFactory.fromDoc((ODocument) key);
return shapeFactory.toGeometry(shape);
}

@Override
protected Object decodeKey(Object key) {
Geometry geom = (Geometry) key;
return shapeFactory.toDoc(geom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public Object evaluateRecord(OIdentifiable iRecord, ODocument iCurrentResult, OS
double lat = left.get(0).doubleValue();
double lon = left.get(1).doubleValue();

Shape shape = factory.SPATIAL_CONTEXT.makePoint(lon, lat);
Shape shape = factory.context().makePoint(lon, lat);
List<Number> right = (List<Number>) iRight;

double lat1 = right.get(0).doubleValue();
double lon1 = right.get(1).doubleValue();
Shape shape1 = factory.SPATIAL_CONTEXT.makePoint(lon1, lat1);
Shape shape1 = factory.context().makePoint(lon1, lat1);

Map map = (Map) right.get(2);
double distance = 0;
Expand All @@ -73,9 +73,9 @@ public Object evaluateRecord(OIdentifiable iRecord, ODocument iCurrentResult, OS
distance = n.doubleValue();
}
Point p = (Point) shape1;
Circle circle = factory.SPATIAL_CONTEXT.makeCircle(p.getX(), p.getY(),
Circle circle = factory.context().makeCircle(p.getX(), p.getY(),
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM));
double docDistDEG = factory.SPATIAL_CONTEXT.getDistCalc().distance((Point) shape, p);
double docDistDEG = factory.context().getDistCalc().distance((Point) shape, p);
final double docDistInKM = DistanceUtils.degrees2Dist(docDistDEG, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM);
iContext.setVariable("distance", docDistInKM);
return shape.relate(circle) == SpatialRelation.WITHIN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index,
cursor = new OIndexCursorCollectionValue(((Collection<OIdentifiable>) indexResult).iterator(), new OSpatialCompositeKey(
keyParams));

indexResult.sendLookupTime(iContext, start);
if (indexResult != null)
indexResult.sendLookupTime(iContext, start);
return cursor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurren
distance = n.doubleValue();
}
Point p = (Point) shape1;
Circle circle = factory.SPATIAL_CONTEXT.makeCircle(p.getX(), p.getY(),
Circle circle = factory.context().makeCircle(p.getX(), p.getY(),
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM));
double docDistDEG = factory.SPATIAL_CONTEXT.getDistCalc().distance((Point) shape, p);
double docDistDEG = factory.context().getDistCalc().distance((Point) shape, p);
final double docDistInKM = DistanceUtils.degrees2Dist(docDistDEG, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM);
iContext.setVariable("distance", docDistInKM);
return shape.relate(circle) == SpatialRelation.WITHIN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

public abstract class OShapeBuilder<T extends Shape> {

public static final JtsSpatialContext SPATIAL_CONTEXT;
public static final GeometryFactory GEOMETRY_FACTORY;
protected static final JtsSpatialContext SPATIAL_CONTEXT;
protected static final GeometryFactory GEOMETRY_FACTORY;
static {

JtsSpatialContextFactory factory = new JtsSpatialContextFactory();
Expand All @@ -46,8 +46,8 @@ public abstract class OShapeBuilder<T extends Shape> {
SPATIAL_CONTEXT = new JtsSpatialContext(factory);
GEOMETRY_FACTORY = SPATIAL_CONTEXT.getGeometryFactory();
}
public static final String COORDINATES = "coordinates";
public static final String BASE_CLASS = "OShape";
public static final String COORDINATES = "coordinates";
public static final String BASE_CLASS = "OShape";

public abstract String getName();

Expand Down Expand Up @@ -143,10 +143,6 @@ public ODocument toDoc(String wkt) throws ParseException {
return toDoc(parsed);
}

public static SpatialContext getSpatialContext() {
return SPATIAL_CONTEXT;
}

public int getSRID(Shape shape) {
Geometry geometry = toGeometry(shape);
return geometry.getSRID();
Expand All @@ -172,4 +168,8 @@ private void bindParameters(BufferParameters parameters, Map<String, Object> par

}

public SpatialContext context() {
return SPATIAL_CONTEXT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ public Shape fromMapGeoJson(Map geoJsonMap) {
// TODO handle exception shape not found
}

public Geometry toGeometry(Shape shape) {
return SPATIAL_CONTEXT.getGeometryFrom(shape);
}

public ODocument toDoc(Geometry geometry) {
if (geometry instanceof com.vividsolutions.jts.geom.Point) {
com.vividsolutions.jts.geom.Point point = (com.vividsolutions.jts.geom.Point) geometry;
Point point1 = context().makePoint(point.getX(), point.getY());
return toDoc(point1);
}
return toDoc(SPATIAL_CONTEXT.makeShape(geometry));
}

public OShapeOperation operation() {
return operation;
}
Expand Down
Loading

0 comments on commit 0250e10

Please sign in to comment.