Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added management of data transfer on JVM-CLR boundary based on ByteBuffer #452

Merged
merged 7 commits into from
May 15, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 MASES s.r.l.
*
* 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
*
* http://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.
*
* Refer to LICENSE for more information.
*/

package org.mases.knet.common.serialization;

import org.apache.kafka.common.serialization.Deserializer;
import org.mases.jcobridge.JCSharedBuffer;

import java.io.IOException;
import java.nio.ByteBuffer;

public class ByteBufferDeserializer implements Deserializer<ByteBuffer> {

@Override
public ByteBuffer deserialize(String topic, byte[] data) {
if (data == null)
return null;

try {
return JCSharedBuffer.Create(data);
} catch (IOException e) {
// fallback to non buffered version
return ByteBuffer.wrap(data);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 MASES s.r.l.
*
* 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
*
* http://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.
*
* Refer to LICENSE for more information.
*/

package org.mases.knet.common.serialization;

import org.apache.kafka.common.serialization.Serializer;
import org.mases.jcobridge.JCSharedBuffer;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
* {@code ByteBufferSerializer} always {@link ByteBuffer#rewind() rewinds} the position of the input buffer to zero for
* serialization. A manual rewind is not necessary.
* <p>
* Note: any existing buffer position is ignored.
* <p>
* The position is also rewound back to zero before {@link #serialize(String, ByteBuffer)}
* returns.
*/
public class ByteBufferSerializer implements Serializer<ByteBuffer> {
public byte[] serialize(String topic, ByteBuffer data) {
if (data == null)
return null;

try {
try (JCSharedBuffer sb = JCSharedBuffer.Create(data)) {
sb.rewind();

if (sb.hasArray()) {
byte[] arr = sb.array();
if (sb.arrayOffset() == 0 && arr.length == sb.remaining()) {
return arr;
}
}

byte[] ret = new byte[data.remaining()];
sb.get(ret, 0, ret.length);
sb.rewind();
return ret;
}
} catch (IOException iae) {
// fallback to non buffered version
data.rewind();

if (data.hasArray()) {
byte[] arr = data.array();
if (data.arrayOffset() == 0 && arr.length == data.remaining()) {
return arr;
}
}

byte[] ret = new byte[data.remaining()];
data.get(ret, 0, ret.length);
data.rewind();
return ret;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 MASES s.r.l.
*
* 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
*
* http://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.
*
* Refer to LICENSE for more information.
*/

package org.mases.knet.common.serialization;

import org.apache.kafka.common.serialization.Serde;

import java.nio.ByteBuffer;

public class Serdes {
static public final class ByteBufferSerde extends org.apache.kafka.common.serialization.Serdes.WrapperSerde<ByteBuffer> {
public ByteBufferSerde() {
super(new ByteBufferSerializer(), new ByteBufferDeserializer());
}
}

/**
* A serde for nullable {@code ByteBuffer} type.
*
* @return {@link Serde} of {@link ByteBuffer}
*/
static public Serde<ByteBuffer> ByteBuffer() {
return new ByteBufferSerde();
}
}
2 changes: 1 addition & 1 deletion src/net/Common/Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(KNET_DOCKER_BUILD_ACTIONS)' != 'true'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TargetFrameworks>net462;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net462;net6.0;net8.0</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(KNET_DOCKER_BUILD_ACTIONS)' == 'true'">
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
Expand Down
Loading