Skip to content

Commit

Permalink
added test for network serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Oct 12, 2015
1 parent 2a00420 commit b757c02
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
Expand Up @@ -54,6 +54,8 @@ public String toString() {
return NAME; return NAME;
} }




@Override @Override
public ORecord fromStream(final byte[] iSource, ORecord iRecord, final String[] iFields) { public ORecord fromStream(final byte[] iSource, ORecord iRecord, final String[] iFields) {
if (iSource == null || iSource.length == 0) if (iSource == null || iSource.length == 0)
Expand Down
Expand Up @@ -25,15 +25,14 @@
import com.orientechnologies.common.serialization.types.ODecimalSerializer; import com.orientechnologies.common.serialization.types.ODecimalSerializer;
import com.orientechnologies.common.serialization.types.OIntegerSerializer; import com.orientechnologies.common.serialization.types.OIntegerSerializer;
import com.orientechnologies.common.serialization.types.OLongSerializer; import com.orientechnologies.common.serialization.types.OLongSerializer;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.*; import com.orientechnologies.orient.core.db.record.*;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag; import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.OSerializationException; import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.exception.OStorageException;
import com.orientechnologies.orient.core.exception.OValidationException; import com.orientechnologies.orient.core.exception.OValidationException;
import com.orientechnologies.orient.core.id.ORID; import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId; import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OGlobalProperty;
import com.orientechnologies.orient.core.metadata.schema.OProperty; import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OType; import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.ORecord; import com.orientechnologies.orient.core.record.ORecord;
Expand All @@ -43,7 +42,6 @@
import com.orientechnologies.orient.core.record.impl.ODocumentInternal; import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
import com.orientechnologies.orient.core.serialization.ODocumentSerializable; import com.orientechnologies.orient.core.serialization.ODocumentSerializable;
import com.orientechnologies.orient.core.serialization.OSerializableStream; import com.orientechnologies.orient.core.serialization.OSerializableStream;
import com.orientechnologies.orient.core.storage.OStorageProxy;
import com.orientechnologies.orient.core.type.tree.OMVRBTreeRIDSet; import com.orientechnologies.orient.core.type.tree.OMVRBTreeRIDSet;
import com.orientechnologies.orient.core.util.ODateHelper; import com.orientechnologies.orient.core.util.ODateHelper;


Expand Down Expand Up @@ -113,21 +111,13 @@ public void deserializePartial(final ODocument document, final BytesContainer by
valuePos = readInteger(bytes); valuePos = readInteger(bytes);
type = readOType(bytes); type = readOType(bytes);
} else { } else {
// LOAD GLOBAL PROPERTY BY ID throw new OStorageException("property id not supported in network serialization");
OGlobalProperty prop = getGlobalProperty(document, len);
fieldName = prop.getName();
valuePos = readInteger(bytes);
if (prop.getType() != OType.ANY)
type = prop.getType();
else
type = readOType(bytes);

} }


if (valuePos != 0) { if (valuePos != 0) {
int headerCursor = bytes.offset; int headerCursor = bytes.offset;
bytes.offset = valuePos; bytes.offset = valuePos;
final Object value = readSingleValue(bytes, type, document); final Object value = deserializeValue(bytes, type, document);
bytes.offset = headerCursor; bytes.offset = headerCursor;
ODocumentInternal.rawField(document, fieldName, value, type); ODocumentInternal.rawField(document, fieldName, value, type);
} else } else
Expand All @@ -150,7 +140,6 @@ public void deserialize(final ODocument document, final BytesContainer bytes) {
int valuePos; int valuePos;
OType type; OType type;
while (true) { while (true) {
OGlobalProperty prop = null;
final int len = OVarIntSerializer.readAsInteger(bytes); final int len = OVarIntSerializer.readAsInteger(bytes);
if (len == 0) { if (len == 0) {
// SCAN COMPLETED // SCAN COMPLETED
Expand All @@ -162,14 +151,7 @@ public void deserialize(final ODocument document, final BytesContainer bytes) {
valuePos = readInteger(bytes); valuePos = readInteger(bytes);
type = readOType(bytes); type = readOType(bytes);
} else { } else {
// LOAD GLOBAL PROPERTY BY ID throw new OStorageException("property id not supported in network serialization");
prop = getGlobalProperty(document, len);
fieldName = prop.getName();
valuePos = readInteger(bytes);
if (prop.getType() != OType.ANY)
type = prop.getType();
else
type = readOType(bytes);
} }


if (ODocumentInternal.rawContainsField(document, fieldName)) { if (ODocumentInternal.rawContainsField(document, fieldName)) {
Expand All @@ -179,7 +161,7 @@ public void deserialize(final ODocument document, final BytesContainer bytes) {
if (valuePos != 0) { if (valuePos != 0) {
int headerCursor = bytes.offset; int headerCursor = bytes.offset;
bytes.offset = valuePos; bytes.offset = valuePos;
final Object value = readSingleValue(bytes, type, document); final Object value = deserializeValue(bytes, type, document);
if (bytes.offset > last) if (bytes.offset > last)
last = bytes.offset; last = bytes.offset;
bytes.offset = headerCursor; bytes.offset = headerCursor;
Expand All @@ -204,8 +186,6 @@ public void serialize(final ODocument document, final BytesContainer bytes, fina
return; return;
} }


final Map<String, OProperty> props = clazz != null ? clazz.propertiesMap() : null;

final Set<Entry<String, ODocumentEntry>> fields = ODocumentInternal.rawEntries(document); final Set<Entry<String, ODocumentEntry>> fields = ODocumentInternal.rawEntries(document);


final int[] pos = new int[fields.size()]; final int[] pos = new int[fields.size()];
Expand All @@ -217,19 +197,8 @@ public void serialize(final ODocument document, final BytesContainer bytes, fina
ODocumentEntry docEntry = entry.getValue(); ODocumentEntry docEntry = entry.getValue();
if (!docEntry.exist()) if (!docEntry.exist())
continue; continue;
if (docEntry.property == null && props != null) writeString(bytes, entry.getKey());
docEntry.property = props.get(entry.getKey()); pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE + 1);

if (docEntry.property != null) {
OVarIntSerializer.write(bytes, (docEntry.property.getId() + 1) * -1);
if (docEntry.property.getType() != OType.ANY)
pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE);
else
pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE + 1);
} else {
writeString(bytes, entry.getKey());
pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE + 1);
}
values[i] = entry; values[i] = entry;
i++; i++;
} }
Expand All @@ -242,13 +211,11 @@ public void serialize(final ODocument document, final BytesContainer bytes, fina
if (value != null) { if (value != null) {
final OType type = getFieldType(values[i].getValue()); final OType type = getFieldType(values[i].getValue());
if (type == null) { if (type == null) {
throw new OSerializationException("Impossible serialize value of type " + value.getClass() throw new OSerializationException("Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer");
+ " with the ODocument binary serializer");
} }
pointer = writeSingleValue(bytes, value, type, getLinkedType(document, type, values[i].getKey())); pointer = serializeValue(bytes, value, type, getLinkedType(document, type, values[i].getKey()));
OIntegerSerializer.INSTANCE.serializeLiteral(pointer, bytes.bytes, pos[i]); OIntegerSerializer.INSTANCE.serializeLiteral(pointer, bytes.bytes, pos[i]);
if (values[i].getValue().property == null || values[i].getValue().property.getType() == OType.ANY) writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type);
writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type);
} }
} }


Expand All @@ -263,11 +230,6 @@ protected OClass serializeClass(final ODocument document, final BytesContainer b
return clazz; return clazz;
} }


