@SuppressWarnings("unchecked") @Test void testFindAllQueriesInBatches() { when(transactionInformation.isEntireRepositoryDirty(emd)).thenReturn(false); when(transactionInformation.isEntityDirty(any(EntityKey.class))) .thenAnswer( invocation -> Integer.parseInt(((EntityKey) invocation.getArguments()[0]).getId().toString()) % 3 == 0); List<Entity> lotsOfEntities = entityTestHarness.createTestRefEntities(emd, NUMBER_OF_ENTITIES); Stream<Object> ids = IntStream.range(0, NUMBER_OF_ENTITIES).mapToObj(Integer::toString); when(l2Cache.getBatch(eq(delegateRepository), any(Iterable.class))) .thenAnswer( invocation -> { List<Object> queried = Lists.newArrayList((Iterable<Object>) invocation.getArguments()[1]); return Lists.transform( queried, id -> lotsOfEntities.get(Integer.parseInt(id.toString()))); }); when(delegateRepository.findAll(any(Stream.class))) .thenAnswer( invocation -> { List<Object> queried = ((Stream<Object>) invocation.getArguments()[0]).collect(toList()); return Lists.transform( queried, id -> lotsOfEntities.get(Integer.parseInt(id.toString()))) .stream(); }); List<Entity> retrievedEntities = l2CacheRepositoryDecorator.findAll(ids).collect(toList()); assertEquals(NUMBER_OF_ENTITIES, retrievedEntities.size()); for (int i = 0; i < NUMBER_OF_ENTITIES; i++) { assertTrue(EntityUtils.equals(retrievedEntities.get(i), lotsOfEntities.get(i))); } }
@SuppressWarnings("UnstableApiUsage") @Override public Stream<Entity> findAll(Stream<Object> ids) { if (doRetrieveFromCache()) { Iterator<List<Object>> batches = Iterators.partition(ids.iterator(), ID_BATCH_SIZE); Iterable<List<Object>> iterable = () -> batches; return Streams.stream(iterable).flatMap(batch -> findAllCache(batch).stream()); } else { return delegate().findAll(ids); } }
L2CacheRepositoryDecorator extends AbstractRepositoryDecorator<Entity> { @SuppressWarnings("UnstableApiUsage") @Override public Stream<Entity> findAll(Stream<Object> ids) { if (doRetrieveFromCache()) { Iterator<List<Object>> batches = Iterators.partition(ids.iterator(), ID_BATCH_SIZE); Iterable<List<Object>> iterable = () -> batches; return Streams.stream(iterable).flatMap(batch -> findAllCache(batch).stream()); } else { return delegate().findAll(ids); } } }
L2CacheRepositoryDecorator extends AbstractRepositoryDecorator<Entity> { @SuppressWarnings("UnstableApiUsage") @Override public Stream<Entity> findAll(Stream<Object> ids) { if (doRetrieveFromCache()) { Iterator<List<Object>> batches = Iterators.partition(ids.iterator(), ID_BATCH_SIZE); Iterable<List<Object>> iterable = () -> batches; return Streams.stream(iterable).flatMap(batch -> findAllCache(batch).stream()); } else { return delegate().findAll(ids); } } L2CacheRepositoryDecorator(
      Repository<Entity> delegateRepository,
      L2Cache l2Cache,
      TransactionInformation transactionInformation); }
L2CacheRepositoryDecorator extends AbstractRepositoryDecorator<Entity> { @SuppressWarnings("UnstableApiUsage") @Override public Stream<Entity> findAll(Stream<Object> ids) { if (doRetrieveFromCache()) { Iterator<List<Object>> batches = Iterators.partition(ids.iterator(), ID_BATCH_SIZE); Iterable<List<Object>> iterable = () -> batches; return Streams.stream(iterable).flatMap(batch -> findAllCache(batch).stream()); } else { return delegate().findAll(ids); } } L2CacheRepositoryDecorator(
      Repository<Entity> delegateRepository,
      L2Cache l2Cache,
      TransactionInformation transactionInformation); @Override Entity findOneById(Object id); @SuppressWarnings("UnstableApiUsage") @Override Stream<Entity> findAll(Stream<Object> ids); @SuppressWarnings("UnstableApiUsage") @Override Stream<Entity> findAll(Stream<Object> ids, Fetch fetch); @Override Entity findOneById(Object id, Fetch fetch); }
L2CacheRepositoryDecorator extends AbstractRepositoryDecorator<Entity> { @SuppressWarnings("UnstableApiUsage") @Override public Stream<Entity> findAll(Stream<Object> ids) { if (doRetrieveFromCache()) { Iterator<List<Object>> batches = Iterators.partition(ids.iterator(), ID_BATCH_SIZE); Iterable<List<Object>> iterable = () -> batches; return Streams.stream(iterable).flatMap(batch -> findAllCache(batch).stream()); } else { return delegate().findAll(ids); } } L2CacheRepositoryDecorator(
      Repository<Entity> delegateRepository,
      L2Cache l2Cache,
      TransactionInformation transactionInformation); @Override Entity findOneById(Object id); @SuppressWarnings("UnstableApiUsage") @Override Stream<Entity> findAll(Stream<Object> ids); @SuppressWarnings("UnstableApiUsage") @Override Stream<Entity> findAll(Stream<Object> ids, Fetch fetch); @Override Entity findOneById(Object id, Fetch fetch);  }