Beispiel #1
0
  @Test
  public void stringSerialization() {
    // Characters
    DataOutput out = serialize.getDataOutput(((int) Character.MAX_VALUE) * 2 + 8, true);
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
      out.writeObjectNotNull(Character.valueOf(c));
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
      assertEquals(c, serialize.readObjectNotNull(b, Character.class).charValue());
    }

    // String
    for (int t = 0; t < 10000; t++) {
      DataOutput out1 = serialize.getDataOutput(32 + 5, true);
      DataOutput out2 = serialize.getDataOutput(32 + 5, true);
      String s1 = RandomGenerator.randomString(1, 32);
      String s2 = RandomGenerator.randomString(1, 32);
      out1.writeObjectNotNull(s1);
      out2.writeObjectNotNull(s2);
      StaticBuffer b1 = out1.getStaticBuffer();
      StaticBuffer b2 = out2.getStaticBuffer();
      assertEquals(s1, serialize.readObjectNotNull(b1.asReadBuffer(), String.class));
      assertEquals(s2, serialize.readObjectNotNull(b2.asReadBuffer(), String.class));
      assertEquals(
          s1 + " vs " + s2, Integer.signum(s1.compareTo(s2)), Integer.signum(b1.compareTo(b2)));
    }
  }
Beispiel #2
0
 @Test
 public void objectWriteRead() {
   // serialize.registerClass(short[].class);
   // serialize.registerClass(TestClass.class);
   DataOutput out = serialize.getDataOutput(128, true);
   String str = "This is a test";
   int i = 5;
   TestClass c = new TestClass(5, 8, new short[] {1, 2, 3, 4, 5}, TestEnum.Two);
   Number n = new Double(3.555);
   out.writeObjectNotNull(str);
   out.putInt(i);
   out.writeObject(c, TestClass.class);
   out.writeClassAndObject(n);
   ReadBuffer b = out.getStaticBuffer().asReadBuffer();
   if (printStats) log.debug(bufferStats(b));
   String str2 = serialize.readObjectNotNull(b, String.class);
   assertEquals(str, str2);
   if (printStats) log.debug(bufferStats(b));
   assertEquals(b.getInt(), i);
   TestClass c2 = serialize.readObject(b, TestClass.class);
   assertEquals(c, c2);
   if (printStats) log.debug(bufferStats(b));
   assertEquals(n, serialize.readClassAndObject(b));
   if (printStats) log.debug(bufferStats(b));
   assertFalse(b.hasRemaining());
 }
Beispiel #3
0
  @Before
  public void setUp() throws Exception {
    serialize = new KryoSerializer(false);
    serialize.registerClass(TestEnum.class, RESERVED_ID_OFFSET + 1);
    serialize.registerClass(TestClass.class, RESERVED_ID_OFFSET + 2);
    serialize.registerClass(short[].class, RESERVED_ID_OFFSET + 3);

    printStats = true;
  }
Beispiel #4
0
 @Test
 public void enumSerializeTest() {
   DataOutput out = serialize.getDataOutput(128, true);
   out.writeObjectNotNull(TestEnum.Two);
   ReadBuffer b = out.getStaticBuffer().asReadBuffer();
   if (printStats) log.debug(bufferStats(b));
   assertEquals(TestEnum.Two, serialize.readObjectNotNull(b, TestEnum.class));
   assertFalse(b.hasRemaining());
 }
Beispiel #5
0
 @Test
 public void classSerialization() {
   DataOutput out = serialize.getDataOutput(128, true);
   out.writeObjectNotNull(Boolean.class);
   out.writeObjectNotNull(Byte.class);
   out.writeObjectNotNull(Double.class);
   ReadBuffer b = out.getStaticBuffer().asReadBuffer();
   assertEquals(Boolean.class, serialize.readObjectNotNull(b, Class.class));
   assertEquals(Byte.class, serialize.readObjectNotNull(b, Class.class));
   assertEquals(Double.class, serialize.readObjectNotNull(b, Class.class));
 }
 private Object readInline(ByteBuffer read, TitanType type) {
   if (type.isPropertyKey()) {
     TitanKey proptype = ((TitanKey) type);
     if (hasGenericDataType(proptype)) return serializer.readClassAndObject(read);
     else return serializer.readObject(read, proptype.getDataType());
   } else {
     assert type.isEdgeLabel();
     Long id = Long.valueOf(VariableLong.readPositive(read));
     if (id.longValue() == 0) return null;
     else return id;
   }
 }
Beispiel #7
0
 @Test
 public void largeWriteTest() {
   String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 chars
   String str = "";
   for (int i = 0; i < 100; i++) str += base;
   DataOutput out = serialize.getDataOutput(128, true);
   out.writeObjectNotNull(str);
   ReadBuffer b = out.getStaticBuffer().asReadBuffer();
   if (printStats) log.debug(bufferStats(b));
   assertEquals(str, serialize.readObjectNotNull(b, String.class));
   assertFalse(b.hasRemaining());
 }
Beispiel #8
0
  @Test
  public void parallelDeserialization() throws InterruptedException {
    DataOutput out = serialize.getDataOutput(128, true);
    out.putLong(8);
    out.writeClassAndObject(Long.valueOf(8));
    TestClass c = new TestClass(5, 8, new short[] {1, 2, 3, 4, 5}, TestEnum.Two);
    out.writeObject(c, TestClass.class);
    final StaticBuffer b = out.getStaticBuffer();

    int numThreads = 100;
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
      threads[i] =
          new Thread(
              new Runnable() {
                @Override
                public void run() {
                  for (int j = 0; j < 100000; j++) {
                    ReadBuffer c = b.asReadBuffer();
                    assertEquals(8, c.getLong());
                    Long l = (Long) serialize.readClassAndObject(c);
                    assertEquals(8, l.longValue());
                    TestClass c2 = serialize.readObjectNotNull(c, TestClass.class);
                  }
                }
              });
      threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
      threads[i].join();
    }
  }
