From fe0a9cf14fb65f18bb36194a07d16a79af736d14 Mon Sep 17 00:00:00 2001 From: RikeVoltz Date: Fri, 15 May 2020 17:18:45 +0300 Subject: [PATCH] Implement write/read UTFArray methods for PortableWriter/PortableReader --- .../serialization/ClassDefinitionBuilder.h | 2 ++ .../client/serialization/PortableReader.h | 8 +++++ .../client/serialization/PortableWriter.h | 8 +++++ .../pimpl/ClassDefinitionWriter.h | 2 ++ .../pimpl/DefaultPortableWriter.h | 2 ++ .../serialization/pimpl/PortableReaderBase.h | 2 ++ .../src/hazelcast/client/serialization.cpp | 31 +++++++++++++++++++ hazelcast/test/src/HazelcastTests2.cpp | 30 +++++++++++++++--- hazelcast/test/src/serialization/Employee.h | 1 + .../src/serialization/TestInnerPortable.h | 3 +- 10 files changed, 84 insertions(+), 5 deletions(-) diff --git a/hazelcast/include/hazelcast/client/serialization/ClassDefinitionBuilder.h b/hazelcast/include/hazelcast/client/serialization/ClassDefinitionBuilder.h index d7026a27ef..27385f12ca 100644 --- a/hazelcast/include/hazelcast/client/serialization/ClassDefinitionBuilder.h +++ b/hazelcast/include/hazelcast/client/serialization/ClassDefinitionBuilder.h @@ -76,6 +76,8 @@ namespace hazelcast { ClassDefinitionBuilder& addDoubleArrayField(const std::string& fieldName); + ClassDefinitionBuilder& addUTFArrayField(const std::string& fieldName); + ClassDefinitionBuilder& addFloatArrayField(const std::string& fieldName); ClassDefinitionBuilder& addShortArrayField(const std::string& fieldName); diff --git a/hazelcast/include/hazelcast/client/serialization/PortableReader.h b/hazelcast/include/hazelcast/client/serialization/PortableReader.h index 2885513290..3a54629e20 100644 --- a/hazelcast/include/hazelcast/client/serialization/PortableReader.h +++ b/hazelcast/include/hazelcast/client/serialization/PortableReader.h @@ -167,6 +167,14 @@ namespace hazelcast { */ std::unique_ptr > readDoubleArray(const char *fieldName); + + /** + * @param fieldName name of the field + * @return the utf string array value read + * @throws IOException + */ + std::unique_ptr > readUTFArray(const char *fieldName); + /** * @param fieldName name of the field * @return the float array value read diff --git a/hazelcast/include/hazelcast/client/serialization/PortableWriter.h b/hazelcast/include/hazelcast/client/serialization/PortableWriter.h index eb18fd33fd..bf33f9dc66 100644 --- a/hazelcast/include/hazelcast/client/serialization/PortableWriter.h +++ b/hazelcast/include/hazelcast/client/serialization/PortableWriter.h @@ -167,6 +167,14 @@ namespace hazelcast { void writeDoubleArray(const char *fieldName, const std::vector *values); /** + * @param fieldName name of the field + * @param values utf string array to be written + * @throws IOException + */ + void writeUTFArray(const char *fieldName, const std::vector *values); + + + /** * Internal api , should not be called by end user. */ void end(); diff --git a/hazelcast/include/hazelcast/client/serialization/pimpl/ClassDefinitionWriter.h b/hazelcast/include/hazelcast/client/serialization/pimpl/ClassDefinitionWriter.h index 34cba08f5f..adf8f0ed3e 100644 --- a/hazelcast/include/hazelcast/client/serialization/pimpl/ClassDefinitionWriter.h +++ b/hazelcast/include/hazelcast/client/serialization/pimpl/ClassDefinitionWriter.h @@ -88,6 +88,8 @@ namespace hazelcast { void writeDoubleArray(const char *fieldName, const std::vector *data); + void writeUTFArray(const char *fieldName, const std::vector *data); + template void writeNullPortable(const char *fieldName) { diff --git a/hazelcast/include/hazelcast/client/serialization/pimpl/DefaultPortableWriter.h b/hazelcast/include/hazelcast/client/serialization/pimpl/DefaultPortableWriter.h index 2c10c942f4..f250af9b87 100644 --- a/hazelcast/include/hazelcast/client/serialization/pimpl/DefaultPortableWriter.h +++ b/hazelcast/include/hazelcast/client/serialization/pimpl/DefaultPortableWriter.h @@ -92,6 +92,8 @@ namespace hazelcast { void writeDoubleArray(const char *fieldName, const std::vector *data); + void writeUTFArray(const char *fieldName, const std::vector *data); + void end(); template diff --git a/hazelcast/include/hazelcast/client/serialization/pimpl/PortableReaderBase.h b/hazelcast/include/hazelcast/client/serialization/pimpl/PortableReaderBase.h index 9f3165b44c..b7c99c12b1 100644 --- a/hazelcast/include/hazelcast/client/serialization/pimpl/PortableReaderBase.h +++ b/hazelcast/include/hazelcast/client/serialization/pimpl/PortableReaderBase.h @@ -76,6 +76,8 @@ namespace hazelcast { virtual std::unique_ptr > readDoubleArray(const char *fieldName); + virtual std::unique_ptr > readUTFArray(const char *fieldName); + virtual std::unique_ptr > readFloatArray(const char *fieldName); virtual std::unique_ptr > readShortArray(const char *fieldName); diff --git a/hazelcast/src/hazelcast/client/serialization.cpp b/hazelcast/src/hazelcast/client/serialization.cpp index aa10b07e2c..b73ce078f2 100644 --- a/hazelcast/src/hazelcast/client/serialization.cpp +++ b/hazelcast/src/hazelcast/client/serialization.cpp @@ -225,6 +225,12 @@ namespace hazelcast { return classDefinitionWriter->writeDoubleArray(fieldName, data); } + void PortableWriter::writeUTFArray(const char *fieldName, const std::vector *data) { + if (isDefaultWriter) + return defaultPortableWriter->writeUTFArray(fieldName, data); + return classDefinitionWriter->writeUTFArray(fieldName, data); + } + void PortableWriter::end() { if (isDefaultWriter) return defaultPortableWriter->end(); @@ -409,6 +415,11 @@ namespace hazelcast { return *this; } + ClassDefinitionBuilder &ClassDefinitionBuilder::addUTFArrayField(const std::string &fieldName) { + addField(fieldName, FieldTypes::TYPE_UTF_ARRAY); + return *this; + } + ClassDefinitionBuilder &ClassDefinitionBuilder::addFloatArrayField(const std::string &fieldName) { addField(fieldName, FieldTypes::TYPE_FLOAT_ARRAY); return *this; @@ -1012,6 +1023,12 @@ namespace hazelcast { return morphingPortableReader->readDoubleArray(fieldName); } + std::unique_ptr > PortableReader::readUTFArray(const char *fieldName) { + if (isDefaultReader) + return defaultPortableReader->readUTFArray(fieldName); + return morphingPortableReader->readUTFArray(fieldName); + } + std::unique_ptr > PortableReader::readFloatArray(const char *fieldName) { if (isDefaultReader) return defaultPortableReader->readFloatArray(fieldName); @@ -1213,6 +1230,10 @@ namespace hazelcast { builder.addDoubleArrayField(fieldName); } + void ClassDefinitionWriter::writeUTFArray(const char *fieldName, const std::vector *values) { + builder.addUTFArrayField(fieldName); + } + void ClassDefinitionWriter::writeFloatArray(const char *fieldName, const std::vector *values) { builder.addFloatArrayField(fieldName); } @@ -1755,6 +1776,11 @@ namespace hazelcast { dataOutput.writeFloatArray(data); } + void DefaultPortableWriter::writeUTFArray(const char *fieldName, const std::vector *data) { + setPosition(fieldName, FieldTypes::TYPE_UTF_ARRAY); + dataOutput.writeUTFArray(data); + } + void DefaultPortableWriter::writeDoubleArray(const char *fieldName, const std::vector *data) { setPosition(fieldName, FieldTypes::TYPE_DOUBLE_ARRAY); dataOutput.writeDoubleArray(data); @@ -2457,6 +2483,11 @@ namespace hazelcast { return dataInput.readDoubleArray(); } + std::unique_ptr > PortableReaderBase::readUTFArray(const char *fieldName) { + setPosition(fieldName, FieldTypes::TYPE_UTF_ARRAY); + return dataInput.readUTFArray(); + } + std::unique_ptr > PortableReaderBase::readFloatArray(const char *fieldName) { setPosition(fieldName, FieldTypes::TYPE_FLOAT_ARRAY); return dataInput.readFloatArray(); diff --git a/hazelcast/test/src/HazelcastTests2.cpp b/hazelcast/test/src/HazelcastTests2.cpp index c858e495d0..a19aeb8c7d 100644 --- a/hazelcast/test/src/HazelcastTests2.cpp +++ b/hazelcast/test/src/HazelcastTests2.cpp @@ -1411,6 +1411,10 @@ namespace hazelcast { ff = std::vector(floatArray, floatArray + 3); double doubleArray[] = {456.456, 789.789, 321.321}; dd = std::vector(doubleArray, doubleArray + 3); + std::string stringArray[] = {"イロハニホヘト", "チリヌルヲ", "ワカヨタレソ"}; + for (int j = 0; j < 3; ++j) { + stringVector.push_back(stringArray[j]); + } } bool Employee::operator==(const Employee &rhs) const { @@ -1451,6 +1455,7 @@ namespace hazelcast { writer.writeIntArray("ii", &ii); writer.writeFloatArray("ff", &ff); writer.writeDoubleArray("dd", &dd); + writer.writeUTFArray("stringVector", &stringVector); serialization::ObjectDataOutput &out = writer.getRawDataOutput(); out.writeObject(&by); @@ -1486,6 +1491,7 @@ namespace hazelcast { ii = *reader.readIntArray("ii");; ff = *reader.readFloatArray("ff");; dd = *reader.readDoubleArray("dd");; + stringVector = *reader.readUTFArray("stringVector"); serialization::ObjectDataInput &in = reader.getRawDataInput(); by = *in.readObject(); @@ -1953,8 +1959,10 @@ namespace hazelcast { std::vector l, std::vector f, std::vector d, + std::vector stringVector, std::vector n) : ii(i), bb(b), ba(ba), cc(c), ss(s), - ll(l), ff(f), dd(d), nn(n) { + ll(l), ff(f), dd(d), + stringVector(std::move(stringVector)), nn(n) { } TestInnerPortable::~TestInnerPortable() { @@ -1969,6 +1977,7 @@ namespace hazelcast { ll = rhs.ll; ff = rhs.ff; dd = rhs.dd; + stringVector = rhs.stringVector; nn = rhs.nn; return (*this); } @@ -1990,6 +1999,7 @@ namespace hazelcast { if (ll != m.ll) return false; if (ff != m.ff) return false; if (dd != m.dd) return false; + if (stringVector != m.stringVector) return false; size_t size = nn.size(); for (size_t i = 0; i < size; i++) if (nn[i] != m.nn[i]) @@ -2010,6 +2020,7 @@ namespace hazelcast { writer.writeLongArray("l", &ll); writer.writeFloatArray("f", &ff); writer.writeDoubleArray("d", &dd); + writer.writeUTFArray("stringVector", &stringVector); writer.writePortableArray("nn", &nn); } @@ -2022,6 +2033,7 @@ namespace hazelcast { ll = *reader.readLongArray("l"); ff = *reader.readFloatArray("f"); dd = *reader.readDoubleArray("d"); + stringVector = *reader.readUTFArray("stringVector"); nn = reader.readPortableArray("nn"); } @@ -2458,6 +2470,10 @@ namespace hazelcast { std::vector ff(floatArray, floatArray + 3); double doubleArray[] = {456.456, 789.789, 321.321}; std::vector dd(doubleArray, doubleArray + 3); + std::string stringArray[] = {"イロハニホヘト", "チリヌルヲ", "ワカヨタレソ"}; + std::vector stringVector; + for (int i = 0; i < 3; i++) + stringVector.push_back(stringArray[i]); TestNamedPortable portableArray[5]; for (int i = 0; i < 5; i++) { portableArray[i].name = "named-portable-" + hazelcast::util::IOUtil::to_string(i); @@ -2465,7 +2481,7 @@ namespace hazelcast { } std::vector nn(portableArray, portableArray + 5); - TestInnerPortable inner(bb, ba, cc, ss, ii, ll, ff, dd, nn); + TestInnerPortable inner(bb, ba, cc, ss, ii, ll, ff, dd, stringVector, nn); data = serializationService.toData(&inner); @@ -2517,6 +2533,7 @@ namespace hazelcast { double *doubleArray; doubleArray = new double[LARGE_ARRAY_SIZE]; std::vector dd(doubleArray, doubleArray + LARGE_ARRAY_SIZE); + std::vector stringVector(LARGE_ARRAY_SIZE); TestNamedPortable portableArray[5]; @@ -2526,7 +2543,7 @@ namespace hazelcast { } std::vector nn(portableArray, portableArray + 5); - TestInnerPortable inner(bb, ba, cc, ss, ii, ll, ff, dd, nn); + TestInnerPortable inner(bb, ba, cc, ss, ii, ll, ff, dd, stringVector, nn); data = serializationService.toData(&inner); @@ -2589,6 +2606,11 @@ namespace hazelcast { std::vector ff(floatArray, floatArray + 3); double doubleArray[] = {456.456, 789.789, 321.321}; std::vector dd(doubleArray, doubleArray + 3); + std::string stringArray[] = {"イロハニホヘト", "チリヌルヲ", "ワカヨタレソ"}; + std::vector stringVector; + for (int i = 0; i < 3; ++i) { + stringVector.push_back(stringArray[i]); + } TestNamedPortable portableArray[5]; for (int i = 0; i < 5; i++) { portableArray[i].name = "named-portable-" + hazelcast::util::IOUtil::to_string(i); @@ -2596,7 +2618,7 @@ namespace hazelcast { } std::vector nn(portableArray, portableArray + 5); - TestInnerPortable inner(bb, ba, cc, ss, ii, ll, ff, dd, nn); + TestInnerPortable inner(bb, ba, cc, ss, ii, ll, ff, dd, stringVector, nn); data = serializationService.toData(&inner); diff --git a/hazelcast/test/src/serialization/Employee.h b/hazelcast/test/src/serialization/Employee.h index 799e33c2fc..8f371ad52a 100644 --- a/hazelcast/test/src/serialization/Employee.h +++ b/hazelcast/test/src/serialization/Employee.h @@ -79,6 +79,7 @@ namespace hazelcast { std::vector ll; std::vector ff; std::vector dd; + std::vector stringVector; }; // Compares based on the employee age diff --git a/hazelcast/test/src/serialization/TestInnerPortable.h b/hazelcast/test/src/serialization/TestInnerPortable.h index 89a18fd0c6..9071641f3a 100644 --- a/hazelcast/test/src/serialization/TestInnerPortable.h +++ b/hazelcast/test/src/serialization/TestInnerPortable.h @@ -39,7 +39,7 @@ namespace hazelcast { TestInnerPortable(std::vector b, std::vector ba, std::vector c, std::vector s, std::vector i, std::vector l, std::vector f, std::vector d, - std::vector n); + std::vector stringVector, std::vector n); TestInnerPortable& operator = (const TestInnerPortable& rhs); @@ -66,6 +66,7 @@ namespace hazelcast { std::vector ll; std::vector ff; std::vector dd; + std::vector stringVector; std::vector< TestNamedPortable > nn; };