| 1 | |
package org.galagosearch.tupleflow.execution; |
| 2 | |
|
| 3 | |
import java.net.URL; |
| 4 | |
import java.net.URLConnection; |
| 5 | |
import java.net.URLEncoder; |
| 6 | |
import org.galagosearch.tupleflow.Counter; |
| 7 | |
|
| 8 | |
public class NetworkedCounter implements Counter { |
| 9 | 0 | long count = 0; |
| 10 | 0 | long lastFlushCount = Long.MIN_VALUE; |
| 11 | |
String counterName; |
| 12 | |
String stageName; |
| 13 | |
String instance; |
| 14 | |
String url; |
| 15 | |
|
| 16 | |
NetworkedCounter(String counterName, String stageName, String instance, String url) { |
| 17 | 0 | super(); |
| 18 | 0 | this.counterName = counterName; |
| 19 | 0 | this.stageName = stageName; |
| 20 | 0 | this.instance = instance; |
| 21 | 0 | this.url = url; |
| 22 | 0 | } |
| 23 | |
|
| 24 | |
public void increment() { |
| 25 | 0 | incrementBy(1); |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
public void incrementBy(int value) { |
| 29 | 0 | count += value; |
| 30 | 0 | } |
| 31 | |
|
| 32 | |
public void flush() { |
| 33 | |
|
| 34 | 0 | if (lastFlushCount == count) |
| 35 | 0 | return; |
| 36 | |
|
| 37 | |
try { |
| 38 | 0 | String fullUrl = String.format("%s/setcounter?counterName=%s&stageName=%s&instance=%s&value=%d", |
| 39 | |
url, URLEncoder.encode(counterName, "UTF-8"), |
| 40 | |
URLEncoder.encode(stageName, "UTF-8"), |
| 41 | |
URLEncoder.encode(instance, "UTF-8"), count); |
| 42 | 0 | URLConnection connection = new URL(fullUrl).openConnection(); |
| 43 | 0 | connection.connect(); |
| 44 | 0 | connection.getInputStream().close(); |
| 45 | 0 | connection.getOutputStream().close(); |
| 46 | 0 | lastFlushCount = count; |
| 47 | 0 | } catch (Exception e) { |
| 48 | 0 | } |
| 49 | 0 | } |
| 50 | |
} |