-
Notifications
You must be signed in to change notification settings - Fork 23
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
Issue 40: Serializers Implementation for Schema Registry #41
Changes from 68 commits
76d99a3
b634867
a0876cc
05cc270
8ee1fef
193776d
600ff74
be5115e
409c106
88af2e7
b1a8894
1961ba1
58e7f65
026b725
7027d88
4be3f40
cc223ec
a0a2db7
8a8c872
3f64c11
fba77f6
a449b4b
22a0a0d
9a50464
8cc3091
d063e7c
5a7d639
a9cca3a
0b6230a
911f793
7ff74af
c9f9d3f
5d91574
7c30b4f
98a9c6b
dcff47c
28660e4
53b8988
8e2c704
ce1aa98
151b808
bffd0f7
26e34f1
049fb6d
910f870
d690a21
a44c0fb
a82ee56
7b34571
48da360
72919bb
48e54e9
7c9ba8e
214ab54
2241865
456e6bd
9741bd5
6f7f4d5
87c9ebc
f8fd8ad
c98ee7b
c3789ca
32933ba
d8b0675
322d3c0
a586f4f
cdc0c0c
8693357
223df77
8c69b3f
572f34f
cf82e07
c4a732b
5daaeb4
eedee88
b63a8a4
23e6470
4925759
1dafddb
53b8f9c
a744198
c6a02c8
bf0dfb4
d70ba7e
4117557
0f770fa
9823974
7ee6fe0
ff47289
6c3fd87
2d6711d
ade09af
9cbaaae
c0e57be
174c4e6
fe0974f
d464b6b
9137aa6
ed740ea
a8fb0dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
package io.pravega.schemaregistry.common; | ||
|
||
public class NameUtil { | ||
/** | ||
* Extracts the name from the fully qualified type name. Name represents the last token after ".". | ||
* If the qualified name does not contain "." then the name is same as qualified name. | ||
shiveshr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param qualifiedName qualified name to extract name from. | ||
* @return extracted name. | ||
*/ | ||
public static String extractName(String qualifiedName) { | ||
int nameStart = qualifiedName.lastIndexOf("."); | ||
return qualifiedName.substring(nameStart + 1); | ||
} | ||
|
||
/** | ||
* Extracts name and the prefix qualifier before the name. Name represents the last token after ".". | ||
* Qualifier is the prefix before the name. | ||
* If the qualified name does not contain "." then the name is same as qualified name and qualifier is empty string. | ||
* | ||
* @param qualifiedName qualified name to extract tokens from. | ||
* @return an array containing name at index 0 and qualifier at index 1. | ||
*/ | ||
public static String[] extractNameAndQualifier(String qualifiedName) { | ||
int nameStart = qualifiedName.lastIndexOf("."); | ||
String name = qualifiedName.substring(nameStart + 1); | ||
String pckg = nameStart < 0 ? "" : qualifiedName.substring(0, nameStart); | ||
return new String[]{name, pckg}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
package io.pravega.schemaregistry; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.pravega.shared.NameUtils; | ||
import lombok.SneakyThrows; | ||
|
||
/** | ||
* Defines strategies for generating groupId for stream. | ||
* Currently there is only one naming strategy that uses streams fully qualified scoped stream name. | ||
*/ | ||
public class GroupIdGenerator { | ||
private GroupIdGenerator() { | ||
} | ||
|
||
@SneakyThrows | ||
shiveshr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static String getGroupId(Type type, String... args) { | ||
switch (type) { | ||
case QualifiedStreamName: | ||
Preconditions.checkNotNull(args); | ||
Preconditions.checkArgument(args.length == 2); | ||
shiveshr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return NameUtils.getScopedStreamName(args[0], args[1]); | ||
default: | ||
throw new IllegalArgumentException(); | ||
shiveshr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
public enum Type { | ||
QualifiedStreamName, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
package io.pravega.schemaregistry.codec; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Codec interface that defines methods to encode and decoder data for a given codec type. | ||
*/ | ||
public interface Codec { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this extend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, its not a serializer but a transformer of bytes. Extending the Serializer interface with type byteBuffer will be an unnecessary semantic overload. |
||
String getCodecType(); | ||
|
||
ByteBuffer encode(ByteBuffer data); | ||
|
||
ByteBuffer decode(ByteBuffer data); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
package io.pravega.schemaregistry.codec; | ||
|
||
import lombok.SneakyThrows; | ||
import org.xerial.snappy.Snappy; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.zip.GZIPInputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
/** | ||
* Factory class for creating codecs for codec types . | ||
*/ | ||
public class CodecFactory { | ||
shiveshr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static final String NONE = ""; | ||
public static final String MIME_GZIP = "application/x-gzip"; | ||
public static final String MIME_SNAPPY = "application/x-snappy-framed"; | ||
|
||
private static final Noop NOOP = new Noop(); | ||
shiveshr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final GZipCodec GZIP_COMPRESSOR = new GZipCodec(); | ||
private static final SnappyCodec SNAPPY_COMPRESSOR = new SnappyCodec(); | ||
|
||
public static Codec none() { | ||
return NOOP; | ||
} | ||
|
||
public static Codec gzip() { | ||
return GZIP_COMPRESSOR; | ||
} | ||
|
||
public static Codec snappy() { | ||
return SNAPPY_COMPRESSOR; | ||
} | ||
|
||
private static class Noop implements Codec { | ||
@Override | ||
public String getCodecType() { | ||
return NONE; | ||
} | ||
|
||
@Override | ||
public ByteBuffer encode(ByteBuffer data) { | ||
return data; | ||
} | ||
|
||
@Override | ||
public ByteBuffer decode(ByteBuffer data) { | ||
return data; | ||
} | ||
} | ||
|
||
private static class GZipCodec implements Codec { | ||
@Override | ||
public String getCodecType() { | ||
return MIME_GZIP; | ||
} | ||
|
||
@SneakyThrows(IOException.class) | ||
@Override | ||
public ByteBuffer encode(ByteBuffer data) { | ||
byte[] array = new byte[data.remaining()]; | ||
data.get(array); | ||
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream(array.length); | ||
GZIPOutputStream gzipOS = new GZIPOutputStream(bos); | ||
gzipOS.write(array); | ||
gzipOS.close(); | ||
byte[] compressed = bos.toByteArray(); | ||
return ByteBuffer.wrap(compressed); | ||
} | ||
|
||
@SneakyThrows(IOException.class) | ||
@Override | ||
public ByteBuffer decode(ByteBuffer data) { | ||
byte[] array = new byte[data.remaining()]; | ||
data.get(array); | ||
|
||
ByteArrayInputStream bis = new ByteArrayInputStream(array); | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
GZIPInputStream gzipIS = new GZIPInputStream(bis); | ||
byte[] buffer = new byte[1024]; | ||
int len; | ||
while ((len = gzipIS.read(buffer)) != -1) { | ||
bos.write(buffer, 0, len); | ||
} | ||
byte[] uncompressed = bos.toByteArray(); | ||
return ByteBuffer.wrap(uncompressed); | ||
} | ||
} | ||
|
||
private static class SnappyCodec implements Codec { | ||
@Override | ||
public String getCodecType() { | ||
return MIME_SNAPPY; | ||
} | ||
|
||
@SneakyThrows(IOException.class) | ||
@Override | ||
public ByteBuffer encode(ByteBuffer data) { | ||
byte[] array = new byte[data.remaining()]; | ||
data.get(array); | ||
|
||
byte[] compressed = Snappy.compress(array); | ||
return ByteBuffer.wrap(compressed); | ||
} | ||
|
||
@SneakyThrows(IOException.class) | ||
@Override | ||
public ByteBuffer decode(ByteBuffer data) { | ||
byte[] array = new byte[data.remaining()]; | ||
data.get(array); | ||
|
||
byte[] uncompressed = Snappy.uncompress(array); | ||
return ByteBuffer.wrap(uncompressed); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should probably be separated into individual targets so that users don't need to depend on more than what they actually need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created issue #45 to tackle this later as it would require considerable refactoring at this time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it require a change to
SerializerFactory
? I'm concerned that this could be an API change, and even though we are making this beta, this is an anticipated change, it is not something that we learned later. If we can do it now, I'd say it is better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way i am thinking is as follows:
have format specific serializers implemented in format specific module.
So one for protobuf, one for json, one for avro. Each such module will have a factory with only serializers/deserializers for that format.
Then we will have a consolidated one, which is this library and factory class. This is because here we have serializers which are multi format.
So this way users can take dependency on one common module (all type 1 apps that want to work for multiple formats can depend on this one module).
So the user contract, which is the SerializerFactory, doesnt change.
Only the serializer implementations would move internal to their respective modules and this base module would take dependence on those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If applications are supposed to use the specific modules, why do we need the base module? If I understand correctly, the base module is a level of indirection to the specific modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it serves as two purposes:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with this direction, @tkaitchuck needs to resolve this thread if he is satisfied.