Automatic Persistence cache: Key exists in cache, but not value

classic Classic list List threaded Threaded
5 messages Options
Dave Ellery Dave Ellery
Reply | Threaded
Open this post in threaded view
|

Automatic Persistence cache: Key exists in cache, but not value

I have a strange issue, using Ignite 1.2

I have set up a JDBC backed cache, loading the cache works fine, I even can see that its setting the appropriate values for my test object, however when I try to get the object out for the specified key it gives me back nothing, however if I iterate over the entry list, the key in the cache is not null but the value is null.

##object in question


/**
 * UuidTest definition.
 *
 * Code generated by Apache Ignite Schema Import utility: 06/26/2015.
 */
public class UuidTest implements Serializable {
    /** */
    private static final long serialVersionUID = 0L;

    /** Value for myid. */
    private Object myid;

    /** Value for md5hash. */
    private String md5hash;

    /**
     * Empty constructor.
     */
    public UuidTest() {
        // No-op.
    }

    /**
     * Full constructor.
     */
    public UuidTest(
        Object myid,
        String md5hash
    ) {
        this.myid = myid;
        this.md5hash = md5hash;
    }

    /**
     * Gets myid.
     *
     * @return Value for myid.
     */
    public Object getMyid() {
        return myid;
    }

    /**
     * Sets myid.
     *
     * @param myid New value for myid.
     */
    public void setMyid(Object myid) {

        System.out.println("setting id: " + myid);

        this.myid = myid;
    }

    /**
     * Gets md5hash.
     *
     * @return Value for md5hash.
     */
    public String getMd5hash() {


        return md5hash;
    }

    /**
     * Sets md5hash.
     *
     * @param md5hash New value for md5hash.
     */
    public void setMd5hash(String md5hash) {
        System.out.println("setting md5: " + md5hash);
        this.md5hash = md5hash;
    }

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

        if (!(o instanceof UuidTest))
            return false;

        UuidTest that = (UuidTest)o;

        if (myid != null ? !myid.equals(that.myid) : that.myid != null)
            return false;

        if (md5hash != null ? !md5hash.equals(that.md5hash) : that.md5hash != null)
            return false;

        return true;
    }

    /** {@inheritDoc} */
    @Override public int hashCode() {
        int res = myid != null ? myid.hashCode() : 0;

        res = 31 * res + (md5hash != null ? md5hash.hashCode() : 0);

        return res;
    }

    /** {@inheritDoc} */
    @Override public String toString() {
        return "UuidTest [myid=" + myid +
            ", md5hash=" + md5hash +
            "]";
    }
}

###key class

/**
 * UuidTestKey definition.
 *
 * Code generated by Apache Ignite Schema Import utility: 06/26/2015.
 */
public class UuidTestKey implements Serializable {
    /** */
    private static final long serialVersionUID = 0L;

    /** Value for myid. */
    private Object myid;

    /**
     * Empty constructor.
     */
    public UuidTestKey() {
        // No-op.
    }

    /**
     * Full constructor.
     */
    public UuidTestKey(
        Object myid
    ) {
        this.myid = myid;
    }

    /**
     * Gets myid.
     *
     * @return Value for myid.
     */
    public Object getMyid() {
        return myid;
    }

    /**
     * Sets myid.
     *
     * @param myid New value for myid.
     */
    public void setMyid(Object myid) {
        this.myid = myid;
    }

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

        if (!(o instanceof UuidTestKey))
            return false;

        UuidTestKey that = (UuidTestKey)o;

        if (myid != null ? !myid.equals(that.myid) : that.myid != null)
            return false;

        return true;
    }

    /** {@inheritDoc} */
    @Override public int hashCode() {
        int res = myid != null ? myid.hashCode() : 0;

        return res;
    }

    /** {@inheritDoc} */
    @Override public String toString() {
        return "UuidTestKey [myid=" + myid +
            "]";
    }
}

##cache config


/**
 * CacheConfig definition.
 *
 * Code generated by Apache Ignite Schema Import utility: 06/26/2015.
 */
public class CacheConfig {
    /**
    * Configure cache.
    *
    * @param name Cache name.
    * @param storeFactory Cache store factory.
    */
    public static <K, V> CacheConfiguration<K, V> cache(String name, Factory<CacheStore<K, V>> storeFactory) {
        if (storeFactory == null)
             throw new IllegalArgumentException("Cache store factory cannot be null.");

        CacheConfiguration<K, V> ccfg = new CacheConfiguration<>(name);

        ccfg.setCacheStoreFactory(storeFactory);
        ccfg.setReadThrough(true);
        ccfg.setWriteThrough(true);
        ccfg.setBackups(0);

        // Configure cache types.
        Collection<CacheTypeMetadata> meta = new ArrayList<>();

        // uuid_test.
        CacheTypeMetadata type = new CacheTypeMetadata();

        meta.add(type);

        type.setDatabaseSchema("TEST");
        type.setDatabaseTable("uuid_test");
        type.setKeyType(UuidTestKey.class.getName());
        type.setValueType(UuidTest.class.getName());

        // Key fields for uuid_test.
        Collection<CacheTypeFieldMetadata> keys = new ArrayList<>();
        keys.add(new CacheTypeFieldMetadata("myid", Types.BINARY, "myid", Object.class));
        type.setKeyFields(keys);

        // Value fields for uuid_test.
        Collection<CacheTypeFieldMetadata> vals = new ArrayList<>();
        vals.add(new CacheTypeFieldMetadata("myid", Types.BINARY, "myid", Object.class));
        vals.add(new CacheTypeFieldMetadata("md5hash", Types.VARCHAR, "md5hash", String.class));
        type.setValueFields(vals);

        // Query fields for uuid_test.
        Map<String, Class<?>> qryFlds = new LinkedHashMap<>();

        qryFlds.put("myid", Object.class);
        qryFlds.put("md5hash", String.class);

        type.setQueryFields(qryFlds);

        // Ascending fields for uuid_test.
        Map<String, Class<?>> ascFlds = new LinkedHashMap<>();

        ascFlds.put("myid", Object.class);

        type.setAscendingFields(ascFlds);

        ccfg.setTypeMetadata(meta);

        return ccfg;
    }
}


##actual application


public static void main(String args[]){

    CacheConfiguration ccfg = CacheConfig.cache(CACHE_NAME, new Factory<CacheStore<UuidTestKey, UuidTest>>() {
        @Override
        public CacheStore<UuidTestKey, UuidTest> create() {
            CacheJdbcPojoStore<UuidTestKey, UuidTest> store = new CacheJdbcPojoStore<>();

            MysqlDataSource dataSource = new MysqlDataSource();

            dataSource.setUrl("jdbc:mysql://localhost:3306");
            dataSource.setUser("blag");
            dataSource.setPassword("blah");

            store.setDataSource(dataSource);

            return store;
        }
    });

    ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);


    IgniteConfiguration cfg = new IgniteConfiguration();

    cfg.setPeerClassLoadingEnabled(false); //from config

    OptimizedMarshaller opm = new OptimizedMarshaller();
    opm.setRequireSerializable(true);
    cfg.setMarshaller(opm);


    System.out.println("start ignite");
    Ignite ignite = Ignition.start(cfg);
    System.out.println("started ignite");

    IgniteCache<UuidTestKey, UuidTest> bob = ignite.getOrCreateCache(ccfg);

    System.out.println("loading cache");
    bob.loadCache(null);
    System.out.println("loaded cache");

    for(Cache.Entry<UuidTestKey, UuidTest> entry : bob ){
        System.out.println("############################");
        System.out.println("KEY: " + entry.getKey().toString());

        if( entry.getValue() != null ){
            System.out.println("not null");
        } else {
            System.out.println("null value");
        }

        System.out.println("############################");
    }

}





