Skip to content

Instantly share code, notes, and snippets.

@MFrancesco
Created February 12, 2015 10:02
Show Gist options
  • Save MFrancesco/c2e9c38b91f873246251 to your computer and use it in GitHub Desktop.
Save MFrancesco/c2e9c38b91f873246251 to your computer and use it in GitHub Desktop.
Just some utils i created to work with and test Titan graph db
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.TitanIndexQuery;
import com.thinkaurelius.titan.core.schema.Mapping;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.tinkerpop.blueprints.Vertex;
import org.apache.log4j.Logger;
import org.apache.commons.collections.IteratorUtils;
import java.util.LinkedList;
import java.util.List;
/**
* Graph utils, useful for testing
*/
public class GraphUtils {
static Logger logger = Logger.getLogger(GraphUtils.class);
static String DEFAULT_IN_MEMORY_DIR = "/tmp/inMemoryGraph";
static String DEFAULT_SEARCH_INDEX_DIR = "/tmp/searchIndex";
static String BACKEND_INDEX_NAME = "search";
public static TitanGraph getInMemoryElasticSearchGraph(final String storageTitanDir, final String storageElasticSearchDir) {
TitanFactory.Builder config = TitanFactory.build();
config.set("storage.backend", "inmemory");
config.set("storage.directory", storageTitanDir);
config.set("index."+BACKEND_INDEX_NAME+".backend", "elasticsearch");
config.set("index."+BACKEND_INDEX_NAME+".directory",storageElasticSearchDir);
config.set("index."+BACKEND_INDEX_NAME+".elasticsearch.client-only", false);
config.set("index."+BACKEND_INDEX_NAME+".elasticsearch.local-mode", true);
return config.open();
}
public static TitanGraph getInMemoryElasticSearchGraph() {
return getInMemoryElasticSearchGraph(DEFAULT_IN_MEMORY_DIR, DEFAULT_SEARCH_INDEX_DIR);
}
/**
* @return a simple Titan in memory stuff, no backend indexing service included
*/
public static TitanGraph getInMemoryGraph(final String storageTitanDir){
TitanFactory.Builder config = TitanFactory.build();
config.set("storage.backend", "inmemory");
config.set("storage.directory", storageTitanDir);
return config.open();
}
public static TitanGraph getInMemoryGraph() {return getInMemoryGraph(DEFAULT_IN_MEMORY_DIR);}
/**
* Create a full text index with given property key name and indexName
* The String search index does not tokenize or stuff like that and is handled by the @backendSearchName ,
* the name of the backend index service such as Elastic or Solr
* @param graph
* @param propertyKeyName The name of the property key
* @param indexName The name of the index that will be queried
* @param backendSearchName The name od the backend search service, usually just search
*/
public static void createPropertyAndStringIndex(TitanGraph graph, String propertyKeyName, String indexName, String backendSearchName){
TitanManagement titanManagement = graph.getManagementSystem();
if (titanManagement.getGraphIndex(indexName) != null) { //This index already exist, simply return.
logger.warn(String.format("Tried to create an index with name %s that already existed", indexName));
return;
}
PropertyKey name = getOrCreatePropertyKey(titanManagement, propertyKeyName, String.class);
titanManagement.buildIndex(indexName, Vertex.class).addKey(name, Mapping.STRING.getParameter()).buildMixedIndex(backendSearchName);//We could generalize it, it's the only stuff that change
titanManagement.commit();
}
/**
* Create titan index. WARNING IT'S YOUR DUTY YO COMMIT THE @management !
* @param management
* @param indexName
* @param propertyKey
* @param unique
*/
public static void createTitanIndex(TitanManagement management,final String indexName,final PropertyKey propertyKey ,final boolean unique){
if (management.containsGraphIndex(indexName)) {
logger.warn("Tried do create duplicate index");
return;
}
if (unique)
management.buildIndex(indexName, Vertex.class).addKey(propertyKey).unique().buildCompositeIndex();
else
management.buildIndex(indexName, Vertex.class).addKey(propertyKey).buildCompositeIndex();
}
/**
* Return or create a PropertyKey with the given @propertyKeyName of class @propertyClass . WARNING, it will be your duty to commit the @management
* @param management The management obj
* @param propertyKeyName The propertyKeyName
* @param propertyClass The propertyClass
* @return
*/
public static PropertyKey getOrCreatePropertyKey(TitanManagement management, final String propertyKeyName, Class propertyClass){
return management.containsRelationType(propertyKeyName) ? management.getPropertyKey(propertyKeyName) : management.makePropertyKey(propertyKeyName).dataType(propertyClass).make();
}
/**
* Query the
* @param graph
* @param indexName
* @param propertyName
* @param query
* @return
*/
public static List<TitanIndexQuery.Result> queryElasticSearchIndex(TitanGraph graph, final String indexName, final String propertyName, final String query){
try {
return IteratorUtils.toList(graph.indexQuery(indexName, "v." + propertyName + ":" + query).vertices().iterator());
} catch (Exception e ){
logger.error(String.format("This should never happen, exception when querying the index %s on property %s with query %s", indexName, propertyName, query));
e.printStackTrace();
return new LinkedList<>();
}
}
/**
* Return or create a property type created with the given parameters
* @param graph
* @param aClass
* @param propertyName
* @return the property key if present, creating it otherwise
*/
public static PropertyKey createType(final TitanGraph graph,Class aClass,final String propertyName){
TitanManagement mgmt = graph.getManagementSystem();
PropertyKey p = mgmt.containsRelationType(propertyName) ? mgmt.getPropertyKey(propertyName) : mgmt.makePropertyKey(propertyName).dataType(aClass).make();
mgmt.commit();
return p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment