@Before public void setUp() { Store.Configuration configuration = mock(Store.Configuration.class); when(configuration.getResourcePools()) .thenReturn(newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build()); when(configuration.getKeyType()).thenReturn(Long.class); when(configuration.getValueType()).thenReturn(Value.class); when(configuration.getExpiry()).thenReturn(Expirations.noExpiration()); Copier<Value> valueCopier = new Copier<Value>() { @Override public Value copyForRead(Value obj) { if (copyForRead) { return new Value(obj.state); } return obj; } @Override public Value copyForWrite(Value obj) { if (copyForWrite) { return new Value(obj.state); } return obj; } }; store = new OnHeapStore<Long, Value>( configuration, SystemTimeSource.INSTANCE, new IdentityCopier<Long>(), valueCopier); }
@SuppressWarnings("unchecked") private static <K, V> Store.Configuration<K, V> mockStoreConfig() { @SuppressWarnings("rawtypes") Store.Configuration config = mock(Store.Configuration.class); when(config.getExpiry()).thenReturn(Expirations.noExpiration()); when(config.getKeyType()).thenReturn(Number.class); when(config.getValueType()).thenReturn(CharSequence.class); return config; }
@Test public void testBulkComputeFunctionGetsValuesOfEntries() throws Exception { @SuppressWarnings("rawtypes") Store.Configuration config = mock(Store.Configuration.class); when(config.getExpiry()).thenReturn(Expirations.noExpiration()); when(config.getKeyType()).thenReturn(Number.class); when(config.getValueType()).thenReturn(Number.class); Store.Configuration<Number, Number> configuration = config; OnHeapStore<Number, Number> store = new OnHeapStore<Number, Number>(configuration, SystemTimeSource.INSTANCE, false); store.put(1, 2); store.put(2, 3); store.put(3, 4); Map<Number, Store.ValueHolder<Number>> result = store.bulkCompute( Arrays.asList(1, 2, 3, 4, 5, 6), new Function< Iterable<? extends Map.Entry<? extends Number, ? extends Number>>, Iterable<? extends Map.Entry<? extends Number, ? extends Number>>>() { @Override public Iterable<? extends Map.Entry<? extends Number, ? extends Number>> apply( Iterable<? extends Map.Entry<? extends Number, ? extends Number>> entries) { Map<Number, Number> newValues = new HashMap<Number, Number>(); for (Map.Entry<? extends Number, ? extends Number> entry : entries) { final Number currentValue = entry.getValue(); if (currentValue == null) { if (entry.getKey().equals(4)) { newValues.put(entry.getKey(), null); } else { newValues.put(entry.getKey(), 0); } } else { newValues.put(entry.getKey(), currentValue.intValue() * 2); } } return newValues.entrySet(); } }); ConcurrentMap<Number, Number> check = new ConcurrentHashMap<Number, Number>(); check.put(1, 4); check.put(2, 6); check.put(3, 8); check.put(4, 0); check.put(5, 0); check.put(6, 0); assertThat(result.get(1).value(), Matchers.<Number>is(check.get(1))); assertThat(result.get(2).value(), Matchers.<Number>is(check.get(2))); assertThat(result.get(3).value(), Matchers.<Number>is(check.get(3))); assertThat(result.get(4), nullValue()); assertThat(result.get(5).value(), Matchers.<Number>is(check.get(5))); assertThat(result.get(6).value(), Matchers.<Number>is(check.get(6))); for (Number key : check.keySet()) { final Store.ValueHolder<Number> holder = store.get(key); if (holder != null) { check.remove(key, holder.value()); } } assertThat(check.size(), is(1)); assertThat(check.containsKey(4), is(true)); }