incubator-accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject svn commit: r1242352 - /incubator/accumulo/trunk/src/examples/wikisearch/ingest/src/main/java/org/apache/accumulo/examples/wikisearch/ingest/LRUOutputCombiner.java
Date Thu, 09 Feb 2012 15:07:38 GMT
Author: kturner
Date: Thu Feb  9 15:07:38 2012
New Revision: 1242352

URL: http://svn.apache.org/viewvc?rev=1242352&view=rev
Log:
ACCUMULO-381 merged form 1.4

Added:
    incubator/accumulo/trunk/src/examples/wikisearch/ingest/src/main/java/org/apache/accumulo/examples/wikisearch/ingest/LRUOutputCombiner.java

Added: incubator/accumulo/trunk/src/examples/wikisearch/ingest/src/main/java/org/apache/accumulo/examples/wikisearch/ingest/LRUOutputCombiner.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/src/examples/wikisearch/ingest/src/main/java/org/apache/accumulo/examples/wikisearch/ingest/LRUOutputCombiner.java?rev=1242352&view=auto
==============================================================================
--- incubator/accumulo/trunk/src/examples/wikisearch/ingest/src/main/java/org/apache/accumulo/examples/wikisearch/ingest/LRUOutputCombiner.java
(added)
+++ incubator/accumulo/trunk/src/examples/wikisearch/ingest/src/main/java/org/apache/accumulo/examples/wikisearch/ingest/LRUOutputCombiner.java
Thu Feb  9 15:07:38 2012
@@ -0,0 +1,61 @@
+package org.apache.accumulo.examples.wikisearch.ingest;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public class LRUOutputCombiner<Key,Value> extends LinkedHashMap<Key,Value> {
+  
+  private static final long serialVersionUID = 1L;
+
+  public static abstract class Fold <Value>
+  {
+    public abstract Value fold(Value oldValue, Value newValue);
+  }
+  
+  public static abstract class Output<Key,Value>
+  {
+    public abstract void output(Key key, Value value);
+  }
+  
+  private final int capacity;
+  private final Fold<Value> fold;
+  private final Output<Key,Value> output;
+  
+  private long cacheHits = 0;
+  private long cacheMisses = 0;
+  
+  public LRUOutputCombiner(int capacity, Fold<Value> fold, Output<Key,Value>
output) {
+    super(capacity + 1, 1.1f, true);
+    this.capacity = capacity;
+    this.fold = fold;
+    this.output = output;
+  }
+  
+  protected boolean removeEldestEntry(Map.Entry<Key,Value> eldest) {
+    if (size() > capacity) {
+      output.output(eldest.getKey(), eldest.getValue());
+      return true;
+    }
+    return false;
+  }
+  
+  @Override
+  public Value put(Key key, Value value) {
+    Value val = get(key);
+    if (val != null) {
+      value = fold.fold(val, value);
+      cacheHits++;
+    } else {
+      cacheMisses++;
+    }
+    super.put(key, value);
+    return null;
+  }
+  
+  public void flush() {
+    for (Map.Entry<Key,Value> e : entrySet()) {
+      output.output(e.getKey(), e.getValue());
+    }
+    clear();
+  }
+}



Mime
View raw message