io.atomix.utils.time.Versioned Java Examples

The following examples show how to use io.atomix.utils.time.Versioned. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultDocumentTreeService.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public void clear() {
  Queue<DocumentPath> toClearQueue = Queues.newArrayDeque();
  Map<String, Versioned<byte[]>> topLevelChildren = docTree.getChildren(DocumentPath.ROOT);
  toClearQueue.addAll(topLevelChildren.keySet()
      .stream()
      .map(name -> new DocumentPath(name, DocumentPath.ROOT))
      .collect(Collectors.toList()));
  while (!toClearQueue.isEmpty()) {
    DocumentPath path = toClearQueue.remove();
    Map<String, Versioned<byte[]>> children = docTree.getChildren(path);
    if (children.size() == 0) {
      docTree.remove(path);
    } else {
      children.keySet().forEach(name -> toClearQueue.add(new DocumentPath(name, path)));
      toClearQueue.add(path);
    }
  }
}
 
Example #2
Source File: AbstractAtomicMapProxy.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized CompletableFuture<Void> addListener(CollectionEventListener<Versioned<byte[]>> listener, Executor executor) {
  AtomicMapEventListener<K, byte[]> mapListener = event -> {
    switch (event.type()) {
      case INSERT:
        listener.event(new CollectionEvent<>(CollectionEvent.Type.ADD, event.newValue()));
        break;
      case REMOVE:
        listener.event(new CollectionEvent<>(CollectionEvent.Type.REMOVE, event.oldValue()));
        break;
      default:
        break;
    }
  };
  if (eventListeners.putIfAbsent(listener, mapListener) == null) {
    return AbstractAtomicMapProxy.this.addListener(mapListener, executor);
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #3
Source File: AbstractAtomicMapService.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public void clear() {
  Iterator<Map.Entry<K, MapEntryValue>> iterator = entries().entrySet().iterator();
  Map<K, MapEntryValue> entriesToAdd = new HashMap<>();
  while (iterator.hasNext()) {
    Map.Entry<K, MapEntryValue> entry = iterator.next();
    K key = entry.getKey();
    MapEntryValue value = entry.getValue();
    if (!valueIsNull(value)) {
      Versioned<byte[]> removedValue = new Versioned<>(value.value(), value.version());
      publish(new AtomicMapEvent<>(AtomicMapEvent.Type.REMOVE, key, null, removedValue));
      cancelTtl(value);
      if (activeTransactions.isEmpty()) {
        iterator.remove();
      } else {
        entriesToAdd.put(key, new MapEntryValue(MapEntryValue.Type.TOMBSTONE, value.version, null, 0, 0));
      }
    }
  }
  entries().putAll(entriesToAdd);
}
 
Example #4
Source File: DefaultAtomicDocumentTree.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Versioned<V>> getChildren(DocumentPath path) {
  DocumentTreeNode<V> node = getNode(path);
  if (node != null) {
    Map<String, Versioned<V>> childrenValues = Maps.newLinkedHashMap();
    node.children().forEachRemaining(n -> childrenValues.put(simpleName(n.path()), n.value()));
    return childrenValues;
  }
  throw new NoSuchDocumentPathException();
}
 
Example #5
Source File: DefaultDocumentTreeService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public DocumentTreeResult<Map<String, Versioned<byte[]>>> getChildren(DocumentPath path) {
  try {
    return DocumentTreeResult.ok(docTree.getChildren(path));
  } catch (NoSuchDocumentPathException e) {
    return DocumentTreeResult.invalidPath();
  }
}
 
Example #6
Source File: CachedAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value, Duration ttl) {
  return super.putIfAbsent(key, value, ttl)
      .thenApply(result -> {
        cache.put(key, () -> map.get(key));
        return result;
      });
}
 
Example #7
Source File: TranscodingAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Versioned<V1>> getOrDefault(K1 key, V1 defaultValue) {
  try {
    return backingMap.getOrDefault(keyEncoder.apply(key), valueEncoder.apply(defaultValue))
        .thenApply(versionedValueDecoder);
  } catch (Exception e) {
    return Futures.exceptionalFuture(e);
  }
}
 
Example #8
Source File: TranscodingAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Versioned<V1>> putAndGet(K1 key, V1 value, Duration ttl) {
  try {
    return backingMap.putAndGet(keyEncoder.apply(key), valueEncoder.apply(value), ttl)
        .thenApply(versionedValueDecoder);
  } catch (Exception e) {
    return Futures.exceptionalFuture(e);
  }
}
 
Example #9
Source File: DefaultDocumentTreeService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public Versioned<byte[]> get(DocumentPath path) {
  try {
    Versioned<byte[]> value = docTree.get(path);
    return value == null ? null : value.map(node -> node);
  } catch (IllegalStateException e) {
    return null;
  }
}
 
Example #10
Source File: DefaultDocumentTreeService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public DocumentTreeResult<Void> replace(DocumentPath path, byte[] newValue, long version) {
  try {
    Versioned<byte[]> oldValue = docTree.get(path);
    if (docTree.replace(path, newValue, version)) {
      notifyListeners(new DocumentTreeEvent<>(DocumentTreeEvent.Type.UPDATED, path, Optional.of(docTree.get(path)), Optional.of(oldValue)));
      return DocumentTreeResult.ok(null);
    }
    return DocumentTreeResult.NOOP;
  } catch (NoSuchDocumentPathException e) {
    return DocumentTreeResult.invalidPath();
  }
}
 
Example #11
Source File: AbstractAtomicMapService.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Handles a remove commit.
 *
 * @param index     the commit index
 * @param key       the key to remove
 * @param predicate predicate to determine whether to remove the entry
 * @return map entry update result
 */
private MapEntryUpdateResult<K, byte[]> removeIf(long index, K key, Predicate<MapEntryValue> predicate) {
  MapEntryValue value = entries().get(key);

  // Check if there's a pessimistic lock on the key and return a WRITE_LOCK error if the lock does not belong
  // to the requester's session.
  LockContext lock = locks.get(key);
  if (lock != null && lock.isLocked() && !lock.isLockedBy(getCurrentSession().sessionId())) {
    return new MapEntryUpdateResult<>(
        MapEntryUpdateResult.Status.WRITE_LOCK,
        getCurrentIndex(),
        key,
        toVersioned(value));
  }

  // If the value does not exist or doesn't match the predicate, return a PRECONDITION_FAILED error.
  if (valueIsNull(value) || !predicate.test(value)) {
    return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.PRECONDITION_FAILED, index, key, null);
  }

  // If the key has been locked by a transaction, return a WRITE_LOCK error.
  if (preparedKeys.contains(key)) {
    return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.WRITE_LOCK, index, key, null);
  }

  // If no transactions are active, remove the key. Otherwise, replace it with a tombstone.
  if (activeTransactions.isEmpty()) {
    entries().remove(key);
  } else {
    entries().put(key, new MapEntryValue(MapEntryValue.Type.TOMBSTONE, index, null, 0, 0));
  }

  // Cancel the timer if one is scheduled.
  cancelTtl(value);

  Versioned<byte[]> result = toVersioned(value);
  publish(new AtomicMapEvent<>(AtomicMapEvent.Type.REMOVE, key, null, result));
  return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, index, key, result);
}
 
