Serialize a char array member as part of class serialization into cache - C++ API

classic Classic list List threaded Threaded
3 messages Options
danami danami
Reply | Threaded
Open this post in threaded view
|

Serialize a char array member as part of class serialization into cache - C++ API

Hi,

I couldn't find an answer anywhere else, hopefully you can help me.

I have the following class:

class Pair {
friend struct ignite::binary::BinaryType<Pair>;
public:
Pair() {
_len = 0;
_buff = nullptr;
}

Pair(char* buff, int len) {
_len = len;
_buff = new char[len];
for (int i = 0; i < len; i++) {
_buff[i] = buff[i];
}
}

~ Pair() {
delete[] _buff;
}

private:
char* _buff;
int _len;
};

I try to serialize the class into cache in the following manner:

template<>
struct ignite::binary::BinaryType<Pair>
{
static int32_t GetTypeId()
{
return GetBinaryStringHashCode("Pair");
}

static void GetTypeName(std::string& name)
{
name = "Pair";
}

static int32_t GetFieldId(const char* name)
{
return GetBinaryStringHashCode(name);
}

static bool IsNull(const Pair& obj)
{
return false;
}

static void GetNull(Pair& dst)
{
dst = Pair();
}

static void Write(BinaryWriter& writer, const Pair& obj)
{
BinaryRawWriter rawWriter = writer.RawWriter();

int len = obj._len;
char* buff = obj._buff;

rawWriter.WriteInt32(len);

auto binWriter = rawWriter.WriteArray<char>();
for (int i = 0; i < len; i++) {
binWriter.Write(buff[i]);
}
binWriter.Close();
}

static void Read(BinaryReader& reader, Pair& dst)
{
BinaryRawReader rawReader = reader.RawReader();

dst._len = rawReader.ReadInt32();

dst._buff = new char[dst._len];
auto binReader = rawReader.ReadArray<char>();
for (int i = 0; i < dst._len; i++) {
dst._buff[i] = binReader.GetNext();
}
}
};

When I try to compile I get errors as following:
image.png
If I comment out the parts of reading and writing the array it compiles successfully.

Does this happen because I also need to serialize the write of 'char'? (If so, how do I do it?)
Am I using the ReadArray and WriteArray correctly?
Is there another way of storing the char buffer in cache?

If someone can provide a working code snippet that would be amazing, but any help would be appreciated.
Thanks a lot!

dmagda dmagda
Reply | Threaded
Open this post in threaded view
|

Re: Serialize a char array member as part of class serialization into cache - C++ API

Igor, 

Would you please join the discussion and help Data tackle the issue?

-
Denis


On Thu, Jul 30, 2020 at 1:03 AM Dana Milan <[hidden email]> wrote:
Hi,

I couldn't find an answer anywhere else, hopefully you can help me.

I have the following class:

class Pair {
friend struct ignite::binary::BinaryType<Pair>;
public:
Pair() {
_len = 0;
_buff = nullptr;
}

Pair(char* buff, int len) {
_len = len;
_buff = new char[len];
for (int i = 0; i < len; i++) {
_buff[i] = buff[i];
}
}

~ Pair() {
delete[] _buff;
}

private:
char* _buff;
int _len;
};

I try to serialize the class into cache in the following manner:

template<>
struct ignite::binary::BinaryType<Pair>
{
static int32_t GetTypeId()
{
return GetBinaryStringHashCode("Pair");
}

static void GetTypeName(std::string& name)
{
name = "Pair";
}

static int32_t GetFieldId(const char* name)
{
return GetBinaryStringHashCode(name);
}

static bool IsNull(const Pair& obj)
{
return false;
}

static void GetNull(Pair& dst)
{
dst = Pair();
}

static void Write(BinaryWriter& writer, const Pair& obj)
{
BinaryRawWriter rawWriter = writer.RawWriter();

int len = obj._len;
char* buff = obj._buff;

rawWriter.WriteInt32(len);

auto binWriter = rawWriter.WriteArray<char>();
for (int i = 0; i < len; i++) {
binWriter.Write(buff[i]);
}
binWriter.Close();
}

static void Read(BinaryReader& reader, Pair& dst)
{
BinaryRawReader rawReader = reader.RawReader();

dst._len = rawReader.ReadInt32();

dst._buff = new char[dst._len];
auto binReader = rawReader.ReadArray<char>();
for (int i = 0; i < dst._len; i++) {
dst._buff[i] = binReader.GetNext();
}
}
};

When I try to compile I get errors as following:
image.png
If I comment out the parts of reading and writing the array it compiles successfully.

Does this happen because I also need to serialize the write of 'char'? (If so, how do I do it?)
Am I using the ReadArray and WriteArray correctly?
Is there another way of storing the char buffer in cache?

If someone can provide a working code snippet that would be amazing, but any help would be appreciated.
Thanks a lot!

Igor Sapego-1 Igor Sapego-1
Reply | Threaded
Open this post in threaded view
|

Re: Serialize a char array member as part of class serialization into cache - C++ API

Hi Dana,

"char" type is not supported by Ignite as it may have different sign on
different platforms. You can use int8_t if you want to have a byte array,
or WriteString/ReadString if you want it to be a string.

Also, I've re-factored your BinaryType code a little bit so it's more readable:

template<>
struct ignite::binary::BinaryType<Pair> : BinaryTypeDefaultAll<Pair>
{
static void GetTypeName(std::string& name)
{
name = "Pair";
}

static void Write(BinaryWriter& writer, const Pair& obj)
{
BinaryRawWriter rawWriter = writer.RawWriter();

int len = obj._len;
char* buff = obj._buff;

rawWriter.WriteInt32(len);

auto binWriter = rawWriter.WriteArray<int8_t>();
for (int i = 0; i < len; i++) {
binWriter.Write(buff[i]);
}
binWriter.Close();
}

static void Read(BinaryReader& reader, Pair& dst)
{
BinaryRawReader rawReader = reader.RawReader();

dst._len = rawReader.ReadInt32();

dst._buff = new char[dst._len];
auto binReader = rawReader.ReadArray<int8_t>();
for (int i = 0; i < dst._len; i++) {
dst._buff[i] = binReader.GetNext();
}
}
};

Best Regards,
Igor


On Tue, Aug 4, 2020 at 1:22 AM Denis Magda <[hidden email]> wrote:
Igor, 

Would you please join the discussion and help Data tackle the issue?

-
Denis


On Thu, Jul 30, 2020 at 1:03 AM Dana Milan <[hidden email]> wrote:
Hi,

I couldn't find an answer anywhere else, hopefully you can help me.

I have the following class:

class Pair {
friend struct ignite::binary::BinaryType<Pair>;
public:
Pair() {
_len = 0;
_buff = nullptr;
}

Pair(char* buff, int len) {
_len = len;
_buff = new char[len];
for (int i = 0; i < len; i++) {
_buff[i] = buff[i];
}
}

~ Pair() {
delete[] _buff;
}

private:
char* _buff;
int _len;
};

I try to serialize the class into cache in the following manner:

template<>
struct ignite::binary::BinaryType<Pair>
{
static int32_t GetTypeId()
{
return GetBinaryStringHashCode("Pair");
}

static void GetTypeName(std::string& name)
{
name = "Pair";
}

static int32_t GetFieldId(const char* name)
{
return GetBinaryStringHashCode(name);
}

static bool IsNull(const Pair& obj)
{
return false;
}

static void GetNull(Pair& dst)
{
dst = Pair();
}

static void Write(BinaryWriter& writer, const Pair& obj)
{
BinaryRawWriter rawWriter = writer.RawWriter();

int len = obj._len;
char* buff = obj._buff;

rawWriter.WriteInt32(len);

auto binWriter = rawWriter.WriteArray<char>();
for (int i = 0; i < len; i++) {
binWriter.Write(buff[i]);
}
binWriter.Close();
}

static void Read(BinaryReader& reader, Pair& dst)
{
BinaryRawReader rawReader = reader.RawReader();

dst._len = rawReader.ReadInt32();

dst._buff = new char[dst._len];
auto binReader = rawReader.ReadArray<char>();
for (int i = 0; i < dst._len; i++) {
dst._buff[i] = binReader.GetNext();
}
}
};

When I try to compile I get errors as following:
image.png
If I comment out the parts of reading and writing the array it compiles successfully.

Does this happen because I also need to serialize the write of 'char'? (If so, how do I do it?)
Am I using the ReadArray and WriteArray correctly?
Is there another way of storing the char buffer in cache?

If someone can provide a working code snippet that would be amazing, but any help would be appreciated.
Thanks a lot!