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

io.activej.ot.reducers.AbstractGraphReducer Maven / Gradle / Ivy

Go to download

Implementation of operational transformation technology. Allows building collaborative software systems.

There is a newer version: 6.0-rc2
Show newest version
/*
 * Copyright (C) 2020 ActiveJ LLC.
 *
 * 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 io.activej.ot.reducers;

import io.activej.ot.OTCommit;
import io.activej.promise.Promise;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static io.activej.ot.reducers.GraphReducer.Result.completePromise;
import static io.activej.ot.reducers.GraphReducer.Result.resumePromise;
import static java.util.Collections.singletonMap;

public abstract class AbstractGraphReducer implements GraphReducer {
	private final DiffsReducer diffsReducer;
	private final Map> accumulators = new HashMap<>();
	private final Map> headCommits = new HashMap<>();

	protected AbstractGraphReducer(DiffsReducer diffsReducer) {
		this.diffsReducer = diffsReducer;
	}

	@Override
	public void onStart(@NotNull Collection> queue) {
		for (OTCommit headCommit : queue) {
			this.headCommits.put(headCommit.getId(), headCommit);
			this.accumulators.put(headCommit.getId(), new HashMap<>(singletonMap(headCommit.getId(), diffsReducer.initialValue())));
		}
	}

	protected abstract @NotNull Promise> tryGetResult(OTCommit commit, Map> accumulators,
			Map> headCommits);

	@Override
	public final @NotNull Promise> onCommit(@NotNull OTCommit commit) {
		//noinspection OptionalGetWithoutIsPresent
		return tryGetResult(commit, accumulators, headCommits)
				.thenIfElse(Optional::isPresent,
						result -> completePromise(result.get()),
						$ -> {
							Map toHeads = accumulators.remove(commit.getId());
							for (K parent : commit.getParents().keySet()) {
								Map parentToHeads = accumulators.computeIfAbsent(parent, $2 -> new HashMap<>());
								for (Map.Entry entry : toHeads.entrySet()) {
									A newAccumulatedDiffs = diffsReducer.accumulate(entry.getValue(), commit.getParents().get(parent));
									A existingAccumulatedDiffs = parentToHeads.get(entry.getKey());
									A combinedAccumulatedDiffs = existingAccumulatedDiffs == null ?
											newAccumulatedDiffs :
											diffsReducer.combine(existingAccumulatedDiffs, newAccumulatedDiffs);
									parentToHeads.put(entry.getKey(), combinedAccumulatedDiffs);
								}
							}
							return resumePromise();
						});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy