Search in sources :

Example 1 with Element

use of com.tinkerpop.blueprints.Element in project blueprints by tinkerpop.

the class ElementHelper method removeProperties.

/**
 * Clear all the properties from an iterable of elements.
 *
 * @param elements the elements to remove properties from
 */
public static void removeProperties(final Iterable<Element> elements) {
    for (final Element element : elements) {
        final List<String> keys = new ArrayList<String>();
        keys.addAll(element.getPropertyKeys());
        for (final String key : keys) {
            element.removeProperty(key);
        }
    }
}
Also used : Element(com.tinkerpop.blueprints.Element) ArrayList(java.util.ArrayList)

Example 2 with Element

use of com.tinkerpop.blueprints.Element in project blueprints by tinkerpop.

the class KeyIndexableGraphHelper method reIndexElements.

/**
 * For those graphs that do no support automatic reindexing of elements when a key is provided for indexing, this method can be used to simulate that behavior.
 * The elements in the graph are iterated and their properties (for the provided keys) are removed and then added.
 * Be sure that the key indices have been created prior to calling this method so that they can pick up the property mutations calls.
 * Finally, if the graph is a TransactionalGraph, then a 1000 mutation buffer is used for each commit.
 *
 * @param graph    the graph containing the provided elements
 * @param elements the elements to index into the key indices
 * @param keys     the keys of the key indices
 * @return the number of element properties that were indexed
 */
public static long reIndexElements(final Graph graph, final Iterable<? extends Element> elements, final Set<String> keys) {
    final boolean isTransactional = graph instanceof TransactionalGraph;
    long counter = 0;
    for (final Element element : elements) {
        for (final String key : keys) {
            final Object value = element.removeProperty(key);
            if (null != value) {
                counter++;
                element.setProperty(key, value);
                if (isTransactional && (counter % 1000 == 0)) {
                    ((TransactionalGraph) graph).commit();
                }
            }
        }
    }
    if (isTransactional) {
        ((TransactionalGraph) graph).commit();
    }
    return counter;
}
Also used : Element(com.tinkerpop.blueprints.Element) TransactionalGraph(com.tinkerpop.blueprints.TransactionalGraph)

Example 3 with Element

use of com.tinkerpop.blueprints.Element in project blueprints by tinkerpop.

the class GraphSONUtility method createJSONMap.

private static ObjectNode createJSONMap(final Map map, final List<String> propertyKeys, final boolean showTypes) {
    final ObjectNode jsonMap = jsonNodeFactory.objectNode();
    for (Object key : map.keySet()) {
        Object value = map.get(key);
        if (value != null) {
            if (value instanceof List) {
                value = createJSONList((List) value, propertyKeys, showTypes);
            } else if (value instanceof Iterable) {
                value = createJSONList(getList((Iterable) value), propertyKeys, showTypes);
            } else if (value instanceof Iterator) {
                value = createJSONList(getList((Iterator) value), propertyKeys, showTypes);
            } else if (value instanceof Map) {
                value = createJSONMap((Map) value, propertyKeys, showTypes);
            } else if (value instanceof Element) {
                value = objectNodeFromElement((Element) value, propertyKeys, showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL);
            } else if (value.getClass().isArray()) {
                value = createJSONList(convertArrayToList(value), propertyKeys, showTypes);
            }
        }
        putObject(jsonMap, key.toString(), getValue(value, showTypes));
    }
    return jsonMap;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Element(com.tinkerpop.blueprints.Element) Iterator(java.util.Iterator) JSONObject(org.codehaus.jettison.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Element

use of com.tinkerpop.blueprints.Element in project blueprints by tinkerpop.

the class RexsterGraph method getIndices.

public Iterable<Index<? extends Element>> getIndices() {
    List<Index<? extends Element>> indices = new ArrayList<Index<? extends Element>>();
    JSONArray json = RestHelper.getResultArray(this.graphURI + RexsterTokens.SLASH_INDICES);
    for (int ix = 0; ix < json.length(); ix++) {
        JSONObject index = json.optJSONObject(ix);
        Class c;
        String clazz = index.optString(RexsterTokens.CLASS);
        if (clazz.toLowerCase().contains(RexsterTokens.VERTEX))
            c = Vertex.class;
        else if (clazz.toLowerCase().contains(RexsterTokens.EDGE))
            c = Edge.class;
        else
            throw new RuntimeException("Can not determine whether " + clazz + " is a vertex or edge class");
        indices.add(new RexsterIndex(this, index.optString(RexsterTokens.NAME), c));
    }
    return indices;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) Element(com.tinkerpop.blueprints.Element) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) Index(com.tinkerpop.blueprints.Index)

Example 5 with Element

use of com.tinkerpop.blueprints.Element in project frames by tinkerpop.

the class JavaHandlerModule method createHandler.

<T> T createHandler(final Object framedElement, final FramedGraph<?> graph, final Element element, Class<?> frameClass, final Method method) {
    try {
        Class<T> implClass = (Class<T>) classCache.get(frameClass);
        T handler = factory.create(implClass);
        ((Proxy) handler).setHandler(new MethodHandler() {

            private JavaHandlerContextImpl<Element> defaultJavahandlerImpl = new JavaHandlerContextImpl<Element>(graph, method, element);

            @Override
            public Object invoke(Object o, Method m, Method proceed, Object[] args) throws Throwable {
                if (!Modifier.isAbstract(m.getModifiers())) {
                    return proceed.invoke(o, args);
                } else {
                    if (m.getAnnotation(JavaHandler.class) != null) {
                        throw new JavaHandlerException("Method " + m + " is marked with @JavaHandler but is not implemented");
                    }
                    if (m.getDeclaringClass() == JavaHandlerContext.class) {
                        return m.invoke(defaultJavahandlerImpl, args);
                    }
                    return m.invoke(framedElement, args);
                }
            }
        });
        return handler;
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ClassNotFoundException) {
            throw new JavaHandlerException("Problem locating handler class for " + frameClass, e);
        } else {
            throw new JavaHandlerException("Cannot create class for handling framed method", cause);
        }
    } catch (InstantiationException e) {
        throw new JavaHandlerException("Problem instantiating handler class", e);
    } catch (IllegalAccessException e) {
        throw new JavaHandlerException("Problem instantiating handler class", e);
    }
}
Also used : Element(com.tinkerpop.blueprints.Element) Method(java.lang.reflect.Method) Proxy(javassist.util.proxy.Proxy) MethodHandler(javassist.util.proxy.MethodHandler) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Element (com.tinkerpop.blueprints.Element)10 Vertex (com.tinkerpop.blueprints.Vertex)4 ArrayList (java.util.ArrayList)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Map (java.util.Map)2 Geoshape (com.thinkaurelius.titan.core.attribute.Geoshape)1 Edge (com.tinkerpop.blueprints.Edge)1 Graph (com.tinkerpop.blueprints.Graph)1 Index (com.tinkerpop.blueprints.Index)1 TransactionalGraph (com.tinkerpop.blueprints.TransactionalGraph)1 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)1 FramedVertexIterable (com.tinkerpop.frames.structures.FramedVertexIterable)1 FramedVertexMap (com.tinkerpop.frames.structures.FramedVertexMap)1 Pipe (com.tinkerpop.pipes.Pipe)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1