public KeyValue(Cell c) { this(c.getRowArray(), c.getRowOffset(), c.getRowLength(), c.getFamilyArray(), c.getFamilyOffset(), c.getFamilyLength(), c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength(), c.getTimestamp(), Type.codeToType(c.getTypeByte()), c.getValueArray(), c.getValueOffset(), c.getValueLength(), c.getTagsArray(), c.getTagsOffset(), c.getTagsLength()); this.seqId = c.getSequenceId(); }
CellWrapper(Cell cell) { assert !(cell instanceof ExtendedCell); this.cell = cell; this.sequenceId = cell.getSequenceId(); this.timestamp = cell.getTimestamp(); }
public RowColBloomHashKey(Cell cell) { super(cell); rowLength = cell.getRowLength(); // We don't consider the family length for ROWCOL bloom. So subtract the famLen from the // length calculation. Timestamp and type are of no relevance here qualLength = cell.getQualifierLength(); }
@Test public void testSimpleVisibilityLabels() throws Exception { TableName tableName = TableName.valueOf(TEST_NAME.getMethodName()); try (Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "|" + CONFIDENTIAL, PRIVATE + "|" + CONFIDENTIAL)) { Scan s = new Scan(); s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL, PRIVATE)); ResultScanner scanner = table.getScanner(s); Result[] next = scanner.next(3); assertTrue(next.length == 2); CellScanner cellScanner = next[0].cellScanner(); cellScanner.advance(); Cell current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), row1, 0, row1.length)); cellScanner = next[1].cellScanner(); cellScanner.advance(); current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), row2, 0, row2.length)); } }
Put put = new Put(ROWS[i]); put.addColumn(FAMILY, QUALIFIERS[i], VALUE); ht.put(put); Scan scan = new Scan(); scan.setReversed(true); scan.addFamily(FAMILY); Filter filter = new KeyOnlyFilter(true); scan.setFilter(filter); ResultScanner scanner = ht.getScanner(scan); int count = 0; for (Result result : ht.getScanner(scan)) { assertEquals(1, result.size()); assertEquals(Bytes.SIZEOF_INT, result.rawCells()[0].getValueLength()); assertEquals(VALUE.length, Bytes.toInt(CellUtil.cloneValue(result.rawCells()[0]))); count++; scanner.close(); ht.close();
@Test public void testBasics() throws IOException { final TableName tableName = TableName.valueOf(name.getMethodName()); final byte [][] fs = new byte[][] {Bytes.toBytes("cf1"), Bytes.toBytes("cf2"), Bytes.toBytes("cf3") }; Table ht = TEST_UTIL.createTable(tableName, fs); p.addColumn(f, f, f); ht.put(p); Result r = ht.get(new Get(row)); int i = 0; for (CellScanner cellScanner = r.cellScanner(); cellScanner.advance();) { Cell cell = cellScanner.current(); byte [] f = fs[i++]; assertTrue(Bytes.toString(f), Bytes.equals(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), f, 0, f.length)); Get get = new Get(row); get.addFamily(f); r = ht.get(get); assertTrue(r.toString(), r.containsColumn(f, f)); ResultScanner scanner = ht.getScanner(new Scan()); int count = 0; while ((r = scanner.next()) != null) { assertTrue(r.listCells().size() == 3); count++;
/** * The latest seqnum that the server writing to meta observed when opening the region. * E.g. the seqNum when the result of {@link #getServerName(Result, int)} was written. * @param r Result to pull the seqNum from * @return SeqNum, or HConstants.NO_SEQNUM if there's no value written. */ private static long getSeqNumDuringOpen(final Result r, final int replicaId) { Cell cell = r.getColumnLatestCell(getCatalogFamily(), getSeqNumColumn(replicaId)); if (cell == null || cell.getValueLength() == 0) return HConstants.NO_SEQNUM; return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); }
private void verify(final Table table) throws IOException { Scan scan = new Scan(); scan.addColumn(FAMILY_NAME, COLUMN_NAME); scan.setMaxVersions(1); ResultScanner scanner = table.getScanner(scan); for (Result r: scanner) { for (Cell kv : r.listCells()) { log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv)) + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv)) + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv))); org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()), Bytes.toBoolean(CellUtil.cloneValue(kv))); } } scanner.close(); }
CellScanner cellScanner = next[0].cellScanner(); cellScanner.advance(); Cell current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), ROW_1, 0, ROW_1.length)); assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(), current.getQualifierLength(), Q1, 0, Q1.length)); assertTrue(Bytes.equals(current.getValueArray(), current.getValueOffset(), current.getValueLength(), value1, 0, value1.length)); cellScanner.advance(); current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), ROW_1, 0, ROW_1.length)); assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(), current.getQualifierLength(), Q2, 0, Q2.length)); assertTrue(Bytes.equals(current.getValueArray(), current.getValueOffset(), current.getValueLength(), value2, 0, value2.length)); cellScanner.advance(); current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), ROW_1, 0, ROW_1.length)); assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(), current.getQualifierLength(), Q3, 0, Q3.length)); assertTrue(Bytes.equals(current.getValueArray(), current.getValueOffset(), current.getValueLength(), value3, 0, value3.length));
private Result scanAfterBulkLoad(ResultScanner scanner, Result result, String expctedVal) throws IOException { while (result != null) { List<Cell> cells = result.getColumnCells(Bytes.toBytes("col"), Bytes.toBytes("q")); for (Cell _c : cells) { if (Bytes.toString(_c.getRowArray(), _c.getRowOffset(), _c.getRowLength()) .equals("row1")) { System.out .println(Bytes.toString(_c.getRowArray(), _c.getRowOffset(), _c.getRowLength())); System.out.println(Bytes.toString(_c.getQualifierArray(), _c.getQualifierOffset(), _c.getQualifierLength())); System.out.println( Bytes.toString(_c.getValueArray(), _c.getValueOffset(), _c.getValueLength())); Assert.assertEquals(expctedVal, Bytes.toString(_c.getValueArray(), _c.getValueOffset(), _c.getValueLength())); } } result = scanner.next(); } return result; }
@Override public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c, final Put put, final WALEdit edit, final Durability durability) throws IOException { if (put.getAttribute("ttl") != null) { Cell cell = put.getFamilyCellMap().values().stream().findFirst().get().get(0); ttls.put( TableName.valueOf(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())), Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); c.bypass(); } else if (put.getAttribute("versions") != null) { Cell cell = put.getFamilyCellMap().values().stream().findFirst().get().get(0); versions.put( TableName.valueOf(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())), Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); c.bypass(); } }
private void loadRecord(Result r) { Cell[] cells = r.rawCells(); Cell cell = cells[0]; if (Bytes.compareTo(CF_B, 0, CF_B.length, cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()) != 0 // || Bytes.compareTo(COL_B, 0, COL_B.length, cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()) != 0) throw new IllegalStateException(); rec.loadCellBlock(0, ByteBuffer.wrap(cell.getRowArray(), cell.getRowOffset() + ID_LEN, cell.getRowLength() - ID_LEN)); rec.loadCellBlock(1, ByteBuffer.wrap(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); }
@Override public void map(ImmutableBytesWritable key, Result value, Context context) throws IOException { for (Cell cell : value.listCells()) { context.getCounter(TestTableInputFormat.class.getName() + ":row", Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())) .increment(1l); context.getCounter(TestTableInputFormat.class.getName() + ":family", Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength())) .increment(1l); context.getCounter(TestTableInputFormat.class.getName() + ":value", Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())) .increment(1l); } }
private void migrateNamespaceTable() throws IOException { try (Table nsTable = masterServices.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME); ResultScanner scanner = nsTable.getScanner( new Scan().addFamily(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES).readAllVersions()); BufferedMutator mutator = masterServices.getConnection().getBufferedMutator(TableName.META_TABLE_NAME)) { for (Result result;;) { result = scanner.next(); if (result == null) { break; } Put put = new Put(result.getRow()); result .getColumnCells(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES, TableDescriptorBuilder.NAMESPACE_COL_DESC_BYTES) .forEach(c -> put.addColumn(HConstants.NAMESPACE_FAMILY, HConstants.NAMESPACE_COL_DESC_QUALIFIER, c.getTimestamp(), CellUtil.cloneValue(c))); mutator.mutate(put); } } // schedule a disable procedure instead of block waiting here, as when disabling a table we will // wait until master is initialized, but we are part of the initialization... masterServices.getMasterProcedureExecutor().submitProcedure( new DisableTableProcedure(masterServices.getMasterProcedureExecutor().getEnvironment(), TableName.NAMESPACE_TABLE_NAME, false)); }
@Test public void testFirstSeveralCellsFilterAndBatch() throws IOException { Scan scan = new Scan(); scan.setFilter(new FirstSeveralCellsFilter()); scan.setBatch(NUM_COLS); InternalScanner scanner = REGION.getScanner(scan); List<Cell> results = new ArrayList<>(); for (int i = 0; i < NUM_ROWS; i++) { results.clear(); scanner.next(results); assertEquals(NUM_COLS, results.size()); Cell cell = results.get(0); assertArrayEquals(ROWS[i], Bytes.copy(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())); assertArrayEquals(FAMILIES[0], Bytes.copy(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength())); assertArrayEquals(QUALIFIERS[0], Bytes.copy(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())); } assertFalse(scanner.next(results)); scanner.close(); } }
private void verifyRows(Table t, byte[] family, byte[] column) throws IOException { for (int i = 0; i < 10; i++) { byte[] row = Bytes.toBytes("row" + i); Get g = new Get(row).addFamily(family); Result r = t.get(g); Assert.assertNotNull(r); Assert.assertEquals(1, r.size()); Cell cell = r.rawCells()[0]; Assert.assertTrue(CellUtil.matchingQualifier(cell, column)); Assert.assertEquals(Bytes.compareTo(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), row, 0, row.length), 0); } }
@Test public void testIncrementWithCustomTimestamp() throws IOException { TableName TABLENAME = TableName.valueOf(name.getMethodName()); Table table = TEST_UTIL.createTable(TABLENAME, FAMILY); long timestamp = 999; Increment increment = new Increment(ROW); increment.add(CellUtil.createCell(ROW, FAMILY, QUALIFIER, timestamp, KeyValue.Type.Put.getCode(), Bytes.toBytes(100L))); Result r = table.increment(increment); assertEquals(1, r.size()); assertEquals(timestamp, r.rawCells()[0].getTimestamp()); r = table.get(new Get(ROW)); assertEquals(1, r.size()); assertEquals(timestamp, r.rawCells()[0].getTimestamp()); r = table.increment(increment); assertEquals(1, r.size()); assertNotEquals(timestamp, r.rawCells()[0].getTimestamp()); r = table.get(new Get(ROW)); assertEquals(1, r.size()); assertNotEquals(timestamp, r.rawCells()[0].getTimestamp()); }
@Test public void testAppendWithCustomTimestamp() throws IOException { TableName TABLENAME = TableName.valueOf(name.getMethodName()); Table table = TEST_UTIL.createTable(TABLENAME, FAMILY); long timestamp = 999; Append append = new Append(ROW); append.add(CellUtil.createCell(ROW, FAMILY, QUALIFIER, timestamp, KeyValue.Type.Put.getCode(), Bytes.toBytes(100L))); Result r = table.append(append); assertEquals(1, r.size()); assertEquals(timestamp, r.rawCells()[0].getTimestamp()); r = table.get(new Get(ROW)); assertEquals(1, r.size()); assertEquals(timestamp, r.rawCells()[0].getTimestamp()); r = table.append(append); assertEquals(1, r.size()); assertNotEquals(timestamp, r.rawCells()[0].getTimestamp()); r = table.get(new Get(ROW)); assertEquals(1, r.size()); assertNotEquals(timestamp, r.rawCells()[0].getTimestamp()); } }
private static void verifyRow(Result result) throws IOException { byte[] row = result.getRow(); CellScanner scanner = result.cellScanner(); while (scanner.advance()) { Cell cell = scanner.current(); //assert that all Cells in the Result have the same key Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length, cell.getRowArray(), cell.getRowOffset(), cell.getRowLength())); } for (int j = 0; j < FAMILIES.length; j++) { byte[] actual = result.getValue(FAMILIES[j], FAMILIES[j]); Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row) + " ,actual:" + Bytes.toString(actual), row, actual); } }
private int calculateHashForKey(Cell cell) { // pre-calculate the 3 hashes made of byte ranges int rowHash = Bytes.hashCode(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()); int familyHash = Bytes.hashCode(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()); int qualifierHash = Bytes.hashCode(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()); // combine the 6 sub-hashes int hash = 31 * rowHash + familyHash; hash = 31 * hash + qualifierHash; hash = 31 * hash + (int) cell.getTimestamp(); hash = 31 * hash + cell.getTypeByte(); return hash; }