Re: Cross plat form put/get

2018-08-27 Thread wengyao04
Thank you very much, I will remove GetHashCode and hashCode in c++ and java
client code and tried it again



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/


Re: Cross plat form put/get

2018-08-27 Thread Igor Sapego
GetHashCode() method is not used for hashing in Ignite C++ any
more, as well as hashCode() method in Java. You can still find it
in legacy code, but since Ignite 2.1 this method is not used and
you can safely remove it.

By default, hashing algorithm in Ignite is now operates on byte-array
which is obtained when object is serialized in the same way for all
platforms. So now you should not care much about it, as long as your
serialization/deserialization algorithms are implemented the same way
on all platforms.

Best Regards,
Igor


On Sat, Aug 25, 2018 at 1:18 AM wengyao04  wrote:

> Hi, we have c++ ignite client and java client to read/write to the same
> cache.
> Our key is a composite object, my cache is  IgniteCache
>
> ---c++ ---
> namespace ignite
> {
> namespace binary
> {
> struct Key
> {
> std::string id;
> bool flag;
> Key()
> : id() , flag(false)
> {
> }
>
> Key(const std::string& id, bool flag = false)
> : id(id)
> , flag(flag)
> {
> }
>
> Key(const  Key& rhs)
> : id(rhs.id)
> , flag(rhs.flag)
> {
> }
>
> bool operator<(const Key& rhs) const
> {
> return (id == rhs.id) ?
>   flag < rhs.flag : id < rhs.id;
> }
>
> bool operator==(const Key& rhs) const
> {
> return id == rhs.id && flag == rhs.flag;
> }
>
> bool operator!=(const Key& rhs) const
> {
> return !(*this == rhs);
> }
>
> ~Key()
> {
> }
>
> // METHODS
> bool isEmpty() const { return id.empty(); }
> };
> template<>
> struct BinaryType
> {
> static int32_t GetTypeId()
> {
> return GetBinaryStringHashCode("Key");
> }
>
> static void GetTypeName(native_std::string& dst)
> {
> dst = "Key";
> }
>
> static int32_t GetFieldId(const char* name)
> {
> return GetBinaryStringHashCode(name);
> }
>
> static bool IsNull(const Key& key)
> {
> return und.isEmpty();
> }
>
> static void GetNull(Key& key)
> {
> key = Key();
> }
>
> static void Write(BinaryWriter& writer, Key& key)
> {
> writer.WriteString("id", key.id);
> writer.WriteBool("flag", key.flag);
> }
>
> static void Read(BinaryReader& reader, Key& key)
> {
> key.id = reader.ReadString("id");
> key.flag = reader.ReadBool("flag");
> }
>
> int32_t GetHashCode(const Key& key) const
> {
> const int32_t prime = 31;
> int32_t boolHashCode = key.is_delayed ? 1231 : 1237;
> int32_t strHashCode = 0;
> for (int i = 0; i < (int)key.bbgid.size(); ++i)
> {
> strHashCode = prime * strHashCode + key.bbgid[i];
> }
> return strHashCode + prime * boolHashCode;
> }
> };
> }
> }
>
> Our java key is
> ---
> mport org.apache.ignite.binary.BinaryObjectException;
> import org.apache.ignite.binary.BinaryReader;
> import org.apache.ignite.binary.BinaryWriter;
> import org.apache.ignite.binary.Binarylizable;
>
> public class Key implements Binarylizable {
>
> public String id;
> public Boolean flag;
> private final static int prime = 31;
>
> public Key() {
> //no-op
> }
>
> public Key(String id) {
> this.id = id;
> this.flag = false;
> }
>
> public Key(String id, boolean flag) {
> this.id = id;
> this.flag = flag;
> }
>
> @Override
> public int hashCode() {
>
> int boolHashCode = flag ? 1231 : 1237;
> int strHashCode = 0;
>
> for (int i = 0; i < id.length(); i++) {
> strHashCode = prime * strHashCode + id.charAt(i);
> }
>
> return strHashCode + prime * boolHashCode;
> }
>
> @Override
> public boolean equals(Object o) {
> if (o == this) {
> return true;
> }
>
> if (!(o instanceof Key)) {
> return false;
> }
>
> Key cast = (Key) o;
> return this.id.equals(cast.id) && this.flag == cast.flag;
> }
>
> public void readBinary(BinaryReader reader) throws
> BinaryObjectException
> {
> id = reader.readString("id");
> flag = reader.readBoolean("flag");
> }
>
> public void writeBinary(BinaryWriter writer) throws
> BinaryObjectException {
> writer.writeString("id", id);
> writer.writeBoolean("flag", flag);
> }
>
> @Override public String toString() {
> return "Key = [" + id + " " + flag +  "]\n";
> }
> }
>
> I see people put the  GetHashCode in c++ BinaryType, some one use static,
> and others just put it as a const method. This GetHashCode implements hash
> code as java class Key. Where this GetHashCode is used ? Say if I write a
> record from c++ client, is this key the cache hash key ? Do I also need to
> implement hash in c++ class Key ? Is this GetHhashCode needed 

