/*MUST be called under the WriteLock*/
    @NotNull
    private Map<Integer, SerializedStubTree> readOldData(final int key) throws StorageException {
      final Map<Integer, SerializedStubTree> result = new HashMap<Integer, SerializedStubTree>();

      IndexStorage<Integer, SerializedStubTree> indexStorage = myStorage;
      if (indexStorage instanceof MemoryIndexStorage) {
        final MemoryIndexStorage<Integer, SerializedStubTree> memIndexStorage =
            (MemoryIndexStorage<Integer, SerializedStubTree>) indexStorage;
        if (!memIndexStorage.isBufferingEnabled()) {
          // if buffering is not enabled, use backend storage to make sure
          // the returned stub tree contains no data corresponding to unsaved documents.
          // This will ensure that correct set of old keys is used for update
          indexStorage = memIndexStorage.getBackendStorage();
        }
      }
      try {
        final ValueContainer<SerializedStubTree> valueContainer = indexStorage.read(key);
        if (valueContainer.size() != 1) {
          LOG.assertTrue(valueContainer.size() == 0);
          return result;
        }

        result.put(key, valueContainer.getValueIterator().next());
        return result;
      } catch (RuntimeException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof IOException) {
          throw new StorageException(cause);
        }
        throw e;
      }
    }
  @NotNull
  @Override
  public UpdatableIndex<Integer, SerializedStubTree, FileContent> createIndexImplementation(
      final ID<Integer, SerializedStubTree> indexId,
      @NotNull final FileBasedIndex owner,
      @NotNull IndexStorage<Integer, SerializedStubTree> storage)
      throws StorageException {
    if (storage instanceof MemoryIndexStorage) {
      final MemoryIndexStorage<Integer, SerializedStubTree> memStorage =
          (MemoryIndexStorage<Integer, SerializedStubTree>) storage;
      memStorage.addBufferingStateListsner(
          new MemoryIndexStorage.BufferingStateListener() {
            @Override
            public void bufferingStateChanged(final boolean newState) {
              ((StubIndexImpl) StubIndexImpl.getInstance()).setDataBufferingEnabled(newState);
            }

            @Override
            public void memoryStorageCleared() {
              ((StubIndexImpl) StubIndexImpl.getInstance()).cleanupMemoryStorage();
            }
          });
    }
    return new MyIndex(indexId, storage, getIndexer());
  }
 public void cleanupMemoryStorage() {
   for (UpdatableIndex index : myIndices.values()) {
     final IndexStorage indexStorage = ((MapReduceIndex) index).getStorage();
     index.getWriteLock().lock();
     try {
       ((MemoryIndexStorage) indexStorage).clearMemoryMap();
     } finally {
       index.getWriteLock().unlock();
     }
   }
 }
 public void setDataBufferingEnabled(final boolean enabled) {
   for (UpdatableIndex index : myIndices.values()) {
     final IndexStorage indexStorage = ((MapReduceIndex) index).getStorage();
     ((MemoryIndexStorage) indexStorage).setBufferingEnabled(enabled);
   }
 }