use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableSet in project bazel by bazelbuild.
the class RecursiveFilesystemTraversalFunctionTest method setUp.
@Before
public final void setUp() throws Exception {
AnalysisMock analysisMock = AnalysisMock.get();
pkgLocator = new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory)));
AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
BlazeDirectories directories = new BlazeDirectories(rootDirectory, outputBase, rootDirectory, analysisMock.getProductName());
ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories);
ConfiguredRuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider();
Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction(new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper));
skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator));
skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction());
skyFunctions.put(SkyFunctions.DIRECTORY_LISTING_STATE, new DirectoryListingStateFunction(externalFilesHelper));
skyFunctions.put(SkyFunctions.RECURSIVE_FILESYSTEM_TRAVERSAL, new RecursiveFilesystemTraversalFunction());
skyFunctions.put(SkyFunctions.PACKAGE_LOOKUP, new PackageLookupFunction(deletedPackages, CrossRepositoryLabelViolationStrategy.ERROR, ImmutableList.of(BuildFileName.BUILD_DOT_BAZEL, BuildFileName.BUILD)));
skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES, new BlacklistedPackagePrefixesFunction());
skyFunctions.put(SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null));
skyFunctions.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider));
skyFunctions.put(SkyFunctions.WORKSPACE_FILE, new WorkspaceFileFunction(ruleClassProvider, analysisMock.getPackageFactoryForTesting().create(ruleClassProvider, scratch.getFileSystem()), directories));
skyFunctions.put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction());
skyFunctions.put(SkyFunctions.LOCAL_REPOSITORY_LOOKUP, new LocalRepositoryLookupFunction());
progressReceiver = new RecordingEvaluationProgressReceiver();
differencer = new RecordingDifferencer();
evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer, progressReceiver);
driver = new SequentialBuildDriver(evaluator);
PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.set(differencer, PathFragment.EMPTY_FRAGMENT);
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableSet in project bazel by bazelbuild.
the class ProtobufSupport method getInputsToOutputsMap.
private ImmutableSetMultimap<ImmutableSet<Artifact>, Artifact> getInputsToOutputsMap() {
Iterable<ObjcProtoProvider> objcProtoProviders = getObjcProtoProviders();
Iterable<ProtoSourcesProvider> protoProviders = getProtoSourcesProviders();
ImmutableList.Builder<NestedSet<Artifact>> protoSets = new ImmutableList.Builder<NestedSet<Artifact>>();
// all the transitive groups of proto.
for (ObjcProtoProvider objcProtoProvider : objcProtoProviders) {
protoSets.addAll(objcProtoProvider.getProtoGroups());
}
for (ProtoSourcesProvider protoProvider : protoProviders) {
protoSets.add(protoProvider.getTransitiveProtoSources());
}
HashMap<Artifact, Set<Artifact>> protoToGroupMap = new HashMap<>();
// group will be considered the smallest input group with which the proto can be generated.
for (NestedSet<Artifact> nestedProtoSet : protoSets.build()) {
ImmutableSet<Artifact> protoSet = ImmutableSet.copyOf(nestedProtoSet.toSet());
for (Artifact proto : protoSet) {
// generated with the runtime library.
if (attributes.isProtoWellKnown(proto)) {
continue;
}
if (!protoToGroupMap.containsKey(proto)) {
protoToGroupMap.put(proto, protoSet);
} else {
protoToGroupMap.put(proto, Sets.intersection(protoSet, protoToGroupMap.get(proto)));
}
}
}
// Now that we have the smallest proto inputs groups for each proto to be generated, we reverse
// that map into a multimap to take advantage of the fact that multiple protos can be generated
// with the same inputs, to avoid having multiple generation actions with the same inputs and
// different ouputs. This only applies for the generation actions, as the compilation actions
// compile one generated file at a time.
// It's OK to use ImmutableSet<Artifact> as the key, since Artifact caches it's hashCode, and
// ImmutableSet calculates it's hashCode in O(n).
ImmutableSetMultimap.Builder<ImmutableSet<Artifact>, Artifact> inputsToOutputsMapBuilder = ImmutableSetMultimap.builder();
for (Artifact proto : protoToGroupMap.keySet()) {
inputsToOutputsMapBuilder.put(ImmutableSet.copyOf(protoToGroupMap.get(proto)), proto);
}
return inputsToOutputsMapBuilder.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableSet in project zipkin by openzipkin.
the class Indexer method index.
ImmutableSet<ListenableFuture<?>> index(List<Span> spans) {
// First parse each span into partition keys used to support query requests
Builder<PartitionKeyToTraceId, Long> parsed = ImmutableSetMultimap.builder();
for (Span span : spans) {
Long timestamp = guessTimestamp(span);
if (timestamp == null)
continue;
for (String partitionKey : index.partitionKeys(span)) {
parsed.put(new PartitionKeyToTraceId(index.table(), partitionKey, span.traceId), // index precision is millis
1000 * (timestamp / 1000));
}
}
// The parsed results may include inserts that already occur, or are redundant as they don't
// impact QueryRequest.endTs or QueryRequest.loopback. For example, a parsed timestamp could
// be between timestamps of rows that already exist for a particular trace.
ImmutableSetMultimap<PartitionKeyToTraceId, Long> maybeInsert = parsed.build();
ImmutableSetMultimap<PartitionKeyToTraceId, Long> toInsert;
if (sharedState == null) {
// special-case when caching is disabled.
toInsert = maybeInsert;
} else {
// Optimized results will be smaller when the input includes traces with local spans, or when
// other threads indexed the same trace.
toInsert = entriesThatIncreaseGap(sharedState, maybeInsert);
if (maybeInsert.size() > toInsert.size() && LOG.isDebugEnabled()) {
int delta = maybeInsert.size() - toInsert.size();
LOG.debug("optimized out {}/{} inserts into {}", delta, maybeInsert.size(), index.table());
}
}
// For each entry, insert a new row in the index table asynchronously
ImmutableSet.Builder<ListenableFuture<?>> result = ImmutableSet.builder();
for (Map.Entry<PartitionKeyToTraceId, Long> entry : toInsert.entries()) {
BoundStatement bound = bindWithName(prepared, boundName).setLong("trace_id", entry.getKey().traceId).setBytesUnsafe("ts", timestampCodec.serialize(entry.getValue()));
if (indexTtl != null) {
bound.setInt("ttl_", indexTtl);
}
index.bindPartitionKey(bound, entry.getKey().partitionKey);
result.add(session.executeAsync(bound));
}
return result.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableSet in project presto by prestodb.
the class InCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
BytecodeNode value = generatorContext.generate(arguments.get(0));
List<RowExpression> values = arguments.subList(1, arguments.size());
ImmutableList.Builder<BytecodeNode> valuesBytecode = ImmutableList.builder();
for (int i = 1; i < arguments.size(); i++) {
BytecodeNode testNode = generatorContext.generate(arguments.get(i));
valuesBytecode.add(testNode);
}
Type type = arguments.get(0).getType();
Class<?> javaType = type.getJavaType();
SwitchGenerationCase switchGenerationCase = checkSwitchGenerationCase(type, values);
Signature hashCodeSignature = internalOperator(HASH_CODE, BIGINT, ImmutableList.of(type));
MethodHandle hashCodeFunction = generatorContext.getRegistry().getScalarFunctionImplementation(hashCodeSignature).getMethodHandle();
ImmutableListMultimap.Builder<Integer, BytecodeNode> hashBucketsBuilder = ImmutableListMultimap.builder();
ImmutableList.Builder<BytecodeNode> defaultBucket = ImmutableList.builder();
ImmutableSet.Builder<Object> constantValuesBuilder = ImmutableSet.builder();
for (RowExpression testValue : values) {
BytecodeNode testBytecode = generatorContext.generate(testValue);
if (testValue instanceof ConstantExpression && ((ConstantExpression) testValue).getValue() != null) {
ConstantExpression constant = (ConstantExpression) testValue;
Object object = constant.getValue();
switch(switchGenerationCase) {
case DIRECT_SWITCH:
case SET_CONTAINS:
constantValuesBuilder.add(object);
break;
case HASH_SWITCH:
try {
int hashCode = toIntExact(Long.hashCode((Long) hashCodeFunction.invoke(object)));
hashBucketsBuilder.put(hashCode, testBytecode);
} catch (Throwable throwable) {
throw new IllegalArgumentException("Error processing IN statement: error calculating hash code for " + object, throwable);
}
break;
default:
throw new IllegalArgumentException("Not supported switch generation case: " + switchGenerationCase);
}
} else {
defaultBucket.add(testBytecode);
}
}
ImmutableListMultimap<Integer, BytecodeNode> hashBuckets = hashBucketsBuilder.build();
ImmutableSet<Object> constantValues = constantValuesBuilder.build();
LabelNode end = new LabelNode("end");
LabelNode match = new LabelNode("match");
LabelNode noMatch = new LabelNode("noMatch");
LabelNode defaultLabel = new LabelNode("default");
Scope scope = generatorContext.getScope();
BytecodeNode switchBlock;
BytecodeBlock switchCaseBlocks = new BytecodeBlock();
LookupSwitch.LookupSwitchBuilder switchBuilder = lookupSwitchBuilder();
switch(switchGenerationCase) {
case DIRECT_SWITCH:
// For these types, it's safe to not use presto HASH_CODE and EQUAL operator.
for (Object constantValue : constantValues) {
switchBuilder.addCase(toIntExact((Long) constantValue), match);
}
switchBuilder.defaultCase(defaultLabel);
switchBlock = new BytecodeBlock().comment("lookupSwitch(<stackValue>))").dup(javaType).append(new IfStatement().condition(new BytecodeBlock().dup(javaType).invokeStatic(InCodeGenerator.class, "isInteger", boolean.class, long.class)).ifFalse(new BytecodeBlock().pop(javaType).gotoLabel(defaultLabel))).longToInt().append(switchBuilder.build());
break;
case HASH_SWITCH:
for (Map.Entry<Integer, Collection<BytecodeNode>> bucket : hashBuckets.asMap().entrySet()) {
LabelNode label = new LabelNode("inHash" + bucket.getKey());
switchBuilder.addCase(bucket.getKey(), label);
Collection<BytecodeNode> testValues = bucket.getValue();
BytecodeBlock caseBlock = buildInCase(generatorContext, scope, type, label, match, defaultLabel, testValues, false);
switchCaseBlocks.append(caseBlock.setDescription("case " + bucket.getKey()));
}
switchBuilder.defaultCase(defaultLabel);
Binding hashCodeBinding = generatorContext.getCallSiteBinder().bind(hashCodeFunction);
switchBlock = new BytecodeBlock().comment("lookupSwitch(hashCode(<stackValue>))").dup(javaType).append(invoke(hashCodeBinding, hashCodeSignature)).invokeStatic(Long.class, "hashCode", int.class, long.class).append(switchBuilder.build()).append(switchCaseBlocks);
break;
case SET_CONTAINS:
Set<?> constantValuesSet = toFastutilHashSet(constantValues, type, registry);
Binding constant = generatorContext.getCallSiteBinder().bind(constantValuesSet, constantValuesSet.getClass());
switchBlock = new BytecodeBlock().comment("inListSet.contains(<stackValue>)").append(new IfStatement().condition(new BytecodeBlock().comment("value").dup(javaType).comment("set").append(loadConstant(constant)).invokeStatic(FastutilSetHelper.class, "in", boolean.class, javaType.isPrimitive() ? javaType : Object.class, constantValuesSet.getClass())).ifTrue(jump(match)));
break;
default:
throw new IllegalArgumentException("Not supported switch generation case: " + switchGenerationCase);
}
BytecodeBlock defaultCaseBlock = buildInCase(generatorContext, scope, type, defaultLabel, match, noMatch, defaultBucket.build(), true).setDescription("default");
BytecodeBlock block = new BytecodeBlock().comment("IN").append(value).append(ifWasNullPopAndGoto(scope, end, boolean.class, javaType)).append(switchBlock).append(defaultCaseBlock);
BytecodeBlock matchBlock = new BytecodeBlock().setDescription("match").visitLabel(match).pop(javaType).append(generatorContext.wasNull().set(constantFalse())).push(true).gotoLabel(end);
block.append(matchBlock);
BytecodeBlock noMatchBlock = new BytecodeBlock().setDescription("noMatch").visitLabel(noMatch).pop(javaType).push(false).gotoLabel(end);
block.append(noMatchBlock);
block.visitLabel(end);
return block;
}
use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableSet in project j2objc by google.
the class UnusedCodeTracker method markUsedElements.
/**
* Add to root set, methods from CodeReferenceMap and also all public methods in input classes.
* Then, do tree shaker traversal starting from this root set.
* @param publicRootSet: CodeReferenceMap with public root methods and classes.
*/
//TODO(malvania): Current paradigm: All methods in input CodeReferenceMap are assumed to be
// public roots to traverse from.
//Classes in input CodeReferenceMap here allow user to add Dynamically Loaded Classes and keep
// their public methods in the public root set.
//In the future, when we add support for libraries, we will want to include protected methods
// of those library classes as well, so we should add "|| ElementUtil.isProtected(method)" after
// the isPublic check.
public void markUsedElements(CodeReferenceMap publicRootSet) {
if (publicRootSet == null) {
markUsedElements();
return;
}
//Add all public methods in publicRootClasses to root set
for (String clazz : publicRootSet.getReferencedClasses()) {
ClassReferenceNode classNode = (ClassReferenceNode) elementReferenceMap.get(ElementReferenceMapper.stitchClassIdentifier(clazz));
assert (classNode != null);
Iterable<ExecutableElement> methods = ElementUtil.getMethods(classNode.classElement);
for (ExecutableElement method : methods) {
if (ElementUtil.isPublic(method)) {
rootSet.add(ElementReferenceMapper.stitchMethodIdentifier(method, env.typeUtil(), env.elementUtil()));
}
}
}
//Add input root methods to static set
for (Table.Cell<String, String, ImmutableSet<String>> cell : publicRootSet.getReferencedMethods().cellSet()) {
String clazzName = cell.getRowKey();
String methodName = cell.getColumnKey();
for (String signature : cell.getValue()) {
rootSet.add(ElementReferenceMapper.stitchMethodIdentifier(clazzName, methodName, signature));
}
}
markUsedElements(staticSet);
markUsedElements(rootSet);
}
Aggregations