Skip to content

Remove pointerTestHack #246

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

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 15 additions & 19 deletions src/main/java/com/maxmind/db/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@
*
* This class CANNOT be shared between threads
*/
final class Decoder {
class Decoder {

private static final Charset UTF_8 = StandardCharsets.UTF_8;

private static final int[] POINTER_VALUE_OFFSETS = {0, 0, 1 << 11, (1 << 19) + (1 << 11), 0};

// XXX - This is only for unit testings. We should possibly make a
// constructor to set this
boolean pointerTestHack = false;

private final NodeCache cache;

private final long pointerBase;
Expand Down Expand Up @@ -65,7 +61,7 @@ final class Decoder {

private final NodeCache.Loader cacheLoader = this::decode;

public <T> T decode(int offset, Class<T> cls) throws IOException {
<T> T decode(int offset, Class<T> cls) throws IOException {
if (offset >= this.buffer.capacity()) {
throw new InvalidDatabaseException(
"The MaxMind DB file's data section contains bad data: "
Expand Down Expand Up @@ -104,19 +100,7 @@ private <T> DecodedValue decode(Class<T> cls, java.lang.reflect.Type genericType
int packed = this.decodeInteger(base, pointerSize);
long pointer = packed + this.pointerBase + POINTER_VALUE_OFFSETS[pointerSize];

// for unit testing
if (this.pointerTestHack) {
return new DecodedValue(pointer);
}

int targetOffset = (int) pointer;
int position = buffer.position();

CacheKey key = new CacheKey(targetOffset, cls, genericType);
DecodedValue o = cache.get(key, cacheLoader);

buffer.position(position);
return o;
return decodePointer(pointer, cls, genericType);
}

if (type.equals(Type.EXTENDED)) {
Expand Down Expand Up @@ -151,6 +135,18 @@ private <T> DecodedValue decode(Class<T> cls, java.lang.reflect.Type genericType
return new DecodedValue(this.decodeByType(type, size, cls, genericType));
}

DecodedValue decodePointer(long pointer, Class<?> cls, java.lang.reflect.Type genericType)
throws IOException {
int targetOffset = (int) pointer;
int position = buffer.position();

CacheKey key = new CacheKey(targetOffset, cls, genericType);
DecodedValue o = cache.get(key, cacheLoader);

buffer.position(position);
return o;
}

private <T> Object decodeByType(
Type type,
int size,
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/maxmind/db/DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ private static <T> void testTypeDecoding(Type type, Map<T, byte[]> tests)
try (FileChannel fc = DecoderTest.getFileChannel(input)) {
MappedByteBuffer mmap = fc.map(MapMode.READ_ONLY, 0, fc.size());

Decoder decoder = new Decoder(cache, mmap, 0);
decoder.pointerTestHack = true;
Decoder decoder = new TestDecoder(cache, mmap, 0);

// XXX - this could be streamlined
if (type.equals(Type.BYTES)) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/maxmind/db/TestDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.maxmind.db;

import java.lang.reflect.Type;
import java.nio.ByteBuffer;

final class TestDecoder extends Decoder {

TestDecoder(NodeCache cache, ByteBuffer buffer, long pointerBase) {
super(cache, buffer, pointerBase);
}

@Override
DecodedValue decodePointer(long pointer, Class<?> cls, Type genericType) {
// bypass cache
return new DecodedValue(pointer);
}

}
Loading