Esempio n. 1
0
  @Override
  public AbstractLongList getRawNeighborhood(AtomicQuery query, InternalTitanTransaction tx) {
    Preconditions.checkArgument(
        QueryUtil.queryCoveredByDiskIndexes(query),
        "Raw retrieval is currently does not support in-memory filtering");
    List<Entry> entries = queryForEntries(query, tx.getTxHandle());

    InternalTitanVertex node = query.getNode();
    TitanType titanType = null;
    if (query.hasEdgeTypeCondition()) titanType = query.getTypeCondition();

    AbstractLongList result = new LongArrayList();

    for (Entry entry : entries) {
      if (!query.hasEdgeTypeCondition()) {
        long etid = IDHandler.readEdgeType(entry.getColumn(), idManager);
        if (titanType == null || titanType.getID() != etid) {
          titanType = getTypeFromID(etid, tx);
        }
      }
      if (titanType.isPropertyKey() || (!titanType.isModifiable() && !query.queryUnmodifiable())) {
        continue; // Skip since it does not match query
      }
      // Get neighboring node id
      long iddiff = VariableLong.read(entry.getValue());
      long nghid = iddiff + node.getID();
      result.add(nghid);

      if (result.size() >= query.getLimit()) break;
    }
    return result;
  }
Esempio n. 2
0
  private void writeInlineEdge(
      DataOutput out, InternalRelation edge, TitanType type, boolean writeEdgeType) {
    assert type.isSimple();
    assert writeEdgeType
        || type.isEdgeLabel()
        || (type.isPropertyKey() && !hasGenericDataType((TitanKey) type));

    if (edge == null) {
      assert !writeEdgeType;
      if (type.isPropertyKey()) {
        out.writeObject(null);
      } else {
        assert type.isEdgeLabel();
        VariableLong.writePositive(out, 0);
      }
    } else {
      if (writeEdgeType) {
        IDHandler.writeInlineEdgeType(out, type.getID(), idManager);
      }
      if (edge.isProperty()) {
        Object attribute = ((TitanProperty) edge).getAttribute();
        if (hasGenericDataType((TitanKey) type)) out.writeClassAndObject(attribute);
        else out.writeObject(attribute);
      } else {
        assert edge.isUnidirected() && edge.isEdge();
        VariableLong.writePositive(out, edge.getVertex(1).getID());
      }
    }
  }
Esempio n. 3
0
  private Entry getEntry(
      InternalTitanTransaction tx,
      InternalRelation edge,
      InternalTitanVertex perspective,
      Map<TitanType, TypeSignature> signatures,
      boolean columnOnly) {
    TitanType et = edge.getType();
    long etid = et.getID();

    int dirID;
    if (edge.isProperty()) {
      dirID = 0;
    } else if (edge.getVertex(0).equals(perspective)) {
      // Out TitanRelation
      assert edge.isDirected() || edge.isUnidirected();
      dirID = 2;
    } else {
      // In TitanRelation
      assert !edge.isUnidirected() && edge.getVertex(1).equals(perspective);
      dirID = 3;
    }

    int etIDLength = IDHandler.edgeTypeLength(etid, idManager);

    ByteBuffer column = null, value = null;
    if (et.isSimple()) {
      if (et.isFunctional()) {
        column = ByteBuffer.allocate(etIDLength);
        IDHandler.writeEdgeType(column, etid, dirID, idManager);
      } else {
        column = ByteBuffer.allocate(etIDLength + VariableLong.positiveLength(edge.getID()));
        IDHandler.writeEdgeType(column, etid, dirID, idManager);
        VariableLong.writePositive(column, edge.getID());
      }
      column.flip();
      if (!columnOnly) {
        if (edge.isEdge()) {
          long nodeIDDiff =
              ((TitanEdge) edge).getOtherVertex(perspective).getID() - perspective.getID();
          int nodeIDDiffLength = VariableLong.length(nodeIDDiff);
          if (et.isFunctional()) {
            value =
                ByteBuffer.allocate(nodeIDDiffLength + VariableLong.positiveLength(edge.getID()));
            VariableLong.write(value, nodeIDDiff);
            VariableLong.writePositive(value, edge.getID());
          } else {
            value = ByteBuffer.allocate(nodeIDDiffLength);
            VariableLong.write(value, nodeIDDiff);
          }
          value.flip();
        } else {
          assert edge.isProperty();
          DataOutput out = serializer.getDataOutput(defaultOutputCapacity, true);
          // Write object
          writeAttribute(out, (TitanProperty) edge);
          if (et.isFunctional()) {
            VariableLong.writePositive(out, edge.getID());
          }
          value = out.getByteBuffer();
        }
      }
    } else {
      TypeSignature ets = getSignature(tx, et, signatures);

      InternalRelation[] keys = new InternalRelation[ets.keyLength()],
          values = new InternalRelation[ets.valueLength()];
      List<InternalRelation> rest = new ArrayList<InternalRelation>();
      ets.sort(edge.getRelations(SimpleAtomicQuery.queryAll(edge), false), keys, values, rest);

      DataOutput out = serializer.getDataOutput(defaultOutputCapacity, true);
      IDHandler.writeEdgeType(out, etid, dirID, idManager);

      for (int i = 0; i < keys.length; i++) writeInlineEdge(out, keys[i], ets.getKeyType(i));

      if (!et.isFunctional()) {
        VariableLong.writePositive(out, edge.getID());
      }
      column = out.getByteBuffer();

      if (!columnOnly) {
        out = serializer.getDataOutput(defaultOutputCapacity, true);

        if (edge.isEdge()) {
          long nodeIDDiff =
              ((TitanEdge) edge).getOtherVertex(perspective).getID() - perspective.getID();
          VariableLong.write(out, nodeIDDiff);
        } else {
          assert edge.isProperty();
          writeAttribute(out, (TitanProperty) edge);
        }

        if (et.isFunctional()) {
          assert edge.isEdge();
          VariableLong.writePositive(out, edge.getID());
        }
        for (int i = 0; i < values.length; i++)
          writeInlineEdge(out, values[i], ets.getValueType(i));
        for (InternalRelation v : rest) writeInlineEdge(out, v);
        value = out.getByteBuffer();
      }
    }
    return new Entry(column, value);
  }
