Skip to content
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
6 changes: 3 additions & 3 deletions src/main/java/com/maxmind/db/CHMCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CHMCache implements NodeCache {
private static final int DEFAULT_CAPACITY = 4096;

private final int capacity;
private final ConcurrentHashMap<CacheKey, Object> cache;
private final ConcurrentHashMap<CacheKey, DecodedValue> cache;
private boolean cacheFull = false;

public CHMCache() {
Expand All @@ -26,8 +26,8 @@ public CHMCache(int capacity) {
}

@Override
public Object get(CacheKey key, Loader loader) throws IOException {
Object value = cache.get(key);
public DecodedValue get(CacheKey key, Loader loader) throws IOException {
DecodedValue value = cache.get(key);
if (value == null) {
value = loader.load(key);
if (!cacheFull) {
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/maxmind/db/CacheKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,9 @@ public boolean equals(Object o) {
}

if (this.type == null) {
if (other.type != null) {
return false;
}
} else if (!this.type.equals(other.type)) {
return false;
return other.type == null;
}

return true;
return this.type.equals(other.type);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/maxmind/db/DecodedValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.maxmind.db;

public final class DecodedValue {
final Object value;

DecodedValue(Object value) {
this.value = value;
}

Object getValue() {
return value;
}
}
25 changes: 12 additions & 13 deletions src/main/java/com/maxmind/db/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -74,10 +73,10 @@ public <T> T decode(int offset, Class<T> cls) throws IOException {
}

this.buffer.position(offset);
return cls.cast(decode(cls, null));
return cls.cast(decode(cls, null).getValue());
}

private <T> Object decode(CacheKey<T> key) throws IOException {
private <T> DecodedValue decode(CacheKey<T> key) throws IOException {
int offset = key.getOffset();
if (offset >= this.buffer.capacity()) {
throw new InvalidDatabaseException(
Expand All @@ -90,7 +89,7 @@ private <T> Object decode(CacheKey<T> key) throws IOException {
return decode(cls, key.getType());
}

private <T> Object decode(Class<T> cls, java.lang.reflect.Type genericType)
private <T> DecodedValue decode(Class<T> cls, java.lang.reflect.Type genericType)
throws IOException {
int ctrlByte = 0xFF & this.buffer.get();

Expand All @@ -107,14 +106,14 @@ private <T> Object decode(Class<T> cls, java.lang.reflect.Type genericType)

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

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

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

buffer.position(position);
return o;
Expand Down Expand Up @@ -149,7 +148,7 @@ private <T> Object decode(Class<T> cls, java.lang.reflect.Type genericType)
}
}

return this.decodeByType(type, size, cls, genericType);
return new DecodedValue(this.decodeByType(type, size, cls, genericType));
}

private <T> Object decodeByType(
Expand Down Expand Up @@ -284,7 +283,7 @@ private <T, V> List<V> decodeArray(
Class<V> elementClass
) throws IOException {
if (!List.class.isAssignableFrom(cls) && !cls.equals(Object.class)) {
throw new DeserializationException();
throw new DeserializationException("Unable to deserialize an array into an " + cls);
}

List<V> array;
Expand All @@ -310,7 +309,7 @@ private <T, V> List<V> decodeArray(
}

for (int i = 0; i < size; i++) {
Object e = this.decode(elementClass, null);
Object e = this.decode(elementClass, null).getValue();
array.add(elementClass.cast(e));
}

Expand Down Expand Up @@ -371,8 +370,8 @@ private <T, V> Map<String, V> decodeMapIntoMap(
}

for (int i = 0; i < size; i++) {
String key = (String) this.decode(String.class, null);
Object value = this.decode(valueClass, null);
String key = (String) this.decode(String.class, null).getValue();
Object value = this.decode(valueClass, null).getValue();
map.put(key, valueClass.cast(value));
}

Expand Down Expand Up @@ -418,7 +417,7 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)

Object[] parameters = new Object[parameterTypes.length];
for (int i = 0; i < size; i++) {
String key = (String) this.decode(String.class, null);
String key = (String) this.decode(String.class, null).getValue();

Integer parameterIndex = parameterIndexes.get(key);
if (parameterIndex == null) {
Expand All @@ -430,7 +429,7 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
parameters[parameterIndex] = this.decode(
parameterTypes[parameterIndex],
parameterGenericTypes[parameterIndex]
);
).getValue();
}

try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/maxmind/db/NoCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private NoCache() {
}

@Override
public Object get(CacheKey key, Loader loader) throws IOException {
public DecodedValue get(CacheKey key, Loader loader) throws IOException {
return loader.load(key);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/maxmind/db/NodeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
public interface NodeCache {

interface Loader {
Object load(CacheKey key) throws IOException;
DecodedValue load(CacheKey key) throws IOException;
}

Object get(CacheKey key, Loader loader) throws IOException;
DecodedValue get(CacheKey key, Loader loader) throws IOException;

}
2 changes: 1 addition & 1 deletion src/main/java/com/maxmind/db/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public <T> T get(InetAddress ipAddress, Class<T> cls) throws IOException {
*
* @param ipAddress the IP address to look up.
* @return the record for the IP address. If there is no data for the
* address, the non-null {@link Record} will still be returned.
* address, the non-null {@link DatabaseRecord} will still be returned.
* @throws IOException if a file I/O error occurs.
*/
public <T> DatabaseRecord<T> getRecord(InetAddress ipAddress, Class<T> cls)
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/maxmind/db/DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/maxmind/db/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,16 @@ public void testCacheKey() {

CacheKey a = new CacheKey(1, cls, getType(cls, 0));
CacheKey b = new CacheKey(1, cls, getType(cls, 0));
assertTrue(a.equals(b));
assertEquals(a, b);

CacheKey c = new CacheKey(2, cls, getType(cls, 0));
assertFalse(a.equals(c));
assertNotEquals(a, c);

CacheKey d = new CacheKey(1, String.class, getType(cls, 0));
assertFalse(a.equals(d));
assertNotEquals(a, d);

CacheKey e = new CacheKey(1, cls, getType(cls, 1));
assertFalse(a.equals(e));
assertNotEquals(a, e);
}

private <T> java.lang.reflect.Type getType(Class<T> cls, int i) {
Expand Down