use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedGetRangeColumnLimit.
private MethodSpec renderNamedGetRangeColumnLimit(NamedColumnDescription col) {
Preconditions.checkArgument(tableMetadata.getRowMetadata().getRowParts().size() == 1);
NameComponentDescription rowComponent = tableMetadata.getRowMetadata().getRowParts().get(0);
MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder("getSmallRowRange" + VarName(col)).addModifiers(Modifier.PUBLIC).addJavadoc("Returns a mapping from the first sizeLimit row keys in a rangeRequest to their value\n" + "at column $L (if that column exists). As the $L entries are all loaded in memory,\n" + "do not use for large values of sizeLimit. The order of results is preserved in the map.", VarName(col), VarName(col)).addParameter(RangeRequest.class, "rangeRequest").addParameter(int.class, "sizeLimit").returns(ParameterizedTypeName.get(ClassName.get(LinkedHashMap.class), ClassName.get(rowComponent.getType().getJavaClass()), ClassName.get(getColumnClassForGenericTypeParameter(col))));
getterBuilder.addStatement("$T colSelection =\n" + "$T.create($T.of($T.toCachedBytes($L)))", ColumnSelection.class, ColumnSelection.class, ImmutableList.class, PtBytes.class, ColumnRenderers.short_name(col)).addStatement("rangeRequest = rangeRequest.getBuilder()." + "retainColumns(colSelection).batchHint(sizeLimit).build()").addStatement("$T.checkArgument(rangeRequest.getColumnNames().size() <= 1,\n$S)", Preconditions.class, "Must not request columns other than " + VarName(col) + ".").addCode("\n").addStatement("$T<$T, $T> resultsMap = new $T<>()", LinkedHashMap.class, rowComponent.getType().getJavaClass(), getColumnClassForGenericTypeParameter(col), LinkedHashMap.class).addStatement("$T.of(t.getRange(tableRef, rangeRequest))\n" + ".batchAccept(sizeLimit, batch -> {\n" + " batch.forEach(entry -> {\n" + " $T resultEntry =\n " + " $T.of(entry);\n" + " resultsMap.put(resultEntry.getRowName().get$L(), resultEntry.get$L());\n" + " });\n" + " return false; // stops the traversal after the first batch\n" + "})", BatchingVisitableView.class, rowResultType, rowResultType, CamelCase(rowComponent.getComponentName()), VarName(col)).addStatement("return resultsMap");
return getterBuilder.build();
}
use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedGetSeveralRows.
private MethodSpec renderNamedGetSeveralRows(NamedColumnDescription col) {
Preconditions.checkArgument(tableMetadata.getRowMetadata().getRowParts().size() == 1);
NameComponentDescription rowComponent = tableMetadata.getRowMetadata().getRowParts().get(0);
MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder("get" + VarName(col)).addModifiers(Modifier.PUBLIC).addJavadoc("Returns a mapping from the specified row keys to their value at column $L.\n" + "As the $L values are all loaded in memory, do not use for large amounts of data.\n" + "If the column does not exist for a key, the entry will be omitted from the map.", VarName(col), VarName(col)).addParameter(ParameterizedTypeName.get(ClassName.get(Iterable.class), ClassName.get(rowComponent.getType().getJavaClass())), "rowKeys");
getterBuilder.returns(ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(rowComponent.getType().getJavaClass()), TypeName.get(getColumnClassForGenericTypeParameter(col))));
getterBuilder.addStatement("$T colSelection = \n " + "$T.create($T.of($T.toCachedBytes($S)))", ColumnSelection.class, ColumnSelection.class, ImmutableList.class, PtBytes.class, col.getShortName()).addStatement("$T<$T> rows = $T\n" + ".newArrayList(rowKeys)\n" + ".stream()\n" + ".map($T::of)\n" + ".collect($T.toList())", List.class, rowType, Lists.class, rowType, Collectors.class).addCode("\n").addStatement("$T<byte[], $T<byte[]>> results = " + "t.getRows(tableRef, $T.persistAll(rows), colSelection)", SortedMap.class, RowResult.class, Persistables.class).addStatement("return results\n" + ".values()\n" + ".stream()\n" + ".map(entry -> $T.of(entry))\n" + ".collect($T.toMap(\n" + " entry -> entry.getRowName().get$L(), \n" + " $T::get$L))", rowResultType, Collectors.class, CamelCase(rowComponent.getComponentName()), rowResultType, VarName(col));
return getterBuilder.build();
}
use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedDelete.
private List<MethodSpec> renderNamedDelete() {
List<MethodSpec> deleteResults = new ArrayList<>();
deleteResults.add(renderNamedDeleteRow());
for (NamedColumnDescription col : ColumnRenderers.namedColumns(tableMetadata)) {
deleteResults.add(renderNamedDeleteColumn(col));
}
return deleteResults;
}
use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedDeleteRow.
private MethodSpec renderNamedDeleteRow() {
MethodSpec.Builder deleteRowBuilder = MethodSpec.methodBuilder("deleteRow").addModifiers(Modifier.PUBLIC).addJavadoc("Delete all columns for specified row components.");
deleteRowBuilder = addParametersFromRowComponents(deleteRowBuilder, tableMetadata);
SortedSet<NamedColumnDescription> namedColumns = ColumnRenderers.namedColumns(tableMetadata);
deleteRowBuilder.addStatement("$T row = $T.of($L)", rowType, rowType, getArgumentsFromRowComponents(tableMetadata)).addStatement("byte[] rowBytes = row.persistToBytes()", Persistables.class).addStatement("$T<$T> cells = $T.newHashSetWithExpectedSize($L)", Set.class, Cell.class, Sets.class, namedColumns.size());
for (NamedColumnDescription col : namedColumns) {
deleteRowBuilder.addStatement("cells.add($T.create(rowBytes, $T.toCachedBytes($L)))", Cell.class, PtBytes.class, ColumnRenderers.short_name(col));
}
deleteRowBuilder.addStatement("t.delete(tableRef, cells)");
return deleteRowBuilder.build();
}
use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.
the class TableClassRendererV2 method renderNamedGet.
private List<MethodSpec> renderNamedGet() {
List<MethodSpec> getterResults = new ArrayList<>();
for (NamedColumnDescription col : ColumnRenderers.namedColumns(tableMetadata)) {
getterResults.add(renderNamedGetColumn(col));
if (tableMetadata.getRowMetadata().getRowParts().size() == 1) {
getterResults.add(renderNamedGetSeveralRows(col));
if (tableMetadata.isRangeScanAllowed()) {
getterResults.add(renderNamedGetRangeColumn(col));
getterResults.add(renderNamedGetRangeStartEnd(col));
getterResults.add(renderNamedGetRangeColumnLimit(col));
}
} else {
getterResults.add(renderNamedGetSeveralRowObjects(col));
if (tableMetadata.isRangeScanAllowed()) {
getterResults.add(renderNamedGetRangeColumnRowObjects(col));
getterResults.add(renderNamedGetRangeColumnRowObjectsLimit(col));
}
}
}
return getterResults;
}
Aggregations