protected OGlobalProperty getGlobalProperty(final ODocument document, final int len) {
final int id = (len * -1) - 1;
return ODocumentInternal.getGlobalPropertyById(document, id);
}

protected OType readOType(final BytesContainer bytes) { protected OType readOType(final BytesContainer bytes) {
return OType.getById(readByte(bytes)); return OType.getById(readByte(bytes));
} }
Expand All @@ -276,7 +238,7 @@ private void writeOType(BytesContainer bytes, int pos, OType type) {
bytes.bytes[pos] = (byte) type.getId(); bytes.bytes[pos] = (byte) type.getId();
} }


protected Object readSingleValue(BytesContainer bytes, OType type, ODocument document) { public Object deserializeValue(BytesContainer bytes, OType type, ODocument document) {
Object value = null; Object value = null;
switch (type) { switch (type) {
case INTEGER: case INTEGER:
Expand Down Expand Up @@ -398,7 +360,7 @@ private Map<Object, OIdentifiable> readLinkMap(final BytesContainer bytes, final
Map<Object, OIdentifiable> result = new ORecordLazyMap(document); Map<Object, OIdentifiable> result = new ORecordLazyMap(document);
while ((size--) > 0) { while ((size--) > 0) {
OType keyType = readOType(bytes); OType keyType = readOType(bytes);
Object key = readSingleValue(bytes, keyType, document); Object key = deserializeValue(bytes, keyType, document);
ORecordId value = readOptimizedLink(bytes); ORecordId value = readOptimizedLink(bytes);
if (value.equals(NULL_RECORD_ID)) if (value.equals(NULL_RECORD_ID))
result.put(key, null); result.put(key, null);
Expand All @@ -414,13 +376,13 @@ private Object readEmbeddedMap(final BytesContainer bytes, final ODocument docum
int last = 0; int last = 0;
while ((size--) > 0) { while ((size--) > 0) {
OType keyType = readOType(bytes); OType keyType = readOType(bytes);
Object key = readSingleValue(bytes, keyType, document); Object key = deserializeValue(bytes, keyType, document);
final int valuePos = readInteger(bytes); final int valuePos = readInteger(bytes);
final OType type = readOType(bytes); final OType type = readOType(bytes);
if (valuePos != 0) { if (valuePos != 0) {
int headerCursor = bytes.offset; int headerCursor = bytes.offset;
bytes.offset = valuePos; bytes.offset = valuePos;
Object value = readSingleValue(bytes, type, document); Object value = deserializeValue(bytes, type, document);
if (bytes.offset > last) if (bytes.offset > last)
last = bytes.offset; last = bytes.offset;
bytes.offset = headerCursor; bytes.offset = headerCursor;
Expand Down Expand Up @@ -459,7 +421,7 @@ private Collection<?> readEmbeddedCollection(final BytesContainer bytes, final C
if (itemType == OType.ANY) if (itemType == OType.ANY)
found.add(null); found.add(null);
else else
found.add(readSingleValue(bytes, itemType, document)); found.add(deserializeValue(bytes, itemType, document));
} }
return found; return found;
} }
Expand All @@ -481,7 +443,7 @@ private OType getLinkedType(ODocument document, OType type, String key) {
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private int writeSingleValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) { public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
int pointer = 0; int pointer = 0;
switch (type) { switch (type) {
case INTEGER: case INTEGER:
Expand Down Expand Up @@ -592,8 +554,7 @@ private int writeBinary(final BytesContainer bytes, final byte[] valueBytes) {
} }


private int writeLinkMap(final BytesContainer bytes, final Map<Object, OIdentifiable> map) { private int writeLinkMap(final BytesContainer bytes, final Map<Object, OIdentifiable> map) {
final boolean disabledAutoConversion = map instanceof ORecordLazyMultiValue final boolean disabledAutoConversion = map instanceof ORecordLazyMultiValue && ((ORecordLazyMultiValue) map).isAutoConvertToRecord();
&& ((ORecordLazyMultiValue) map).isAutoConvertToRecord();


if (disabledAutoConversion) if (disabledAutoConversion)
// AVOID TO FETCH RECORD // AVOID TO FETCH RECORD
Expand Down Expand Up @@ -643,10 +604,9 @@ private int writeEmbeddedMap(BytesContainer bytes, Map<Object, Object> map) {
if (value != null) { if (value != null) {
final OType type = getTypeFromValueEmbedded(value); final OType type = getTypeFromValueEmbedded(value);
if (type == null) { if (type == null) {
throw new OSerializationException("Impossible serialize value of type " + value.getClass() throw new OSerializationException("Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer");
+ " with the ODocument binary serializer");
} }
pointer = writeSingleValue(bytes, value, type, null); pointer = serializeValue(bytes, value, type, null);
OIntegerSerializer.INSTANCE.serializeLiteral(pointer, bytes.bytes, pos[i]); OIntegerSerializer.INSTANCE.serializeLiteral(pointer, bytes.bytes, pos[i]);
writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type); writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type);
} }
Expand All @@ -667,8 +627,6 @@ private int writeOptimizedLink(final BytesContainer bytes, OIdentifiable link) {
if (real != null) if (real != null)
link = real; link = real;
} }
assert link.getIdentity().isValid() || (ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy) : "Impossible to serialize invalid link "
+ link.getIdentity();
final int pos = OVarIntSerializer.write(bytes, link.getIdentity().getClusterId()); final int pos = OVarIntSerializer.write(bytes, link.getIdentity().getClusterId());
OVarIntSerializer.write(bytes, link.getIdentity().getClusterPosition()); OVarIntSerializer.write(bytes, link.getIdentity().getClusterPosition());
return pos; return pos;
Expand All @@ -678,8 +636,7 @@ private int writeLinkCollection(final BytesContainer bytes, final Collection<OId
assert (!(value instanceof OMVRBTreeRIDSet)); assert (!(value instanceof OMVRBTreeRIDSet));
final int pos = OVarIntSerializer.write(bytes, value.size()); final int pos = OVarIntSerializer.write(bytes, value.size());


final boolean disabledAutoConversion = value instanceof ORecordLazyMultiValue final boolean disabledAutoConversion = value instanceof ORecordLazyMultiValue && ((ORecordLazyMultiValue) value).isAutoConvertToRecord();
&& ((ORecordLazyMultiValue) value).isAutoConvertToRecord();


if (disabledAutoConversion) if (disabledAutoConversion)
// AVOID TO FETCH RECORD // AVOID TO FETCH RECORD
Expand Down Expand Up @@ -719,10 +676,9 @@ private int writeEmbeddedCollection(final BytesContainer bytes, final Collection
type = linkedType; type = linkedType;
if (type != null) { if (type != null) {
writeOType(bytes, bytes.alloc(1), type); writeOType(bytes, bytes.alloc(1), type);
writeSingleValue(bytes, itemValue, type, null); serializeValue(bytes, itemValue, type, null);
} else { } else {
throw new OSerializationException("Impossible serialize value of type " + value.getClass() throw new OSerializationException("Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer");
+ " with the ODocument binary serializer");
} }
} }
return pos; return pos;
Expand Down Expand Up @@ -798,4 +754,17 @@ protected String stringFromBytes(final byte[] bytes, final int offset, final int
throw OException.wrapException(new OSerializationException("Error on string decoding"), e); throw OException.wrapException(new OSerializationException("Error on string decoding"), e);
} }
} }

public OBinaryField deserializeField(final BytesContainer bytes, final OClass iClass, final String iFieldName) {
//TODO: check if integrate the binary disc binary comparator here
throw new UnsupportedOperationException("network serializer doesn't support comparators");
}

@Override
public OBinaryComparator getComparator() {
//TODO: check if integrate the binary disc binary comparator here
throw new UnsupportedOperationException("network serializer doesn't support comparators");
}


} }
@@ -0,0 +1,15 @@
package com.orientechnologies.orient.core.record.impl;

import com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializerNetwork;

/**
* Created by tglman on 05/10/15.
*/
public class ODocumentSerializerNetworkTest extends ODocumentSchemalessBinarySerializationTest {


public ODocumentSerializerNetworkTest() {
serializer = new ORecordSerializerNetwork();
}

}

0 comments on commit b757c02

Please sign in to comment.