Search in sources :

Example 91 with MethodSpec

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();
}
Also used : NameComponentDescription(com.palantir.atlasdb.table.description.NameComponentDescription) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 92 with MethodSpec

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();
}
Also used : NameComponentDescription(com.palantir.atlasdb.table.description.NameComponentDescription) Collectors(java.util.stream.Collectors) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) MethodSpec(com.squareup.javapoet.MethodSpec) Lists(com.google.common.collect.Lists) SortedMap(java.util.SortedMap) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Persistables(com.palantir.common.persist.Persistables)

Example 93 with MethodSpec

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) NamedColumnDescription(com.palantir.atlasdb.table.description.NamedColumnDescription)

Example 94 with MethodSpec

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();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Persistables(com.palantir.common.persist.Persistables) NamedColumnDescription(com.palantir.atlasdb.table.description.NamedColumnDescription)

Example 95 with MethodSpec

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) NamedColumnDescription(com.palantir.atlasdb.table.description.NamedColumnDescription)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)155 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)43 TypeName (com.squareup.javapoet.TypeName)42 ArrayList (java.util.ArrayList)38 ClassName (com.squareup.javapoet.ClassName)34 TypeSpec (com.squareup.javapoet.TypeSpec)31 ParameterSpec (com.squareup.javapoet.ParameterSpec)24 CodeBlock (com.squareup.javapoet.CodeBlock)23 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)20 Nonnull (javax.annotation.Nonnull)17 TypeMirror (javax.lang.model.type.TypeMirror)15 WireField (com.squareup.wire.WireField)14 Field (com.squareup.wire.schema.Field)14 TypeElement (javax.lang.model.element.TypeElement)14 ByteString (okio.ByteString)13 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)12 List (java.util.List)11 VariableElement (javax.lang.model.element.VariableElement)11 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)11 FieldSpec (com.squareup.javapoet.FieldSpec)10