Skip to content

Commit

Permalink
Add support for byte arrays in primary key of history
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrAuguscik authored and Sunjeet committed Mar 12, 2024
1 parent 2874075 commit 0aca021
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
*/
public class HollowDiff {
private final EnumSet<FieldType> SINGLE_FIELD_SUPPORTED_TYPES = EnumSet.of(FieldType.INT, FieldType.LONG, FieldType.DOUBLE, FieldType.STRING, FieldType.FLOAT, FieldType.BOOLEAN);
private final EnumSet<FieldType> SINGLE_FIELD_SUPPORTED_TYPES = EnumSet.of(FieldType.INT, FieldType.LONG, FieldType.DOUBLE, FieldType.STRING, FieldType.FLOAT, FieldType.BOOLEAN, FieldType.BYTES);

private final Logger log = Logger.getLogger(HollowDiff.class.getName());
private final HollowReadStateEngine fromStateEngine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public Object getObject(int ordinal, FieldType type) {
return getLong(pointer);
case STRING:
return getString(pointer);
case BYTES:
return getBytes(pointer);
default:
throw new IllegalArgumentException("Unknown type " + type);
}
Expand Down Expand Up @@ -82,13 +84,17 @@ public long getLong(long pointer) {
}

public String getString(long pointer) {
return new String(getBytes(pointer));
}

public byte[] getBytes(long pointer) {
ByteData byteData = ordinalMap.getByteData().getUnderlyingArray();
int length = VarInt.readVInt(byteData, pointer);
byte[] bytes = new byte[length];
for(int i=0;i<length;i++) {
bytes[i] = byteData.get(pointer+1+i);
}
return new String(bytes);
return bytes;
}

public int writeAndGetOrdinal(Object objectToIntern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static Object[] parseKey(HollowReadStateEngine readStateEngine, PrimaryKe
key[i] = Float.parseFloat(fields[i]);
break;
case BYTES:
throw new IllegalArgumentException("Primary key contains a field of type BYTES");
key[i] = fields[i];
break;
}
}
return key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.netflix.hollow.tools.util;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import com.netflix.hollow.core.schema.HollowObjectSchema.FieldType;

Expand Down Expand Up @@ -162,6 +163,32 @@ public void testString() {
assertEquals(retrievedString2, "I can do this all day");
}

@Test
public void testBytes() {
byte[] bytesObj1 = "I am Groot".getBytes();
byte[] bytesObj2 = "I am Groot".getBytes();
byte[] bytesObj3 = "I can do this all day".getBytes();
byte[] bytesObj4 = "I can do this all day".getBytes();

int bytes1Ordinal = internPool.writeAndGetOrdinal(bytesObj1);
int bytes2Ordinal = internPool.writeAndGetOrdinal(bytesObj2);
internPool.prepareForRead();

byte[] retrievedString1 = (byte[]) internPool.getObject(bytes1Ordinal, FieldType.BYTES);

assertEquals(bytes1Ordinal, bytes2Ordinal);
assertArrayEquals(retrievedString1, "I am Groot".getBytes());

int bytes3Ordinal = internPool.writeAndGetOrdinal(bytesObj3);
int bytes4Ordinal = internPool.writeAndGetOrdinal(bytesObj4);
internPool.prepareForRead();

byte[] retrievedBytes2 = (byte[]) internPool.getObject(bytes3Ordinal, FieldType.BYTES);

assertEquals(bytes3Ordinal, bytes4Ordinal);
assertArrayEquals(retrievedBytes2, "I can do this all day".getBytes());
}

@Test
public void testBool() {
Boolean boolObj1 = true;
Expand Down

0 comments on commit 0aca021

Please sign in to comment.