1818package org .apache .omid .transaction ;
1919
2020import org .apache .phoenix .thirdparty .com .google .common .base .Optional ;
21-
22-
21+ import org . slf4j . Logger ;
22+ import org . slf4j . LoggerFactory ;
2323import org .apache .commons .collections4 .map .LRUMap ;
2424import org .apache .hadoop .hbase .Cell ;
2525import org .apache .hadoop .hbase .CellUtil ;
26+ import org .apache .hadoop .hbase .KeyValue ;
27+ import org .apache .hadoop .hbase .KeyValueUtil ;
28+ import org .apache .hadoop .hbase .Cell .Type ;
2629import org .apache .hadoop .hbase .client .Get ;
2730import org .apache .hadoop .hbase .client .Result ;
2831import org .apache .hadoop .hbase .filter .Filter ;
3134
3235import org .apache .hadoop .hbase .util .Bytes ;
3336
34-
3537import java .io .IOException ;
38+ import java .lang .invoke .MethodHandle ;
39+ import java .lang .invoke .MethodHandles ;
40+ import java .lang .invoke .MethodType ;
41+ import java .lang .reflect .Method ;
3642import java .util .HashMap ;
3743
3844import java .util .List ;
3945import java .util .Map ;
4046
4147
42- public class TransactionVisibilityFilterBase extends FilterBase {
48+ public class TransactionVisibilityFilter extends FilterBase {
49+
50+ private static final Logger LOG = LoggerFactory .getLogger (TransactionVisibilityFilter .class );
4351
4452 // optional sub-filter to apply to visible cells
4553 private final Filter userFilter ;
@@ -51,7 +59,14 @@ public class TransactionVisibilityFilterBase extends FilterBase {
5159 // So no need to keep row name
5260 private final Map <ImmutableBytesWritable , Long > familyDeletionCache ;
5361
54- public TransactionVisibilityFilterBase (Filter cellFilter ,
62+ // Using reflection to avoid adding compatibility modules.
63+ private final MethodHandle removedFilterRowKeyMethod ;
64+ private final MethodHandle removedSuperFilterRowKeyMethod ;
65+ static {
66+
67+ }
68+
69+ public TransactionVisibilityFilter (Filter cellFilter ,
5570 SnapshotFilterImpl snapshotFilter ,
5671 HBaseTransaction hbaseTransaction ) {
5772 this .userFilter = cellFilter ;
@@ -60,13 +75,37 @@ public TransactionVisibilityFilterBase(Filter cellFilter,
6075 this .hbaseTransaction = hbaseTransaction ;
6176 familyDeletionCache = new HashMap <>();
6277
78+ MethodHandle tmpFilterRowKeyMethod ;
79+ try {
80+ tmpFilterRowKeyMethod = MethodHandles .lookup ().findVirtual (userFilter .getClass (), "filterRowKey" , MethodType .methodType (boolean .class , byte [].class , int .class , int .class ));
81+ //tmpFilterRowKeyMethod = FilterBase.class.getMethod("filterRowKey", byte[].class, int.class, int.class);
82+ } catch (Exception e ) {
83+ if (userFilter != null ) {
84+ LOG .info ("Could not get filterRowKey method handle by reflection. This is normal for HBase 3.x" , e );
85+ }
86+ tmpFilterRowKeyMethod = null ;
87+ }
88+ removedFilterRowKeyMethod = tmpFilterRowKeyMethod ;
89+
90+ MethodHandle tmpRemovedSuperFilterRowKeyMethod ;
91+ try {
92+ tmpRemovedSuperFilterRowKeyMethod = MethodHandles .lookup ().findSpecial (FilterBase .class , "filterRowKey" ,
93+ MethodType .methodType (boolean .class , byte [].class , int .class , int .class ),
94+ TransactionVisibilityFilter .class );
95+ } catch (Exception e ) {
96+ LOG .info ("Could not get super.filterRowKey method handle by reflection. This is normal for HBase 3.x" , e );
97+ tmpRemovedSuperFilterRowKeyMethod = null ;
98+ }
99+ removedSuperFilterRowKeyMethod = tmpRemovedSuperFilterRowKeyMethod ;
100+
63101 }
64102
65103 /**
66104 * This deprecated method is implemented for backwards compatibility reasons.
67- * use {@link TransactionVisibilityFilterBase#filterCell(Cell)}
105+ * use {@link TransactionVisibilityFilter#filterCell(Cell)}
106+ *
107+ * No @Override so that it compiles with HBase 3.x
68108 */
69- @ Override
70109 public ReturnCode filterKeyValue (Cell cell ) throws IOException {
71110 return filterCell (cell );
72111 }
@@ -202,16 +241,32 @@ public boolean filterRow() throws IOException {
202241 return super .filterRow ();
203242 }
204243
205- /**
244+ /**
206245 * This deprecated method is implemented for backwards compatibility reasons.
207- * use {@link TransactionVisibilityFilterBase#filterRowKey(Cell)}
246+ * use {@link TransactionVisibilityFilter#filterRowKey(Cell)}
247+ *
248+ * No @Override so that it compiles with HBase 3.x
208249 */
209- @ Override
210250 public boolean filterRowKey (byte [] buffer , int offset , int length ) throws IOException {
251+ // Even though this is deprecated, this is the actual implementation that filterRowKey(Cell)
252+ // calls in FilterBase in HBase 2, so we cannot call filterRowKey(Cell), because that would
253+ // cause infinite recursion
211254 if (userFilter != null ) {
212- return userFilter .filterRowKey (buffer , offset , length );
255+ try {
256+ return (boolean ) (removedFilterRowKeyMethod .invoke (userFilter , buffer , offset , length ));
257+ } catch (IOException e ) {
258+ throw e ;
259+ } catch (Throwable t ) {
260+ throw new UnsupportedOperationException (t );
261+ }
262+ }
263+ try {
264+ return (boolean ) (removedSuperFilterRowKeyMethod .invokeExact (this , buffer , offset , length ));
265+ } catch (IOException e ) {
266+ throw e ;
267+ } catch (Throwable t ) {
268+ throw new UnsupportedOperationException (t );
213269 }
214- return super .filterRowKey (buffer , offset , length );
215270 }
216271
217272 @ Override
@@ -271,4 +326,5 @@ public byte[] toByteArray() throws IOException {
271326 public Filter getInnerFilter () {
272327 return userFilter ;
273328 }
329+
274330}
0 commit comments