/** Execute the operation */
    @Override
    protected void onExecute() throws Exception {

      int count = 0;
      com.sparsity.dex.gdb.Graph graph = ((DexGraph) getGraph()).getRawGraph();

      // Get by label

      if (key.compareTo(StringFactory.LABEL) == 0) { // label is "indexed"

        int type = graph.findType(value.toString());
        if (type != com.sparsity.dex.gdb.Type.InvalidType) {
          com.sparsity.dex.gdb.Type tdata = graph.getType(type);
          if (tdata.getObjectType() == ObjectType.Edge) {
            com.sparsity.dex.gdb.Objects objs = graph.select(type);
            com.sparsity.dex.gdb.ObjectsIterator objsItr = objs.iterator();

            while (objsItr.hasNext()) {
              @SuppressWarnings("unused")
              long v = objsItr.nextObject();
              count++;
            }

            objsItr.close();
            objs.close();
          }
        }
      } else {

        // Get by a different property

        for (int type : types) {
          int attr = graph.findAttribute(type, key);
          if (com.sparsity.dex.gdb.Attribute.InvalidAttribute != attr) {
            com.sparsity.dex.gdb.Attribute adata = graph.getAttribute(attr);

            if (adata.getKind() == AttributeKind.Basic) {
              throw new RuntimeException("Key " + key + " is not indexed");
            } else { // use the index
              com.sparsity.dex.gdb.Objects objs = DexUtils.getUsingIndex(graph, adata, value);
              com.sparsity.dex.gdb.ObjectsIterator objsItr = objs.iterator();

              while (objsItr.hasNext()) {
                @SuppressWarnings("unused")
                long v = objsItr.nextObject();
                count++;
              }

              objsItr.close();
              objs.close();
            }
          }
        }
      }

      setResult(count);
    }
Пример #2
0
  /**
   * Create an automatic indexing structure for indexing provided key for element class.
   *
   * <p>Dex attributes are restricted to an specific vertex/edge type. The property {@link #label}
   * must be used to specify the vertex/edge label.
   *
   * <p>The index could be created even before the vertex/edge label had been created (that is,
   * there are no instances for the given vertex/edge label). If so, this will create the
   * vertex/edge type automatically. The same way, if necessary the attribute will be created
   * automatically.
   *
   * <p>FIXME: In case the attribute is created, this always creates an String attribute, could this
   * be set somehow?
   */
  @Override
  public <T extends Element> void createKeyIndex(
      String key, Class<T> elementClass, final Parameter... indexParameters) {
    autoStartTransaction();

    String label = this.label.get();
    if (label == null) {
      throw new IllegalArgumentException("Label must be given");
    }

    com.sparsity.dex.gdb.Graph rawGraph = getRawGraph();

    int type = rawGraph.findType(label);
    if (type == com.sparsity.dex.gdb.Type.InvalidType) {
      // create the node/edge type
      if (Vertex.class.isAssignableFrom(elementClass)) {
        type = rawGraph.newNodeType(label);
      } else if (Edge.class.isAssignableFrom(elementClass)) {
        type = rawGraph.newEdgeType(label, true, true);
      } else {
        throw ExceptionFactory.classIsNotIndexable(elementClass);
      }
    } else {
      // validate the node/edge type
      com.sparsity.dex.gdb.Type tdata = rawGraph.getType(type);
      if (tdata.getObjectType() == ObjectType.Node) {
        if (!Vertex.class.isAssignableFrom(elementClass)) {
          throw new IllegalArgumentException(
              "Given element class '"
                  + elementClass.getName()
                  + "' is not valid for the given node type '"
                  + label
                  + "'");
        }
      } else if (tdata.getObjectType() == ObjectType.Edge) {
        if (!Edge.class.isAssignableFrom(elementClass)) {
          throw new IllegalArgumentException(
              "Given element class '"
                  + elementClass.getName()
                  + "' is not valid for the given edge type '"
                  + label
                  + "'");
        }
      }
    }

    int attr = rawGraph.findAttribute(type, key);
    if (com.sparsity.dex.gdb.Attribute.InvalidAttribute == attr) {
      // create the attribute (indexed)
      attr =
          rawGraph.newAttribute(
              type,
              key,
              com.sparsity.dex.gdb.DataType.String,
              com.sparsity.dex.gdb.AttributeKind.Indexed);
    } else {
      // it already exists, let's indexe it if necessary
      com.sparsity.dex.gdb.Attribute adata = rawGraph.getAttribute(attr);
      if (adata.getKind() == AttributeKind.Indexed || adata.getKind() == AttributeKind.Unique) {
        throw ExceptionFactory.indexAlreadyExists(label + " " + key);
      }
      rawGraph.indexAttribute(attr, com.sparsity.dex.gdb.AttributeKind.Indexed);
    }
  }
