View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.iterators;
18  
19  import java.io.IOException;
20  import java.util.Collection;
21  import java.util.Map;
22  
23  import org.apache.accumulo.core.data.ByteSequence;
24  import org.apache.accumulo.core.data.Key;
25  import org.apache.accumulo.core.data.Range;
26  import org.apache.accumulo.core.data.Value;
27  
28  public class ColumnFamilyCounter implements SortedKeyValueIterator<Key,Value> {
29    
30    private SortedKeyValueIterator<Key,Value> source;
31    private Key key;
32    private Value value;
33    
34    @Override
35    public void init(SortedKeyValueIterator<Key,Value> source, Map<String,String> options, IteratorEnvironment env) throws IOException {
36      this.source = source;
37    }
38    
39    @Override
40    public boolean hasTop() {
41      return key != null;
42    }
43    
44    @Override
45    public void next() throws IOException {
46      if (source.hasTop()) {
47        ByteSequence currentRow = source.getTopKey().getRowData();
48        ByteSequence currentColf = source.getTopKey().getColumnFamilyData();
49        long ts = source.getTopKey().getTimestamp();
50        
51        source.next();
52        
53        int count = 1;
54        
55        while (source.hasTop() && source.getTopKey().getRowData().equals(currentRow) && source.getTopKey().getColumnFamilyData().equals(currentColf)) {
56          count++;
57          source.next();
58        }
59        
60        this.key = new Key(currentRow.toArray(), currentColf.toArray(), new byte[0], new byte[0], ts);
61        this.value = new Value(Integer.toString(count).getBytes());
62        
63      } else {
64        this.key = null;
65        this.value = null;
66      }
67    }
68    
69    @Override
70    public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive) throws IOException {
71      source.seek(range, columnFamilies, inclusive);
72      next();
73    }
74    
75    @Override
76    public Key getTopKey() {
77      return key;
78    }
79    
80    @Override
81    public Value getTopValue() {
82      return value;
83    }
84    
85    @Override
86    public SortedKeyValueIterator<Key,Value> deepCopy(IteratorEnvironment env) {
87      // TODO Auto-generated method stub
88      return null;
89    }
90    
91  }