4 Replies Latest reply on Aug 4, 2015 8:12 AM by Andreas Pabst

    SoftIndexFileStore does not restart correctly when multiple puts are made on the same key

    Andreas Pabst Newbie

      This is very odd behaviour.

       

      I've attached a maven project, it's basically just the test class below and the respective dependencies.

       

      All I'm doing is put a value multiple times, then restart the cache, update a value, then restart again and read the value.

      If there was more than one put on the same key before the first restart, any updates after the restart are not persisted.

      Is it a bug or am I doing something horribly wrong? The same test works perfectly fine when using the single file store.

       

       

      public class SoftIndexCacheStoreUpdateTest {

       

         public EmbeddedCacheManager manager = new DefaultCacheManager();

       

        private ConfigurationChildBuilder createSoftIndexConfigBuilder(String location) {

         return new ConfigurationBuilder()

             .persistence().addStore(SoftIndexFileStoreConfigurationBuilder.class)

             .indexLocation(location + "/index").dataLocation(location + "/data").maxNodeSize(1000)

             .preload(true).purgeOnStartup(false)

             .eviction().strategy(EvictionStrategy.LRU).maxEntries((long) 100);

        }

       

         /**
        * This test puts a value three times (can also be different values, same effect),
        * then restarts the cache, updates the value, restarts the cache again and reads the value.
        *
        * The test fails because if there are multiple puts on the key before the first restart,
        * the update is not persisted and the old value is read.
        *
        * @throws IOException
        */
        @Test
        public void testUpdate() throws IOException {

        String key = "my_key";

        String cacheName = "update-test-cache";

        File cacheDir = new File(cacheName);

         if (cacheDir.exists()) {

             FileUtils.deleteDirectory(cacheDir);

        }

       

         // initialization of cache
         manager.defineConfiguration(cacheName, createSoftIndexConfigBuilder(cacheName).build());

        Cache<String,Point2D.Double> cache = manager.getCache(cacheName);

       

        Point2D.Double originalPoint = new Point2D.Double(24, 46);

        cache.put(key, originalPoint);

         // put value two more times (this is what causes the problem)
         cache.put(key, originalPoint);

        cache.put(key, originalPoint);

        cache.stop();

         manager.stop();

       

         // restart cache
         manager = new DefaultCacheManager();

         manager.defineConfiguration(cacheName, createSoftIndexConfigBuilder(cacheName).build());

        Cache<String,Point2D.Double> cacheAfterFirstRestart = manager.getCache(cacheName);

       

         // update cache entry
         Point2D.Double pointAfterFirstRestart = cacheAfterFirstRestart.get(key);

         assertNotNull(pointAfterFirstRestart);

         assertEquals(24, pointAfterFirstRestart.getX(), 0.0);

        pointAfterFirstRestart.setLocation(33, 44);

        cacheAfterFirstRestart.put(key, pointAfterFirstRestart);

        cacheAfterFirstRestart.stop();

         manager.stop();

       

         // restart cache
         manager = new DefaultCacheManager();

         manager.defineConfiguration(cacheName, createSoftIndexConfigBuilder(cacheName).build());

        Cache<String,Point2D.Double> cacheAfterSecondRestart = manager.getCache(cacheName);

       

         // cache should contain updated entry
         Point2D.Double pointAfterSecondRestart = cacheAfterSecondRestart.get(key);

         assertNotNull(pointAfterSecondRestart);

         assertEquals("Cache should contain updated entry (33.0), not original value (24.0)",

                               33, pointAfterSecondRestart.getX(), 0.0);

       

         cacheAfterSecondRestart.stop();

         manager.stop();

        }

       

       

      }