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

io.atomix.core.set.impl.TranscodingAsyncDistributedSet Maven / Gradle / Ivy

There is a newer version: 3.2.0-alpha12
Show newest version
/*
 * Copyright 2016-present Open Networking Foundation
 *
 * 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.atomix.core.set.impl;

import io.atomix.core.collection.impl.TranscodingAsyncDistributedCollection;
import io.atomix.core.set.AsyncDistributedSet;
import io.atomix.core.set.DistributedSet;
import io.atomix.core.transaction.TransactionId;
import io.atomix.core.transaction.TransactionLog;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
 * An {@code AsyncDistributedSet} that maps its operations to operations on a
 * differently typed {@code AsyncDistributedSet} by transcoding operation inputs and outputs.
 *
 * @param  key type of other map
 * @param  key type of this map
 */
public class TranscodingAsyncDistributedSet extends TranscodingAsyncDistributedCollection implements AsyncDistributedSet {
  private final AsyncDistributedSet backingSet;
  protected final Function entryEncoder;
  protected final Function entryDecoder;

  public TranscodingAsyncDistributedSet(
      AsyncDistributedSet backingSet,
      Function entryEncoder,
      Function entryDecoder) {
    super(backingSet, entryEncoder, entryDecoder);
    this.backingSet = backingSet;
    this.entryEncoder = e -> e == null ? null : entryEncoder.apply(e);
    this.entryDecoder = e -> e == null ? null : entryDecoder.apply(e);
  }

  @Override
  public CompletableFuture prepare(TransactionLog> transactionLog) {
    return backingSet.prepare(transactionLog.map(record -> record.map(entryEncoder)));
  }

  @Override
  public CompletableFuture commit(TransactionId transactionId) {
    return backingSet.commit(transactionId);
  }

  @Override
  public CompletableFuture rollback(TransactionId transactionId) {
    return backingSet.rollback(transactionId);
  }

  @Override
  public DistributedSet sync(Duration operationTimeout) {
    return new BlockingDistributedSet<>(this, operationTimeout.toMillis());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy