1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.filter;
19
20 import java.io.DataInput;
21 import java.io.DataOutput;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.TreeSet;
26 import java.util.ArrayList;
27
28 import org.apache.hadoop.hbase.KeyValue;
29 import com.google.common.base.Preconditions;
30
31
32
33
34
35
36
37
38
39
40 public class TimestampsFilter extends FilterBase {
41
42 TreeSet<Long> timestamps;
43 private static final int MAX_LOG_TIMESTAMPS = 5;
44
45
46
47 long minTimeStamp = Long.MAX_VALUE;
48
49
50
51
52 public TimestampsFilter() {
53 super();
54 }
55
56
57
58
59
60
61
62
63 public TimestampsFilter(List<Long> timestamps) {
64 for (Long timestamp : timestamps) {
65 Preconditions.checkArgument(timestamp >= 0, "must be positive %s", timestamp);
66 }
67 this.timestamps = new TreeSet<Long>(timestamps);
68 init();
69 }
70
71
72
73
74 public List<Long> getTimestamps() {
75 List<Long> list = new ArrayList<Long>(timestamps.size());
76 list.addAll(timestamps);
77 return list;
78 }
79
80 private void init() {
81 if (this.timestamps.size() > 0) {
82 minTimeStamp = this.timestamps.first();
83 }
84 }
85
86
87
88
89
90 public long getMin() {
91 return minTimeStamp;
92 }
93
94 @Override
95 public ReturnCode filterKeyValue(KeyValue v) {
96 if (this.timestamps.contains(v.getTimestamp())) {
97 return ReturnCode.INCLUDE;
98 } else if (v.getTimestamp() < minTimeStamp) {
99
100
101 return ReturnCode.NEXT_COL;
102 }
103 return ReturnCode.SKIP;
104 }
105
106 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
107 ArrayList<Long> timestamps = new ArrayList<Long>();
108 for (int i = 0; i<filterArguments.size(); i++) {
109 long timestamp = ParseFilter.convertByteArrayToLong(filterArguments.get(i));
110 timestamps.add(timestamp);
111 }
112 return new TimestampsFilter(timestamps);
113 }
114
115 @Override
116 public void readFields(DataInput in) throws IOException {
117 int numTimestamps = in.readInt();
118 this.timestamps = new TreeSet<Long>();
119 for (int idx = 0; idx < numTimestamps; idx++) {
120 this.timestamps.add(in.readLong());
121 }
122 init();
123 }
124
125 @Override
126 public void write(DataOutput out) throws IOException {
127 int numTimestamps = this.timestamps.size();
128 out.writeInt(numTimestamps);
129 for (Long timestamp : this.timestamps) {
130 out.writeLong(timestamp);
131 }
132 }
133
134 @Override
135 public String toString() {
136 return toString(MAX_LOG_TIMESTAMPS);
137 }
138
139 protected String toString(int maxTimestamps) {
140 StringBuilder tsList = new StringBuilder();
141
142 int count = 0;
143 for (Long ts : this.timestamps) {
144 if (count >= maxTimestamps) {
145 break;
146 }
147 ++count;
148 tsList.append(ts.toString());
149 if (count < this.timestamps.size() && count < maxTimestamps) {
150 tsList.append(", ");
151 }
152 }
153
154 return String.format("%s (%d/%d): [%s]", this.getClass().getSimpleName(),
155 count, this.timestamps.size(), tsList.toString());
156 }
157 }