You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/01/28 14:43:17 UTC
[GitHub] [ignite-3] ibessonov opened a new pull request #605: IGNITE-14698 Improved configuration values serialization.
ibessonov opened a new pull request #605:
URL: https://github.com/apache/ignite-3/pull/605
https://issues.apache.org/jira/browse/IGNITE-14698
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795592455
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Short.BYTES * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Integer.BYTES * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Long.BYTES * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Character.BYTES * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Float.BYTES * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Double.BYTES * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + Integer.BYTES * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalArgumentException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts a byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
+ ByteBuffer buf = ByteBuffer.wrap(bytes);
+
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+
+ byte header = buf.get();
+
+ switch (header) {
+ case BOOLEAN:
+ return buf.get() == 1;
+
+ case BYTE:
+ return buf.get();
+
+ case SHORT:
+ return buf.getShort();
+
+ case INT:
+ return buf.getInt();
+
+ case LONG:
+ return buf.getLong();
+
+ case CHAR:
+ return buf.getChar();
+
+ case FLOAT:
+ return buf.getFloat();
+
+ case DOUBLE:
+ return buf.getDouble();
+
+ case STRING:
+ return new String(bytes, 1, bytes.length - 1, StandardCharsets.UTF_8);
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = new boolean[bytes.length - 1];
+
+ for (int i = 0; i < booleans.length; i++) {
+ booleans[i] = buf.get() == 1;
+ }
+
+ return booleans;
+ }
+
+ case BYTE | ARRAY:
+ return Arrays.copyOfRange(bytes, 1, bytes.length);
+
+ case SHORT | ARRAY: {
+ short[] shorts = new short[bytes.length / Short.BYTES];
+
+ for (int i = 0; i < shorts.length; i++) {
+ shorts[i] = buf.getShort();
+ }
+
+ return shorts;
+ }
+
+ case INT | ARRAY: {
+ int[] ints = new int[bytes.length / Integer.BYTES];
+
+ for (int i = 0; i < ints.length; i++) {
+ ints[i] = buf.getInt();
+ }
+
+ return ints;
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = new long[bytes.length / Long.BYTES];
+
+ for (int i = 0; i < longs.length; i++) {
+ longs[i] = buf.getLong();
+ }
+
+ return longs;
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = new char[bytes.length / Character.BYTES];
+
+ for (int i = 0; i < chars.length; i++) {
+ chars[i] = buf.getChar();
+ }
+
+ return chars;
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = new float[bytes.length / Float.BYTES];
+
+ for (int i = 0; i < floats.length; i++) {
+ floats[i] = buf.getFloat();
+ }
+
+ return floats;
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = new double[bytes.length / Double.BYTES];
+
+ for (int i = 0; i < doubles.length; i++) {
+ doubles[i] = buf.getDouble();
+ }
+
+ return doubles;
+ }
+
+ case STRING | ARRAY: {
+ List<String> res = new ArrayList<>();
+
+ int offset = 1;
+
+ while (offset != bytes.length) {
+ int size = buf.getInt(offset);
+
+ res.add(new String(bytes, offset + Integer.BYTES, size));
Review comment:
How could I forget about it, thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] SammyVimes commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
SammyVimes commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795544665
##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtilTest.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.fromBytes;
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.toBytes;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.Serializable;
+import java.lang.reflect.Array;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationSerializationUtilTest {
+ @Test
+ void testSingleValuesConsistency() {
+ assertSingleValue(Boolean.FALSE);
+ assertSingleValue(Boolean.TRUE);
+
+ assertSingleValue((byte) 123);
+
+ assertSingleValue((short) 0x1234);
+
+ assertSingleValue(0x12345678);
+
+ assertSingleValue(0x123456789ABCDEF0L);
+
+ assertSingleValue('F');
+
+ assertSingleValue(0.3f);
+
+ assertSingleValue(0.3d);
+
+ assertSingleValue("foo");
+ }
+
+ @Test
+ void testArraysConsistency() {
+ assertArray(new boolean[]{false, true});
+
+ assertArray(new byte[]{10, -10});
+
+ assertArray(new short[]{1000, -1000});
+
+ assertArray(new int[]{1000_000, -1000_000});
+
+ assertArray(new long[]{1000_000_000_000L, -1000_000_000_000L});
+
+ assertArray(new char[]{'X', 'Y'});
+
+ assertArray(new float[]{0.1f, -0.1f});
+
+ assertArray(new double[]{0.1d, -0.1d});
+
+ assertArray(new String[]{"foo", "bar"});
+ }
+
+ private void assertSingleValue(Object value) {
+ assertEquals(value, fromBytes(toBytes(value)));
+ }
+
+ private void assertArray(Object value) {
+ Serializable res = fromBytes(toBytes(value));
+
+ assertEquals(value.getClass(), res.getClass());
+
+ for (int i = 0; i < Array.getLength(value); i++) {
+ assertEquals(Array.get(value, i), Array.get(res, i));
+ }
+ }
+}
Review comment:
Missing newline
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r794580606
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(5).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(9).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(3).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(5).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(9).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalStateException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
Review comment:
could be Object, IDK
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795463184
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
Review comment:
Changed to Object
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] sashapolo commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
sashapolo commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r794576561
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
Review comment:
I think you should write some tests for this functionality
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795591709
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
Review comment:
I wonder how many boolean arrays we will store. This is definitely a good idea. Should I implement it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795467472
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(5).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(9).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(3).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(5).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(9).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalStateException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
+ ByteBuffer buf = ByteBuffer.wrap(bytes);
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+
+ byte header = buf.get();
+
+ switch (header) {
+ case BOOLEAN:
+ return buf.get() == 1;
+
+ case BYTE:
+ return buf.get();
+
+ case SHORT:
+ return buf.getShort();
+
+ case INT:
+ return buf.getInt();
+
+ case LONG:
+ return buf.getLong();
+
+ case CHAR:
+ return buf.getChar();
+
+ case FLOAT:
+ return buf.getFloat();
+
+ case DOUBLE:
+ return buf.getDouble();
+
+ case STRING:
+ return new String(bytes, 1, bytes.length - 1, StandardCharsets.UTF_8);
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = new boolean[bytes.length - 1];
+
+ for (int i = 0; i < booleans.length; i++) {
+ booleans[i] = buf.get() == 1;
+ }
+
+ return booleans;
+ }
+
+ case BYTE | ARRAY:
+ return Arrays.copyOfRange(bytes, 1, bytes.length);
+
+ case SHORT | ARRAY: {
+ short[] shorts = new short[bytes.length / 2];
+
+ for (int i = 0; i < shorts.length; i++) {
+ shorts[i] = buf.getShort();
+ }
+
+ return shorts;
+ }
+
+ case INT | ARRAY: {
+ int[] ints = new int[bytes.length / 4];
+
+ for (int i = 0; i < ints.length; i++) {
+ ints[i] = buf.getInt();
+ }
+
+ return ints;
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = new long[bytes.length / 8];
+
+ for (int i = 0; i < longs.length; i++) {
+ longs[i] = buf.getLong();
+ }
+
+ return longs;
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = new char[bytes.length / 2];
+
+ for (int i = 0; i < chars.length; i++) {
+ chars[i] = buf.getChar();
+ }
+
+ return chars;
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = new float[bytes.length / 4];
+
+ for (int i = 0; i < floats.length; i++) {
+ floats[i] = buf.getFloat();
+ }
+
+ return floats;
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = new double[bytes.length / 8];
+
+ for (int i = 0; i < doubles.length; i++) {
+ doubles[i] = buf.getDouble();
+ }
+
+ return doubles;
+ }
+
+ case STRING | ARRAY: {
+ List<String> res = new ArrayList<>();
+
+ int offset = 1;
+
+ while (offset != bytes.length) {
+ int size = buf.getInt(offset);
+
+ res.add(new String(bytes, offset + 4, size));
+
+ offset += 4 + size;
+ }
+
+ return res.toArray(EMPTY_STRING_ARRAY);
+ }
+
+ default:
+ throw new IllegalArgumentException(Arrays.toString(bytes));
+ }
+ }
+
+ private static ByteBuffer allocateBuffer(int size) {
+ ByteBuffer buf = ByteBuffer.allocate(size);
Review comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795464426
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
Review comment:
I will add some tests
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] sashapolo commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
sashapolo commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r794570356
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(5).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(9).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(3).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(5).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(9).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalStateException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
+ ByteBuffer buf = ByteBuffer.wrap(bytes);
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+
+ byte header = buf.get();
+
+ switch (header) {
+ case BOOLEAN:
+ return buf.get() == 1;
+
+ case BYTE:
+ return buf.get();
+
+ case SHORT:
+ return buf.getShort();
+
+ case INT:
+ return buf.getInt();
+
+ case LONG:
+ return buf.getLong();
+
+ case CHAR:
+ return buf.getChar();
+
+ case FLOAT:
+ return buf.getFloat();
+
+ case DOUBLE:
+ return buf.getDouble();
+
+ case STRING:
+ return new String(bytes, 1, bytes.length - 1, StandardCharsets.UTF_8);
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = new boolean[bytes.length - 1];
+
+ for (int i = 0; i < booleans.length; i++) {
+ booleans[i] = buf.get() == 1;
+ }
+
+ return booleans;
+ }
+
+ case BYTE | ARRAY:
+ return Arrays.copyOfRange(bytes, 1, bytes.length);
+
+ case SHORT | ARRAY: {
+ short[] shorts = new short[bytes.length / 2];
+
+ for (int i = 0; i < shorts.length; i++) {
+ shorts[i] = buf.getShort();
+ }
+
+ return shorts;
+ }
+
+ case INT | ARRAY: {
+ int[] ints = new int[bytes.length / 4];
+
+ for (int i = 0; i < ints.length; i++) {
+ ints[i] = buf.getInt();
+ }
+
+ return ints;
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = new long[bytes.length / 8];
+
+ for (int i = 0; i < longs.length; i++) {
+ longs[i] = buf.getLong();
+ }
+
+ return longs;
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = new char[bytes.length / 2];
+
+ for (int i = 0; i < chars.length; i++) {
+ chars[i] = buf.getChar();
+ }
+
+ return chars;
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = new float[bytes.length / 4];
+
+ for (int i = 0; i < floats.length; i++) {
+ floats[i] = buf.getFloat();
+ }
+
+ return floats;
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = new double[bytes.length / 8];
+
+ for (int i = 0; i < doubles.length; i++) {
+ doubles[i] = buf.getDouble();
+ }
+
+ return doubles;
+ }
+
+ case STRING | ARRAY: {
+ List<String> res = new ArrayList<>();
+
+ int offset = 1;
+
+ while (offset != bytes.length) {
+ int size = buf.getInt(offset);
+
+ res.add(new String(bytes, offset + 4, size));
+
+ offset += 4 + size;
+ }
+
+ return res.toArray(EMPTY_STRING_ARRAY);
+ }
+
+ default:
+ throw new IllegalArgumentException(Arrays.toString(bytes));
+ }
+ }
+
+ private static ByteBuffer allocateBuffer(int size) {
+ ByteBuffer buf = ByteBuffer.allocate(size);
Review comment:
These methods can be chained, code will be a little bit shorter
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(5).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(9).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(3).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(5).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(9).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalStateException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
Review comment:
Why do you still need to return `Serializable`?
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
Review comment:
```suggestion
* Converts a configuration value into a byte array.
```
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(5).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(9).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(3).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(5).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(9).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalStateException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts byte array into a configuration value.
Review comment:
```suggestion
* Converts a byte array into a configuration value.
```
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
Review comment:
```suggestion
private static final String[] EMPTY_STRING_ARRAY = new String[0];
```
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
Review comment:
I would suggest using `SHORT.BYTES + 1` and similar constants
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
Review comment:
why `Serializable`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795590783
##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtilTest.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.fromBytes;
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.toBytes;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.Serializable;
+import java.lang.reflect.Array;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationSerializationUtilTest {
+ @Test
+ void testSingleValuesConsistency() {
+ assertSingleValue(Boolean.FALSE);
+ assertSingleValue(Boolean.TRUE);
+
+ assertSingleValue((byte) 123);
+
+ assertSingleValue((short) 0x1234);
+
+ assertSingleValue(0x12345678);
+
+ assertSingleValue(0x123456789ABCDEF0L);
+
+ assertSingleValue('F');
+
+ assertSingleValue(0.3f);
+
+ assertSingleValue(0.3d);
+
+ assertSingleValue("foo");
+ }
+
+ @Test
+ void testArraysConsistency() {
+ assertArray(new boolean[]{false, true});
+
+ assertArray(new byte[]{10, -10});
+
+ assertArray(new short[]{1000, -1000});
+
+ assertArray(new int[]{1000_000, -1000_000});
+
+ assertArray(new long[]{1000_000_000_000L, -1000_000_000_000L});
+
+ assertArray(new char[]{'X', 'Y'});
+
+ assertArray(new float[]{0.1f, -0.1f});
+
+ assertArray(new double[]{0.1d, -0.1d});
+
+ assertArray(new String[]{"foo", "bar"});
+ }
+
+ private void assertSingleValue(Object value) {
+ assertEquals(value, fromBytes(toBytes(value)));
+ }
+
+ private void assertArray(Object value) {
+ Serializable res = fromBytes(toBytes(value));
+
+ assertEquals(value.getClass(), res.getClass());
+
+ for (int i = 0; i < Array.getLength(value); i++) {
+ assertEquals(Array.get(value, i), Array.get(res, i));
+ }
+ }
+}
Review comment:
Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795606242
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
Review comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] rpuch commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
rpuch commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795550096
##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtilTest.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.fromBytes;
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.toBytes;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.Serializable;
+import java.lang.reflect.Array;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationSerializationUtilTest {
+ @Test
+ void testSingleValuesConsistency() {
+ assertSingleValue(Boolean.FALSE);
+ assertSingleValue(Boolean.TRUE);
+
+ assertSingleValue((byte) 123);
+
+ assertSingleValue((short) 0x1234);
+
+ assertSingleValue(0x12345678);
+
+ assertSingleValue(0x123456789ABCDEF0L);
+
+ assertSingleValue('F');
+
+ assertSingleValue(0.3f);
+
+ assertSingleValue(0.3d);
+
+ assertSingleValue("foo");
+ }
+
+ @Test
+ void testArraysConsistency() {
+ assertArray(new boolean[]{false, true});
+
+ assertArray(new byte[]{10, -10});
+
+ assertArray(new short[]{1000, -1000});
+
+ assertArray(new int[]{1000_000, -1000_000});
+
+ assertArray(new long[]{1000_000_000_000L, -1000_000_000_000L});
+
+ assertArray(new char[]{'X', 'Y'});
+
+ assertArray(new float[]{0.1f, -0.1f});
+
+ assertArray(new double[]{0.1d, -0.1d});
+
+ assertArray(new String[]{"foo", "bar"});
+ }
+
+ private void assertSingleValue(Object value) {
+ assertEquals(value, fromBytes(toBytes(value)));
+ }
+
+ private void assertArray(Object value) {
+ Serializable res = fromBytes(toBytes(value));
+
+ assertEquals(value.getClass(), res.getClass());
+
+ for (int i = 0; i < Array.getLength(value); i++) {
Review comment:
Let's also assert that arrays have same length
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795594188
##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtilTest.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.fromBytes;
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.toBytes;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.Serializable;
+import java.lang.reflect.Array;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationSerializationUtilTest {
+ @Test
+ void testSingleValuesConsistency() {
+ assertSingleValue(Boolean.FALSE);
+ assertSingleValue(Boolean.TRUE);
+
+ assertSingleValue((byte) 123);
+
+ assertSingleValue((short) 0x1234);
+
+ assertSingleValue(0x12345678);
+
+ assertSingleValue(0x123456789ABCDEF0L);
+
+ assertSingleValue('F');
+
+ assertSingleValue(0.3f);
+
+ assertSingleValue(0.3d);
+
+ assertSingleValue("foo");
+ }
+
+ @Test
+ void testArraysConsistency() {
+ assertArray(new boolean[]{false, true});
+
+ assertArray(new byte[]{10, -10});
+
+ assertArray(new short[]{1000, -1000});
+
+ assertArray(new int[]{1000_000, -1000_000});
+
+ assertArray(new long[]{1000_000_000_000L, -1000_000_000_000L});
+
+ assertArray(new char[]{'X', 'Y'});
+
+ assertArray(new float[]{0.1f, -0.1f});
+
+ assertArray(new double[]{0.1d, -0.1d});
+
+ assertArray(new String[]{"foo", "bar"});
+ }
+
+ private void assertSingleValue(Object value) {
+ assertEquals(value, fromBytes(toBytes(value)));
+ }
+
+ private void assertArray(Object value) {
+ Serializable res = fromBytes(toBytes(value));
+
+ assertEquals(value.getClass(), res.getClass());
+
+ for (int i = 0; i < Array.getLength(value); i++) {
Review comment:
Sure
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795592211
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
Review comment:
Ok, this will make code easier, I agree
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795462407
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(5).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(9).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(3).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(5).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(9).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 2 * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + 8 * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + 4 * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalStateException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
Review comment:
Serializable is stated in every usage of the method, I'll leave it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795463553
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,409 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Serializable value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(3).put(SHORT).putShort((short) value).array();
Review comment:
Makes sense, thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] rpuch commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
rpuch commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795669889
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,414 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ byte header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{header, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{header, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(header).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(header).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(header).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(header).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(header).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(header).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(header).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ int payloadSize = (booleans.length + Byte.SIZE - 1) / Byte.SIZE;
+
+ ByteBuffer buf = allocateBuffer(2 + payloadSize);
+
+ buf.put(header);
+
+ buf.put((byte) (payloadSize * Byte.SIZE - booleans.length));
+
+ byte[] res = buf.array();
+
+ for (int i = 0; i < booleans.length; i++) {
+ if (booleans[i]) {
+ res[2 + (i >>> 3)] |= (1 << (i & 7));
+ }
+ }
+
+ return res;
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put(header).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Short.BYTES * shorts.length);
+
+ buf.put(header);
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Integer.BYTES * ints.length);
+
+ buf.put(header);
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Long.BYTES * longs.length);
+
+ buf.put(header);
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Character.BYTES * chars.length);
+
+ buf.put(header);
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Float.BYTES * floats.length);
+
+ buf.put(header);
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Double.BYTES * doubles.length);
+
+ buf.put(header);
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + Integer.BYTES * strBytes.length + totalSize);
+
+ buf.put(header);
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalArgumentException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts a byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
+ ByteBuffer buf = ByteBuffer.wrap(bytes);
+
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+
+ byte header = buf.get();
+
+ switch (header) {
+ case BOOLEAN:
+ return buf.get() == 1;
+
+ case BYTE:
+ return buf.get();
+
+ case SHORT:
+ return buf.getShort();
+
+ case INT:
+ return buf.getInt();
+
+ case LONG:
+ return buf.getLong();
+
+ case CHAR:
+ return buf.getChar();
+
+ case FLOAT:
+ return buf.getFloat();
+
+ case DOUBLE:
+ return buf.getDouble();
+
+ case STRING:
+ return new String(bytes, 1, bytes.length - 1, StandardCharsets.UTF_8);
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = new boolean[Byte.SIZE * (bytes.length - 2) - bytes[1]];
Review comment:
I suggest to extract `bytes[1]` to a variable and name it something like `paddingLength` to make it obvious for a reader.
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,414 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ byte header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{header, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{header, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(header).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(header).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(header).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(header).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(header).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(header).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(header).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ int payloadSize = (booleans.length + Byte.SIZE - 1) / Byte.SIZE;
+
+ ByteBuffer buf = allocateBuffer(2 + payloadSize);
+
+ buf.put(header);
+
+ buf.put((byte) (payloadSize * Byte.SIZE - booleans.length));
Review comment:
I suggest to extract `payloadSize * Byte.SIZE - booleans.length` and name the variable like `paddingLength` to make it clear for the reader what it is.
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
Review comment:
Cool :) How about extracting the whole switch branch for boolean arrays to its own method, as it became pretty long now?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] rpuch commented on a change in pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
rpuch commented on a change in pull request #605:
URL: https://github.com/apache/ignite-3/pull/605#discussion_r795547187
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + booleans.length);
+
+ buf.put((byte) (BOOLEAN | ARRAY));
+
+ for (boolean b : booleans) {
+ buf.put((byte) (b ? 1 : 0));
+ }
+
+ return buf.array();
+ }
+
+ case BYTE | ARRAY: {
+ byte[] bytes = (byte[]) value;
+
+ return allocateBuffer(1 + bytes.length).put((byte) (BYTE | ARRAY)).put(bytes).array();
+ }
+
+ case SHORT | ARRAY: {
+ short[] shorts = (short[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Short.BYTES * shorts.length);
+
+ buf.put((byte) (SHORT | ARRAY));
+
+ for (short s : shorts) {
+ buf.putShort(s);
+ }
+
+ return buf.array();
+ }
+
+ case INT | ARRAY: {
+ int[] ints = (int[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Integer.BYTES * ints.length);
+
+ buf.put((byte) (INT | ARRAY));
+
+ for (int n : ints) {
+ buf.putInt(n);
+ }
+
+ return buf.array();
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = (long[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Long.BYTES * longs.length);
+
+ buf.put((byte) (LONG | ARRAY));
+
+ for (long n : longs) {
+ buf.putLong(n);
+ }
+
+ return buf.array();
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = (char[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Character.BYTES * chars.length);
+
+ buf.put((byte) (CHAR | ARRAY));
+
+ for (char c : chars) {
+ buf.putChar(c);
+ }
+
+ return buf.array();
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = (float[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Float.BYTES * floats.length);
+
+ buf.put((byte) (FLOAT | ARRAY));
+
+ for (float f : floats) {
+ buf.putFloat(f);
+ }
+
+ return buf.array();
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = (double[]) value;
+
+ ByteBuffer buf = allocateBuffer(1 + Double.BYTES * doubles.length);
+
+ buf.put((byte) (DOUBLE | ARRAY));
+
+ for (double d : doubles) {
+ buf.putDouble(d);
+ }
+
+ return buf.array();
+ }
+
+ case STRING | ARRAY: {
+ String[] strings = (String[]) value;
+
+ byte[][] strBytes = Arrays.stream(strings).map(s -> s.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
+
+ int totalSize = Arrays.stream(strBytes).mapToInt(bytes -> bytes.length).sum();
+
+ ByteBuffer buf = allocateBuffer(1 + Integer.BYTES * strBytes.length + totalSize);
+
+ buf.put((byte) (STRING | ARRAY));
+
+ for (int i = 0; i < strings.length; i++) {
+ buf.putInt(strings[i].length());
+
+ buf.put(strBytes[i]);
+ }
+
+ return buf.array();
+ }
+
+ default:
+ throw new IllegalArgumentException(value.getClass().getName());
+ }
+ }
+
+ /**
+ * Converts a byte array into a configuration value.
+ *
+ * @param bytes Serialized value.
+ * @return Deserialized configuration value.
+ */
+ public static Serializable fromBytes(byte[] bytes) {
+ ByteBuffer buf = ByteBuffer.wrap(bytes);
+
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+
+ byte header = buf.get();
+
+ switch (header) {
+ case BOOLEAN:
+ return buf.get() == 1;
+
+ case BYTE:
+ return buf.get();
+
+ case SHORT:
+ return buf.getShort();
+
+ case INT:
+ return buf.getInt();
+
+ case LONG:
+ return buf.getLong();
+
+ case CHAR:
+ return buf.getChar();
+
+ case FLOAT:
+ return buf.getFloat();
+
+ case DOUBLE:
+ return buf.getDouble();
+
+ case STRING:
+ return new String(bytes, 1, bytes.length - 1, StandardCharsets.UTF_8);
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = new boolean[bytes.length - 1];
+
+ for (int i = 0; i < booleans.length; i++) {
+ booleans[i] = buf.get() == 1;
+ }
+
+ return booleans;
+ }
+
+ case BYTE | ARRAY:
+ return Arrays.copyOfRange(bytes, 1, bytes.length);
+
+ case SHORT | ARRAY: {
+ short[] shorts = new short[bytes.length / Short.BYTES];
+
+ for (int i = 0; i < shorts.length; i++) {
+ shorts[i] = buf.getShort();
+ }
+
+ return shorts;
+ }
+
+ case INT | ARRAY: {
+ int[] ints = new int[bytes.length / Integer.BYTES];
+
+ for (int i = 0; i < ints.length; i++) {
+ ints[i] = buf.getInt();
+ }
+
+ return ints;
+ }
+
+ case LONG | ARRAY: {
+ long[] longs = new long[bytes.length / Long.BYTES];
+
+ for (int i = 0; i < longs.length; i++) {
+ longs[i] = buf.getLong();
+ }
+
+ return longs;
+ }
+
+ case CHAR | ARRAY: {
+ char[] chars = new char[bytes.length / Character.BYTES];
+
+ for (int i = 0; i < chars.length; i++) {
+ chars[i] = buf.getChar();
+ }
+
+ return chars;
+ }
+
+ case FLOAT | ARRAY: {
+ float[] floats = new float[bytes.length / Float.BYTES];
+
+ for (int i = 0; i < floats.length; i++) {
+ floats[i] = buf.getFloat();
+ }
+
+ return floats;
+ }
+
+ case DOUBLE | ARRAY: {
+ double[] doubles = new double[bytes.length / Double.BYTES];
+
+ for (int i = 0; i < doubles.length; i++) {
+ doubles[i] = buf.getDouble();
+ }
+
+ return doubles;
+ }
+
+ case STRING | ARRAY: {
+ List<String> res = new ArrayList<>();
+
+ int offset = 1;
+
+ while (offset != bytes.length) {
+ int size = buf.getInt(offset);
+
+ res.add(new String(bytes, offset + Integer.BYTES, size));
Review comment:
Probably an explicit charset needs to be specified
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
Review comment:
We could put `header` in all cases instead of the concrete type (`SHORT` in this case), that would reduce variability and could make the code a bit less error-prone.
##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtilTest.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.fromBytes;
+import static org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil.toBytes;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.Serializable;
+import java.lang.reflect.Array;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationSerializationUtilTest {
+ @Test
+ void testSingleValuesConsistency() {
+ assertSingleValue(Boolean.FALSE);
+ assertSingleValue(Boolean.TRUE);
+
+ assertSingleValue((byte) 123);
+
+ assertSingleValue((short) 0x1234);
+
+ assertSingleValue(0x12345678);
+
+ assertSingleValue(0x123456789ABCDEF0L);
+
+ assertSingleValue('F');
+
+ assertSingleValue(0.3f);
+
+ assertSingleValue(0.3d);
+
+ assertSingleValue("foo");
+ }
+
+ @Test
+ void testArraysConsistency() {
+ assertArray(new boolean[]{false, true});
+
+ assertArray(new byte[]{10, -10});
+
+ assertArray(new short[]{1000, -1000});
+
+ assertArray(new int[]{1000_000, -1000_000});
+
+ assertArray(new long[]{1000_000_000_000L, -1000_000_000_000L});
+
+ assertArray(new char[]{'X', 'Y'});
+
+ assertArray(new float[]{0.1f, -0.1f});
+
+ assertArray(new double[]{0.1d, -0.1d});
+
+ assertArray(new String[]{"foo", "bar"});
+ }
+
+ private void assertSingleValue(Object value) {
+ assertEquals(value, fromBytes(toBytes(value)));
+ }
+
+ private void assertArray(Object value) {
+ Serializable res = fromBytes(toBytes(value));
+
+ assertEquals(value.getClass(), res.getClass());
+
+ for (int i = 0; i < Array.getLength(value); i++) {
Review comment:
Let's also assert that array have same length
##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/util/ConfigurationSerializationUtil.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.ignite.internal.configuration.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Utility class to help serializing deserializing configuration values - primitives, strings, or arrays of primitives or strings.
+ */
+public class ConfigurationSerializationUtil {
+ private static final byte BOOLEAN = 1;
+
+ private static final byte BYTE = 2;
+
+ private static final byte SHORT = 3;
+
+ private static final byte INT = 4;
+
+ private static final byte LONG = 5;
+
+ private static final byte CHAR = 6;
+
+ private static final byte FLOAT = 7;
+
+ private static final byte DOUBLE = 8;
+
+ private static final byte STRING = 9;
+
+ private static final byte ARRAY = (byte) 0x80;
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Converts a configuration value into a byte array.
+ *
+ * @param value Configuration values.
+ * @return Serialized value.
+ */
+ public static byte[] toBytes(Object value) {
+ Objects.requireNonNull(value);
+
+ int header = header(value.getClass());
+
+ switch (header) {
+ case BOOLEAN:
+ return new byte[]{BOOLEAN, (byte) ((boolean) value ? 1 : 0)};
+
+ case BYTE:
+ return new byte[]{BYTE, (byte) value};
+
+ case SHORT:
+ return allocateBuffer(Short.BYTES + 1).put(SHORT).putShort((short) value).array();
+
+ case INT:
+ return allocateBuffer(Integer.BYTES + 1).put(INT).putInt((int) value).array();
+
+ case LONG:
+ return allocateBuffer(Long.BYTES + 1).put(LONG).putLong((long) value).array();
+
+ case CHAR:
+ return allocateBuffer(Character.BYTES + 1).put(CHAR).putChar((char) value).array();
+
+ case FLOAT:
+ return allocateBuffer(Float.BYTES + 1).put(FLOAT).putFloat((float) value).array();
+
+ case DOUBLE:
+ return allocateBuffer(Double.BYTES + 1).put(DOUBLE).putDouble((double) value).array();
+
+ case STRING: {
+ byte[] strBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
+
+ return allocateBuffer(1 + strBytes.length).put(STRING).put(strBytes).array();
+ }
+
+ case BOOLEAN | ARRAY: {
+ boolean[] booleans = (boolean[]) value;
Review comment:
Would it make sense to pack booleans as bits, not bytes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [ignite-3] ibessonov merged pull request #605: IGNITE-14698 Improved configuration values serialization.
Posted by GitBox <gi...@apache.org>.
ibessonov merged pull request #605:
URL: https://github.com/apache/ignite-3/pull/605
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org