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

com.scylladb.cdc.cql.BaseMasterCQL Maven / Gradle / Ivy

package com.scylladb.cdc.cql;

import java.nio.ByteBuffer;
import java.util.Date;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.scylladb.cdc.model.GenerationId;
import com.scylladb.cdc.model.StreamId;
import com.scylladb.cdc.model.Timestamp;
import com.scylladb.cdc.model.master.GenerationMetadata;

public abstract class BaseMasterCQL implements MasterCQL {

    protected abstract CompletableFuture> fetchSmallestGenerationAfter(Date after);

    protected abstract CompletableFuture> fetchStreamsForGeneration(Date generationStart);

    @Override
    public CompletableFuture> fetchFirstGenerationId() {
        return fetchSmallestGenerationAfter(new Date(0))
                .thenApply(opt -> opt.map(t -> new GenerationId(new Timestamp(t))));
    }

    private static SortedSet convertStreams(Set input) {
        return input.stream().map(StreamId::new).collect(Collectors.toCollection(() -> new TreeSet<>()));
    }

    @Override
    public CompletableFuture fetchGenerationMetadata(GenerationId id) {
        CompletableFuture> endFut = fetchGenerationEnd(id);
        CompletableFuture> streamsFut = fetchStreamsForGeneration(id.getGenerationStart().toDate());
        return endFut.thenCombine(streamsFut,
                (end, streams) -> new GenerationMetadata(id.getGenerationStart(), end, convertStreams(streams)));
    }

    @Override
    public CompletableFuture> fetchGenerationEnd(GenerationId id) {
        return fetchSmallestGenerationAfter(id.getGenerationStart().toDate()).thenApply(opt -> opt.map(Timestamp::new));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy