<commit_before>/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.gis.spatial.indexprovider;

import java.util.List;
import java.util.Map;

import org.neo4j.gis.spatial.EditableLayer;
import org.neo4j.gis.spatial.Search;
import org.neo4j.gis.spatial.SpatialDatabaseRecord;
import org.neo4j.gis.spatial.SpatialDatabaseService;
import org.neo4j.gis.spatial.query.SearchPointsWithinOrthodromicDistance;
import org.neo4j.gis.spatial.query.SearchWithin;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Point;

public class LayerNodeIndex implements Index<Node>
{

    public static final String LON_PROPERTY_KEY = "lon";
    public static final String LAT_PROPERTY_KEY = "lat";
    public static final String WITHIN_QUERY = "within";
    public static final String WITHIN_DISTANCE_QUERY = "withinDistance";
    public static final String ENVELOPE_PARAMETER = "envelope";
    public static final String POINT_PARAMETER = "point";
    public static final String DISTANCE_IN_KM_PARAMETER = "distanceInKm";
    private final String layerName;
    private final GraphDatabaseService db;
    private SpatialDatabaseService spatialDB;
    private EditableLayer layer;

    public LayerNodeIndex( String indexName, GraphDatabaseService db,
            Map<String, String> config )
    {
        this.layerName = indexName;
        this.db = db;
        spatialDB = new SpatialDatabaseService( this.db );
        layer = (EditableLayer) spatialDB.getOrCreateEditableLayer( layerName );

    }

    public String getName()
    {
        return layerName;
    }

    public Class<Node> getEntityType()
    {
        return Node.class;
    }

    /**
     * right now we are assuming only Points with "lat" and "long" properties
     */
    public void add( Node geometry, String key, Object value )
    {
        double lon = (Double) geometry.getProperty( LON_PROPERTY_KEY );
        double lat = (Double) geometry.getProperty( LAT_PROPERTY_KEY );
        layer.add( layer.getGeometryFactory().createPoint(
        new Coordinate( lon, lat ) ), new String[] { "id" },
        new Object[] { geometry.getId() } );

    }

    public void remove( Node entity, String key, Object value )
    {
       layer.delete( entity.getId() );
    }

    public void delete()
    {
    }

    /**
     * Not supported at the moment
     */
    public IndexHits<Node> get( String key, Object value )
    {
        return null;
    }

    public IndexHits<Node> query( String key, Object params )
    {
        if ( key.equals( WITHIN_QUERY ) )
        {
            Map<?, ?> p = (Map<?, ?>) params;
            Double[] bounds = (Double[]) p.get( ENVELOPE_PARAMETER );
            SearchWithin withinQuery = new SearchWithin(
                    layer.getGeometryFactory().toGeometry(
                            new Envelope( bounds[0], bounds[1], bounds[2], bounds[3] ) ) );
            layer.getIndex().executeSearch( withinQuery );
            List<SpatialDatabaseRecord> res = withinQuery.getResults();
            IndexHits<Node> results = new SpatialRecordHits( res );
            return results;
        }
        else if ( key.equals( WITHIN_DISTANCE_QUERY ) )
        {
			Map<?, ?> p = (Map<?, ?>) params;
			Double[] point = (Double[]) p.get( POINT_PARAMETER );
			Double distance = (Double) p.get( DISTANCE_IN_KM_PARAMETER );
			Point refPoint = layer.getGeometryFactory().createPoint(
					new Coordinate( point[1], point[0] ) );
			Search withinDistanceQuery = 
				new SearchPointsWithinOrthodromicDistance( refPoint, distance );
			layer.getIndex().executeSearch( withinDistanceQuery );
			List<SpatialDatabaseRecord> res = withinDistanceQuery.getResults();
			IndexHits<Node> results = new SpatialRecordHits( res );
			return results;
		}
        else
        {
            throw new UnsupportedOperationException( String.format(
            		"only %s and %s are implemented.", 
            		WITHIN_QUERY, WITHIN_DISTANCE_QUERY ) );
        }
    }

    public IndexHits<Node> query( Object queryOrQueryObject )
    {
        return query(layerName, queryOrQueryObject);
    }

    @Override
    public void remove(Node node, String s) {
        layer.delete(node.getId());
    }

    @Override
    public void remove(Node node) {
        layer.delete(node.getId());
    }
}<commit_msg>Removed compile errors from eclipse compile<commit_after>/**
 * Copyright (c) 2002-2011 "<NAME>,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.gis.spatial.indexprovider;

import java.util.List;
import java.util.Map;

import org.neo4j.gis.spatial.EditableLayer;
import org.neo4j.gis.spatial.Search;
import org.neo4j.gis.spatial.SpatialDatabaseRecord;
import org.neo4j.gis.spatial.SpatialDatabaseService;
import org.neo4j.gis.spatial.query.SearchPointsWithinOrthodromicDistance;
import org.neo4j.gis.spatial.query.SearchWithin;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Point;

public class LayerNodeIndex implements Index<Node>
{

    public static final String LON_PROPERTY_KEY = "lon";
    public static final String LAT_PROPERTY_KEY = "lat";
    public static final String WITHIN_QUERY = "within";
    public static final String WITHIN_DISTANCE_QUERY = "withinDistance";
    public static final String ENVELOPE_PARAMETER = "envelope";
    public static final String POINT_PARAMETER = "point";
    public static final String DISTANCE_IN_KM_PARAMETER = "distanceInKm";
    private final String layerName;
    private final GraphDatabaseService db;
    private SpatialDatabaseService spatialDB;
    private EditableLayer layer;

    public LayerNodeIndex( String indexName, GraphDatabaseService db,
            Map<String, String> config )
    {
        this.layerName = indexName;
        this.db = db;
        spatialDB = new SpatialDatabaseService( this.db );
        layer = (EditableLayer) spatialDB.getOrCreateEditableLayer( layerName );

    }

    public String getName()
    {
        return layerName;
    }

    public Class<Node> getEntityType()
    {
        return Node.class;
    }

    /**
     * right now we are assuming only Points with "lat" and "long" properties
     */
    public void add( Node geometry, String key, Object value )
    {
        double lon = (Double) geometry.getProperty( LON_PROPERTY_KEY );
        double lat = (Double) geometry.getProperty( LAT_PROPERTY_KEY );
        layer.add( layer.getGeometryFactory().createPoint(
        new Coordinate( lon, lat ) ), new String[] { "id" },
        new Object[] { geometry.getId() } );

    }

    public void remove( Node entity, String key, Object value )
    {
       layer.delete( entity.getId() );
    }

    public void delete()
    {
    }

    /**
     * Not supported at the moment
     */
    public IndexHits<Node> get( String key, Object value )
    {
        return null;
    }

    public IndexHits<Node> query( String key, Object params )
    {
        if ( key.equals( WITHIN_QUERY ) )
        {
            Map<?, ?> p = (Map<?, ?>) params;
            Double[] bounds = (Double[]) p.get( ENVELOPE_PARAMETER );
            SearchWithin withinQuery = new SearchWithin(
                    layer.getGeometryFactory().toGeometry(
                            new Envelope( bounds[0], bounds[1], bounds[2], bounds[3] ) ) );
            layer.getIndex().executeSearch( withinQuery );
            List<SpatialDatabaseRecord> res = withinQuery.getResults();
            IndexHits<Node> results = new SpatialRecordHits( res );
            return results;
        }
        else if ( key.equals( WITHIN_DISTANCE_QUERY ) )
        {
			Map<?, ?> p = (Map<?, ?>) params;
			Double[] point = (Double[]) p.get( POINT_PARAMETER );
			Double distance = (Double) p.get( DISTANCE_IN_KM_PARAMETER );
			Point refPoint = layer.getGeometryFactory().createPoint(
					new Coordinate( point[1], point[0] ) );
			Search withinDistanceQuery = 
				new SearchPointsWithinOrthodromicDistance( refPoint, distance );
			layer.getIndex().executeSearch( withinDistanceQuery );
			List<SpatialDatabaseRecord> res = withinDistanceQuery.getResults();
			IndexHits<Node> results = new SpatialRecordHits( res );
			return results;
		}
        else
        {
            throw new UnsupportedOperationException( String.format(
            		"only %s and %s are implemented.", 
            		WITHIN_QUERY, WITHIN_DISTANCE_QUERY ) );
        }
    }

    public IndexHits<Node> query( Object queryOrQueryObject )
    {
        return query(layerName, queryOrQueryObject);
    }

    public void remove(Node node, String s) {
        layer.delete(node.getId());
    }

    public void remove(Node node) {
        layer.delete(node.getId());
    }
}
{'fraction_non_alphanumeric': 0.09724320241691843, 'fraction_numerical': 0.005287009063444109, 'mean_word_length': 2.989830508474576, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873523', 'n_tokens_mistral': 3203, 'n_tokens_neox': 3080, 'n_words': 874}