Skip to content

Commit e0a7dc3

Browse files
authored
Merge pull request #80 from maxmind/greg/cache-value-class
Add cache value class
2 parents 3431dac + 09c13ca commit e0a7dc3

9 files changed

Lines changed: 38 additions & 32 deletions

File tree

src/main/java/com/maxmind/db/CHMCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CHMCache implements NodeCache {
1313
private static final int DEFAULT_CAPACITY = 4096;
1414

1515
private final int capacity;
16-
private final ConcurrentHashMap<CacheKey, Object> cache;
16+
private final ConcurrentHashMap<CacheKey, DecodedValue> cache;
1717
private boolean cacheFull = false;
1818

1919
public CHMCache() {
@@ -26,8 +26,8 @@ public CHMCache(int capacity) {
2626
}
2727

2828
@Override
29-
public Object get(CacheKey key, Loader loader) throws IOException {
30-
Object value = cache.get(key);
29+
public DecodedValue get(CacheKey key, Loader loader) throws IOException {
30+
DecodedValue value = cache.get(key);
3131
if (value == null) {
3232
value = loader.load(key);
3333
if (!cacheFull) {

src/main/java/com/maxmind/db/CacheKey.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,9 @@ public boolean equals(Object o) {
4444
}
4545

4646
if (this.type == null) {
47-
if (other.type != null) {
48-
return false;
49-
}
50-
} else if (!this.type.equals(other.type)) {
51-
return false;
47+
return other.type == null;
5248
}
53-
54-
return true;
49+
return this.type.equals(other.type);
5550
}
5651

5752
@Override
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.maxmind.db;
2+
3+
public final class DecodedValue {
4+
final Object value;
5+
6+
DecodedValue(Object value) {
7+
this.value = value;
8+
}
9+
10+
Object getValue() {
11+
return value;
12+
}
13+
}

src/main/java/com/maxmind/db/Decoder.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.lang.annotation.Annotation;
5-
import java.lang.reflect.Array;
65
import java.lang.reflect.Constructor;
76
import java.lang.reflect.InvocationTargetException;
87
import java.lang.reflect.ParameterizedType;
@@ -74,10 +73,10 @@ public <T> T decode(int offset, Class<T> cls) throws IOException {
7473
}
7574

7675
this.buffer.position(offset);
77-
return cls.cast(decode(cls, null));
76+
return cls.cast(decode(cls, null).getValue());
7877
}
7978

80-
private <T> Object decode(CacheKey<T> key) throws IOException {
79+
private <T> DecodedValue decode(CacheKey<T> key) throws IOException {
8180
int offset = key.getOffset();
8281
if (offset >= this.buffer.capacity()) {
8382
throw new InvalidDatabaseException(
@@ -90,7 +89,7 @@ private <T> Object decode(CacheKey<T> key) throws IOException {
9089
return decode(cls, key.getType());
9190
}
9291

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

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

108107
// for unit testing
109108
if (this.POINTER_TEST_HACK) {
110-
return pointer;
109+
return new DecodedValue(pointer);
111110
}
112111

113112
int targetOffset = (int) pointer;
114113
int position = buffer.position();
115114

116115
CacheKey key = new CacheKey(targetOffset, cls, genericType);
117-
Object o = decode(key);
116+
DecodedValue o = cache.get(key, cacheLoader);
118117

119118
buffer.position(position);
120119
return o;
@@ -149,7 +148,7 @@ private <T> Object decode(Class<T> cls, java.lang.reflect.Type genericType)
149148
}
150149
}
151150

152-
return this.decodeByType(type, size, cls, genericType);
151+
return new DecodedValue(this.decodeByType(type, size, cls, genericType));
153152
}
154153

155154
private <T> Object decodeByType(
@@ -284,7 +283,7 @@ private <T, V> List<V> decodeArray(
284283
Class<V> elementClass
285284
) throws IOException {
286285
if (!List.class.isAssignableFrom(cls) && !cls.equals(Object.class)) {
287-
throw new DeserializationException();
286+
throw new DeserializationException("Unable to deserialize an array into an " + cls);
288287
}
289288

290289
List<V> array;
@@ -310,7 +309,7 @@ private <T, V> List<V> decodeArray(
310309
}
311310

312311
for (int i = 0; i < size; i++) {
313-
Object e = this.decode(elementClass, null);
312+
Object e = this.decode(elementClass, null).getValue();
314313
array.add(elementClass.cast(e));
315314
}
316315

@@ -371,8 +370,8 @@ private <T, V> Map<String, V> decodeMapIntoMap(
371370
}
372371

373372
for (int i = 0; i < size; i++) {
374-
String key = (String) this.decode(String.class, null);
375-
Object value = this.decode(valueClass, null);
373+
String key = (String) this.decode(String.class, null).getValue();
374+
Object value = this.decode(valueClass, null).getValue();
376375
map.put(key, valueClass.cast(value));
377376
}
378377

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

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

423422
Integer parameterIndex = parameterIndexes.get(key);
424423
if (parameterIndex == null) {
@@ -430,7 +429,7 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
430429
parameters[parameterIndex] = this.decode(
431430
parameterTypes[parameterIndex],
432431
parameterGenericTypes[parameterIndex]
433-
);
432+
).getValue();
434433
}
435434

436435
try {

src/main/java/com/maxmind/db/NoCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private NoCache() {
1313
}
1414

1515
@Override
16-
public Object get(CacheKey key, Loader loader) throws IOException {
16+
public DecodedValue get(CacheKey key, Loader loader) throws IOException {
1717
return loader.load(key);
1818
}
1919

src/main/java/com/maxmind/db/NodeCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
public interface NodeCache {
66

77
interface Loader {
8-
Object load(CacheKey key) throws IOException;
8+
DecodedValue load(CacheKey key) throws IOException;
99
}
1010

11-
Object get(CacheKey key, Loader loader) throws IOException;
11+
DecodedValue get(CacheKey key, Loader loader) throws IOException;
1212

1313
}

src/main/java/com/maxmind/db/Reader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public <T> T get(InetAddress ipAddress, Class<T> cls) throws IOException {
154154
*
155155
* @param ipAddress the IP address to look up.
156156
* @return the record for the IP address. If there is no data for the
157-
* address, the non-null {@link Record} will still be returned.
157+
* address, the non-null {@link DatabaseRecord} will still be returned.
158158
* @throws IOException if a file I/O error occurs.
159159
*/
160160
public <T> DatabaseRecord<T> getRecord(InetAddress ipAddress, Class<T> cls)

src/test/java/com/maxmind/db/DecoderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.RandomAccessFile;
6-
import java.lang.reflect.Array;
76
import java.math.BigInteger;
87
import java.nio.ByteBuffer;
98
import java.nio.MappedByteBuffer;

src/test/java/com/maxmind/db/ReaderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,16 @@ public void testCacheKey() {
659659

660660
CacheKey a = new CacheKey(1, cls, getType(cls, 0));
661661
CacheKey b = new CacheKey(1, cls, getType(cls, 0));
662-
assertTrue(a.equals(b));
662+
assertEquals(a, b);
663663

664664
CacheKey c = new CacheKey(2, cls, getType(cls, 0));
665-
assertFalse(a.equals(c));
665+
assertNotEquals(a, c);
666666

667667
CacheKey d = new CacheKey(1, String.class, getType(cls, 0));
668-
assertFalse(a.equals(d));
668+
assertNotEquals(a, d);
669669

670670
CacheKey e = new CacheKey(1, cls, getType(cls, 1));
671-
assertFalse(a.equals(e));
671+
assertNotEquals(a, e);
672672
}
673673

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

0 commit comments

Comments
 (0)