Esempio n. 4
0
  private List<Entry> queryForEntries(AtomicQuery query, TransactionHandle txh) {
    ByteBuffer key = IDHandler.getKey(query.getVertexID());
    List<Entry> entries = null;
    LimitTracker limit = new LimitTracker(query);

    boolean dirs[] = getAllowedDirections(query);

    if (query.hasEdgeTypeCondition()) {
      TitanType et = query.getTypeCondition();
      if (!et.isNew()) { // Result set must be empty if TitanType is new
        ArrayList<Object> applicableConstraints = null;
        boolean isRange = false;
        if (query.hasConstraints()) {
          assert !et.isSimple();
          TypeDefinition def = ((InternalTitanType) et).getDefinition();
          String[] keysig = def.getKeySignature();
          applicableConstraints = new ArrayList<Object>(keysig.length);
          Map<String, Object> constraints = query.getConstraints();
          for (int i = 0; i < keysig.length; i++) {
            if (constraints.containsKey(keysig[i])) {
              Object iv = constraints.get(keysig[i]);
              applicableConstraints.add(iv);
              if (iv != null && (iv instanceof AtomicInterval) && ((AtomicInterval) iv).isRange()) {
                isRange = true;
                break;
              }
            } else break;
          }
          if (applicableConstraints.isEmpty()) applicableConstraints = null;
        }

        for (int dirID = 0; dirID < 4; dirID++) {
          if (dirs[dirID]) {
            if (applicableConstraints != null) {
              assert !applicableConstraints.isEmpty();

              DataOutput start = serializer.getDataOutput(defaultOutputCapacity, true);
              DataOutput end = null;
              if (isRange) end = serializer.getDataOutput(defaultOutputCapacity, true);

              IDHandler.writeEdgeType(start, et.getID(), dirID, idManager);
              if (isRange) IDHandler.writeEdgeType(end, et.getID(), dirID, idManager);

              // Write all applicable key constraints
              for (Object iv : applicableConstraints) {
                if (iv instanceof AtomicInterval) {
                  AtomicInterval interval = (AtomicInterval) iv;
                  if (interval.isPoint()) {
                    start.writeObject(interval.getStartPoint());
                    if (isRange) end.writeObject(interval.getStartPoint());
                  } else {
                    assert isRange;
                    assert interval.isRange();

                    ByteBuffer startColumn, endColumn;

                    if (interval.getStartPoint() != null) {
                      start.writeObject(interval.getStartPoint());
                      startColumn = start.getByteBuffer();
                      if (!interval.startInclusive())
                        startColumn = ByteBufferUtil.nextBiggerBuffer(startColumn);
                    } else {
                      assert interval.startInclusive();
                      startColumn = start.getByteBuffer();
                    }

                    if (interval.getEndPoint() != null) {
                      end.writeObject(interval.getEndPoint());
                    } else {
                      assert interval.endInclusive();
                    }
                    endColumn = end.getByteBuffer();
                    if (interval.endInclusive())
                      endColumn = ByteBufferUtil.nextBiggerBuffer(endColumn);

                    entries = appendResults(key, startColumn, endColumn, entries, limit, txh);
                    break; // redundant, this must be the last iteration because its a range
                  }
                } else {
                  assert iv == null || (iv instanceof TitanVertex);
                  long id = 0;
                  if (iv != null) id = ((TitanVertex) iv).getID();
                  VariableLong.writePositive(start, id);
                  if (isRange) VariableLong.writePositive(end, id);
                }
              }
              if (!isRange)
                entries = appendResults(key, start.getByteBuffer(), entries, limit, txh);
            } else {
              ByteBuffer columnStart = IDHandler.getEdgeType(et.getID(), dirID, idManager);
              entries = appendResults(key, columnStart, entries, limit, txh);
            }
          }
        }
      }
    } else if (query.hasGroupCondition()) {
      int groupid = query.getGroupCondition().getID();
      for (int dirID = 0; dirID < 4; dirID++) {
        if (dirs[dirID]) {
          ByteBuffer columnStart = IDHandler.getEdgeTypeGroup(groupid, dirID, idManager);
          entries = appendResults(key, columnStart, entries, limit, txh);
        }
      }
    } else {
      int lastDirID = -1;
      for (int dirID = 0; dirID <= 4; dirID++) {
        if ((dirID >= 4 || !dirs[dirID]) && lastDirID >= 0) {
          ByteBuffer columnStart = IDHandler.getEdgeTypeGroup(0, lastDirID, idManager);
          ByteBuffer columnEnd =
              IDHandler.getEdgeTypeGroup(idManager.getMaxGroupID() + 1, dirID - 1, idManager);
          entries = appendResults(key, columnStart, columnEnd, entries, limit, txh);
          lastDirID = -1;
        }
        if (dirID < 4) {
          if (dirs[dirID] && lastDirID == -1) lastDirID = dirID;
        }
      }
    }

    if (entries == null) return ImmutableList.of();
    else return entries;
  }