Cross plat form put/get

2018-08-24 Thread wengyao04
Hi, we have c++ ignite client and java client to read/write to the same
cache. 
Our key is a composite object, my cache is  IgniteCache

---c++ ---
namespace ignite
{
namespace binary
{
struct Key
{
std::string id;
bool flag;
Key()
: id() , flag(false)
{
}

Key(const std::string& id, bool flag = false)
: id(id)
, flag(flag)
{
}

Key(const  Key& rhs)
: id(rhs.id)
, flag(rhs.flag)
{
}

bool operator<(const Key& rhs) const
{
return (id == rhs.id) ?
  flag < rhs.flag : id < rhs.id;
}

bool operator==(const Key& rhs) const
{
return id == rhs.id && flag == rhs.flag;
}

bool operator!=(const Key& rhs) const
{
return !(*this == rhs);
}

~Key()
{
}

// METHODS
bool isEmpty() const { return id.empty(); }
};
template<>
struct BinaryType
{
static int32_t GetTypeId()
{
return GetBinaryStringHashCode("Key");
}

static void GetTypeName(native_std::string& dst)
{
dst = "Key";
}

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

static bool IsNull(const Key& key)
{
return und.isEmpty();
}

static void GetNull(Key& key)
{
key = Key();
}

static void Write(BinaryWriter& writer, Key& key)
{
writer.WriteString("id", key.id);
writer.WriteBool("flag", key.flag);
}

static void Read(BinaryReader& reader, Key& key)
{
key.id = reader.ReadString("id");
key.flag = reader.ReadBool("flag");
}

int32_t GetHashCode(const Key& key) const
{
const int32_t prime = 31;
int32_t boolHashCode = key.is_delayed ? 1231 : 1237;
int32_t strHashCode = 0;
for (int i = 0; i < (int)key.bbgid.size(); ++i)
{
strHashCode = prime * strHashCode + key.bbgid[i];
}
return strHashCode + prime * boolHashCode;
}
};
}
}

Our java key is
---
mport org.apache.ignite.binary.BinaryObjectException;
import org.apache.ignite.binary.BinaryReader;
import org.apache.ignite.binary.BinaryWriter;
import org.apache.ignite.binary.Binarylizable;

public class Key implements Binarylizable {

public String id;
public Boolean flag;
private final static int prime = 31;

public Key() {
//no-op
}

public Key(String id) {
this.id = id;
this.flag = false;
}

public Key(String id, boolean flag) {
this.id = id;
this.flag = flag;
}

@Override
public int hashCode() {

int boolHashCode = flag ? 1231 : 1237;
int strHashCode = 0;

for (int i = 0; i < id.length(); i++) {
strHashCode = prime * strHashCode + id.charAt(i);
}

return strHashCode + prime * boolHashCode;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}

if (!(o instanceof Key)) {
return false;
}

Key cast = (Key) o;
return this.id.equals(cast.id) && this.flag == cast.flag;
}

public void readBinary(BinaryReader reader) throws BinaryObjectException
{
id = reader.readString("id");
flag = reader.readBoolean("flag");
}

public void writeBinary(BinaryWriter writer) throws
BinaryObjectException {
writer.writeString("id", id);
writer.writeBoolean("flag", flag);
}

@Override public String toString() {
return "Key = [" + id + " " + flag +  "]\n";
}
}

I see people put the  GetHashCode in c++ BinaryType, some one use static,
and others just put it as a const method. This GetHashCode implements hash
code as java class Key. Where this GetHashCode is used ? Say if I write a
record from c++ client, is this key the cache hash key ? Do I also need to
implement hash in c++ class Key ? Is this GetHhashCode needed to be static ?

How does ignite ensure cross plat form read/write when key is not primitive
object (a composite object) ?
Thanks




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/