Skip to content

Commit

Permalink
Optimization by avoiding fetching records during serialization of LIN…
Browse files Browse the repository at this point in the history
…KMAPs
  • Loading branch information
lvca committed Aug 24, 2015
1 parent 331521d commit 4512083
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
Expand Up @@ -19,19 +19,18 @@
*/
package com.orientechnologies.orient.core.db.record;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.ORecordMultiValueHelper.MULTIVALUE_CONTENT_TYPE;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.ORecordInternal;
import com.orientechnologies.orient.core.record.impl.ODirtyManager;
import com.orientechnologies.orient.core.record.impl.ODocument;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
* Lazy implementation of LinkedHashMap. It's bound to a source ORecord object to keep track of changes. This avoid to call the
* makeDirty() by hand when the map is changed.
Expand Down
Expand Up @@ -20,12 +20,21 @@

package com.orientechnologies.orient.core.serialization.serializer.record.binary;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.serialization.types.ODecimalSerializer;
import com.orientechnologies.common.serialization.types.OIntegerSerializer;
import com.orientechnologies.common.serialization.types.OLongSerializer;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMap;
Expand All @@ -35,7 +44,6 @@
import com.orientechnologies.orient.core.db.record.OTrackedMap;
import com.orientechnologies.orient.core.db.record.OTrackedSet;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
Expand All @@ -50,21 +58,10 @@
import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
import com.orientechnologies.orient.core.serialization.ODocumentSerializable;
import com.orientechnologies.orient.core.serialization.OSerializableStream;
import com.orientechnologies.orient.core.serialization.serializer.ONetworkThreadLocalSerializer;
import com.orientechnologies.orient.core.storage.OStorageProxy;
import com.orientechnologies.orient.core.type.tree.OMVRBTreeRIDSet;
import com.orientechnologies.orient.core.util.ODateHelper;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ORecordSerializerBinaryV0 implements ODocumentSerializer {

private static final String CHARSET_UTF_8 = "UTF-8";
Expand Down Expand Up @@ -601,20 +598,33 @@ private int writeBinary(BytesContainer bytes, byte[] valueBytes) {
return pointer;
}

private int writeLinkMap(BytesContainer bytes, Map<Object, OIdentifiable> map) {
int fullPos = OVarIntSerializer.write(bytes, map.size());
for (Entry<Object, OIdentifiable> entry : map.entrySet()) {
// TODO:check skip of complex types
// FIXME: changed to support only string key on map
OType type = OType.STRING;
writeOType(bytes, bytes.alloc(1), type);
writeString(bytes, entry.getKey().toString());
if (entry.getValue() == null)
writeNullLink(bytes);
else
writeOptimizedLink(bytes, entry.getValue());
private int writeLinkMap(final BytesContainer bytes, final Map<Object, OIdentifiable> map) {
final boolean disabledAutoConversion = map instanceof ORecordLazyMultiValue
&& ((ORecordLazyMultiValue) map).isAutoConvertToRecord();

if (disabledAutoConversion)
// AVOID TO FETCH RECORD
((ORecordLazyMultiValue) map).setAutoConvertToRecord(false);

try {
int fullPos = OVarIntSerializer.write(bytes, map.size());
for (Entry<Object, OIdentifiable> entry : map.entrySet()) {
// TODO:check skip of complex types
// FIXME: changed to support only string key on map
OType type = OType.STRING;
writeOType(bytes, bytes.alloc(1), type);
writeString(bytes, entry.getKey().toString());
if (entry.getValue() == null)
writeNullLink(bytes);
else
writeOptimizedLink(bytes, entry.getValue());
}
return fullPos;

} finally {
if (disabledAutoConversion)
((ORecordLazyMultiValue) map).setAutoConvertToRecord(true);
}
return fullPos;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -664,7 +674,8 @@ private int writeOptimizedLink(BytesContainer bytes, OIdentifiable link) {
if (real != null)
link = real;
}
assert link.getIdentity().isValid() || (ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy) : "Impossible to serialize invalid link "+ link.getIdentity();
assert link.getIdentity().isValid() || (ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy) : "Impossible to serialize invalid link "
+ link.getIdentity();
int pos = OVarIntSerializer.write(bytes, link.getIdentity().getClusterId());
OVarIntSerializer.write(bytes, link.getIdentity().getClusterPosition());
return pos;
Expand All @@ -674,10 +685,10 @@ private int writeLinkCollection(BytesContainer bytes, Collection<OIdentifiable>
assert (!(value instanceof OMVRBTreeRIDSet));
int pos = OVarIntSerializer.write(bytes, value.size());

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

if (disabledAutoConvertion)
if (disabledAutoConversion)
// AVOID TO FETCH RECORD
((ORecordLazyMultiValue) value).setAutoConvertToRecord(false);

Expand All @@ -691,7 +702,7 @@ private int writeLinkCollection(BytesContainer bytes, Collection<OIdentifiable>
}

} finally {
if (disabledAutoConvertion)
if (disabledAutoConversion)
((ORecordLazyMultiValue) value).setAutoConvertToRecord(true);
}

Expand Down

0 comments on commit 4512083

Please sign in to comment.