@Override public void writeAll(Iterable<? extends java.util.Map.Entry<? extends K, ? extends V>> entries) { if (cacheWriter != null) { Collection<Cache.Entry<? extends K, ? extends V>> toWrite = new ArrayList<>(); for (Map.Entry<? extends K, ? extends V> entry : entries) { toWrite.add(cacheEntryFor(entry.getKey(), entry.getValue())); } try { cacheWriter.writeAll(toWrite); } catch (Exception e) { // the remaining entries were not written per 107 spec Map<K, Exception> failures = new HashMap<>(); for (Cache.Entry<? extends K, ? extends V> entry : toWrite) { failures.put(entry.getKey(), e); } Set<K> successes = new HashSet<>(); for (Map.Entry<? extends K, ? extends V> entry : entries) { K key = entry.getKey(); if (!failures.containsKey(key)) { successes.add(key); } } throw new BulkCacheWritingException(failures, successes); } } }
@SuppressWarnings({ "unchecked" }) private static <K> void collectSuccessesAndFailures(BulkCacheWritingException bcwe, Set<K> successes, Map<K, Exception> failures) { successes.addAll((Collection<K>)bcwe.getSuccesses()); failures.putAll((Map<K, Exception>)bcwe.getFailures()); }
fail("expected CacheWritingException"); } catch (BulkCacheWritingException ex) { assertThat(ex.getFailures().size(), is(1)); assertThat(ex.getFailures().get(2), is(notNullValue())); assertThat(ex.getSuccesses().size(), is(3)); assertThat(ex.getSuccesses().containsAll(Arrays.asList(1, 3, 4)), is(true));
@Override public void deleteAll(Iterable<? extends K> keys) throws BulkCacheWritingException { if (cacheWriter != null) { Set<K> allKeys = new HashSet<>(); for (K key : keys) { allKeys.add(key); } try { cacheWriter.deleteAll(allKeys); } catch (Exception e) { // the remaining keys were not written per 107 spec Map<?, Exception> failures = failures(allKeys, e); Set<?> successes = successes(keys, failures.keySet()); throw new BulkCacheWritingException(failures, successes); } } }
@Test public void testPutAll_with_cache_writer_that_throws_exception() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100)); CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class); CacheLoaderWriter cacheLoaderWriterThatThrows = mock(CacheLoaderWriter.class); doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriterThatThrows).write(ArgumentMatchers .any(), ArgumentMatchers.any()); doThrow(new Exception("Simulating an exception from the cache writer")).when(cacheLoaderWriterThatThrows).writeAll(ArgumentMatchers.any(Iterable.class)); when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriterThatThrows); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.withLoaderWriter(cacheLoaderWriterThatThrows).build(); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); HashMap<String, String> stringStringHashMap = new HashMap<>(); for (int i = 0; i < 3; i++) { stringStringHashMap.put("key" + i, "value" + i); } // the call to putAll try { myCache.putAll(stringStringHashMap); fail(); } catch (BulkCacheWritingException bcwe) { assertThat(bcwe.getFailures().size(), is(3)); assertThat(bcwe.getSuccesses().size(), is(0)); } }
throw new BulkCacheWritingException(deleteFailures, deleteSuccesses); } else { return map;
fail(); } catch (BulkCacheWritingException bcwe) { assertThat(bcwe.getFailures().size(), is(2)); assertThat(bcwe.getSuccesses().size(), is(0));
throw new BulkCacheWritingException(failures, successes);
@SuppressWarnings({ "unchecked" }) private static <K> void collectSuccessesAndFailures(BulkCacheWritingException bcwe, Set<K> successes, Map<K, Exception> failures) { successes.addAll((Collection<K>)bcwe.getSuccesses()); failures.putAll((Map<K, Exception>)bcwe.getFailures()); }
/** * {@inheritDoc} * <p> * If this method throws an exception <i>other</i> than a * {@link BulkCacheWritingException BulkCacheWritingException}, the * cache image maintained by this {@code CacheLoaderWriter} is in an inconsistent state. */ @Override public void deleteAll(final Iterable<? extends String> keys) throws Exception { final Set<String> successes = new LinkedHashSet<>(); final Map<String, Exception> failures = new LinkedHashMap<>(); for (final String key : keys) { if (key.equals(this.completeFailureKey)) { throw new CompleteFailureException(); } try { this.delete(key); successes.add(key); } catch (Exception e) { //noinspection ThrowableResultOfMethodCallIgnored failures.put(key, e); } } if (!failures.isEmpty()) { throw new BulkCacheWritingException(failures, successes); } }
/** * {@inheritDoc} * <p> * If this method throws an exception <i>other</i> than a * {@link BulkCacheWritingException BulkCacheWritingException}, the * cache image maintained by this {@code CacheLoaderWriter} is in an inconsistent state. */ @Override public void writeAll(final Iterable<? extends Map.Entry<? extends String, ? extends String>> entries) throws Exception { final Set<String> successes = new LinkedHashSet<>(); final Map<String, Exception> failures = new LinkedHashMap<>(); for (final Entry<? extends String, ? extends String> entry : entries) { final String key = entry.getKey(); if (key.equals(this.completeFailureKey)) { throw new CompleteFailureException(); } try { this.write(key, entry.getValue()); successes.add(key); } catch (Exception e) { //noinspection ThrowableResultOfMethodCallIgnored failures.put(key, e); } } if (!failures.isEmpty()) { throw new BulkCacheWritingException(failures, successes); } }
@Override public void deleteAll(Iterable<? extends K> keys) throws BulkCacheWritingException { if (cacheWriter != null) { Set<K> allKeys = new HashSet<>(); for (K key : keys) { allKeys.add(key); } try { cacheWriter.deleteAll(allKeys); } catch (Exception e) { // the remaining keys were not written per 107 spec Map<?, Exception> failures = failures(allKeys, e); Set<?> successes = successes(keys, failures.keySet()); throw new BulkCacheWritingException(failures, successes); } } }
@Override public void writeAll(Iterable<? extends java.util.Map.Entry<? extends K, ? extends V>> entries) { if (cacheWriter != null) { Collection<Cache.Entry<? extends K, ? extends V>> toWrite = new ArrayList<>(); for (Map.Entry<? extends K, ? extends V> entry : entries) { toWrite.add(cacheEntryFor(entry.getKey(), entry.getValue())); } try { cacheWriter.writeAll(toWrite); } catch (Exception e) { // the remaining entries were not written per 107 spec Map<K, Exception> failures = new HashMap<>(); for (Cache.Entry<? extends K, ? extends V> entry : toWrite) { failures.put(entry.getKey(), e); } Set<K> successes = new HashSet<>(); for (Map.Entry<? extends K, ? extends V> entry : entries) { K key = entry.getKey(); if (!failures.containsKey(key)) { successes.add(key); } } throw new BulkCacheWritingException(failures, successes); } } }
addBulkMethodEntriesCount(BulkOps.REMOVE_ALL, actualRemoveCount[0]); if (!failures.isEmpty()) { throw new BulkCacheWritingException(failures, successes);
addBulkMethodEntriesCount(BulkOps.PUT_ALL, actualPutCount[0]); if (!failures.isEmpty()) { throw new BulkCacheWritingException(failures, successes);