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

io.activej.ot.repository.OTRepository 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.repository;

import io.activej.async.function.AsyncSupplier;
import io.activej.ot.OTCommit;
import io.activej.ot.OTCommitFactory;
import io.activej.promise.Promise;
import io.activej.promise.Promises;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.IntStream;

import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.*;

public interface OTRepository extends OTCommitFactory {
	Promise push(Collection> commits);

	default Promise push(OTCommit commit) {
		return push(singletonList(commit));
	}

	Promise updateHeads(Set newHeads, Set excludedHeads);

	default Promise pushAndUpdateHead(OTCommit commit) {
		return push(commit)
				.then(() -> updateHeads(singleton(commit.getId()), commit.getParentIds()));
	}

	default Promise pushAndUpdateHeads(Collection> commits) {
		Set parents = commits.stream()
				.flatMap(commit -> commit.getParentIds().stream())
				.collect(toSet());
		Set heads = commits.stream()
				.map(OTCommit::getId)
				.filter(commit -> !parents.contains(commit)).collect(toSet());
		return push(commits)
				.then(() -> updateHeads(heads, parents));
	}

	default @NotNull Promise getLevel(@NotNull K commitId) {
		return loadCommit(commitId)
				.map(OTCommit::getLevel);
	}

	default @NotNull Promise> getLevels(@NotNull Set commitIds) {
		ArrayList ids = new ArrayList<>(commitIds);
		return Promises.toList(ids.stream().map(this::getLevel))
				.map(list -> IntStream.range(0, ids.size()).boxed().collect(toMap(ids::get, list::get)));
	}

	default @NotNull Promise> getHeads() {
		return getHeadCommits()
				.map(headCommits -> headCommits.stream().map(OTCommit::getId).collect(toSet()));
	}

	default @NotNull Promise>> getHeadCommits() {
		return getAllHeadCommits()
				.map(allHeadCommits -> {
					int maxEpoch = allHeadCommits.stream().mapToInt(OTCommit::getEpoch).max().orElse(0);
					return allHeadCommits.stream().filter(commit -> commit.getEpoch() == maxEpoch).collect(toList());
				});
	}

	@NotNull Promise> getAllHeads();

	default @NotNull Promise>> getAllHeadCommits() {
		return getAllHeads()
				.then(allHeads -> Promises.toList(allHeads.stream().map(this::loadCommit)));
	}

	default @NotNull AsyncSupplier> pollHeads() {
		return this::getHeads;
	}

	@NotNull Promise hasCommit(@NotNull K revisionId);

	@NotNull Promise> loadCommit(@NotNull K revisionId);

	default @NotNull Promise hasSnapshot(@NotNull K revisionId) {
		return loadSnapshot(revisionId).map(Optional::isPresent);
	}

	@NotNull Promise>> loadSnapshot(@NotNull K revisionId);

	@NotNull Promise saveSnapshot(@NotNull K revisionId, @NotNull List diffs);

	default Promise cleanup(K revisionId) {
		throw new UnsupportedOperationException();
	}

	default Promise backup(OTCommit commit, List snapshot) {
		throw new UnsupportedOperationException();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy