All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.accumulo.server.compaction.CountingIterator Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.accumulo.server.compaction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Value;
import org.apache.accumulo.core.iterators.IteratorEnvironment;
import org.apache.accumulo.core.iterators.SortedKeyValueIterator;
import org.apache.accumulo.core.iterators.WrappingIterator;

public class CountingIterator extends WrappingIterator {

  private long count;
  private final ArrayList deepCopies;
  private final AtomicLong entriesRead;

  @Override
  public CountingIterator deepCopy(IteratorEnvironment env) {
    return new CountingIterator(this, env);
  }

  private CountingIterator(CountingIterator other, IteratorEnvironment env) {
    setSource(other.getSource().deepCopy(env));
    count = 0;
    this.deepCopies = other.deepCopies;
    this.entriesRead = other.entriesRead;
    deepCopies.add(this);
  }

  public CountingIterator(SortedKeyValueIterator source, AtomicLong entriesRead) {
    deepCopies = new ArrayList<>();
    this.setSource(source);
    count = 0;
    this.entriesRead = entriesRead;
  }

  @Override
  public void init(SortedKeyValueIterator source, Map options,
      IteratorEnvironment env) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void next() throws IOException {
    super.next();
    count++;
    if (count % 1024 == 0) {
      entriesRead.addAndGet(1024);
    }
  }

  public long getCount() {
    long sum = 0;
    for (CountingIterator dc : deepCopies) {
      sum += dc.count;
    }

    return count + sum;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy