All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Index Maven / Gradle / Ivy

The newest version!
package com.tinkerpop.blueprints.impls.neo4j2;

import com.tinkerpop.blueprints.CloseableIterable;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Index;
import com.tinkerpop.blueprints.Parameter;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.StringFactory;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.graphdb.index.IndexManager;

import java.util.HashMap;
import java.util.Map;


/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class Neo4j2Index implements Index {

    private final Class indexClass;
    protected final Neo4j2Graph graph;
    private final String indexName;
    protected org.neo4j.graphdb.index.Index rawIndex;

    protected Neo4j2Index(final String indexName, final Class indexClass, final Neo4j2Graph graph, final Parameter... indexParameters) {
        this.indexClass = indexClass;
        this.graph = graph;
        this.indexName = indexName;
        this.generateIndex(indexParameters);
    }

    public Class getIndexClass() {
        if (Vertex.class.isAssignableFrom(this.indexClass))
            return (Class) Vertex.class;
        else
            return (Class) Edge.class;
    }

    public String getIndexName() {
        return this.indexName;
    }

    public void put(final String key, final Object value, final T element) {
        try {
            this.graph.autoStartTransaction(true);
            this.rawIndex.add((S) element.getRawElement(), key, value);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * {@inheritDoc}
     * 

* The underlying Neo4j graph does not natively support this method within a transaction. * If the graph is not currently in a transaction, then the operation runs efficiently. * If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction. */ public CloseableIterable get(final String key, final Object value) { this.graph.autoStartTransaction(false); final IndexHits itty = this.rawIndex.get(key, value); if (this.indexClass.isAssignableFrom(Neo4j2Vertex.class)) return new Neo4j2VertexIterable((Iterable) itty, this.graph, this.graph.checkElementsInTransaction()); return new Neo4j2EdgeIterable((Iterable) itty, this.graph, this.graph.checkElementsInTransaction()); } /** * {@inheritDoc} *

* The underlying Neo4j graph does not natively support this method within a transaction. * If the graph is not currently in a transaction, then the operation runs efficiently. * If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction. */ public CloseableIterable query(final String key, final Object query) { this.graph.autoStartTransaction(false); final IndexHits itty = this.rawIndex.query(key, query); if (this.indexClass.isAssignableFrom(Neo4j2Vertex.class)) return new Neo4j2VertexIterable((Iterable) itty, this.graph, this.graph.checkElementsInTransaction()); return new Neo4j2EdgeIterable((Iterable) itty, this.graph, this.graph.checkElementsInTransaction()); } /** * {@inheritDoc} *

* The underlying Neo4j graph does not natively support this method within a transaction. * If the graph is not currently in a transaction, then the operation runs efficiently. * If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction. */ public CloseableIterable query(final Object query) { this.graph.autoStartTransaction(false); final IndexHits itty = this.rawIndex.query(query); if (this.indexClass.isAssignableFrom(Neo4j2Vertex.class)) return new Neo4j2VertexIterable((Iterable) itty, this.graph, this.graph.checkElementsInTransaction()); return new Neo4j2EdgeIterable((Iterable) itty, this.graph, this.graph.checkElementsInTransaction()); } /** * {@inheritDoc} *

* The underlying Neo4j graph does not natively support this method within a transaction. * If the graph is not currently in a transaction, then the operation runs efficiently. * If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction. */ public long count(final String key, final Object value) { this.graph.autoStartTransaction(false); if (!this.graph.checkElementsInTransaction()) { final IndexHits hits = this.rawIndex.get(key, value); final long count = hits.size(); hits.close(); return count; } else { final CloseableIterable hits = this.get(key, value); long count = 0; for (final T t : hits) { count++; } hits.close(); return count; } } public void remove(final String key, final Object value, final T element) { try { this.graph.autoStartTransaction(true); this.rawIndex.remove((S) element.getRawElement(), key, value); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } private void generateIndex(final Parameter... indexParameters) { this.graph.autoStartTransaction(true); final IndexManager manager = this.graph.getRawGraph().index(); if (Vertex.class.isAssignableFrom(this.indexClass)) { if (indexParameters.length > 0) this.rawIndex = (org.neo4j.graphdb.index.Index) manager.forNodes(this.indexName, generateParameterMap(indexParameters)); else this.rawIndex = (org.neo4j.graphdb.index.Index) manager.forNodes(this.indexName); } else { if (indexParameters.length > 0) this.rawIndex = (org.neo4j.graphdb.index.Index) manager.forRelationships(this.indexName, generateParameterMap(indexParameters)); else this.rawIndex = (org.neo4j.graphdb.index.Index) manager.forRelationships(this.indexName); } } public String toString() { return StringFactory.indexString(this); } private static Map generateParameterMap(final Parameter... indexParameters) { final Map map = new HashMap(); for (final Parameter parameter : indexParameters) { map.put(parameter.getKey().toString(), parameter.getValue().toString()); } return map; } public org.neo4j.graphdb.index.Index getRawIndex() { return this.rawIndex; } }