public LeadsIntermediateIterator(String key, String prefix, InfinispanManager imanager, int i) {
    log = LoggerFactory.getLogger(LeadsIntermediateIterator.class);
    this.imanager = imanager;
    // initialize cache
    intermediateDataCache = (Cache) imanager.getPersisentCache(prefix + ".data");
    indexSiteCache = (Cache) imanager.getPersisentCache(prefix + ".indexed");
    baseIntermKey = new ComplexIntermediateKey();
    baseIntermKey.setCounter(currentCounter);
    baseIntermKey.setKey(key);

    // read all the IndexComplexIntermediateKeys with attribute == keys
    this.list = new ArrayList<>();
    try {
      CloseableIterable<Map.Entry<String, Object>> myIterable =
          ((Cache) indexSiteCache)
              .getAdvancedCache()
              .filterEntries(new IndexedComplexIntermKeyFilter(key));
      for (Map.Entry<String, Object> entry : myIterable) {
        //        System.err.println("ADDING TO LIST key: " + entry.getKey() + " value " +
        // entry.getValue().toString());
        if (entry.getValue() instanceof IndexedComplexIntermediateKey) {
          ComplexIntermediateKey c =
              new ComplexIntermediateKey((IndexedComplexIntermediateKey) entry.getValue());
          list.add((IndexedComplexIntermediateKey) entry.getValue());
        } else {
          System.err.println("\n\nGET [B once again");
        }
      }
      // check if the number of iterations is correct //this constructor is for debugging purposes
      if (i != list.size()) {
        System.err.println(
            "iterator size "
                + key
                + " "
                + list.size()
                + " correct number: "
                + i
                + " size of cache with IndexedComplexIntermediateKey "
                + indexSiteCache.size());
      }
    } catch (Exception e) {
      System.err.println("Exception on LeadsIntermediateIterator " + e.getClass().toString());
      System.err.println("Message: " + e.getMessage());
      log.error("Exception on LeadsIntermediateIterator " + e.getClass().toString());
      log.error("Message: " + e.getMessage());
    }

    chunkIterator = list.iterator();

    if (chunkIterator.hasNext()) {
      currentChunk = chunkIterator.next();
      baseIntermKey = new ComplexIntermediateKey(currentChunk);
    } else {
      currentChunk = null;
    }
  }
  @Override
  public String toString() {
    String result =
        "LeadsIntermediateIterator{"
            + "intermediateDataCache="
            + intermediateDataCache.getName()
            + ", indexSiteCache="
            + indexSiteCache.getName()
            + ", imanager="
            + imanager.getCacheManager().getAddress().toString();
    //    System.err.println(result);
    String resultb = ", baseIntermKey=" + baseIntermKey.toString();
    result += resultb;
    //    System.err.println(resultb);

    String resultc =
        ", list=" + list.size() + ", currentCounter=" + currentCounter.toString() + '}';

    //    PrintUtilities.printList(list);
    result += resultc;
    //    System.err.println(resultc);
    result += ", currentChunk=" + currentChunk;
    //    System.err.println(result);
    return result;
  }
  public LeadsIntermediateIterator(String key, String prefix, InfinispanManager imanager) {
    log = LoggerFactory.getLogger(LeadsIntermediateIterator.class);
    this.imanager = imanager;
    intermediateDataCache = (Cache) imanager.getPersisentCache(prefix + ".data");
    //    intermediateDataCache =
    // intermediateDataCache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL);

    // createIndexCache for getting all the nodes that contain values with the same key! in a mc
    indexSiteCache = (Cache) imanager.getPersisentCache(prefix + ".indexed");
    //    indexSiteCache = indexSiteCache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL);
    baseIntermKey = new ComplexIntermediateKey();
    baseIntermKey.setCounter(currentCounter);
    baseIntermKey.setKey(key);
    //    log.error("INDEXED SITE = " + indexSiteCache.size());
    //    for(Object keys : indexSiteCache.keySet()){
    //      log.error("key: " + keys.toString() + indexSiteCache.get(keys).toString());
    //    }
    // create query
    //    SearchManager sm = org.infinispan.query.Search.getSearchManager((Cache<?, ?>)
    // indexSiteCache);
    //    QueryFactory qf = org.infinispan.query.Search.getQueryFactory((Cache<?, ?>)
    // indexSiteCache); //Search.getQueryFactory((RemoteCache) indexSiteCache);
    //    org.infinispan.query.dsl.Query lucenequery = qf.from(IndexedComplexIntermediateKey.class)
    //                                                   .having("key").eq(key)
    //                                                   .toBuilder().build();
    //    ListIterator<Object> anIterator = lucenequery.list().listIterator();
    ProfileEvent event = new ProfileEvent("ReadIndexedKeys", log);
    this.list = new ArrayList<>();
    try {
      CloseableIterable<Map.Entry<Object, Object>> myIterable =
          ((Cache) indexSiteCache)
              .getAdvancedCache()
              .filterEntries(new IndexedComplexIntermKeyFilter(key));
      for (Map.Entry<Object, Object> entry : myIterable) {
        //        System.err.println("ADDING TO LIST key: " + entry.getKey() + " value " +
        // entry.getValue().toString());
        //        if(entry.getKey() instanceof  IndexedComplexIntermediateKey) {
        //          ComplexIntermediateKey c = new
        // ComplexIntermediateKey((IndexedComplexIntermediateKey) entry.getKey());
        //          if (intermediateDataCache.containsKey(c)){
        list.add((IndexedComplexIntermediateKey) entry.getValue());
        //          }
      }

      //      }
    } catch (Exception e) {
      System.err.println("Exception on LeadsIntermediateIterator " + e.getClass().toString());
      System.err.println("Message: " + e.getMessage());
      log.error("Exception on LeadsIntermediateIterator " + e.getClass().toString());
      log.error("Message: " + e.getMessage());
    }
    event.end();

    chunkIterator = list.iterator();

    if (chunkIterator.hasNext()) {
      currentChunk = chunkIterator.next();
      baseIntermKey = new ComplexIntermediateKey(currentChunk);
    } else {
      currentChunk = null;
    }
    values = new LinkedList<>();
    event.start("ReadAllValues");
    while (true) {
      try {
        Object value = nextInternal();
        if (value != null) {
          values.add(value);
        }
      } catch (Exception e) {
        if (e instanceof NoSuchElementException) {
          break;
        }
      }
    }
    event.end();
    valuesIterator = values.iterator();
  }