orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

Index stores temporary RIDs for vertices created in transaction (remote storage)

Open buzzkillington opened this issue 11 years ago • 2 comments

Using OrientDB 2.1-rc3 and remote storage, an index on edge in/out link properties will store the temporary (negative) RIDs for endpoint vertices created in the same transaction as an edge. (This does not happen if memory storage is used - the permanent RIDs are correctly stored in the index in that case.)

Example output (from code below):

Vertices: #11:0 #11:1 #11:2 #11:3 #11:4 

Index data: CustomEdge.in
key: #-1:-7, rid: CustomEdge#12:3{out:#11:0,in:#11:1} v2
key: #-1:-5, rid: CustomEdge#12:2{out:#11:1,in:#11:2} v2
key: #-1:-3, rid: CustomEdge#12:1{out:#11:2,in:#11:3} v2
key: #-1:-2, rid: CustomEdge#12:0{out:#11:3,in:#11:4} v2

Index data: CustomEdge.out
key: #-1:-9, rid: CustomEdge#12:3{out:#11:0,in:#11:1} v2
key: #-1:-7, rid: CustomEdge#12:2{out:#11:1,in:#11:2} v2
key: #-1:-5, rid: CustomEdge#12:1{out:#11:2,in:#11:3} v2
key: #-1:-3, rid: CustomEdge#12:0{out:#11:3,in:#11:4} v2

Index data: CustomEdge.compositeInOut
key: OCompositeKey{keys=[#-1:-9, #-1:-7]}, rid: CustomEdge#12:3{out:#11:0,in:#11:1} v2
key: OCompositeKey{keys=[#-1:-7, #-1:-5]}, rid: CustomEdge#12:2{out:#11:1,in:#11:2} v2
key: OCompositeKey{keys=[#-1:-5, #-1:-3]}, rid: CustomEdge#12:1{out:#11:2,in:#11:3} v2
key: OCompositeKey{keys=[#-1:-3, #-1:-2]}, rid: CustomEdge#12:0{out:#11:3,in:#11:4} v2

Example code:

package com.example.orientdb.index;

import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;

public class EdgeIndexTxProblem {
    private static final String CUSTOM_VERTEX = "CustomVertex";
    private static final String CUSTOM_EDGE = "CustomEdge";
    private static final String IN_INDEX_NAME = CUSTOM_EDGE + ".in";
    private static final String OUT_INDEX_NAME = CUSTOM_EDGE + ".out";
    private static final String COMPOSITE_INDEX_NAME = CUSTOM_EDGE + ".compositeInOut";

    public static void main(String[] args) {
        OrientGraphFactory graphFactory = null;
        // Use NoTx for schema config to avoid warning messages
        OrientGraphNoTx graphNoTx = null;
        OrientGraph graph = null;
        try {
            graphFactory = new OrientGraphFactory("remote:localhost/edgeTxProblem", "user", "pass");
            graphNoTx = graphFactory.getNoTx();
            OrientVertexType vertexType = null;
            if( ! graphNoTx.getRawGraph().existsCluster(CUSTOM_VERTEX)) {
                vertexType = graphNoTx.createVertexType(CUSTOM_VERTEX);
            }
            else {
                vertexType = graphNoTx.getVertexType(CUSTOM_VERTEX);
            }

            if( ! graphNoTx.getRawGraph().existsCluster(CUSTOM_EDGE)) {
                OrientEdgeType edgeType = graphNoTx.createEdgeType(CUSTOM_EDGE);
                edgeType.createProperty("out", OType.LINK, vertexType);
                edgeType.createProperty("in", OType.LINK, vertexType);
                edgeType.createIndex(IN_INDEX_NAME, OClass.INDEX_TYPE.UNIQUE, "in");
                edgeType.createIndex(OUT_INDEX_NAME, OClass.INDEX_TYPE.UNIQUE, "out");
                edgeType.createIndex(COMPOSITE_INDEX_NAME, OClass.INDEX_TYPE.UNIQUE, "out", "in");
            }
            graphNoTx.shutdown();

            graph = graphFactory.getTx();
            Vertex inVert = null;
            for(int i = 0; i < 5; ++i) {
                Vertex currentVert = graph.addVertex("class:" + CUSTOM_VERTEX);
                if(inVert != null) {
                    graph.addEdge("class:" + CUSTOM_EDGE, currentVert, inVert, CUSTOM_EDGE);
                }
                inVert = currentVert;
            }
            graph.commit();

            Iterable<Vertex> verts = graph.getVertices();
            StringBuilder vertIds = new StringBuilder();
            for(Vertex vert : verts) {
                vertIds.append(vert.getId().toString()).append(" ");
            }
            System.out.println("Vertices: " + vertIds);
            System.out.println();

            printIndexData(graph, IN_INDEX_NAME);
            printIndexData(graph, OUT_INDEX_NAME);
            printIndexData(graph, COMPOSITE_INDEX_NAME);
        }
        finally {
            if(graphNoTx != null) {
                try {
                    graphNoTx.shutdown();
                }
                catch(Throwable t) {
                    System.err.println("Error shutting down graphNoTx:" + t);
                }
            }
            if(graph != null) {
                try {
                    graph.shutdown();
                }
                catch(Throwable t) {
                    System.err.println("Error shutting down graph:" + t);
                }
            }
            if(graphFactory != null) {
                graphFactory.close();
            }
        }
    }

    private static void printIndexData(OrientGraph graph, String indexName) {
        System.out.println("Index data: " + indexName);
        Iterable<ODocument> indexDataDocs = graph.getRawGraph().query(
            new OSQLSynchQuery<ODocument>("select from index:" + indexName));
        for(ODocument indexDataDoc : indexDataDocs) {
            System.out.println("key: " + indexDataDoc.field("key") + ", rid: " + indexDataDoc.field("rid"));
        }
        System.out.println("");
    }
}

buzzkillington avatar May 27 '15 18:05 buzzkillington

fixed, was a wrong optimization, added the relative test.

tglman avatar Jun 01 '15 17:06 tglman

Assigning the missing labels + milestone...

lvca avatar Jun 02 '15 16:06 lvca