Skip to content

Commit

Permalink
Bugfix: Type pointers which started at >2GB were not serialized corre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
dkoszewnik committed Feb 15, 2022
1 parent f2035c7 commit e22707f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/main/java/com/netflix/nfgraph/util/ByteArrayBuffer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 Netflix, Inc.
* Copyright 2013-2022 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,17 +58,23 @@ public void write(ByteArrayBuffer buf) {
* Writes a variable-byte encoded integer to the byte array.
*/
public void writeVInt(int value) {
if(value < 0) {
if(value == -1) {
writeByte((byte)0x80);
return;
} else if(value < 0) {
writeByte((byte)(0x80 | ((value >>> 28))));
writeByte((byte)(0x80 | ((value >>> 21) & 0x7F)));
writeByte((byte)(0x80 | ((value >>> 14) & 0x7F)));
writeByte((byte)(0x80 | ((value >>> 7) & 0x7F)));
writeByte((byte)(value & 0x7F));
} else {
if(value > 0x0FFFFFFF) writeByte((byte)(0x80 | ((value >>> 28))));
if(value > 0x1FFFFF) writeByte((byte)(0x80 | ((value >>> 21) & 0x7F)));
if(value > 0x3FFF) writeByte((byte)(0x80 | ((value >>> 14) & 0x7F)));
if(value > 0x7F) writeByte((byte)(0x80 | ((value >>> 7) & 0x7F)));

writeByte((byte)(value & 0x7F));
}

if(value > 0x0FFFFFFF) writeByte((byte)(0x80 | ((value >>> 28))));
if(value > 0x1FFFFF) writeByte((byte)(0x80 | ((value >>> 21) & 0x7F)));
if(value > 0x3FFF) writeByte((byte)(0x80 | ((value >>> 14) & 0x7F)));
if(value > 0x7F) writeByte((byte)(0x80 | ((value >>> 7) & 0x7F)));

writeByte((byte)(value & 0x7F));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 Netflix, Inc.
* Copyright 2014-2022 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,5 +68,28 @@ public void dataLengthGreaterThan4GBUsesLongPointers() throws IOException {

Assert.assertTrue(deserialized instanceof NFCompressedGraphLongPointers);
}

@Test
public void pointersMightStartGreaterThan2GB() throws IOException {
NFCompressedGraphLongPointers pointers = new NFCompressedGraphLongPointers();

long bigStartVal = Integer.MAX_VALUE;
bigStartVal += 5;

long[] ptrs = new long[] { bigStartVal, bigStartVal + 10, bigStartVal + 20, bigStartVal + 100 };
pointers.addPointers("Test", ptrs);

NFCompressedGraphPointersSerializer serializer = new NFCompressedGraphPointersSerializer(pointers, bigStartVal + 125);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serializePointers(new DataOutputStream(baos));

NFCompressedGraphPointersDeserializer deserializer = new NFCompressedGraphPointersDeserializer();
NFCompressedGraphPointers deserialized = deserializer.deserializePointers(new DataInputStream(new ByteArrayInputStream(baos.toByteArray())));

for(int i=0;i<ptrs.length;i++) {
Assert.assertEquals(ptrs[i], deserialized.getPointer("Test", i));
}
}


}

0 comments on commit e22707f

Please sign in to comment.