So as I stated above, when im loading the cache I can see it actually create the objects, however if I try and loop over those entries the value is always null..

##example output

started ignite
loading cache
setting id: [B@31dbcc67
setting md5: 1ce4ccbc5d0d0856c20320af47082f90
setting id: [B@1e82e976
setting md5: 8938399f02eba8d8c686d138f8a22948
setting id: [B@6d387443
setting md5: ae0548a8ea6595b54c7a78dc90b73226
setting id: [B@392d642a
setting md5: 213d360701055e707442348f5fae4900
setting id: [B@4ac3bf8e
setting md5: 673bdd0dcd9f4079c615e855b9eb36da
setting id: [B@5dfec287
setting md5: af4c86aaabcfa8c908af5ed57643f329
setting id: [B@409ae7a8
setting md5: 74fcc8fb1aa4c0c7b73b259e6ecbbdd8
setting id: [B@6857421c
setting md5: 534dd648af15d8d2c148ab1b36699393
setting id: [B@7b6b9fa7
setting md5: 1d7c2da5ddeb618cdab01be628a83914
setting id: [B@5ee53ff4
setting md5: 1302e7bf2de21047fdaa885e7c1aabc5
setting id: [B@7f3a3b4e
setting md5: 38f4887816ef90ece12ad73a24818d48
setting id: [B@3a5250cf
setting md5: c912dec6d3ed3a7407ac1251f8ad13aa
setting id: [B@70dde5ec
setting md5: 5958b700438901fb4db7f10eef6f675e
loaded cache
############################
KEY: UuidTestKey [myid=[B@16e248af]
null value
############################
############################
KEY: UuidTestKey [myid=[B@378a230]
null value
############################
############################
KEY: UuidTestKey [myid=[B@761c3f71]
null value
############################
############################
KEY: UuidTestKey [myid=[B@20078e95]
null value
############################
############################
KEY: UuidTestKey [myid=[B@aaae957]
null value



I am positive I am doing something slightly wrong but I can't for the life of me work out what!

Thanks,


--

Dave












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

Re: Automatic Persistence cache: Key exists in cache, but not value

Dave,

It looks like you use byte array as an inner object of the key. Equals on a byte array will return true only if it's compared with the same instance, so your UuidTestKey.equals() implementation is wrong right now. Please try using Arrays.equals(..) method for array comparison.

Was this code generated by schema utility? What is the database type for UuidTestKey.myid field?

-Val
Dave Ellery Dave Ellery
Reply | Threaded
Open this post in threaded view
|

Re: Automatic Persistence cache: Key exists in cache, but not value

Val - thanks, I actually had a similar thought to that last night, that the comparison is somehow broken.

this was generated by the 1.1 schema tool, MySQL5.6 - what I actually need to do is get UUIDs working from a binary(16) column, but one step at a time!

Will try out your suggestion and report back!

On 2015-07-02 05:50 PM, vkulichenko wrote:

Dave,

It looks like you use byte array as an inner object of the key. Equals on a
byte array will return true only if it's compared with the same instance, so
your UuidTestKey.equals() implementation is wrong right now. Please try
using Arrays.equals(..) method for array comparison.

Was this code generated by schema utility? What is the database type for
UuidTestKey.myid field?

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Automatic-Persistence-cache-Key-exists-in-cache-but-not-value-tp605p606.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.



--
--
Dave Ellery
Senior Architect / Leonardo<http://www.leonardo.com>


office: +1 416 885 1450
email: [hidden email]<mailto:[hidden email]>
[cid:[hidden email]]<http://www.leonardo.com/products/vizlly/>
[cid:[hidden email]]<http://www.leonardo.com> [cid:[hidden email]] <http://www.twitter.com/vfmleonardo>  [cid:[hidden email]] <http://www.facebook.com/vfmleonardo>  [cid:[hidden email]] <http://www.linkedin.com/company/352778>  [cid:[hidden email]] <http://www.youtube.com/vfmleonardoonline>  [cid:[hidden email]] <http://blog.leonardo.com/>  [cid:[hidden email]] <http://instagram.com/leonardo_social>  [cid:[hidden email]] <https://plus.google.com/b/101309675710269958115/>

email-vizlly2.png (94K) Download Attachment
Leo_RGB_120px_0924.png (1K) Download Attachment
social-043_twitter.png (23K) Download Attachment
social-046_facebook.png (23K) Download Attachment
social-051_linkedin.png (23K) Download Attachment
social-058_youtube.png (24K) Download Attachment
social-060_rss.png (23K) Download Attachment
social-078_instagram.png (23K) Download Attachment
social-080_google_plus.png (23K) Download Attachment
Dave Ellery Dave Ellery
Reply | Threaded
Open this post in threaded view
|

Re: Automatic Persistence cache: Key exists in cache, but not value

so you and I were on the right track, it was actually the hashCode that was the issue (which makes sense), just had to change the hashCode generation in the Key class, also I changed the datatype to byte[], instead of object - I changed the .equals as well, but it never actually uses it (in lieu it uses the hashCode)

Before:


/** {@inheritDoc} */
@Override public int hashCode() {
    int res = myid != null ? myid.hashCode() : 0;

    return res;
}


After: using Arrays.hashCode to calculate the correct hashCode based on the array values (ref: http://stackoverflow.com/questions/1058149/using-a-byte-array-as-hashmap-key-java )


/** {@inheritDoc} */
@Override public int hashCode() {
    int res = myid != null ? Arrays.hashCode(myid) : 0;

    return res;
}


Now to get UUIDs to work!


On 2015-07-03 09:16 AM, Dave Ellery wrote:

Val - thanks, I actually had a similar thought to that last night, that the comparison is somehow broken.

this was generated by the 1.1 schema tool, MySQL5.6 - what I actually need to do is get UUIDs working from a binary(16) column, but one step at a time!

Will try out your suggestion and report back!

On 2015-07-02 05:50 PM, vkulichenko wrote:

Dave,

It looks like you use byte array as an inner object of the key. Equals on a
byte array will return true only if it's compared with the same instance, so
your UuidTestKey.equals() implementation is wrong right now. Please try
using Arrays.equals(..) method for array comparison.

Was this code generated by schema utility? What is the database type for
UuidTestKey.myid field?

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Automatic-Persistence-cache-Key-exists-in-cache-but-not-value-tp605p606.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.



--
--
Dave Ellery
Senior Architect / Leonardo<http://www.leonardo.com><http://www.leonardo.com>


office: +1 416 885 1450
email: [hidden email]<mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]>
[cid:[hidden email]]<http://www.leonardo.com/products/vizlly/><http://www.leonardo.com/products/vizlly/>
[cid:[hidden email]]<http://www.leonardo.com><http://www.leonardo.com> [cid:[hidden email]] <http://www.twitter.com/vfmleonardo><http://www.twitter.com/vfmleonardo>  [cid:[hidden email]] <http://www.facebook.com/vfmleonardo><http://www.facebook.com/vfmleonardo>  [cid:[hidden email]] <http://www.linkedin.com/company/352778><http://www.linkedin.com/company/352778>  [cid:[hidden email]] <http://www.youtube.com/vfmleonardoonline><http://www.youtube.com/vfmleonardoonline>  [cid:[hidden email]] <http://blog.leonardo.com/><http://blog.leonardo.com/>  [cid:[hidden email]] <http://instagram.com/leonardo_social><http://instagram.com/leonardo_social>  [cid:[hidden email]] <https://plus.google.com/b/
101309675710269958115/><https://plus.google.com/b/101309675710269958115/>


--
--
Dave Ellery
Senior Architect / Leonardo<http://www.leonardo.com>


office: +1 416 885 1450
email: [hidden email]<mailto:[hidden email]>
[cid:[hidden email]]<http://www.leonardo.com/products/vizlly/>
[cid:[hidden email]]<http://www.leonardo.com> [cid:[hidden email]] <http://www.twitter.com/vfmleonardo>  [cid:[hidden email]] <http://www.facebook.com/vfmleonardo>  [cid:[hidden email]] <http://www.linkedin.com/company/352778>  [cid:[hidden email]] <http://www.youtube.com/vfmleonardoonline>  [cid:[hidden email]] <http://blog.leonardo.com/>  [cid:[hidden email]] <http://instagram.com/leonardo_social>  [cid:[hidden email]] <https://plus.google.com/b/101309675710269958115/>

email-vizlly2.png (94K) Download Attachment
Leo_RGB_120px_0924.png (1K) Download Attachment
social-043_twitter.png (23K) Download Attachment
social-046_facebook.png (23K) Download Attachment
social-051_linkedin.png (23K) Download Attachment
social-058_youtube.png (24K) Download Attachment
social-060_rss.png (23K) Download Attachment
social-078_instagram.png (23K) Download Attachment
social-080_google_plus.png (23K) Download Attachment
Dave Ellery Dave Ellery
Reply | Threaded
Open this post in threaded view
|

Re: Automatic Persistence cache: Key exists in cache, but not value

Just one last follow up on this - UUIDs worked out of the box, very welcome surprise!

On 2015-07-03 12:00 PM, Dave Ellery wrote:

so you and I were on the right track, it was actually the hashCode that was the issue (which makes sense), just had to change the hashCode generation in the Key class, also I changed the datatype to byte[], instead of object - I changed the .equals as well, but it never actually uses it (in lieu it uses the hashCode)

Before:


/** {@inheritDoc} */
@Override public int hashCode() {
    int res = myid != null ? myid.hashCode() : 0;

    return res;
}


After: using Arrays.hashCode to calculate the correct hashCode based on the array values (ref: http://stackoverflow.com/questions/1058149/using-a-byte-array-as-hashmap-key-java )


/** {@inheritDoc} */
@Override public int hashCode() {
    int res = myid != null ? Arrays.hashCode(myid) : 0;

    return res;
}


Now to get UUIDs to work!


On 2015-07-03 09:16 AM, Dave Ellery wrote:

Val - thanks, I actually had a similar thought to that last night, that the comparison is somehow broken.

this was generated by the 1.1 schema tool, MySQL5.6 - what I actually need to do is get UUIDs working from a binary(16) column, but one step at a time!

Will try out your suggestion and report back!

On 2015-07-02 05:50 PM, vkulichenko wrote:

Dave,

It looks like you use byte array as an inner object of the key. Equals on a
byte array will return true only if it's compared with the same instance, so
your UuidTestKey.equals() implementation is wrong right now. Please try
using Arrays.equals(..) method for array comparison.

Was this code generated by schema utility? What is the database type for
UuidTestKey.myid field?

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Automatic-Persistence-cache-Key-exists-in-cache-but-not-value-tp605p606.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.



--
--
Dave Ellery
Senior Architect / Leonardo<http://www.leonardo.com><http://www.leonardo.com><http://www.leonardo.com><http://www.leonardo.com>


office: +1 416 885 1450
email: [hidden email]<mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]>
[cid:[hidden email]]<http://www.leonardo.com/products/vizlly/><http://www.leonardo.com/products/vizlly/><http://www.leonardo.com/products/vizlly/><http://www.leonardo.com/products/vizlly/>
[cid:[hidden email]]<http://www.leonardo.com><http://www.leonardo.com><http://www.leonardo.com><http://www.leonardo.com> [cid:[hidden email]] <http://www.twitter.com/vfmleonardo><http://www.twitter.com/vfmleonardo><http://www.twitter.com/vfmleonardo><http://www.twitter.com/vfmleonardo>  [cid:[hidden email]] <http://www.facebook.com/vfmleonardo><http://www.facebook.com/vfmleonardo><http://www.facebook.com/vfmleonardo><http://www.
facebook.com/vfmleonardo>  [cid:[hidden email]] <http://www.linkedin.com/company/352778><http://www.linkedin.com/company/352778><http://www.linkedin.com/company/352778><http://www.linkedin.com/company/352778>  [cid:[hidden email]] <http://www.youtube.com/vfmleonardoonline><http://www.youtube.com/vfmleonardoonline><http://www.youtube.com/vfmleonardoonline><http://www.youtube.com/vfmleonardoonline>  [cid:[hidden email]] <http://blog.leonardo.com/><http://blog.leonardo.com/><http://blog.leonardo.com/><http://blog.leonardo.com/>  [cid:[hidden email]] <http://instagram.com/leonardo_social><http://instagram.com/leonardo_social><http://instagram.com/leonardo_social><http://instagram.com/leonardo_social>  [cid:[hidden email]] <https://plus.google.com/b/
101309675710269958115/><https://plus.google.com/b/101309675710269958115/><https://plus.google.com/b/101309675710269958115/><https://plus.google.com/b/101309675710269958115/>


--
--
Dave Ellery
Senior Architect / Leonardo<http://www.leonardo.com><http://www.leonardo.com>


office: +1 416 885 1450
email: [hidden email]<mailto:[hidden email]><mailto:[hidden email]><mailto:[hidden email]>
[cid:[hidden email]]<http://www.leonardo.com/products/vizlly/><http://www.leonardo.com/products/vizlly/>
[cid:[hidden email]]<http://www.leonardo.com><http://www.leonardo.com> [cid:[hidden email]] <http://www.twitter.com/vfmleonardo><http://www.twitter.com/vfmleonardo>  [cid:[hidden email]] <http://www.facebook.com/vfmleonardo><http://www.facebook.com/vfmleonardo>  [cid:[hidden email]] <http://www.linkedin.com/company/352778><http://www.linkedin.com/company/352778>  [cid:[hidden email]] <http://www.youtube.com/vfmleonardoonline><http://www.youtube.com/vfmleonardoonline>  [cid:[hidden email]] <http://blog.leonardo.com/><http://blog.leonardo.com/>  [cid:[hidden email]] <http://instagram.com/leonardo_social><http://instagram.com/leonardo_social>  [cid:[hidden email]] <https://plus.google.com/b/
101309675710269958115/><https://plus.google.com/b/101309675710269958115/>


--
--
Dave Ellery
Senior Architect / Leonardo<http://www.leonardo.com>


office: +1 416 885 1450
email: [hidden email]<mailto:[hidden email]>
[cid:[hidden email]]<http://www.leonardo.com/products/vizlly/>
[cid:[hidden email]]<http://www.leonardo.com> [cid:[hidden email]] <http://www.twitter.com/vfmleonardo>  [cid:[hidden email]] <http://www.facebook.com/vfmleonardo>  [cid:[hidden email]] <http://www.linkedin.com/company/352778>  [cid:[hidden email]] <http://www.youtube.com/vfmleonardoonline>  [cid:[hidden email]] <http://blog.leonardo.com/>  [cid:[hidden email]] <http://instagram.com/leonardo_social>  [cid:[hidden email]] <https://plus.google.com/b/101309675710269958115/>

email-vizlly2.png (94K) Download Attachment
Leo_RGB_120px_0924.png (1K) Download Attachment
social-043_twitter.png (23K) Download Attachment
social-046_facebook.png (23K) Download Attachment
social-051_linkedin.png (23K) Download Attachment
social-058_youtube.png (24K) Download Attachment
social-060_rss.png (23K) Download Attachment
social-078_instagram.png (23K) Download Attachment
social-080_google_plus.png (23K) Download Attachment