Example #12
Source File: DelegatingAsyncDistributedMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> removeListener(CollectionEventListener<V> listener) {
  CollectionEventListener<Versioned<V>> atomicListener = listenerMap.remove(listener);
  if (atomicListener != null) {
    return values.removeListener(atomicListener);
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #13
Source File: AtomicNavigableMapProxy.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> removeListener(CollectionEventListener<Map.Entry<K, Versioned<byte[]>>> listener) {
  AtomicMapEventListener<K, byte[]> boundedListener = listenerMap.remove(listener);
  if (boundedListener != null) {
    return AtomicNavigableMapProxy.this.removeListener(boundedListener);
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #14
Source File: CachedAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Versioned<V>> replace(K key, V value) {
  return super.replace(key, value)
      .thenApply(result -> {
        cache.put(key, () -> map.get(key));
        return result;
      });
}
 
Example #15
Source File: AbstractAtomicMapProxy.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<Versioned<byte[]>> putIfAbsent(K key, byte[] value, Duration ttl) {
  return getProxyClient().applyOn(getPartition(key), service -> service.putIfAbsent(key, value, ttl.toMillis()))
      .whenComplete((r, e) -> throwIfLocked(r))
      .thenApply(v -> v.result());
}
 
Example #16
Source File: AbstractAtomicMapProxy.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized CompletableFuture<Void> removeListener(CollectionEventListener<Map.Entry<K, Versioned<byte[]>>> listener) {
  AtomicMapEventListener<K, byte[]> mapListener = eventListeners.remove(listener);
  if (mapListener != null) {
    return AbstractAtomicMapProxy.this.removeListener(mapListener);
  }
  return CompletableFuture.completedFuture(null);
}
 
Example #17
Source File: CachedAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Versioned<V>> remove(K key) {
  return super.remove(key)
      .thenApply(result -> {
        cache.remove(key);
        return result;
      });
}
 
Example #18
Source File: NotNullAsyncAtomicMap.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
  if (value == null) {
    return super.remove(key).thenApply(v -> null);
  }
  return super.putAndGet(key, value);
}
 
Example #19
Source File: AbstractAtomicNavigableMapService.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public Map.Entry<K, Versioned<byte[]>> pollFirstEntry() {
  return isEmpty() ? null : toVersionedEntry(entries().pollFirstEntry());
}
 
Example #20
Source File: AbstractAtomicMapProxy.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Versioned<byte[]>> get(K key) {
  return getProxyClient().applyOn(getPartition(key), service -> service.get(key));
}
 
Example #21
Source File: DelegatingAsyncDistributedMultimap.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncDistributedMap<K, Collection<V>> asMap() {
  return new TranscodingAsyncDistributedMap<>(atomicMultimap.asMap(), k -> k, k -> k, v -> new Versioned<>(v, 0), Versioned::valueOrNull);
}
 
Example #22
Source File: BlockingAtomicDocumentTree.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public Versioned<V> remove(DocumentPath path) {
  return complete(backingTree.remove(path));
}
 
Example #23
Source File: AtomicMultimapProxy.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Void> removeListener(MapEventListener<String, Versioned<Collection<byte[]>>> listener) {
  return Futures.exceptionalFuture(new UnsupportedOperationException());
}
 
Example #24
Source File: AtomicMultimapProxy.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Versioned<Collection<byte[]>>> get(String key) {
  return getProxyClient().applyBy(key, service -> service.get(key));
}
 
Example #25
Source File: AtomicDocumentTreeProxy.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Versioned<byte[]>> get(DocumentPath path) {
  checkPath(path);
  return getProxyClient().applyBy(name(), service -> service.get(path));
}
 
Example #26
Source File: AtomicNavigableMapProxy.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public DistributedCollection<Versioned<byte[]>> sync(Duration operationTimeout) {
  return new BlockingDistributedCollection<>(this, operationTimeout.toMillis());
}
 
Example #27
Source File: AbstractAtomicNavigableMapService.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public IteratorBatch<Versioned<byte[]>> subMapIterateValues(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
  return iterate(sessionId -> new AscendingIterator(sessionId, fromKey, fromInclusive, toKey, toInclusive), (k, v) -> v);
}
 
Example #28
Source File: CachedAsyncAtomicMap.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Boolean> contains(Versioned<V> element) {
  return Futures.exceptionalFuture(new UnsupportedOperationException());
}
 
Example #29
Source File: AbstractAtomicMapProxy.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Boolean> prepare(TransactionLog<SetUpdate<Entry<K, Versioned<byte[]>>>> transactionLog) {
  throw new UnsupportedOperationException();
}
 
Example #30
Source File: BlockingAtomicMap.java    From atomix with Apache License 2.0 4 votes vote down vote up
@Override
public DistributedCollection<Versioned<V>> values() {
  return new BlockingDistributedCollection<>(asyncMap.values(), operationTimeoutMillis);
}