@Test public void testCacheDoesNotGrowOverMaxCached() { RelatedItem product1 = new RelatedItem("1".toCharArray(),"1920-01-03T01:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product2 = new RelatedItem("1".toCharArray(),"1920-01-04T02:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product3 = new RelatedItem("1".toCharArray(),"1920-01-05T03:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product4 = new RelatedItem("1".toCharArray(),"1920-01-06T04:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product5 = new RelatedItem("1".toCharArray(),"1920-01-07T05:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product6 = new RelatedItem("1".toCharArray(),"1920-01-08T06:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product7 = new RelatedItem("1".toCharArray(),"1920-01-09T07:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product8 = new RelatedItem("1".toCharArray(),"1920-01-10T01:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product9 = new RelatedItem("1".toCharArray(),"1920-01-11T01:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product10 = new RelatedItem("1".toCharArray(),"1920-01-12T01:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product11 = new RelatedItem("1".toCharArray(),"1920-01-13T01:59:59+00:00",null,new RelatedItemAdditionalProperties()); RelatedItem product12 = new RelatedItem("1".toCharArray(),"1920-01-14T01:59:59+00:00",null,new RelatedItemAdditionalProperties()); String name = cachingDayBasedMapper.getLocationName(product1); name = cachingDayBasedMapper.getLocationName(product2); name = cachingDayBasedMapper.getLocationName(product3); name = cachingDayBasedMapper.getLocationName(product4); name = cachingDayBasedMapper.getLocationName(product5); name = cachingDayBasedMapper.getLocationName(product6); name = cachingDayBasedMapper.getLocationName(product7); name = cachingDayBasedMapper.getLocationName(product8); name = cachingDayBasedMapper.getLocationName(product9); name = cachingDayBasedMapper.getLocationName(product10); name = cachingDayBasedMapper.getLocationName(product11); name = cachingDayBasedMapper.getLocationName(product12); try { Field cache = null; cache = DayBasedStorageLocationMapper.class.getDeclaredField("dayCache"); cache.setAccessible(true); ConcurrentMap m = (ConcurrentMap) cache.get(cachingDayBasedMapper); assertEquals(10, m.size()); } catch (NoSuchFieldException e) { fail(); } catch (IllegalAccessException e) { fail(); } assertEquals(cachingDayConfig.getStorageIndexNamePrefix() + "-1920-01-14",name); }
@Override public String getLocationName(RelatedItem product) { String dateStr = product.getDate(); String date; if(dateStr==null) { date = currentDayFormatter.getCurrentDay(); } else { date = currentDayFormatter.parseToDate(dateStr); } if(dateCachingEnabled) { String cachedIndexName = dayCache.get(date); if(cachedIndexName==null) { StringBuilder indexName = new StringBuilder(indexNameSize); String theIndexName = indexName.append(this.indexPrefixName).append(date).toString(); String previous = dayCache.putIfAbsent(date,theIndexName); if(previous!=null) { return previous; } else { return theIndexName; } } else { return cachedIndexName; } } else { StringBuilder indexName = new StringBuilder(indexNameSize); return indexName.append(this.indexPrefixName).append(date).toString(); } }
DayBasedStorageLocationMapper implements RelatedItemStorageLocationMapper { @Override public String getLocationName(RelatedItem product) { String dateStr = product.getDate(); String date; if(dateStr==null) { date = currentDayFormatter.getCurrentDay(); } else { date = currentDayFormatter.parseToDate(dateStr); } if(dateCachingEnabled) { String cachedIndexName = dayCache.get(date); if(cachedIndexName==null) { StringBuilder indexName = new StringBuilder(indexNameSize); String theIndexName = indexName.append(this.indexPrefixName).append(date).toString(); String previous = dayCache.putIfAbsent(date,theIndexName); if(previous!=null) { return previous; } else { return theIndexName; } } else { return cachedIndexName; } } else { StringBuilder indexName = new StringBuilder(indexNameSize); return indexName.append(this.indexPrefixName).append(date).toString(); } } }
DayBasedStorageLocationMapper implements RelatedItemStorageLocationMapper { @Override public String getLocationName(RelatedItem product) { String dateStr = product.getDate(); String date; if(dateStr==null) { date = currentDayFormatter.getCurrentDay(); } else { date = currentDayFormatter.parseToDate(dateStr); } if(dateCachingEnabled) { String cachedIndexName = dayCache.get(date); if(cachedIndexName==null) { StringBuilder indexName = new StringBuilder(indexNameSize); String theIndexName = indexName.append(this.indexPrefixName).append(date).toString(); String previous = dayCache.putIfAbsent(date,theIndexName); if(previous!=null) { return previous; } else { return theIndexName; } } else { return cachedIndexName; } } else { StringBuilder indexName = new StringBuilder(indexNameSize); return indexName.append(this.indexPrefixName).append(date).toString(); } } DayBasedStorageLocationMapper(Configuration configuration,
                                         UTCCurrentDateFormatter dateFormatter); }
DayBasedStorageLocationMapper implements RelatedItemStorageLocationMapper { @Override public String getLocationName(RelatedItem product) { String dateStr = product.getDate(); String date; if(dateStr==null) { date = currentDayFormatter.getCurrentDay(); } else { date = currentDayFormatter.parseToDate(dateStr); } if(dateCachingEnabled) { String cachedIndexName = dayCache.get(date); if(cachedIndexName==null) { StringBuilder indexName = new StringBuilder(indexNameSize); String theIndexName = indexName.append(this.indexPrefixName).append(date).toString(); String previous = dayCache.putIfAbsent(date,theIndexName); if(previous!=null) { return previous; } else { return theIndexName; } } else { return cachedIndexName; } } else { StringBuilder indexName = new StringBuilder(indexNameSize); return indexName.append(this.indexPrefixName).append(date).toString(); } } DayBasedStorageLocationMapper(Configuration configuration,
                                         UTCCurrentDateFormatter dateFormatter); @Override String getLocationName(RelatedItem product); }
DayBasedStorageLocationMapper implements RelatedItemStorageLocationMapper { @Override public String getLocationName(RelatedItem product) { String dateStr = product.getDate(); String date; if(dateStr==null) { date = currentDayFormatter.getCurrentDay(); } else { date = currentDayFormatter.parseToDate(dateStr); } if(dateCachingEnabled) { String cachedIndexName = dayCache.get(date); if(cachedIndexName==null) { StringBuilder indexName = new StringBuilder(indexNameSize); String theIndexName = indexName.append(this.indexPrefixName).append(date).toString(); String previous = dayCache.putIfAbsent(date,theIndexName); if(previous!=null) { return previous; } else { return theIndexName; } } else { return cachedIndexName; } } else { StringBuilder indexName = new StringBuilder(indexNameSize); return indexName.append(this.indexPrefixName).append(date).toString(); } } DayBasedStorageLocationMapper(Configuration configuration,
                                         UTCCurrentDateFormatter dateFormatter); @Override String getLocationName(RelatedItem product);  }