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()); } } }
@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()); }
@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(); } }
@Test public void testObjectVerification() { KryoSerializer s = new KryoSerializer(true); DataOutput out = s.getDataOutput(128, true); Long l = Long.valueOf(128); out.writeClassAndObject(l); Calendar c = Calendar.getInstance(); out.writeClassAndObject(c); NoDefaultConstructor dc = new NoDefaultConstructor(5); try { out.writeClassAndObject(dc); fail(); } catch (IllegalArgumentException e) { } TestTransientClass d = new TestTransientClass(101); try { out.writeClassAndObject(d); fail(); } catch (IllegalArgumentException e) { } out.writeObject(null, TestClass.class); }
@Test(expected = IllegalArgumentException.class) public void checkNonObject() { DataOutput out = serialize.getDataOutput(128, false); out.writeObject("This is a test", String.class); }
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; }