-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce fast-path handling for decoding primitive values.
[resovles #662]
- Loading branch information
Showing
15 changed files
with
294 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/io/r2dbc/postgresql/codec/PrimitiveCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.r2dbc.postgresql.codec; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.r2dbc.postgresql.client.EncodedParameter; | ||
import io.r2dbc.postgresql.message.Format; | ||
import io.r2dbc.postgresql.util.Assert; | ||
|
||
/** | ||
* @since 1.0.6 | ||
*/ | ||
final class PrimitiveCodec<T> implements Codec<Object> { | ||
|
||
private final Class<?> primitiveType; | ||
|
||
private final Class<T> wrapperType; | ||
|
||
private final Codec<T> boxedCodec; | ||
|
||
public PrimitiveCodec(Class<?> primitiveType, Class<T> wrapperType, Codec<T> boxedCodec) { | ||
|
||
this.primitiveType = Assert.requireNonNull(primitiveType, "primitiveType must not be null"); | ||
this.wrapperType = Assert.requireNonNull(wrapperType, "wrapperType must not be null"); | ||
this.boxedCodec = Assert.requireNonNull(boxedCodec, "boxedCodec must not be null"); | ||
|
||
Assert.isTrue(primitiveType.isPrimitive(), "primitiveType must be a primitive type"); | ||
} | ||
|
||
@Override | ||
public boolean canDecode(int dataType, Format format, Class<?> type) { | ||
return this.primitiveType.equals(type) && this.boxedCodec.canDecode(dataType, format, this.wrapperType); | ||
} | ||
|
||
@Override | ||
public boolean canEncode(Object value) { | ||
return this.boxedCodec.canEncode(value); | ||
} | ||
|
||
@Override | ||
public boolean canEncodeNull(Class<?> type) { | ||
return this.primitiveType.equals(type) && this.boxedCodec.canEncodeNull(this.wrapperType); | ||
} | ||
|
||
@Override | ||
public Object decode(ByteBuf buffer, int dataType, Format format, Class<?> type) { | ||
|
||
T value = this.boxedCodec.decode(buffer, dataType, format, this.wrapperType); | ||
|
||
if (value == null) { | ||
throw new NullPointerException("value for primitive type " + this.primitiveType.getName() + " is null"); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
public EncodedParameter encode(Object value) { | ||
return this.boxedCodec.encode(value); | ||
} | ||
|
||
@Override | ||
public EncodedParameter encode(Object value, int dataType) { | ||
return this.boxedCodec.encode(value, dataType); | ||
} | ||
|
||
@Override | ||
public EncodedParameter encodeNull() { | ||
return this.boxedCodec.encodeNull(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/io/r2dbc/postgresql/codec/PrimitiveWrapperCodecProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.r2dbc.postgresql.codec; | ||
|
||
/** | ||
* Codec for a primitive wrapper such as {@link DoubleCodec} for {@link Double}. {@link #getPrimitiveCodec()} is expected to return a {@code double} codec. | ||
* | ||
* @since 1.0.6 | ||
*/ | ||
interface PrimitiveWrapperCodecProvider<T> { | ||
|
||
/** | ||
* Return the codec for its primitive type. | ||
* | ||
* @return the codec for its primitive type | ||
*/ | ||
PrimitiveCodec<T> getPrimitiveCodec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.