Skip to content

Commit caa2966

Browse files
committed
Merge remote-tracking branch 'origin/365.Add-support-of-custom-codecs-in-topics' into 365.Add-support-of-custom-codecs-in-topics
# Conflicts: # topic/src/main/java/tech/ydb/topic/utils/Encoder.java # topic/src/main/java/tech/ydb/topic/write/impl/AsyncWriterImpl.java # topic/src/main/java/tech/ydb/topic/write/impl/SyncWriterImpl.java # topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java # topic/src/test/java/tech/ydb/topic/impl/YdbTopicsCustomCodecIntegrationTest.java # topic/src/test/java/tech/ydb/topic/impl/YdbTopicsCustomCodecTest.java
2 parents 40bbcc1 + fad26f2 commit caa2966

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tech.ydb.topic.description;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
/**
7+
* Register for custom topic codec. Local to TopicClient
8+
*
9+
* @author Evgeny Kuvardin
10+
**/
11+
public class CodecRegistryImpl implements CodecRegistry {
12+
13+
/**
14+
* Make customCodecMap concurrent since register/unregister/read can be from different threads
15+
*/
16+
final Map<Integer, CustomTopicCodec> customCodecMap;
17+
18+
public CodecRegistryImpl() {
19+
customCodecMap = new ConcurrentHashMap<>();
20+
}
21+
22+
@Override
23+
public CustomTopicCodec registerCustomCodec(int codec, CustomTopicCodec customTopicCodec) {
24+
assert customTopicCodec != null;
25+
26+
if (Codec.getInstance().isReserved(codec)) {
27+
throw new RuntimeException(
28+
"Create custom codec for reserved code not allowed: " + codec + " .Use code more than 10000");
29+
}
30+
31+
return customCodecMap.put(codec, customTopicCodec);
32+
}
33+
34+
@Override
35+
public CustomTopicCodec unregisterCustomCodec(int codec) {
36+
if (Codec.getInstance().isReserved(codec)) {
37+
throw new RuntimeException(
38+
"Create custom codec for reserved code not allowed: " + codec + " .Use code more than 10000");
39+
}
40+
41+
return customCodecMap.remove(codec);
42+
}
43+
44+
@Override
45+
public CustomTopicCodec getCustomCodec(int codec) {
46+
return customCodecMap.get(codec);
47+
}
48+
}

topic/src/main/java/tech/ydb/topic/impl/TopicClientImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tech.ydb.topic.TopicClient;
2525
import tech.ydb.topic.TopicRpc;
2626
import tech.ydb.topic.description.CodecRegistry;
27+
import tech.ydb.topic.description.CodecRegistryImpl;
2728
import tech.ydb.topic.description.Consumer;
2829
import tech.ydb.topic.description.ConsumerDescription;
2930
import tech.ydb.topic.description.CustomTopicCodec;

0 commit comments

Comments
 (0)