下面列出了org.apache.hadoop.hbase.KeyValue#createFirstOnRow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ReturnCode filterKeyValue(Cell cell) throws IOException {
if (skipCellVersion(cell)) {
return ReturnCode.NEXT_COL;
}
ReturnCode code = filter.filterKeyValue(cell);
if (code == ReturnCode.NEXT_COL || code == ReturnCode.INCLUDE_AND_NEXT_COL) {
// only store the reference to the keyvalue if we are returning NEXT_COL or INCLUDE_AND_NEXT_COL
skipColumn = KeyValue.createFirstOnRow(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(),
cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
cell.getQualifierArray(), cell.getQualifierOffset(),
cell.getQualifierLength());
} else {
skipColumn = null;
}
return code;
}
@Override
public ReturnCode filterKeyValue(Cell cell) throws IOException {
if (skipCellVersion(cell)) {
return ReturnCode.NEXT_COL;
}
ReturnCode code = filter.filterKeyValue(cell);
if (code == ReturnCode.NEXT_COL || code == ReturnCode.INCLUDE_AND_NEXT_COL) {
// only store the reference to the keyvalue if we are returning NEXT_COL or INCLUDE_AND_NEXT_COL
skipColumn = KeyValue.createFirstOnRow(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(),
cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
cell.getQualifierArray(), cell.getQualifierOffset(),
cell.getQualifierLength());
} else {
skipColumn = null;
}
return code;
}
@SuppressWarnings("deprecation")
@Override
public KeyValue getHint(KeyValue peeked) {
// check to see if we have another column to seek
ImmutableBytesPtr nextFamily =
getNextFamily(new ImmutableBytesPtr(peeked.getBuffer(), peeked.getFamilyOffset(),
peeked.getFamilyLength()));
if (nextFamily == null) {
// no known next family, so we can be completely done
done = true;
return KeyValue.LOWESTKEY;
}
// there is a valid family, so we should seek to that
return KeyValue.createFirstOnRow(peeked.getRow(), nextFamily.copyBytesIfNecessary(),
HConstants.EMPTY_BYTE_ARRAY);
}
@Test
public void testCorrectOverwritting() throws Exception {
IndexMemStore store = new IndexMemStore(IndexMemStore.COMPARATOR);
long ts = 10;
KeyValue kv = new KeyValue(row, family, qual, ts, Type.Put, val);
kv.setSequenceId(2);
KeyValue kv2 = new KeyValue(row, family, qual, ts, Type.Put, val2);
kv2.setSequenceId(0);
store.add(kv, true);
// adding the exact same kv shouldn't change anything stored if not overwritting
store.add(kv2, false);
KeyValueScanner scanner = store.getScanner();
KeyValue first = KeyValue.createFirstOnRow(row);
scanner.seek(first);
assertTrue("Overwrote kv when specifically not!", kv == scanner.next());
scanner.close();
// now when we overwrite, we should get the newer one
store.add(kv2, true);
scanner = store.getScanner();
scanner.seek(first);
assertTrue("Didn't overwrite kv when specifically requested!", kv2 == scanner.next());
scanner.close();
}
@Test
public void testCorrectOverwritting() throws Exception {
IndexMemStore store = new IndexMemStore(IndexMemStore.COMPARATOR);
long ts = 10;
KeyValue kv = new KeyValue(row, family, qual, ts, Type.Put, val);
kv.setMemstoreTS(2);
KeyValue kv2 = new KeyValue(row, family, qual, ts, Type.Put, val2);
kv2.setMemstoreTS(0);
store.add(kv, true);
// adding the exact same kv shouldn't change anything stored if not overwritting
store.add(kv2, false);
KeyValueScanner scanner = store.getScanner();
KeyValue first = KeyValue.createFirstOnRow(row);
scanner.seek(first);
assertTrue("Overwrote kv when specifically not!", kv == scanner.next());
scanner.close();
// now when we overwrite, we should get the newer one
store.add(kv2, true);
scanner = store.getScanner();
scanner.seek(first);
assertTrue("Didn't overwrite kv when specifically requested!", kv2 == scanner.next());
scanner.close();
}
/**
* We don't expect custom KeyValue creation, so we can't get into weird situations, where a
* {@link Type#DeleteFamily} has a column qualifier specified.
* @throws Exception
*/
@Test
public void testExpectedOrdering() throws Exception {
IndexMemStore store = new IndexMemStore();
KeyValue kv = new KeyValue(row, family, qual, 12, Type.Put, val);
store.add(kv, true);
KeyValue kv2 = new KeyValue(row, family, qual, 10, Type.Put, val2);
store.add(kv2, true);
KeyValue df = new KeyValue(row, family, null, 11, Type.DeleteFamily, null);
store.add(df, true);
KeyValue dc = new KeyValue(row, family, qual, 11, Type.DeleteColumn, null);
store.add(dc, true);
KeyValue d = new KeyValue(row, family, qual, 12, Type.Delete, null);
store.add(d, true);
// null qualifiers should always sort before the non-null cases
KeyValueScanner scanner = store.getScanner();
KeyValue first = KeyValue.createFirstOnRow(row);
assertTrue("Didn't have any data in the scanner", scanner.seek(first));
assertTrue("Didn't get delete family first (no qualifier == sort first)", df == scanner.next());
assertTrue("Didn't get point delete before corresponding put", d == scanner.next());
assertTrue("Didn't get larger ts Put", kv == scanner.next());
assertTrue("Didn't get delete column before corresponding put(delete sorts first)",
dc == scanner.next());
assertTrue("Didn't get smaller ts Put", kv2 == scanner.next());
assertNull("Have more data in the scanner", scanner.next());
}
/**
* Hinting with this filter is a little convoluted as we binary search the list of families to
* attempt to find the right one to seek.
*/
@Test
public void testHintCorrectlyToNextFamily() {
// start with doing a family delete, so we will seek to the next column
KeyValue kv = createKvForType(Type.DeleteFamily);
ApplyAndFilterDeletesFilter filter = new ApplyAndFilterDeletesFilter(EMPTY_SET);
assertEquals(ReturnCode.SKIP, filter.filterKeyValue(kv));
KeyValue next = createKvForType(Type.Put);
// make sure the hint is our attempt at the end key, because we have no more families to seek
assertEquals("Didn't get a hint from a family delete", ReturnCode.SEEK_NEXT_USING_HINT,
filter.filterKeyValue(next));
assertEquals("Didn't get END_KEY with no families to match", KeyValue.LOWESTKEY,
filter.getNextCellHint(next));
// check for a family that comes before our family, so we always seek to the end as well
filter = new ApplyAndFilterDeletesFilter(asSet(Bytes.toBytes("afamily")));
assertEquals(ReturnCode.SKIP, filter.filterKeyValue(kv));
// make sure the hint is our attempt at the end key, because we have no more families to seek
assertEquals("Didn't get a hint from a family delete", ReturnCode.SEEK_NEXT_USING_HINT,
filter.filterKeyValue(next));
assertEquals("Didn't get END_KEY with no families to match", KeyValue.LOWESTKEY,
filter.getNextCellHint(next));
// check that we seek to the correct family that comes after our family
byte[] laterFamily = Bytes.toBytes("zfamily");
filter = new ApplyAndFilterDeletesFilter(asSet(laterFamily));
assertEquals(ReturnCode.SKIP, filter.filterKeyValue(kv));
@SuppressWarnings("deprecation")
KeyValue expected = KeyValue.createFirstOnRow(kv.getRow(), laterFamily, new byte[0]);
assertEquals("Didn't get a hint from a family delete", ReturnCode.SEEK_NEXT_USING_HINT,
filter.filterKeyValue(next));
assertEquals("Didn't get correct next key with a next family", expected,
filter.getNextCellHint(next));
}
@Override public void examine(SkipScanFilter skipper) {
KeyValue kv = KeyValue.createFirstOnRow(rowkey);
skipper.reset();
assertFalse(skipper.filterAllRemaining());
assertFalse(skipper.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength()));
assertEquals(kv.toString(), ReturnCode.INCLUDE, skipper.filterKeyValue(kv));
}
@SuppressWarnings("deprecation")
@Override public void examine(SkipScanFilter skipper) throws IOException {
KeyValue kv = KeyValue.createFirstOnRow(rowkey);
skipper.reset();
assertFalse(skipper.filterAllRemaining());
assertFalse(skipper.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength()));
assertEquals(kv.toString(), ReturnCode.INCLUDE, skipper.filterKeyValue(kv));
}
@Override public void examine(SkipScanFilter skipper) throws IOException {
KeyValue kv = KeyValue.createFirstOnRow(rowkey);
skipper.reset();
assertEquals(ReturnCode.NEXT_ROW,skipper.filterKeyValue(kv));
skipper.reset();
assertTrue(skipper.filterAllRemaining());
}
public KeyValueListScanner(final KeyValue.KVComparator comparator, final KeyValue ... incData) {
this.comparator = comparator;
data = new ArrayList<KeyValue>(incData.length);
for (int i = 0; i < incData.length; ++i) {
data.add(incData[i]);
}
Collections.sort(data, this.comparator);
this.iter = data.iterator();
if (!data.isEmpty()) {
this.current = KeyValue.createFirstOnRow(data.get(0).getRow());
}
}
@Override
public KeyValue getHint(KeyValue peeked) {
// check to see if we have another column to seek
ImmutableBytesPtr nextFamily =
getNextFamily(new ImmutableBytesPtr(peeked.getBuffer(), peeked.getFamilyOffset(),
peeked.getFamilyLength()));
if (nextFamily == null) {
// no known next family, so we can be completely done
done = true;
return KeyValue.LOWESTKEY;
}
// there is a valid family, so we should seek to that
return KeyValue.createFirstOnRow(peeked.getRow(), nextFamily.copyBytesIfNecessary(),
HConstants.EMPTY_BYTE_ARRAY);
}
/**
* We don't expect custom KeyValue creation, so we can't get into weird situations, where a
* {@link Type#DeleteFamily} has a column qualifier specified.
* @throws Exception
*/
@Test
public void testExpectedOrdering() throws Exception {
IndexMemStore store = new IndexMemStore();
KeyValue kv = new KeyValue(row, family, qual, 12, Type.Put, val);
store.add(kv, true);
KeyValue kv2 = new KeyValue(row, family, qual, 10, Type.Put, val2);
store.add(kv2, true);
KeyValue df = new KeyValue(row, family, null, 11, Type.DeleteFamily, null);
store.add(df, true);
KeyValue dc = new KeyValue(row, family, qual, 11, Type.DeleteColumn, null);
store.add(dc, true);
KeyValue d = new KeyValue(row, family, qual, 12, Type.Delete, null);
store.add(d, true);
// null qualifiers should always sort before the non-null cases
KeyValueScanner scanner = store.getScanner();
KeyValue first = KeyValue.createFirstOnRow(row);
assertTrue("Didn't have any data in the scanner", scanner.seek(first));
assertTrue("Didn't get delete family first (no qualifier == sort first)", df == scanner.next());
assertTrue("Didn't get point delete before corresponding put", d == scanner.next());
assertTrue("Didn't get larger ts Put", kv == scanner.next());
assertTrue("Didn't get delete column before corresponding put(delete sorts first)",
dc == scanner.next());
assertTrue("Didn't get smaller ts Put", kv2 == scanner.next());
assertNull("Have more data in the scanner", scanner.next());
}
/**
* Hinting with this filter is a little convoluted as we binary search the list of families to
* attempt to find the right one to seek.
*/
@Test
public void testHintCorrectlyToNextFamily() {
// start with doing a family delete, so we will seek to the next column
KeyValue kv = createKvForType(Type.DeleteFamily);
ApplyAndFilterDeletesFilter filter = new ApplyAndFilterDeletesFilter(EMPTY_SET);
assertEquals(ReturnCode.SKIP, filter.filterKeyValue(kv));
KeyValue next = createKvForType(Type.Put);
// make sure the hint is our attempt at the end key, because we have no more families to seek
assertEquals("Didn't get a hint from a family delete", ReturnCode.SEEK_NEXT_USING_HINT,
filter.filterKeyValue(next));
assertEquals("Didn't get END_KEY with no families to match", KeyValue.LOWESTKEY,
filter.getNextKeyHint(next));
// check for a family that comes before our family, so we always seek to the end as well
filter = new ApplyAndFilterDeletesFilter(asSet(Bytes.toBytes("afamily")));
assertEquals(ReturnCode.SKIP, filter.filterKeyValue(kv));
// make sure the hint is our attempt at the end key, because we have no more families to seek
assertEquals("Didn't get a hint from a family delete", ReturnCode.SEEK_NEXT_USING_HINT,
filter.filterKeyValue(next));
assertEquals("Didn't get END_KEY with no families to match", KeyValue.LOWESTKEY,
filter.getNextKeyHint(next));
// check that we seek to the correct family that comes after our family
byte[] laterFamily = Bytes.toBytes("zfamily");
filter = new ApplyAndFilterDeletesFilter(asSet(laterFamily));
assertEquals(ReturnCode.SKIP, filter.filterKeyValue(kv));
KeyValue expected = KeyValue.createFirstOnRow(kv.getRow(), laterFamily, new byte[0]);
assertEquals("Didn't get a hint from a family delete", ReturnCode.SEEK_NEXT_USING_HINT,
filter.filterKeyValue(next));
assertEquals("Didn't get correct next key with a next family", expected,
filter.getNextKeyHint(next));
}
@Override public void examine(SkipScanFilter skipper) {
KeyValue kv = KeyValue.createFirstOnRow(rowkey);
skipper.reset();
assertFalse(skipper.filterAllRemaining());
assertFalse(skipper.filterRowKey(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength()));
assertEquals(ReturnCode.SEEK_NEXT_USING_HINT, skipper.filterKeyValue(kv));
assertEquals(KeyValue.createFirstOnRow(hint), skipper.getNextKeyHint(kv));
}
public KeyValue getFirstKeyValueForRow(byte[] row) {
return KeyValue.createFirstOnRow(row, family, qualifier == ALL_QUALIFIERS ? null : qualifier);
}
public static KeyValue getSearchTerm(Result r, byte[] family, byte[] qualifier) {
byte[] rbytes = getRawBytes(r);
int roffset = getKeyOffset(r);
int rlength = getKeyLength(r);
return KeyValue.createFirstOnRow(rbytes, roffset, rlength, family, 0, family.length, qualifier, 0, qualifier.length);
}
/**
* Binary search for latest column value without allocating memory in the process
*/
public static KeyValue getColumnLatest(Result r, byte[] row, int roffset, int rlength, byte[] family, int foffset, int flength, byte[] qualifier, int qoffset, int qlength) {
KeyValue searchTerm = KeyValue.createFirstOnRow(row, roffset, rlength, family, foffset, flength, qualifier, qoffset, qlength);
return getColumnLatest(r,searchTerm);
}
public static KeyValue getSearchTerm(Result r, byte[] family, byte[] qualifier) {
byte[] rbytes = getRawBytes(r);
int roffset = getKeyOffset(r);
int rlength = getKeyLength(r);
return KeyValue.createFirstOnRow(rbytes, roffset, rlength, family, 0, family.length, qualifier, 0, qualifier.length);
}
/**
* Binary search for latest column value without allocating memory in the process
*/
public static KeyValue getColumnLatest(Result r, byte[] row, int roffset, int rlength, byte[] family, int foffset, int flength, byte[] qualifier, int qoffset, int qlength) {
KeyValue searchTerm = KeyValue.createFirstOnRow(row, roffset, rlength, family, foffset, flength, qualifier, qoffset, qlength);
return getColumnLatest(r,searchTerm);
}