All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ocadotechnology.indexedcache.CachedGroupBy Maven / Gradle / Ivy

There is a newer version: 16.6.21
Show newest version
/*
 * Copyright © 2017-2023 Ocado (Ocava)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocadotechnology.indexedcache;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;

import javax.annotation.CheckForNull;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.ocadotechnology.id.Identified;

public class CachedGroupBy, G, T> extends AbstractIndex {

    private final Multimap cachedGroupValues = LinkedHashMultimap.create();
    private final Map cachedAggregation = new LinkedHashMap<>();

    private final Function groupByExtractor;
    private final Collector collector;

    private final T emptyAggregation;

    private final Set invalidatedGroups = new LinkedHashSet<>();

    CachedGroupBy(Function groupByExtractor, Collector collector) {
        this(null, groupByExtractor, collector);
    }

    CachedGroupBy(@CheckForNull String name, Function groupByExtractor, Collector collector) {
        super(name);
        this.groupByExtractor = groupByExtractor;
        this.collector = collector;
        this.emptyAggregation = Stream.of().collect(collector);
    }

    public T get(G g) {
        return cachedAggregation.getOrDefault(g, emptyAggregation);
    }

    @Override
    protected void remove(C object) {
        G group = groupByExtractor.apply(object);
        cachedGroupValues.remove(group, object);
        invalidatedGroups.add(group);
    }

    @Override
    protected void add(C object) {
        G group = groupByExtractor.apply(object);
        invalidatedGroups.add(group);
        cachedGroupValues.put(group, object);
    }

    @Override
    protected void afterUpdate() {
        invalidatedGroups.forEach(this::updateGroup);
        invalidatedGroups.clear();
    }

    private void updateGroup(G g) {
        Collection groupValues = cachedGroupValues.get(g);

        // Clear up empty groups to avoid a memory leak
        if (groupValues.isEmpty()) {
            cachedAggregation.remove(g);
            return;
        }

        T collect = groupValues
                .stream()
                .collect(collector);
        cachedAggregation.put(g, collect);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy