Skip to content

Commit

Permalink
Readded BytesReferenceSerializer impl as ICacheKeySerializerTests dep…
Browse files Browse the repository at this point in the history
…ended on it

Signed-off-by: Peter Alfonsi <petealft@amazon.com>
  • Loading branch information
Peter Alfonsi committed Feb 13, 2024
1 parent 9b1e66c commit 01e04a8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.cache.tier;

import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;

import java.util.Arrays;

/**
* A serializer which transforms BytesReference to byte[].
* The type of BytesReference is NOT preserved after deserialization, but nothing in opensearch should care.
*/
public class BytesReferenceSerializer implements Serializer<BytesReference, byte[]> {
// This class does not get passed to ehcache itself, so it's not required that classes match after deserialization.

public BytesReferenceSerializer() {}

@Override
public byte[] serialize(BytesReference object) {
return BytesReference.toBytes(object);
}

@Override
public BytesReference deserialize(byte[] bytes) {
if (bytes == null) {
return null;
}
return new BytesArray(bytes);
}

@Override
public boolean equals(BytesReference object, byte[] bytes) {
return Arrays.equals(serialize(object), bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.cache.tier;

import org.opensearch.common.Randomness;
import org.opensearch.common.bytes.ReleasableBytesReference;
import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.bytes.CompositeBytesReference;
import org.opensearch.core.common.util.ByteArray;
import org.opensearch.test.OpenSearchTestCase;

import java.util.Random;

public class BytesReferenceSerializerTests extends OpenSearchTestCase {
public void testEquality() throws Exception {
BytesReferenceSerializer ser = new BytesReferenceSerializer();
// Test that values are equal before and after serialization, for each implementation of BytesReference.
byte[] bytesValue = new byte[1000];
Random rand = Randomness.get();
rand.nextBytes(bytesValue);

BytesReference ba = new BytesArray(bytesValue);
byte[] serialized = ser.serialize(ba);
assertTrue(ser.equals(ba, serialized));
BytesReference deserialized = ser.deserialize(serialized);
assertEquals(ba, deserialized);

ba = new BytesArray(new byte[] {});
serialized = ser.serialize(ba);
assertTrue(ser.equals(ba, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(ba, deserialized);

BytesReference cbr = CompositeBytesReference.of(new BytesArray(bytesValue), new BytesArray(bytesValue));
serialized = ser.serialize(cbr);
assertTrue(ser.equals(cbr, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(cbr, deserialized);

// We need the PagedBytesReference to be larger than the page size (16 KB) in order to actually create it
byte[] pbrValue = new byte[PageCacheRecycler.PAGE_SIZE_IN_BYTES * 2];
rand.nextBytes(pbrValue);
ByteArray arr = BigArrays.NON_RECYCLING_INSTANCE.newByteArray(pbrValue.length);
arr.set(0L, pbrValue, 0, pbrValue.length);
assert !arr.hasArray();
BytesReference pbr = BytesReference.fromByteArray(arr, pbrValue.length);
serialized = ser.serialize(pbr);
assertTrue(ser.equals(pbr, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(pbr, deserialized);

BytesReference rbr = new ReleasableBytesReference(new BytesArray(bytesValue), ReleasableBytesReference.NO_OP);
serialized = ser.serialize(rbr);
assertTrue(ser.equals(rbr, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(rbr, deserialized);
}
}

0 comments on commit 01e04a8

Please sign in to comment.