static ImmutableSortedMap<Object, Object> instantiate(SerializationStreamReader reader) throws SerializationException { /* * Nothing we can do, but we're already assuming the serialized form is * correctly typed, anyway. */ @SuppressWarnings("unchecked") Comparator<Object> comparator = (Comparator<Object>) reader.readObject(); SortedMap<Object, Object> entries = new TreeMap<>(comparator); Map_CustomFieldSerializerBase.deserialize(reader, entries); return ImmutableSortedMap.orderedBy(comparator).putAll(entries).build(); }
public static <T> ImmutableSortedMap.Builder<byte[], T> newColumnMap() { return ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator()); }
public void testToImmutableSortedMap() { Collector<Entry<String, Integer>, ?, ImmutableSortedMap<String, Integer>> collector = ImmutableSortedMap.toImmutableSortedMap( String.CASE_INSENSITIVE_ORDER, Entry::getKey, Entry::getValue); BiPredicate<ImmutableSortedMap<String, Integer>, ImmutableSortedMap<String, Integer>> equivalence = Equivalence.equals() .onResultOf(ImmutableSortedMap<String, Integer>::comparator) .and(Equivalence.equals().onResultOf(map -> map.entrySet().asList())) .and(Equivalence.equals()); ImmutableSortedMap<String, Integer> expected = ImmutableSortedMap.<String, Integer>orderedBy(String.CASE_INSENSITIVE_ORDER) .put("one", 1) .put("three", 3) .put("two", 2) .build(); CollectorTester.of(collector, equivalence) .expectCollects(expected, mapEntry("one", 1), mapEntry("two", 2), mapEntry("three", 3)); }
public PColumnFamilyImpl(PName name, List<PColumn> columns) { this.name = name; this.columns = ImmutableList.copyOf(columns); ImmutableMap.Builder<String, PColumn> columnByStringBuilder = ImmutableMap.builder(); ImmutableSortedMap.Builder<byte[], PColumn> columnByBytesBuilder = ImmutableSortedMap.orderedBy(Bytes.BYTES_COMPARATOR); for (PColumn column : columns) { columnByBytesBuilder.put(column.getName().getBytes(), column); columnByStringBuilder.put(column.getName().getString(), column); } this.columnByBytes = columnByBytesBuilder.build(); this.columnByString = columnByStringBuilder.build(); }
hashesToBytes.put(rowHash, row); ImmutableSortedMap.Builder<byte[], Value> builder = ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()); rowsToColumns.put(rowHash, builder);
private List<RowResult<Value>> getExpectedResultForRangePagingWithColumnSelectionTest(int numRows, int numColsInSelection, boolean reverse) { List<RowResult<Value>> expected = new ArrayList<>(); for (long row = 1; row <= numRows; ++row) { ImmutableSortedMap.Builder<byte[], Value> builder = ImmutableSortedMap.orderedBy( UnsignedBytes.lexicographicalComparator()); for (long col = 1; col <= row && col <= numColsInSelection; ++col) { byte[] colName = PtBytes.toBytes(Long.MIN_VALUE ^ col); builder.put(colName, Value.create(PtBytes.toBytes(row + "," + col), TEST_TIMESTAMP)); } SortedMap<byte[], Value> columns = builder.build(); if (!columns.isEmpty()) { byte[] rowName = PtBytes.toBytes(Long.MIN_VALUE ^ row); expected.add(RowResult.create(rowName, columns)); } } if (reverse) { return Lists.reverse(expected); } else { return expected; } }
public PColumnFamilyImpl(PName name, List<PColumn> columns) { Preconditions.checkNotNull(name); // Include guidePosts also in estimating the size long estimatedSize = SizedUtil.OBJECT_SIZE + SizedUtil.POINTER_SIZE * 5 + SizedUtil.INT_SIZE + name.getEstimatedSize() + SizedUtil.sizeOfMap(columns.size()) * 2 + SizedUtil.sizeOfArrayList(columns.size()); this.name = name; this.columns = ImmutableList.copyOf(columns); ImmutableMap.Builder<String, PColumn> columnNamesByStringBuilder = ImmutableMap.builder(); ImmutableSortedMap.Builder<byte[], PColumn> columnNamesByBytesBuilder = ImmutableSortedMap.orderedBy(Bytes.BYTES_COMPARATOR); ImmutableSortedMap.Builder<byte[], PColumn> columnsByQualifiersBuilder = ImmutableSortedMap.orderedBy(Bytes.BYTES_COMPARATOR); for (PColumn column : columns) { estimatedSize += column.getEstimatedSize(); columnNamesByBytesBuilder.put(column.getName().getBytes(), column); columnNamesByStringBuilder.put(column.getName().getString(), column); // In certain cases like JOIN, PK columns are assigned a column family. So they // are not evaluated as a PK column. However, their column qualifier bytes are // still null. if (!SchemaUtil.isPKColumn(column) && column.getColumnQualifierBytes() != null) { columnsByQualifiersBuilder.put(column.getColumnQualifierBytes(), column); } } this.columnNamesByBytes = columnNamesByBytesBuilder.build(); this.columnNamesByStrings = columnNamesByStringBuilder.build(); this.columnsByQualifiers = columnsByQualifiersBuilder.build(); this.estimatedSize = (int)estimatedSize; }
@Override protected RowResult<T> computeNext() { while (true) { if (!it.hasNext()) { return endOfData(); } ImmutableSortedMap.Builder<byte[], T> result = ImmutableSortedMap.orderedBy( UnsignedBytes.lexicographicalComparator()); Key key = it.peek().getKey(); byte[] row = key.row; Iterator<Entry<Key, byte[]>> cellIter = takeCell(it, key); collectValueForTimestamp(key.col, cellIter, result, range, resultProducer); while (it.hasNext()) { if (!it.peek().getKey().matchesRow(row)) { break; } key = it.peek().getKey(); cellIter = takeCell(it, key); collectValueForTimestamp(key.col, cellIter, result, range, resultProducer); } SortedMap<byte[], T> columns = result.build(); if (!columns.isEmpty()) { return RowResult.create(row, columns); } } }
private static List<RowResult<Value>> createRowResults(List<RawSqlRow> sqlRows, Map<Long, byte[]> overflowValues, int expectedNumRows) { List<RowResult<Value>> rowResults = new ArrayList<>(expectedNumRows); ImmutableSortedMap.Builder<byte[], Value> currentRowCells = null; byte[] currentRowName = null; for (RawSqlRow sqlRow : sqlRows) { if (currentRowName == null || !Arrays.equals(sqlRow.cell.getRowName(), currentRowName)) { if (currentRowName != null) { rowResults.add(RowResult.create(currentRowName, currentRowCells.build())); } currentRowCells = ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator()); currentRowName = sqlRow.cell.getRowName(); } byte[] value = getValue(sqlRow, overflowValues); currentRowCells.put(sqlRow.cell.getColumnName(), Value.create(value, sqlRow.ts)); } if (currentRowName != null) { rowResults.add(RowResult.create(currentRowName, currentRowCells.build())); } return rowResults; }
public static <T> RowResult<T> of(Cell cell, T value) { return new RowResult<T>(cell.getRowName(), ImmutableSortedMap.<byte[], T>orderedBy(UnsignedBytes.lexicographicalComparator()) .put(cell.getColumnName(), value).build()); }
Set<byte[]> toLoad = Sets.newHashSet(); ImmutableSortedMap.Builder<byte[], RowResult<byte[]>> inCache = ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator()); for (byte[] row : rows) { ImmutableSortedMap.Builder<byte[], byte[]> matches = ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator()); boolean nonEmpty = false; boolean shouldLoad = false;
for (JsonNode rowResult : node.get("data")) { byte[] row = AtlasDeserializers.deserializeRow(metadata.getRowMetadata(), rowResult.get("row")); ImmutableSortedMap.Builder<byte[], byte[]> cols = ImmutableSortedMap.orderedBy( UnsignedBytes.lexicographicalComparator()); if (metadata.getColumns().hasDynamicColumns()) {
public static <T> RowResult<T> merge(RowResult<T> base, RowResult<T> overwrite) { Validate.isTrue(Arrays.equals(base.getRowName(), overwrite.getRowName())); Builder<byte[], T> colBuilder = ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator()); colBuilder.putAll(overwrite.getColumns()); colBuilder.putAll(Maps.difference(base.getColumns(), overwrite.getColumns()).entriesOnlyOnLeft()); return RowResult.create(base.getRowName(), colBuilder.build()); }
private void writeColumns() { Transaction t1 = startTransaction(); int totalPuts = 101; byte[] row = PtBytes.toBytes("row1"); // Record expected results using byte ordering ImmutableSortedMap.Builder<Cell, byte[]> writes = ImmutableSortedMap .orderedBy(Ordering.from(UnsignedBytes.lexicographicalComparator()).onResultOf(Cell::getColumnName)); for (int i = 0; i < totalPuts; i++) { put(t1, "row1", "col" + i, "v" + i); writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("v" + i)); } t1.commit(); } }
.orderedBy(Ordering.from(UnsignedBytes.lexicographicalComparator()).onResultOf(key -> key.getColumnName())); for (int i = 0 ; i < totalPuts ; i++) { put(t, "row1", "col" + i, "v" + i);
@Test public void testReadMyDeletesColumnRangePagingTransaction() { Transaction t = startTransaction(); int totalPuts = 101; byte[] row = PtBytes.toBytes("row1"); // Record expected results using byte ordering ImmutableSortedMap.Builder<Cell, byte[]> writes = ImmutableSortedMap .orderedBy(Ordering.from(UnsignedBytes.lexicographicalComparator()).onResultOf(key -> key.getColumnName())); for (int i = 0 ; i < totalPuts ; i++) { put(t, "row1", "col" + i, "v" + i); if (i % 2 == 0) { writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("v" + i)); } } t.commit(); t = startTransaction(); for (int i = 0 ; i < totalPuts ; i++) { if (i % 2 == 1) { delete(t, "row1", "col" + i); } } Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> columnRange = t.getRowsColumnRange(TEST_TABLE, ImmutableList.of(row), BatchColumnRangeSelection.create(PtBytes.EMPTY_BYTE_ARRAY, PtBytes.EMPTY_BYTE_ARRAY, 1)); List<Map.Entry<Cell, byte[]>> expected = ImmutableList.copyOf(writes.build().entrySet()); verifyMatchingResult(expected, row, columnRange); }
private void doTestGetRangePagingLastRowEdgeCase(int numColumnsInMetadata, int batchSizeHint, boolean reverse) { TableReference tableRef = createTableWithNamedColumns(numColumnsInMetadata); byte[] last = reverse ? RangeRequests.getFirstRowName() : RangeRequests.getLastRowName(); Map<Cell, byte[]> values = ImmutableMap.of( Cell.create(last, PtBytes.toBytes("c1")), PtBytes.toBytes("a"), Cell.create(last, last), PtBytes.toBytes("b")); keyValueService.put(tableRef, values, TEST_TIMESTAMP); RangeRequest request = RangeRequest.builder(reverse).batchHint(batchSizeHint).build(); try (ClosableIterator<RowResult<Value>> iter = keyValueService.getRange(tableRef, request, Long.MAX_VALUE)) { List<RowResult<Value>> results = ImmutableList.copyOf(iter); List<RowResult<Value>> expected = ImmutableList.of( RowResult.create(last, ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()) .put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("a"), TEST_TIMESTAMP)) .put(last, Value.create(PtBytes.toBytes("b"), TEST_TIMESTAMP)) .build())); assertEquals(expected, results); } }
@Test public void testReadMyWritesColumnRangePagingTransaction() { Transaction t = startTransaction(); int totalPuts = 101; byte[] row = PtBytes.toBytes("row1"); // Record expected results using byte ordering ImmutableSortedMap.Builder<Cell, byte[]> writes = ImmutableSortedMap .orderedBy(Ordering.from(UnsignedBytes.lexicographicalComparator()).onResultOf(key -> key.getColumnName())); for (int i = 0 ; i < totalPuts ; i++) { put(t, "row1", "col" + i, "v" + i); if (i % 2 == 0) { writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("v" + i)); } } t.commit(); t = startTransaction(); for (int i = 0 ; i < totalPuts ; i++) { if (i % 2 == 1) { put(t, "row1", "col" + i, "t_v" + i); writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("t_v" + i)); } } Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> columnRange = t.getRowsColumnRange(TEST_TABLE, ImmutableList.of(row), BatchColumnRangeSelection.create(PtBytes.EMPTY_BYTE_ARRAY, PtBytes.EMPTY_BYTE_ARRAY, 1)); List<Map.Entry<Cell, byte[]>> expected = ImmutableList.copyOf(writes.build().entrySet()); verifyMatchingResult(expected, row, columnRange); }
@Test public void testGetRows() { final Set<byte[]> oneRow = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).add(ROW_BYTES).build(); final ColumnSelection oneColumn = ColumnSelection.create(ImmutableList.of(COL_BYTES)); final Set<byte[]> noRows = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).build(); final SortedMap<byte[], RowResult<byte[]>> emptyResults = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR).build(); final RowResult<byte[]> rowResult = RowResult.of(Cell.create(ROW_BYTES, COL_BYTES), VALUE_BYTES); final SortedMap<byte[], RowResult<byte[]>> oneResult = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR) .put(ROW_BYTES, rowResult) .build(); mockery.checking(new Expectations() { { // row result is cached after first call, so second call requests no rows oneOf(txn).getRows(table, oneRow, oneColumn); will(returnValue(oneResult)); oneOf(txn).getRows(table, noRows, oneColumn); will(returnValue(emptyResults)); } }); Assert.assertEquals(oneResult, ct.getRows(table, oneRow, oneColumn)); Assert.assertEquals(oneResult, ct.getRows(table, oneRow, oneColumn)); mockery.assertIsSatisfied(); }
@Test public void testCacheEmptyGets() { final Set<byte[]> oneRow = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).add(ROW_BYTES).build(); final ColumnSelection oneColumn = ColumnSelection.create(ImmutableList.of(COL_BYTES)); final SortedMap<byte[], RowResult<byte[]>> emptyResults = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR).build(); mockery.checking(new Expectations() { { // the cache doesn't actually cache empty results in this case // this is probably an oversight, but this has been the behavior for a long time oneOf(txn).getRows(table, oneRow, oneColumn); will(returnValue(emptyResults)); oneOf(txn).getRows(table, oneRow, oneColumn); will(returnValue(emptyResults)); } }); Assert.assertEquals(emptyResults, ct.getRows(table, oneRow, oneColumn)); Assert.assertEquals(emptyResults, ct.getRows(table, oneRow, oneColumn)); mockery.assertIsSatisfied(); }