Skip to content

Commit d4bc33b

Browse files
committed
Avoid exceptions in PostgresqlObjectId lookup.
We now fully rely onto the OID cache in PostgresqlObjectId instead of falling back to a valueOf(…) lookup that causes an exception. [resolves pgjdbc#420] Signed-off-by: Mark Paluch <[email protected]>
1 parent da66ce1 commit d4bc33b

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

src/main/java/io/r2dbc/postgresql/codec/PostgresqlObjectId.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public enum PostgresqlObjectId implements Type, PostgresTypeIdentifier {
390390
*/
391391
XML_ARRAY(143);
392392

393-
public static final int OID_CACHE_SIZE = 4096;
393+
public static final int OID_CACHE_SIZE = 3810; // JSON_ARRAY is currently the highest one
394394

395395
private static final PostgresqlObjectId[] CACHE = new PostgresqlObjectId[OID_CACHE_SIZE];
396396

@@ -435,12 +435,7 @@ public static boolean isValid(int objectId) {
435435
return oid != null;
436436
}
437437

438-
try {
439-
valueOf(objectId);
440-
return true;
441-
} catch (Exception e) {
442-
return false;
443-
}
438+
return false;
444439
}
445440

446441
/**
@@ -452,22 +447,17 @@ public static boolean isValid(int objectId) {
452447
*/
453448
public static PostgresqlObjectId valueOf(int objectId) {
454449

455-
if (objectId >= 0 && objectId < OID_CACHE_SIZE) {
456-
PostgresqlObjectId oid = CACHE[objectId];
457-
458-
if (oid == null) {
459-
throw new IllegalArgumentException(String.format("%d is not a valid object id", objectId));
460-
}
450+
PostgresqlObjectId oid = null;
461451

462-
return oid;
452+
if (objectId >= 0 && objectId < OID_CACHE_SIZE) {
453+
oid = CACHE[objectId];
463454
}
464455

465-
for (PostgresqlObjectId type : values()) {
466-
if (type.objectId == objectId) {
467-
return type;
468-
}
456+
if (oid == null) {
457+
throw new IllegalArgumentException(String.format("%d is not a valid object id", objectId));
469458
}
470-
throw new IllegalArgumentException(String.format("%d is not a valid object id", objectId));
459+
460+
return oid;
471461
}
472462

473463
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.postgresql.codec;
18+
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.EnumSource;
21+
import org.junit.jupiter.params.provider.ValueSource;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25+
26+
/**
27+
* Unit tests for {@link PostgresqlObjectId}.
28+
*/
29+
class PostgresqlObjectIdUnitTests {
30+
31+
@ParameterizedTest
32+
@EnumSource(PostgresqlObjectId.class)
33+
void shouldReportValidOidForWellKnownOids(PostgresqlObjectId oid) {
34+
assertThat(PostgresqlObjectId.isValid(oid.getObjectId())).isTrue();
35+
assertThat(PostgresqlObjectId.valueOf(oid.getObjectId())).isEqualTo(oid);
36+
}
37+
38+
@ParameterizedTest
39+
@ValueSource(ints = {-1, 1, 4096})
40+
void shouldReportInvalidOid(int oid) {
41+
assertThat(PostgresqlObjectId.isValid(oid)).isFalse();
42+
assertThatIllegalArgumentException().isThrownBy(() -> PostgresqlObjectId.valueOf(oid));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)