Beispiel #9
0
 @Test
 public void longWriteTest() {
   String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 chars
   int no = 100;
   DataOutput out = serialize.getDataOutput(128, true);
   for (int i = 0; i < no; i++) {
     String str = base + (i + 1);
     out.writeObjectNotNull(str);
   }
   ReadBuffer b = out.getStaticBuffer().asReadBuffer();
   if (printStats) log.debug(bufferStats(b));
   for (int i = 0; i < no; i++) {
     String str = base + (i + 1);
     String read = serialize.readObjectNotNull(b, String.class);
     assertEquals(str, read);
   }
   assertFalse(b.hasRemaining());
 }
Beispiel #10
0
 @Test(expected = IllegalArgumentException.class)
 public void checkNonObject() {
   DataOutput out = serialize.getDataOutput(128, false);
   out.writeObject("This is a test", String.class);
 }
Beispiel #11
0
  @Test
  public void primitiveSerialization() {
    DataOutput out = serialize.getDataOutput(128, true);
    out.writeObjectNotNull(Boolean.FALSE);
    out.writeObjectNotNull(Boolean.TRUE);
    out.writeObjectNotNull(Byte.MIN_VALUE);
    out.writeObjectNotNull(Byte.MAX_VALUE);
    out.writeObjectNotNull(new Byte((byte) 0));
    out.writeObjectNotNull(Short.MIN_VALUE);
    out.writeObjectNotNull(Short.MAX_VALUE);
    out.writeObjectNotNull(new Short((short) 0));
    out.writeObjectNotNull(Character.MIN_VALUE);
    out.writeObjectNotNull(Character.MAX_VALUE);
    out.writeObjectNotNull(new Character('a'));
    out.writeObjectNotNull(Integer.MIN_VALUE);
    out.writeObjectNotNull(Integer.MAX_VALUE);
    out.writeObjectNotNull(new Integer(0));
    out.writeObjectNotNull(Long.MIN_VALUE);
    out.writeObjectNotNull(Long.MAX_VALUE);
    out.writeObjectNotNull(new Long(0));
    out.writeObjectNotNull(FloatSerializer.MIN_VALUE);
    out.writeObjectNotNull(FloatSerializer.MAX_VALUE);
    out.writeObjectNotNull(new Float((float) 0.0));
    out.writeObjectNotNull(DoubleSerializer.MIN_VALUE);
    out.writeObjectNotNull(DoubleSerializer.MAX_VALUE);
    out.writeObjectNotNull(new Double(0.0));

    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(Boolean.FALSE, serialize.readObjectNotNull(b, Boolean.class));
    assertEquals(Boolean.TRUE, serialize.readObjectNotNull(b, Boolean.class));
    assertEquals(Byte.MIN_VALUE, serialize.readObjectNotNull(b, Byte.class).longValue());
    assertEquals(Byte.MAX_VALUE, serialize.readObjectNotNull(b, Byte.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Byte.class).longValue());
    assertEquals(Short.MIN_VALUE, serialize.readObjectNotNull(b, Short.class).longValue());
    assertEquals(Short.MAX_VALUE, serialize.readObjectNotNull(b, Short.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Short.class).longValue());
    assertEquals(Character.MIN_VALUE, serialize.readObjectNotNull(b, Character.class).charValue());
    assertEquals(Character.MAX_VALUE, serialize.readObjectNotNull(b, Character.class).charValue());
    assertEquals(new Character('a'), serialize.readObjectNotNull(b, Character.class));
    assertEquals(Integer.MIN_VALUE, serialize.readObjectNotNull(b, Integer.class).longValue());
    assertEquals(Integer.MAX_VALUE, serialize.readObjectNotNull(b, Integer.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Integer.class).longValue());
    assertEquals(Long.MIN_VALUE, serialize.readObjectNotNull(b, Long.class).longValue());
    assertEquals(Long.MAX_VALUE, serialize.readObjectNotNull(b, Long.class).longValue());
    assertEquals(0, serialize.readObjectNotNull(b, Long.class).longValue());
    assertEquals(
        FloatSerializer.MIN_VALUE, serialize.readObjectNotNull(b, Float.class).floatValue(), 1e-20);
    assertEquals(
        FloatSerializer.MAX_VALUE, serialize.readObjectNotNull(b, Float.class).floatValue(), 1e-20);
    assertEquals(0.0, serialize.readObjectNotNull(b, Float.class).floatValue(), 1e-20);
    assertEquals(
        DoubleSerializer.MIN_VALUE,
        serialize.readObjectNotNull(b, Double.class).doubleValue(),
        1e-40);
    assertEquals(
        DoubleSerializer.MAX_VALUE,
        serialize.readObjectNotNull(b, Double.class).doubleValue(),
        1e-40);
    assertEquals(0.0, serialize.readObjectNotNull(b, Double.class).doubleValue(), 1e-20);
  }
  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);
  }
  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;
  }
  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);
        }
      }
    }
  }
 private ByteBuffer getIndexKey(Object att) {
   DataOutput out = serializer.getDataOutput(defaultOutputCapacity, true);
   out.writeObjectNotNull(att);
   return out.getByteBuffer();
 }