Skip to content

Commit 3431dac

Browse files
authored
Merge pull request #79 from maxmind/horgh/primitives-cache
Do not cast() when retrieving from cache
2 parents 1bdaecb + d51bf9c commit 3431dac

3 files changed

Lines changed: 37 additions & 16 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public <T> T decode(int offset, Class<T> cls) throws IOException {
7777
return cls.cast(decode(cls, null));
7878
}
7979

80-
private <T> T decode(CacheKey<T> key) throws IOException {
80+
private <T> Object decode(CacheKey<T> key) throws IOException {
8181
int offset = key.getOffset();
8282
if (offset >= this.buffer.capacity()) {
8383
throw new InvalidDatabaseException(
@@ -87,7 +87,7 @@ private <T> T decode(CacheKey<T> key) throws IOException {
8787

8888
this.buffer.position(offset);
8989
Class<T> cls = key.getCls();
90-
return cls.cast(decode(cls, key.getType()));
90+
return decode(cls, key.getType());
9191
}
9292

9393
private <T> Object decode(Class<T> cls, java.lang.reflect.Type genericType)
@@ -114,7 +114,7 @@ private <T> Object decode(Class<T> cls, java.lang.reflect.Type genericType)
114114
int position = buffer.position();
115115

116116
CacheKey key = new CacheKey(targetOffset, cls, genericType);
117-
T o = cls.cast(cache.get(key, cacheLoader));
117+
Object o = decode(key);
118118

119119
buffer.position(position);
120120
return o;

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,38 @@ private void testNoIpV4SearchTree(Reader reader) throws IOException {
133133
@Test
134134
public void testDecodingTypesFile() throws IOException {
135135
this.testReader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));
136-
this.testDecodingTypes(this.testReader);
137-
this.testDecodingTypesIntoModelObject(this.testReader);
138-
this.testDecodingTypesIntoModelObjectBoxed(this.testReader);
136+
this.testDecodingTypes(this.testReader, true);
137+
this.testDecodingTypesIntoModelObject(this.testReader, true);
138+
this.testDecodingTypesIntoModelObjectBoxed(this.testReader, true);
139139
this.testDecodingTypesIntoModelWithList(this.testReader);
140140
}
141141

142142
@Test
143143
public void testDecodingTypesStream() throws IOException {
144144
this.testReader = new Reader(getStream("MaxMind-DB-test-decoder.mmdb"));
145-
this.testDecodingTypes(this.testReader);
146-
this.testDecodingTypesIntoModelObject(this.testReader);
147-
this.testDecodingTypesIntoModelObjectBoxed(this.testReader);
145+
this.testDecodingTypes(this.testReader, true);
146+
this.testDecodingTypesIntoModelObject(this.testReader, true);
147+
this.testDecodingTypesIntoModelObjectBoxed(this.testReader, true);
148148
this.testDecodingTypesIntoModelWithList(this.testReader);
149149
}
150150

151-
private void testDecodingTypes(Reader reader) throws IOException {
151+
@Test
152+
public void testDecodingTypesPointerDecoderFile() throws IOException {
153+
this.testReader = new Reader(getFile("MaxMind-DB-test-pointer-decoder.mmdb"));
154+
this.testDecodingTypes(this.testReader, false);
155+
this.testDecodingTypesIntoModelObject(this.testReader, false);
156+
this.testDecodingTypesIntoModelObjectBoxed(this.testReader, false);
157+
this.testDecodingTypesIntoModelWithList(this.testReader);
158+
}
159+
160+
private void testDecodingTypes(Reader reader, boolean booleanValue) throws IOException {
152161
Map record = reader.get(InetAddress.getByName("::1.1.1.0"), Map.class);
153162

154-
assertTrue((boolean) record.get("boolean"));
163+
if (booleanValue) {
164+
assertTrue((boolean) record.get("boolean"));
165+
} else {
166+
assertFalse((boolean) record.get("boolean"));
167+
}
155168

156169
assertArrayEquals(new byte[]{0, 0, 0, (byte) 42}, (byte[]) record
157170
.get("bytes"));
@@ -191,11 +204,15 @@ private void testDecodingTypes(Reader reader) throws IOException {
191204
(BigInteger) record.get("uint128"));
192205
}
193206

194-
private void testDecodingTypesIntoModelObject(Reader reader)
207+
private void testDecodingTypesIntoModelObject(Reader reader, boolean booleanValue)
195208
throws IOException {
196209
TestModel model = reader.get(InetAddress.getByName("::1.1.1.0"), TestModel.class);
197210

198-
assertTrue(model.booleanField);
211+
if (booleanValue) {
212+
assertTrue(model.booleanField);
213+
} else {
214+
assertFalse(model.booleanField);
215+
}
199216

200217
assertArrayEquals(new byte[]{0, 0, 0, (byte) 42}, model.bytesField);
201218

@@ -307,11 +324,15 @@ public MapXModel (
307324
}
308325
}
309326

310-
private void testDecodingTypesIntoModelObjectBoxed(Reader reader)
327+
private void testDecodingTypesIntoModelObjectBoxed(Reader reader, boolean booleanValue)
311328
throws IOException {
312329
TestModelBoxed model = reader.get(InetAddress.getByName("::1.1.1.0"), TestModelBoxed.class);
313330

314-
assertTrue(model.booleanField);
331+
if (booleanValue) {
332+
assertTrue(model.booleanField);
333+
} else {
334+
assertFalse(model.booleanField);
335+
}
315336

316337
assertArrayEquals(new byte[]{0, 0, 0, (byte) 42}, model.bytesField);
317338

src/test/resources/maxmind-db

Submodule maxmind-db updated 40 files

0 commit comments

Comments
 (0)