Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-20688 Fixed broken handles after binary object detached (lightweighted). #11292

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,50 @@ public BinaryArrayIdentityResolver() {

/** {@inheritDoc} */
@Override protected int hashCode0(BinaryObject obj) {
int hash = 1;

if (obj instanceof BinaryObjectExImpl) {
BinaryObjectExImpl ex = (BinaryObjectExImpl)obj;

int start = ex.dataStartOffset();
int end = ex.footerStartOffset();

if (ex.hasArray()) {
// Handle heap object.
byte[] data = ex.array();

for (int i = start; i < end; i++)
hash = 31 * hash + data[i];
}
if (ex.hasArray())
return hashCode(ex.array(), start, end);
else {
// Handle offheap object.
int hash = 1;

long ptr = ex.offheapAddress();

for (int i = start; i < end; i++)
hash = 31 * hash + BinaryPrimitives.readByte(ptr, i);

return hash;
}
}
else if (obj instanceof BinaryEnumObjectImpl) {
int hash = 1;

int ord = obj.enumOrdinal();

// Construct hash as if it was an int serialized in little-endian form.
hash = 31 * hash + (ord & 0x000000FF);
hash = 31 * hash + (ord & 0x0000FF00);
hash = 31 * hash + (ord & 0x00FF0000);
hash = 31 * hash + (ord & 0xFF000000);

return hash;
}
else
throw new BinaryObjectException("Array identity resolver cannot be used with provided BinaryObject " +
"implementation: " + obj.getClass().getName());
}

/** */
public int hashCode(byte[] data, int startPos, int endPos) {
int hash = 1;

for (int i = startPos; i < endPos; i++)
hash = 31 * hash + data[i];

return hash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.apache.ignite.internal.GridDirectTransient;
import org.apache.ignite.internal.IgniteCodeGeneratingFail;
import org.apache.ignite.internal.binary.streams.BinaryHeapInputStream;
import org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream;
import org.apache.ignite.internal.binary.streams.BinaryOutputStream;
import org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.CacheObjectAdapter;
import org.apache.ignite.internal.processors.cache.CacheObjectContext;
Expand Down Expand Up @@ -282,16 +284,33 @@ private byte[] valueBytesFromArray(CacheObjectValueContext ctx) {
* @return Detached binary object.
*/
public BinaryObjectImpl detach() {
return detach(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why resolveCrossObjectReferences is true by default? Looks like it can be false.

}

/** */
public BinaryObjectImpl detach(boolean resolveCrossObjectReferences) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abbreviation should be used for Object

if (!detachAllowed || detached())
return this;

int len = length();

byte[] arr0 = new byte[len];
if (resolveCrossObjectReferences) {
ObjectDetachHelper detachHelper = ObjectDetachHelper.create(arr, start);

if (detachHelper.isCrossObjectReferencesDetected()) {
try (BinaryOutputStream out = new BinaryHeapOutputStream(2 * len)) {
detachHelper.detach(out);

return new BinaryObjectImpl(ctx, out.arrayCopy(), 0);
}
}
}

byte[] detachedData = new byte[len];

U.arrayCopy(arr, start, arr0, 0, len);
U.arrayCopy(arr, start, detachedData, 0, len);

return new BinaryObjectImpl(ctx, arr0, 0);
return new BinaryObjectImpl(ctx, detachedData, 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ float readFloat(int fieldId) throws BinaryObjectException {

/** {@inheritDoc} */
@Nullable @Override public Object readObjectDetached(boolean deserialize) throws BinaryObjectException {
return BinaryUtils.unmarshal(in, ctx, ldr, this, true, deserialize);
return BinaryUtils.unmarshal(in, ctx, ldr, new BinaryReaderHandlesHolderImpl(), true, deserialize);
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -2369,6 +2369,11 @@ private void streamPositionRandom(int pos) {
return in.remaining();
}

/** {@inheritDoc} */
@Override public boolean isEmpty() {
return hnds == null || hnds.isEmpty();
}

/** {@inheritDoc} */
@Override public void close() throws IOException {
// No-op.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ public void put(int pos, Object obj) {
data0.put(pos, obj);
}
}

/** */
public boolean isEmpty() {
return mode == MODE_EMPTY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ public interface BinaryReaderHandlesHolder {
* @return Handles.
*/
public BinaryReaderHandles handles();

/** */
public boolean isEmpty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public class BinaryReaderHandlesHolderImpl implements BinaryReaderHandlesHolder

return hnds;
}

/** {@inheritDoc} */
@Override public boolean isEmpty() {
return hnds == null || hnds.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,20 @@ public static int length(BinaryPositionReadable in, int start) {
return in.readIntPositioned(start + GridBinaryMarshaller.TOTAL_LEN_POS);
}

/** */
public static int dataStartRelative(BinaryPositionReadable in, int start) {
int typeId = in.readIntPositioned(start + GridBinaryMarshaller.TYPE_ID_POS);

if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) {
// Gets the length of the type name which is stored as string.
int len = in.readIntPositioned(start + GridBinaryMarshaller.DFLT_HDR_LEN + /** object type */1);

return GridBinaryMarshaller.DFLT_HDR_LEN + /** object type */1 + /** string length */ 4 + len;
}
else
return GridBinaryMarshaller.DFLT_HDR_LEN;
}

/**
* Get footer start of the object.
*
Expand Down Expand Up @@ -1929,21 +1943,22 @@ public static Object doReadOptimized(BinaryInputStream in, BinaryContext ctx, @N
BinaryObjectExImpl po;

if (detach) {
// In detach mode we simply copy object's content.
in.position(start);
BinaryObjectImpl binObj = new BinaryObjectImpl(ctx, in.array(), start);

binObj.detachAllowed(true);

po = new BinaryObjectImpl(ctx, in.readByteArray(len), 0);
po = binObj.detach(!handles.isEmpty());
}
else {
if (in.offheapPointer() == 0)
po = new BinaryObjectImpl(ctx, in.array(), start);
else
po = new BinaryObjectOffheapImpl(ctx, in.offheapPointer(), start,
in.remaining() + in.position());

in.position(start + po.length());
}

in.position(start + len);

handles.setHandle(po, start);

return po;
Expand Down
Loading
Loading