@Override public void update(CachingGroupData value) { if (!initialized) { // This only needs to happen once per summary (i.e. once per group). data = value.partialCopy(); initialized = true; } data.consume(value.getCachedRecord()); }
@SuppressWarnings("unchecked") @Override public GroupDataSummary copy() { GroupDataSummary copy = new GroupDataSummary(); copy.initialized = initialized; copy.data = CachingGroupData.copy(data); return copy; }
/** * Creates a full copy of another {@link GroupData}. * * @param other The other GroupData to copy. If not-null, must have groups and metrics. * @return A {@link CachingGroupData} copy of the GroupData or null if it was null. */ public static CachingGroupData copy(GroupData other) { return other != null ? new CachingGroupData(copy(other.groupFields), copy(other.metrics)) : null; }
/** * Returns the name of the result field to use for the given {@link GroupOperation}. If the operation * does specify a newName, it will be returned. Otherwise, a composite name containing the type of the * operation as well as the field name will be used (if provided). * * @param operation The operation to get the name for. * @return a String representing a name for the result of the operation. */ public static String getResultName(GroupOperation operation) { String name = operation.getNewName(); if (name != null) { return name; } GroupOperation.GroupOperationType type = operation.getType(); String field = operation.getField(); if (field == null) { return type.getName(); } return type.getName() + NAME_SEPARATOR + operation.getField(); }
/** * Constructor that requires an {@link Aggregation}. * * @param aggregation The {@link Aggregation} that specifies how and what this will compute. * @param config The BulletConfig. */ public GroupAll(Aggregation aggregation, BulletConfig config) { // GroupOperations is all we care about - size etc. are meaningless for Group All since it's a single result operations = GroupOperation.getOperations(aggregation.getAttributes()); data = new GroupData(operations); this.provider = config.getBulletRecordProvider(); }
@Override public void reset() { data = new GroupData(operations); } }
@Override public void consume(BulletRecord data) { this.data.consume(data); }
private void mergeInPlace(GroupDataSummary other) { if (other == null) { return; } // This check is unnecessary since all merges will have valid (or at least non empty data) from the very fact // that they were created (see update above). GroupData targetData = other.getData(); if (targetData == null) { return; } // In-place, so not copying targetData if (data == null) { data = targetData; } else { data.combine(targetData); } }
@Override public GroupDataSummary union(GroupDataSummary a, GroupDataSummary b) { return GroupDataSummary.mergeInPlace(a, b); }
/** * Constructor that initializes the GroupData with a {@link Set} of {@link GroupOperation} and a {@link Map} of * Strings that represent the group fields. * * @param groupFields The mappings of field names to their values that represent this group. * @param operations the non-null operations that this will compute metrics for. */ public GroupData(Map<String, String> groupFields, Set<GroupOperation> operations) { this.groupFields = groupFields; this.metrics = makeInitialMetrics(operations); }
@Override public Optional<List<BulletError>> initialize() { return GroupOperation.checkOperations(operations); }
@Override public Summary newSummary() { return new GroupDataSummary(); }
@Override public DeserializeResult summaryFromMemory(Memory serializedSummary) { return GroupDataSummary.fromMemory(serializedSummary); }
@Override public void combine(byte[] data) { this.data.combine(data); }
@Override public int hashCode() { // Not relying on Enum hashcode String typeString = type == null ? null : type.getName(); return Objects.hash(typeString, field); }
/** * Gets the metrics and the group values stored as a {@link BulletRecord}. * * @param provider A BulletRecordProvider to generate BulletRecords. * @return A non-null {@link BulletRecord} containing the data stored in this object. */ public BulletRecord getAsBulletRecord(BulletRecordProvider provider) { return getAsBulletRecord(Collections.emptyMap(), provider); }
/** * Get the {@link Type} of this FunctionCall. * * @param clazz {@link Type} class. * @return The {@link Type}. */ public Type getType(Class<SelectItem.Type> clazz) { if (type.toString().equalsIgnoreCase("count") && distinct) { return Type.COUNT_DISTINCT; } return Type.GROUP; }
/** * Needed to deserialize an instance of this {@link GroupDataSummaryFactory} from a {@link Memory}. * * @param summaryFactory The serialized summary factory. * @return A {@link DeserializeResult} representing the deserialized summary factory. */ public static DeserializeResult<GroupDataSummaryFactory> fromMemory(Memory summaryFactory) { // This has no state so it does not use the Memory return new DeserializeResult<>(new GroupDataSummaryFactory(), SERIALIZED_SIZE); } }
/** * Creates an partial copy of itself. Only the metrics are copied, not the group. * * @return A copied {@link CachingGroupData}. */ public CachingGroupData partialCopy() { return new CachingGroupData(groupFields, copy(metrics)); }
/** * This method merges two {@link GroupDataSummary} into each other. It picks the first non-null parameter to * merge into and returns that after merge. * * @param a The first {@link GroupDataSummary} to merge. * @param b The second {@link GroupDataSummary} to merge. * @return The resulting merged summary or null if both arguments were null. */ public static GroupDataSummary mergeInPlace(GroupDataSummary a, GroupDataSummary b) { if (a != null) { a.mergeInPlace(b); return a; } else if (b != null) { return b; } return null; }