public void testStoreAndLoad() throws Exception {
    final int numKeys = 300;
    for (int i = 0; i < numKeys; i++) {
      cache().put(i, i);
    }

    int keysInDataContainer = cache().getAdvancedCache().getDataContainer().keySet().size();

    assertTrue(keysInDataContainer != numKeys); // some keys got evicted

    AdvancedLoadWriteStore store = (AdvancedLoadWriteStore) TestingUtil.getCacheLoader(cache());
    int keysInCacheStore = PersistenceUtil.count(store, null);

    if (passivationEnabled) {
      assertEquals(numKeys, keysInDataContainer + keysInCacheStore);
    } else {
      assertEquals(numKeys, keysInCacheStore);
    }

    // check if keys survive restart
    cache().stop();
    cache().start();

    store = (AdvancedLoadWriteStore) TestingUtil.getCacheLoader(cache());
    assertEquals(numKeys, PersistenceUtil.count(store, null));

    for (int i = 0; i < numKeys; i++) {
      assertEquals(i, cache().get(i));
    }
  }
  @Override
  public void process(
      KeyFilter<? super K> filter,
      final CacheLoaderTask<K, V> task,
      Executor executor,
      final boolean fetchValue,
      final boolean fetchMetadata) {
    filter = PersistenceUtil.notNull(filter);
    ArrayList<KeyValuePair<K, FileEntry>> keysToLoad = new ArrayList<>(entries.size());
    synchronized (entries) {
      for (Map.Entry<K, FileEntry> e : entries.entrySet()) {
        if (filter.accept(e.getKey())) keysToLoad.add(new KeyValuePair<>(e.getKey(), e.getValue()));
      }
      Collections.sort(
          keysToLoad,
          new Comparator<KeyValuePair<K, FileEntry>>() {
            @Override
            public int compare(KeyValuePair<K, FileEntry> o1, KeyValuePair<K, FileEntry> o2) {
              long offset1 = o1.getValue().offset;
              long offset2 = o2.getValue().offset;
              return offset1 < offset2 ? -1 : offset1 == offset2 ? 0 : 1;
            }
          });
      // keysToLoad values (i.e. FileEntries) must not be used past this point
    }

    ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);

    final TaskContextImpl taskContext = new TaskContextImpl();
    for (KeyValuePair<K, FileEntry> e : keysToLoad) {
      if (taskContext.isStopped()) break;

      final K key = e.getKey();
      eacs.submit(
          new Callable<Void>() {
            @Override
            public Void call() throws Exception {
              try {
                final MarshalledEntry marshalledEntry = _load(key, fetchValue, fetchMetadata);
                if (marshalledEntry != null) {
                  task.processEntry(marshalledEntry, taskContext);
                }
                return null;
              } catch (Exception e) {
                log.errorExecutingParallelStoreTask(e);
                throw e;
              }
            }
          });
    }
    eacs.waitUntilAllCompleted();
    if (eacs.isExceptionThrown()) {
      throw new PersistenceException("Execution exception!", eacs.getFirstException());
    }
  }
Exemple #3
0
  @Override
  public void process(
      KeyFilter filter,
      final CacheLoaderTask task,
      Executor executor,
      final boolean fetchValue,
      final boolean fetchMetadata) {

    if (true) return;

    filter = PersistenceUtil.notNull(filter);
    Set<Object> keysToLoad = new HashSet<Object>(ctx.getCache().keySet());

    ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);

    final TaskContextImpl taskContext = new TaskContextImpl();
    for (final Object key : keysToLoad) {
      if (taskContext.isStopped()) break;

      eacs.submit(
          new Callable<Void>() {
            @Override
            public Void call() throws Exception {
              try {
                final MarshalledEntry marshalledEntry = _load(key, fetchValue, fetchMetadata);
                if (marshalledEntry != null) {
                  Debug.line(task, marshalledEntry, fetchValue, fetchMetadata);
                  task.processEntry(marshalledEntry, taskContext);
                }
                return null;
              } catch (Exception e) {
                log.errorExecutingParallelStoreTask(e);
                throw e;
              }
            }
          });
    }
    eacs.waitUntilAllCompleted();
    if (eacs.isExceptionThrown()) {
      throw new PersistenceException("Execution exception!", eacs.getFirstException());
    }
  }