-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): meta string encoding algorithm in java (#1514)
<!-- **Thanks for contributing to Fury.** **If this is your first time opening a PR on fury, you can refer to [CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).** Contribution Checklist - The **Apache Fury (incubating)** community has restrictions on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md). - Fury has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here. --> ## What does this PR do? This PR implements meta string encoding described in [fury java serialization spec](https://fury.apache.org/docs/specification/fury_java_serialization_spec#meta-string) and [xlang serialization spec](https://fury.apache.org/docs/specification/fury_xlang_serialization_spec#meta-string) We have `3/8` space saveing for most string: ```java // utf8 use 30 bytes, we use only 19 bytes assertEquals(encoder.encode("org.apache.fury.benchmark.data").getBytes().length, 19); // utf8 use 12 bytes, we use only 9 bytes. assertEquals(encoder.encode("MediaContent").getBytes().length, 9); ``` The integration with ClassResolver is left in another PR. ## Related issues #1240 #1413 ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/incubator-fury/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. -->
- Loading branch information
1 parent
4fabf69
commit 225f2b4
Showing
5 changed files
with
834 additions
and
0 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
java/fury-core/src/main/java/org/apache/fury/meta/MetaString.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,152 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package org.apache.fury.meta; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a string with metadata that describes its encoding. It supports different encodings | ||
* including special mechanisms for lower-case alphabets with special characters, and upper-case and | ||
* digit encoding. | ||
*/ | ||
public class MetaString { | ||
/** Defines the types of supported encodings for MetaStrings. */ | ||
public enum Encoding { | ||
LOWER_SPECIAL(0x00), | ||
LOWER_UPPER_DIGIT_SPECIAL(0x01), | ||
FIRST_TO_LOWER_SPECIAL(0x02), | ||
ALL_TO_LOWER_SPECIAL(0x03), | ||
UTF_8(0x04); // Using UTF-8 as the fallback | ||
|
||
private final int value; | ||
|
||
Encoding(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public static Encoding fromInt(int value) { | ||
for (Encoding encoding : values()) { | ||
if (encoding.getValue() == value) { | ||
return encoding; | ||
} | ||
} | ||
throw new IllegalArgumentException("Encoding flag not recognized: " + value); | ||
} | ||
} | ||
|
||
private final String string; | ||
private final Encoding encoding; | ||
private final char specialChar1; | ||
private final char specialChar2; | ||
private final byte[] bytes; | ||
private final int numBits; | ||
|
||
/** | ||
* Constructs a MetaString with the specified encoding and data. | ||
* | ||
* @param encoding The type of encoding used for the string data. | ||
* @param bytes The encoded string data as a byte array. | ||
* @param numBits The number of bits used for encoding. | ||
*/ | ||
public MetaString( | ||
String string, | ||
Encoding encoding, | ||
char specialChar1, | ||
char specialChar2, | ||
byte[] bytes, | ||
int numBits) { | ||
this.string = string; | ||
this.encoding = encoding; | ||
this.specialChar1 = specialChar1; | ||
this.specialChar2 = specialChar2; | ||
this.bytes = bytes; | ||
this.numBits = numBits; | ||
} | ||
|
||
public String getString() { | ||
return string; | ||
} | ||
|
||
public Encoding getEncoding() { | ||
return encoding; | ||
} | ||
|
||
public char getSpecialChar1() { | ||
return specialChar1; | ||
} | ||
|
||
public char getSpecialChar2() { | ||
return specialChar2; | ||
} | ||
|
||
public byte[] getBytes() { | ||
return bytes; | ||
} | ||
|
||
public int getNumBits() { | ||
return numBits; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
MetaString that = (MetaString) o; | ||
return specialChar1 == that.specialChar1 | ||
&& specialChar2 == that.specialChar2 | ||
&& numBits == that.numBits | ||
&& encoding == that.encoding | ||
&& Arrays.equals(bytes, that.bytes); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hash(encoding, specialChar1, specialChar2, numBits); | ||
result = 31 * result + Arrays.hashCode(bytes); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MetaString{" | ||
+ "str=" | ||
+ string | ||
+ ", encoding=" | ||
+ encoding | ||
+ ", specialChar1=" | ||
+ specialChar1 | ||
+ ", specialChar2=" | ||
+ specialChar2 | ||
+ ", bytes=" | ||
+ Arrays.toString(bytes) | ||
+ ", numBits=" | ||
+ numBits | ||
+ '}'; | ||
} | ||
} |
182 changes: 182 additions & 0 deletions
182
java/fury-core/src/main/java/org/apache/fury/meta/MetaStringDecoder.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,182 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package org.apache.fury.meta; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import org.apache.fury.meta.MetaString.Encoding; | ||
import org.apache.fury.util.StringUtils; | ||
|
||
/** Decodes MetaString objects back into their original plain text form. */ | ||
public class MetaStringDecoder { | ||
private final char specialChar1; | ||
private final char specialChar2; | ||
|
||
/** | ||
* Creates a MetaStringDecoder with specified special characters used for decoding. | ||
* | ||
* @param specialChar1 The first special character used in custom decoding. | ||
* @param specialChar2 The second special character used in custom decoding. | ||
*/ | ||
public MetaStringDecoder(char specialChar1, char specialChar2) { | ||
this.specialChar1 = specialChar1; | ||
this.specialChar2 = specialChar2; | ||
} | ||
|
||
/** | ||
* Decodes the encoded data back into a string based on the provided number of bits and encoding | ||
* type. | ||
* | ||
* @param encodedData The encoded string data as a byte array. | ||
* @param numBits The number of bits used for encoding. | ||
* @return The decoded original string. | ||
*/ | ||
public String decode(byte[] encodedData, int numBits) { | ||
if (encodedData.length == 0) { | ||
return ""; | ||
} | ||
// The very first byte signifies the encoding used | ||
Encoding chosenEncoding = Encoding.fromInt(encodedData[0] & 0xFF); | ||
// Extract actual data, skipping the first byte (encoding flag) | ||
encodedData = Arrays.copyOfRange(encodedData, 1, encodedData.length); | ||
return decode(encodedData, chosenEncoding, numBits); | ||
} | ||
|
||
/** | ||
* Decode data based on passed <code>encoding</code>. The data must be encoded using passed | ||
* encoding. | ||
* | ||
* @param encodedData encoded data using passed <code>encoding</code>. | ||
* @param encoding encoding the passed data. | ||
* @param numBits total bits for encoded data. | ||
* @return Decoded string. | ||
*/ | ||
public String decode(byte[] encodedData, Encoding encoding, int numBits) { | ||
switch (encoding) { | ||
case LOWER_SPECIAL: | ||
return decodeLowerSpecial(encodedData, numBits); | ||
case LOWER_UPPER_DIGIT_SPECIAL: | ||
return decodeLowerUpperDigitSpecial(encodedData, numBits); | ||
case FIRST_TO_LOWER_SPECIAL: | ||
return decodeRepFirstLowerSpecial(encodedData, numBits); | ||
case ALL_TO_LOWER_SPECIAL: | ||
return decodeRepAllToLowerSpecial(encodedData, numBits); | ||
case UTF_8: | ||
return new String(encodedData, StandardCharsets.UTF_8); | ||
default: | ||
throw new IllegalStateException("Unexpected encoding flag: " + encoding); | ||
} | ||
} | ||
|
||
/** Decoding method for {@link Encoding#LOWER_SPECIAL}. */ | ||
private String decodeLowerSpecial(byte[] data, int numBits) { | ||
StringBuilder decoded = new StringBuilder(); | ||
int bitIndex = 0; | ||
int bitMask = 0b11111; // 5 bits for mask | ||
while (bitIndex + 5 <= numBits) { | ||
int byteIndex = bitIndex / 8; | ||
int intraByteIndex = bitIndex % 8; | ||
// Extract the 5-bit character value across byte boundaries if needed | ||
int charValue = | ||
((data[byteIndex] & 0xFF) << 8) | ||
| (byteIndex + 1 < data.length ? (data[byteIndex + 1] & 0xFF) : 0); | ||
charValue = ((byte) ((charValue >> (11 - intraByteIndex)) & bitMask)); | ||
bitIndex += 5; | ||
decoded.append(decodeLowerSpecialChar(charValue)); | ||
} | ||
|
||
return decoded.toString(); | ||
} | ||
|
||
/** Decoding method for {@link Encoding#LOWER_UPPER_DIGIT_SPECIAL}. */ | ||
private String decodeLowerUpperDigitSpecial(byte[] data, int numBits) { | ||
StringBuilder decoded = new StringBuilder(); | ||
int bitIndex = 0; | ||
int bitMask = 0b111111; // 6 bits for mask | ||
while (bitIndex + 6 <= numBits) { | ||
int byteIndex = bitIndex / 8; | ||
int intraByteIndex = bitIndex % 8; | ||
|
||
// Extract the 6-bit character value across byte boundaries if needed | ||
int charValue = | ||
((data[byteIndex] & 0xFF) << 8) | ||
| (byteIndex + 1 < data.length ? (data[byteIndex + 1] & 0xFF) : 0); | ||
charValue = ((byte) ((charValue >> (10 - intraByteIndex)) & bitMask)); | ||
bitIndex += 6; | ||
decoded.append(decodeLowerUpperDigitSpecialChar(charValue)); | ||
} | ||
return decoded.toString(); | ||
} | ||
|
||
/** Decoding special char for LOWER_SPECIAL based on encoding mapping. */ | ||
private char decodeLowerSpecialChar(int charValue) { | ||
if (charValue >= 0 && charValue <= 25) { | ||
return (char) ('a' + charValue); | ||
} else if (charValue == 26) { | ||
return '.'; | ||
} else if (charValue == 27) { | ||
return '_'; | ||
} else if (charValue == 28) { | ||
return '$'; | ||
} else if (charValue == 29) { | ||
return '|'; | ||
} else { | ||
throw new IllegalArgumentException("Invalid character value for LOWER_SPECIAL: " + charValue); | ||
} | ||
} | ||
|
||
/** Decoding special char for LOWER_UPPER_DIGIT_SPECIAL based on encoding mapping. */ | ||
private char decodeLowerUpperDigitSpecialChar(int charValue) { | ||
if (charValue >= 0 && charValue <= 25) { | ||
return (char) ('a' + charValue); | ||
} else if (charValue >= 26 && charValue <= 51) { | ||
return (char) ('A' + (charValue - 26)); | ||
} else if (charValue >= 52 && charValue <= 61) { | ||
return (char) ('0' + (charValue - 52)); | ||
} else if (charValue == 62) { | ||
return specialChar1; | ||
} else if (charValue == 63) { | ||
return specialChar2; | ||
} else { | ||
throw new IllegalArgumentException( | ||
"Invalid character value for LOWER_UPPER_DIGIT_SPECIAL: " + charValue); | ||
} | ||
} | ||
|
||
private String decodeRepFirstLowerSpecial(byte[] data, int numBits) { | ||
String str = decodeLowerSpecial(data, numBits); | ||
return StringUtils.capitalize(str); | ||
} | ||
|
||
private String decodeRepAllToLowerSpecial(byte[] data, int numBits) { | ||
String str = decodeLowerSpecial(data, numBits); | ||
StringBuilder builder = new StringBuilder(); | ||
char[] chars = str.toCharArray(); | ||
for (int i = 0; i < chars.length; i++) { | ||
if (chars[i] == '|') { | ||
char c = chars[++i]; | ||
builder.append(Character.toUpperCase(c)); | ||
} else { | ||
builder.append(chars[i]); | ||
} | ||
} | ||
return builder.toString(); | ||
} | ||
} |
Oops, something went wrong.