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
44
45
46 long minTimeStamp = Long.MAX_VALUE;
47
48
49
50
51 public TimestampsFilter() {
52 super();
53 }
54
55
56
57
58
59
60
61
62 public TimestampsFilter(List<Long> timestamps) {
63 for (Long timestamp : timestamps) {
64 Preconditions.checkArgument(timestamp >= 0, "must be positive %s", timestamp);
65 }
66 this.timestamps = new TreeSet<Long>(timestamps);
67 init();
68 }
69
70
71
72
73 public List<Long> getTimestamps() {
74 List<Long> list = new ArrayList<Long>(timestamps.size());
75 list.addAll(timestamps);
76 return list;
77 }
78
79 private void init() {
80 if (this.timestamps.size() > 0) {
81 minTimeStamp = this.timestamps.first();
82 }
83 }
84
85
86
87
88
89 public long getMin() {
90 return minTimeStamp;
91 }
92
93 @Override
94 public ReturnCode filterKeyValue(KeyValue v) {
95 if (this.timestamps.contains(v.getTimestamp())) {
96 return ReturnCode.INCLUDE;
97 } else if (v.getTimestamp() < minTimeStamp) {
98
99
100 return ReturnCode.NEXT_COL;
101 }
102 return ReturnCode.SKIP;
103 }
104
105 public static Filter createFilterFromArguments(ArrayList<byte []> filterArguments) {
106 ArrayList<Long> timestamps = new ArrayList<Long>();
107 for (int i = 0; i<filterArguments.size(); i++) {
108 long timestamp = ParseFilter.convertByteArrayToLong(filterArguments.get(i));
109 timestamps.add(timestamp);
110 }
111 return new TimestampsFilter(timestamps);
112 }
113
114 @Override
115 public void readFields(DataInput in) throws IOException {
116 int numTimestamps = in.readInt();
117 this.timestamps = new TreeSet<Long>();
118 for (int idx = 0; idx < numTimestamps; idx++) {
119 this.timestamps.add(in.readLong());
120 }
121 init();
122 }
123
124 @Override
125 public void write(DataOutput out) throws IOException {
126 int numTimestamps = this.timestamps.size();
127 out.writeInt(numTimestamps);
128 for (Long timestamp : this.timestamps) {
129 out.writeLong(timestamp);
130 }
131 }
132 }