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 long count = 0;
10 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 super();
18 this.counterName = counterName;
19 this.stageName = stageName;
20 this.instance = instance;
21 this.url = url;
22 }
23
24 public void increment() {
25 incrementBy(1);
26 }
27
28 public void incrementBy(int value) {
29 count += value;
30 }
31
32 public void flush() {
33
34 if (lastFlushCount == count)
35 return;
36
37 try {
38 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 URLConnection connection = new URL(fullUrl).openConnection();
43 connection.connect();
44 connection.getInputStream().close();
45 connection.getOutputStream().close();
46 lastFlushCount = count;
47 } catch (Exception e) {
48 }
49 }
50 }