Esempio n. 5
0
  protected void loadRelations(
      Iterable<Entry> entries, VertexRelationLoader loader, InternalTitanTransaction tx) {
    Map<String, TitanType> etCache = new HashMap<String, TitanType>();
    TitanType titanType = null;

    for (Entry entry : entries) {
      ByteBuffer column = entry.getColumn();
      int dirID = IDHandler.getDirectionID(column.get(column.position()));
      long etid = IDHandler.readEdgeType(column, idManager);

      if (titanType == null || titanType.getID() != etid) {
        titanType = getTypeFromID(etid, tx);
      }

      Object[] keys = null;
      if (!titanType.isSimple()) {
        TypeDefinition def = ((InternalTitanType) titanType).getDefinition();
        String[] keysig = def.getKeySignature();
        keys = new Object[keysig.length];
        for (int i = 0; i < keysig.length; i++)
          keys[i] = readInline(column, getEdgeType(keysig[i], etCache, tx));
      }

      long edgeid = 0;
      if (!titanType.isFunctional()) {
        edgeid = VariableLong.readPositive(column);
      }

      ByteBuffer value = entry.getValue();
      if (titanType.isEdgeLabel()) {
        long nodeIDDiff = VariableLong.read(value);
        if (titanType.isFunctional()) edgeid = VariableLong.readPositive(value);
        assert edgeid > 0;
        long otherid = loader.getVertexId() + nodeIDDiff;
        assert dirID == 3 || dirID == 2;
        Direction dir = dirID == 3 ? Direction.IN : Direction.OUT;
        if (!tx.isDeletedRelation(edgeid))
          loader.loadEdge(edgeid, (TitanLabel) titanType, dir, otherid);
      } else {
        assert titanType.isPropertyKey();
        assert dirID == 0;
        TitanKey propType = ((TitanKey) titanType);
        Object attribute = null;

        if (hasGenericDataType(propType)) {
          attribute = serializer.readClassAndObject(value);
        } else {
          attribute = serializer.readObjectNotNull(value, propType.getDataType());
        }
        assert attribute != null;

        if (titanType.isFunctional()) edgeid = VariableLong.readPositive(value);
        assert edgeid > 0;
        if (!tx.isDeletedRelation(edgeid)) loader.loadProperty(edgeid, propType, attribute);
      }

      // Read value inline edges if any
      if (!titanType.isSimple()) {
        TypeDefinition def = ((InternalTitanType) titanType).getDefinition();
        // First create all keys buffered above
        String[] keysig = def.getKeySignature();
        for (int i = 0; i < keysig.length; i++) {
          createInlineEdge(loader, getEdgeType(keysig[i], etCache, tx), keys[i]);
        }

        // value signature
        for (String str : def.getCompactSignature())
          readLabel(loader, value, getEdgeType(str, etCache, tx));

        // Third: read rest
        while (value.hasRemaining()) {
          TitanType type =
              (TitanType) tx.getExistingVertex(IDHandler.readInlineEdgeType(value, idManager));
          readLabel(loader, value, type);
        }
      }
    }
  }