Пример #3
0
  /**
   * Returns an iterable to all the edges in the graph that have a particular key/value property.
   *
   * <p>In case key is {@link StringFactory#LABEL}, it returns an iterable of all the edges having
   * the given value as the label (therefore, belonging to the given type).
   *
   * <p>In case {@link #label} is null, it will return all edges having a particular key/value no
   * matters the type. In case {@link #label} is not null, it will return all edges having a
   * particular key/value belonging to the given type.
   *
   * @see com.tinkerpop.blueprints.Graph#getEdges(String, Object)
   * @see #label
   */
  @Override
  public CloseableIterable<Edge> getEdges(final String key, final Object value) {
    autoStartTransaction();

    com.sparsity.dex.gdb.Graph rawGraph = getRawGraph();

    if (key.compareTo(StringFactory.LABEL) == 0) { // label is "indexed"

      int type = rawGraph.findType(value.toString());
      if (type != com.sparsity.dex.gdb.Type.InvalidType) {
        com.sparsity.dex.gdb.Type tdata = rawGraph.getType(type);
        if (tdata.getObjectType() == ObjectType.Edge) {
          com.sparsity.dex.gdb.Objects objs = rawGraph.select(type);
          return new DexIterable<Edge>(this, objs, Edge.class);
        }
      }
      return null;
    }

    String label = this.label.get();
    if (label == null) { // all vertex types

      com.sparsity.dex.gdb.TypeList tlist = rawGraph.findEdgeTypes();
      List<Iterable<Edge>> edges = new ArrayList<Iterable<Edge>>();
      for (Integer type : tlist) {
        int attr = rawGraph.findAttribute(type, key);
        if (com.sparsity.dex.gdb.Attribute.InvalidAttribute != attr) {
          com.sparsity.dex.gdb.Attribute adata = rawGraph.getAttribute(attr);
          if (adata.getKind() == AttributeKind.Basic) { // "table" scan
            com.sparsity.dex.gdb.Objects objs = rawGraph.select(type);
            edges.add(
                new PropertyFilteredIterable<Edge>(
                    key, value, new DexIterable<Edge>(this, objs, Edge.class)));
          } else { // use the index
            edges.add(new DexIterable<Edge>(this, this.rawGet(adata, value), Edge.class));
          }
        }
      }
      tlist.delete();
      tlist = null;

      if (edges.size() > 0) return new MultiIterable<Edge>(edges);
      else throw new IllegalArgumentException("The given attribute '" + key + "' does not exist");

    } else { // restricted to a type

      int type = rawGraph.findType(label);
      if (type == com.sparsity.dex.gdb.Type.InvalidType) {
        throw new IllegalArgumentException("Unnexisting edge label: " + label);
      }
      com.sparsity.dex.gdb.Type tdata = rawGraph.getType(type);
      if (tdata.getObjectType() != com.sparsity.dex.gdb.ObjectType.Edge) {
        throw new IllegalArgumentException("Given label is not a edge label: " + label);
      }

      int attr = rawGraph.findAttribute(type, key);
      if (com.sparsity.dex.gdb.Attribute.InvalidAttribute == attr) {
        throw new IllegalArgumentException(
            "The given attribute '"
                + key
                + "' does not exist for the given edge label '"
                + label
                + "'");
      }

      com.sparsity.dex.gdb.Attribute adata = rawGraph.getAttribute(attr);
      if (adata.getKind() == AttributeKind.Basic) { // "table" scan
        com.sparsity.dex.gdb.Objects objs = rawGraph.select(type);
        return new PropertyFilteredIterable<Edge>(
            key, value, new DexIterable<Edge>(this, objs, Edge.class));
      } else { // use the index
        return new DexIterable<Edge>(this, this.rawGet(adata, value), Edge.class);
      }
    }
  }