public IndexRepresentation createRelationshipIndex(Map<String, Object> indexSpecification) {
    final String indexName = (String) indexSpecification.get("name");
    if (indexSpecification.containsKey("config")) {

      @SuppressWarnings("unchecked")
      Map<String, String> config = (Map<String, String>) indexSpecification.get("config");
      graphDb.index().forRelationships(indexName, config);

      return new RelationshipIndexRepresentation(indexName, config);
    }

    graphDb.index().forRelationships(indexName);
    return new RelationshipIndexRepresentation(indexName, Collections.<String, String>emptyMap());
  }
  public ListRepresentation getIndexedRelationshipsByQuery(
      String indexName, String key, String query) {
    if (!graphDb.index().existsForRelationships(indexName)) throw new NotFoundException();
    List<Representation> representations = new ArrayList<Representation>();
    Index<Relationship> index = graphDb.index().forRelationships(indexName);

    Transaction tx = graphDb.beginTx();
    try {
      for (Relationship rel : index.query(key, query)) {
        representations.add(new RelationshipRepresentation(rel));
      }
      tx.success();
      return new ListRepresentation(RepresentationType.RELATIONSHIP, representations);
    } finally {
      tx.finish();
    }
  }
  public ListRepresentation getIndexedNodesByQuery(String indexName, String key, String query) {
    if (!graphDb.index().existsForNodes(indexName)) throw new NotFoundException();
    Index<Node> index = graphDb.index().forNodes(indexName);
    List<Representation> representations = new ArrayList<Representation>();

    Transaction tx = graphDb.beginTx();
    try {
      if (query != null) {
        for (Node node : index.query(key, query)) {
          representations.add(new NodeRepresentation(node));
        }
      }
      tx.success();
      return new ListRepresentation(RepresentationType.NODE, representations);
    } finally {
      tx.finish();
    }
  }
  public ListRepresentation getIndexedRelationships(String indexName, String key, String value) {
    if (!graphDb.index().existsForRelationships(indexName)) throw new NotFoundException();
    List<IndexedEntityRepresentation> representations =
        new ArrayList<IndexedEntityRepresentation>();
    Index<Relationship> index = graphDb.index().forRelationships(indexName);

    Transaction tx = graphDb.beginTx();
    try {
      IndexRepresentation indexRepresentation = new RelationshipIndexRepresentation(indexName);
      for (Relationship node : index.get(key, value)) {
        representations.add(new IndexedEntityRepresentation(node, key, value, indexRepresentation));
      }
      tx.success();
      return new ListRepresentation(RepresentationType.RELATIONSHIP, representations);
    } finally {
      tx.finish();
    }
  }
  public ListRepresentation getIndexedNodes(String indexName, String key, String value) {
    if (!graphDb.index().existsForNodes(indexName)) throw new NotFoundException();
    Index<Node> index = graphDb.index().forNodes(indexName);
    List<IndexedEntityRepresentation> representations =
        new ArrayList<IndexedEntityRepresentation>();

    Transaction tx = graphDb.beginTx();
    try {
      IndexRepresentation indexRepresentation = new NodeIndexRepresentation(indexName);
      IndexHits<Node> indexHits = index.get(key, value);
      for (Node node : indexHits) {
        representations.add(new IndexedEntityRepresentation(node, key, value, indexRepresentation));
      }
      tx.success();
      return new ListRepresentation(RepresentationType.NODE, representations);
    } finally {
      tx.finish();
    }
  }
 public void removeRelationshipIndex(String indexName) {
   Index<Relationship> index = graphDb.index().forRelationships(indexName);
   Transaction tx = graphDb.beginTx();
   try {
     index.delete();
     tx.success();
   } finally {
     tx.finish();
   }
 }
 public void removeNodeIndex(String indexName) {
   Index<Node> index = graphDb.index().forNodes(indexName);
   Transaction tx = graphDb.beginTx();
   try {
     index.delete();
     tx.success();
   } finally {
     tx.finish();
   }
 }
 public void removeFromRelationshipIndexNoKeyValue(String indexName, long id) {
   RelationshipIndex index = graphDb.index().forRelationships(indexName);
   Transaction tx = graphDb.beginTx();
   try {
     index.remove(graphDb.getRelationshipById(id));
     tx.success();
   } finally {
     tx.finish();
   }
 }
 public void removeFromNodeIndexNoKeyValue(String indexName, long id) {
   Index<Node> index = graphDb.index().forNodes(indexName);
   Transaction tx = graphDb.beginTx();
   try {
     index.remove(graphDb.getNodeById(id));
     tx.success();
   } finally {
     tx.finish();
   }
 }
  public boolean nodeIsIndexed(String indexName, String key, Object value, long nodeId)
      throws DatabaseBlockedException {

    Index<Node> index = graphDb.index().forNodes(indexName);
    Transaction tx = graphDb.beginTx();
    try {
      Node expectedNode = graphDb.getNodeById(nodeId);
      IndexHits<Node> hits = index.get(key, value);
      boolean contains = iterableContains(hits, expectedNode);
      tx.success();
      return contains;
    } finally {
      tx.finish();
    }
  }
  public boolean relationshipIsIndexed(
      String indexName, String key, Object value, long relationshipId)
      throws DatabaseBlockedException {

    Index<Relationship> index = graphDb.index().forRelationships(indexName);
    Transaction tx = graphDb.beginTx();
    try {
      Relationship expectedNode = graphDb.getRelationshipById(relationshipId);
      IndexHits<Relationship> hits = index.get(key, value);
      boolean contains = iterableContains(hits, expectedNode);
      tx.success();
      return contains;
    } finally {
      tx.finish();
    }
  }
  public Representation getAutoIndexedRelationships(String key, String value) {

    List<Representation> representations = new ArrayList<Representation>();
    ReadableRelationshipIndex index = graphDb.index().getRelationshipAutoIndexer().getAutoIndex();

    Transaction tx = graphDb.beginTx();
    try {
      for (Relationship rel : index.get(key, value)) {
        representations.add(new RelationshipRepresentation(rel));
      }
      tx.success();
      return new ListRepresentation(RepresentationType.RELATIONSHIP, representations);
    } finally {
      tx.finish();
    }
  }
  public Representation getAutoIndexedNodes(String key, String value) {

    List<Representation> representations = new ArrayList<Representation>();
    ReadableIndex<Node> index = graphDb.index().getNodeAutoIndexer().getAutoIndex();

    Transaction tx = graphDb.beginTx();
    try {
      for (Node node : index.get(key, value)) {
        representations.add(new NodeRepresentation(node));
      }
      tx.success();
      return new ListRepresentation(RepresentationType.NODE, representations);
    } finally {
      tx.finish();
    }
  }
 public IndexedEntityRepresentation addToNodeIndex(
     String indexName, String key, String value, long nodeId) {
   Transaction tx = graphDb.beginTx();
   try {
     Node node = graphDb.getNodeById(nodeId);
     Index<Node> index = graphDb.index().forNodes(indexName);
     index.add(node, key, value);
     tx.success();
     return new IndexedEntityRepresentation(
         node,
         key,
         value,
         new NodeIndexRepresentation(indexName, Collections.<String, String>emptyMap()));
   } finally {
     tx.finish();
   }
 }
  public ListRepresentation getAutoIndexedNodesByQuery(String query) {
    ReadableIndex<Node> index = graphDb.index().getNodeAutoIndexer().getAutoIndex();
    List<Representation> representations = new ArrayList<Representation>();

    if (query != null) {
      Transaction tx = graphDb.beginTx();
      try {
        for (Node node : index.query(query)) {
          representations.add(new NodeRepresentation(node));
        }
        tx.success();
      } finally {
        tx.finish();
      }
    }
    return new ListRepresentation(RepresentationType.NODE, representations);
  }
 public IndexedEntityRepresentation addToRelationshipIndex(
     String indexName, String key, String value, long relationshipId) {
   Transaction tx = graphDb.beginTx();
   try {
     Relationship relationship = graphDb.getRelationshipById(relationshipId);
     Index<Relationship> index = graphDb.index().forRelationships(indexName);
     index.add(relationship, key, value);
     tx.success();
     return new IndexedEntityRepresentation(
         relationship,
         key,
         value,
         new RelationshipIndexRepresentation(indexName, Collections.<String, String>emptyMap()));
   } finally {
     tx.finish();
   }
 }
  public ListRepresentation getAutoIndexedRelationshipsByQuery(String query) {
    ReadableRelationshipIndex index = graphDb.index().getRelationshipAutoIndexer().getAutoIndex();
    List<Representation> representations = new ArrayList<Representation>();

    if (query != null) {
      Transaction tx = graphDb.beginTx();
      try {
        for (Relationship rel : index.query(query)) {
          representations.add(new RelationshipRepresentation(rel));
        }
        tx.success();
      } finally {
        tx.finish();
      }
    }
    return new ListRepresentation(RepresentationType.RELATIONSHIP, representations);
  }
 public Representation nodeIndexRoot() {
   return new NodeIndexRootRepresentation(graphDb.index());
 }
 public Representation relationshipIndexRoot() {
   return new RelationshipIndexRootRepresentation(graphDb.index());
 }
 public String[] getRelationshipIndexNames() {
   return graphDb.index().relationshipIndexNames();
 }
 private void index(Node n) {
   db.index().forNodes("people").add(n, "name", n.getProperty("name"));
 }
 public String[] getNodeIndexNames() {
   return graphDb.index().nodeIndexNames();
 }