001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.filter; 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.Objects; 023import org.apache.hadoop.hbase.Cell; 024import org.apache.hadoop.hbase.CellUtil; 025import org.apache.hadoop.hbase.PrivateCellUtil; 026import org.apache.hadoop.hbase.exceptions.DeserializationException; 027import org.apache.hadoop.hbase.util.Bytes; 028import org.apache.yetus.audience.InterfaceAudience; 029 030import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 031import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException; 032import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; 033 034import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos; 035 036/** 037 * A filter, based on the ColumnCountGetFilter, takes two arguments: limit and offset. This filter 038 * can be used for row-based indexing, where references to other tables are stored across many 039 * columns, in order to efficient lookups and paginated results for end users. Only most recent 040 * versions are considered for pagination. 041 */ 042@InterfaceAudience.Public 043public class ColumnPaginationFilter extends FilterBase { 044 045 private int limit = 0; 046 private int offset = -1; 047 private byte[] columnOffset = null; 048 private int count = 0; 049 050 /** 051 * Initializes filter with an integer offset and limit. The offset is arrived at scanning 052 * sequentially and skipping entries. @limit number of columns are then retrieved. If multiple 053 * column families are involved, the columns may be spread across them. 054 * @param limit Max number of columns to return. 055 * @param offset The integer offset where to start pagination. 056 */ 057 public ColumnPaginationFilter(final int limit, final int offset) { 058 Preconditions.checkArgument(limit >= 0, "limit must be positive %s", limit); 059 Preconditions.checkArgument(offset >= 0, "offset must be positive %s", offset); 060 this.limit = limit; 061 this.offset = offset; 062 } 063 064 /** 065 * Initializes filter with a string/bookmark based offset and limit. The offset is arrived at, by 066 * seeking to it using scanner hints. If multiple column families are involved, pagination starts 067 * at the first column family which contains @columnOffset. Columns are then retrieved 068 * sequentially upto @limit number of columns which maybe spread across multiple column families, 069 * depending on how the scan is setup. 070 * @param limit Max number of columns to return. 071 * @param columnOffset The string/bookmark offset on where to start pagination. 072 */ 073 public ColumnPaginationFilter(final int limit, final byte[] columnOffset) { 074 Preconditions.checkArgument(limit >= 0, "limit must be positive %s", limit); 075 Preconditions.checkArgument(columnOffset != null, "columnOffset must be non-null %s", 076 columnOffset); 077 this.limit = limit; 078 this.columnOffset = columnOffset; 079 } 080 081 /** 082 * */ 083 public int getLimit() { 084 return limit; 085 } 086 087 /** 088 * */ 089 public int getOffset() { 090 return offset; 091 } 092 093 /** 094 * */ 095 public byte[] getColumnOffset() { 096 return columnOffset; 097 } 098 099 @Override 100 public boolean filterRowKey(Cell cell) throws IOException { 101 // Impl in FilterBase might do unnecessary copy for Off heap backed Cells. 102 return false; 103 } 104 105 @Override 106 @Deprecated 107 public ReturnCode filterKeyValue(final Cell c) { 108 return filterCell(c); 109 } 110 111 @Override 112 public ReturnCode filterCell(final Cell c) { 113 if (columnOffset != null) { 114 if (count >= limit) { 115 return ReturnCode.NEXT_ROW; 116 } 117 int cmp = 0; 118 // Only compare if no KV's have been seen so far. 119 if (count == 0) { 120 cmp = CellUtil.compareQualifiers(c, this.columnOffset, 0, this.columnOffset.length); 121 } 122 if (cmp < 0) { 123 return ReturnCode.SEEK_NEXT_USING_HINT; 124 } else { 125 count++; 126 return ReturnCode.INCLUDE_AND_NEXT_COL; 127 } 128 } else { 129 if (count >= offset + limit) { 130 return ReturnCode.NEXT_ROW; 131 } 132 133 ReturnCode code = count < offset ? ReturnCode.NEXT_COL : ReturnCode.INCLUDE_AND_NEXT_COL; 134 count++; 135 return code; 136 } 137 } 138 139 @Override 140 public Cell getNextCellHint(Cell cell) { 141 return PrivateCellUtil.createFirstOnRowCol(cell, columnOffset, 0, columnOffset.length); 142 } 143 144 @Override 145 public void reset() { 146 this.count = 0; 147 } 148 149 public static Filter createFilterFromArguments(ArrayList<byte[]> filterArguments) { 150 Preconditions.checkArgument(filterArguments.size() == 2, "Expected 2 but got: %s", 151 filterArguments.size()); 152 int limit = ParseFilter.convertByteArrayToInt(filterArguments.get(0)); 153 int offset = ParseFilter.convertByteArrayToInt(filterArguments.get(1)); 154 return new ColumnPaginationFilter(limit, offset); 155 } 156 157 /** Returns The filter serialized using pb */ 158 @Override 159 public byte[] toByteArray() { 160 FilterProtos.ColumnPaginationFilter.Builder builder = 161 FilterProtos.ColumnPaginationFilter.newBuilder(); 162 builder.setLimit(this.limit); 163 if (this.offset >= 0) { 164 builder.setOffset(this.offset); 165 } 166 if (this.columnOffset != null) { 167 builder.setColumnOffset(UnsafeByteOperations.unsafeWrap(this.columnOffset)); 168 } 169 return builder.build().toByteArray(); 170 } 171 172 /** 173 * @param pbBytes A pb serialized {@link ColumnPaginationFilter} instance 174 * @return An instance of {@link ColumnPaginationFilter} made from <code>bytes</code> 175 * @see #toByteArray 176 */ 177 public static ColumnPaginationFilter parseFrom(final byte[] pbBytes) 178 throws DeserializationException { 179 FilterProtos.ColumnPaginationFilter proto; 180 try { 181 proto = FilterProtos.ColumnPaginationFilter.parseFrom(pbBytes); 182 } catch (InvalidProtocolBufferException e) { 183 throw new DeserializationException(e); 184 } 185 if (proto.hasColumnOffset()) { 186 return new ColumnPaginationFilter(proto.getLimit(), proto.getColumnOffset().toByteArray()); 187 } 188 return new ColumnPaginationFilter(proto.getLimit(), proto.getOffset()); 189 } 190 191 /** 192 * @param o the other filter to compare with 193 * @return true if and only if the fields of the filter that are serialized are equal to the 194 * corresponding fields in other. Used for testing. 195 */ 196 @Override 197 boolean areSerializedFieldsEqual(Filter o) { 198 if (o == this) return true; 199 if (!(o instanceof ColumnPaginationFilter)) return false; 200 201 ColumnPaginationFilter other = (ColumnPaginationFilter) o; 202 if (this.columnOffset != null) { 203 return this.getLimit() == other.getLimit() 204 && Bytes.equals(this.getColumnOffset(), other.getColumnOffset()); 205 } 206 return this.getLimit() == other.getLimit() && this.getOffset() == other.getOffset(); 207 } 208 209 @Override 210 public String toString() { 211 if (this.columnOffset != null) { 212 return (this.getClass().getSimpleName() + "(" + this.limit + ", " 213 + Bytes.toStringBinary(this.columnOffset) + ")"); 214 } 215 return String.format("%s (%d, %d)", this.getClass().getSimpleName(), this.limit, this.offset); 216 } 217 218 @Override 219 public boolean equals(Object obj) { 220 return obj instanceof Filter && areSerializedFieldsEqual((Filter) obj); 221 } 222 223 @Override 224 public int hashCode() { 225 return columnOffset == null 226 ? Objects.hash(this.limit, this.offset) 227 : Objects.hash(this.limit, Bytes.hashCode(this.columnOffset)); 228 } 229}