Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.arangodb.shaded.vertx.core.shareddata.impl.LocalAsyncMapImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package com.arangodb.shaded.vertx.core.shareddata.impl;
import com.arangodb.shaded.vertx.core.Future;
import com.arangodb.shaded.vertx.core.impl.ContextInternal;
import com.arangodb.shaded.vertx.core.impl.VertxInternal;
import com.arangodb.shaded.vertx.core.shareddata.AsyncMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import static java.util.concurrent.TimeUnit.*;
import static java.util.stream.Collectors.*;
/**
* @author Thomas Segismont
*/
public class LocalAsyncMapImpl implements AsyncMap {
private final VertxInternal vertx;
private final ConcurrentMap> map;
public LocalAsyncMapImpl(VertxInternal vertx) {
this.vertx = vertx;
map = new ConcurrentHashMap<>();
}
@Override
public Future get(K k) {
ContextInternal ctx = vertx.getOrCreateContext();
Holder h = map.get(k);
if (h != null && h.hasNotExpired()) {
return ctx.succeededFuture(h.value);
} else {
return ctx.succeededFuture();
}
}
@Override
public Future put(K k, V v) {
ContextInternal ctx = vertx.getOrCreateContext();
Holder previous = map.put(k, new Holder<>(v));
if (previous != null && previous.expires()) {
vertx.cancelTimer(previous.timerId);
}
return ctx.succeededFuture();
}
@Override
public Future putIfAbsent(K k, V v) {
ContextInternal ctx = vertx.getOrCreateContext();
Holder h = map.putIfAbsent(k, new Holder<>(v));
return ctx.succeededFuture(h == null ? null : h.value);
}
@Override
public Future put(K k, V v, long ttl) {
ContextInternal ctx = vertx.getOrCreateContext();
long timestamp = System.nanoTime();
long timerId = vertx.setTimer(ttl, l -> removeIfExpired(k));
Holder previous = map.put(k, new Holder<>(v, timerId, ttl, timestamp));
if (previous != null && previous.expires()) {
vertx.cancelTimer(previous.timerId);
}
return ctx.succeededFuture();
}
private void removeIfExpired(K k) {
map.computeIfPresent(k, (key, holder) -> holder.hasNotExpired() ? holder : null);
}
@Override
public Future putIfAbsent(K k, V v, long ttl) {
ContextInternal ctx = vertx.getOrCreateContext();
long timestamp = System.nanoTime();
long timerId = vertx.setTimer(ttl, l -> removeIfExpired(k));
Holder existing = map.putIfAbsent(k, new Holder<>(v, timerId, ttl, timestamp));
if (existing != null) {
vertx.cancelTimer(timerId);
return ctx.succeededFuture(existing.value);
} else {
return ctx.succeededFuture();
}
}
@Override
public Future removeIfPresent(K k, V v) {
ContextInternal ctx = vertx.getOrCreateContext();
AtomicBoolean result = new AtomicBoolean();
map.computeIfPresent(k, (key, holder) -> {
if (holder.value.equals(v)) {
result.compareAndSet(false, true);
if (holder.expires()) {
vertx.cancelTimer(holder.timerId);
}
return null;
}
return holder;
});
return ctx.succeededFuture(result.get());
}
@Override
public Future replace(K k, V v) {
ContextInternal ctx = vertx.getOrCreateContext();
Holder previous = map.replace(k, new Holder<>(v));
if (previous != null) {
if (previous.expires()) {
vertx.cancelTimer(previous.timerId);
}
return ctx.succeededFuture(previous.value);
} else {
return ctx.succeededFuture();
}
}
@Override
public Future replace(K k, V v, long ttl) {
ContextInternal ctx = vertx.getOrCreateContext();
long timestamp = System.nanoTime();
long timerId = vertx.setTimer(ttl, l -> removeIfExpired(k));
Holder previous = map.replace(k, new Holder<>(v, timerId, ttl, timestamp));
if (previous != null) {
if (previous.expires()) {
vertx.cancelTimer(previous.timerId);
}
return ctx.succeededFuture(previous.value);
} else {
return ctx.succeededFuture();
}
}
@Override
public Future replaceIfPresent(K k, V oldValue, V newValue) {
ContextInternal ctx = vertx.getOrCreateContext();
Holder h = new Holder<>(newValue);
Holder result = map.computeIfPresent(k, (key, holder) -> {
if (holder.value.equals(oldValue)) {
if (holder.expires()) {
vertx.cancelTimer(holder.timerId);
}
return h;
}
return holder;
});
return ctx.succeededFuture(h == result);
}
@Override
public Future replaceIfPresent(K k, V oldValue, V newValue, long ttl) {
ContextInternal ctx = vertx.getOrCreateContext();
long timestamp = System.nanoTime();
long timerId = vertx.setTimer(ttl, l -> removeIfExpired(k));
Holder h = new Holder<>(newValue, timerId, ttl, timestamp);
Holder result = map.computeIfPresent(k, (key, holder) -> {
if (holder.value.equals(oldValue)) {
if (holder.expires()) {
vertx.cancelTimer(holder.timerId);
}
return h;
}
return holder;
});
if(h == result) {
return ctx.succeededFuture(true);
} else {
vertx.cancelTimer(timerId);
return ctx.succeededFuture(false);
}
}
@Override
public Future clear() {
ContextInternal ctx = vertx.getOrCreateContext();
map.clear();
return ctx.succeededFuture();
}
@Override
public Future size() {
ContextInternal ctx = vertx.getOrCreateContext();
return ctx.succeededFuture(map.size());
}
@Override
public Future> keys() {
ContextInternal ctx = vertx.getOrCreateContext();
return ctx.succeededFuture(new HashSet<>(map.keySet()));
}
@Override
public Future> values() {
ContextInternal ctx = vertx.getOrCreateContext();
List result = map.values().stream()
.filter(Holder::hasNotExpired)
.map(h -> h.value)
.collect(toList());
return ctx.succeededFuture(result);
}
@Override
public Future> entries() {
ContextInternal ctx = vertx.getOrCreateContext();
Map result = new HashMap<>(map.size());
map.forEach((key, holder) -> {
if (holder.hasNotExpired()) {
result.put(key, holder.value);
}
});
return ctx.succeededFuture(result);
}
@Override
public Future remove(K k) {
ContextInternal ctx = vertx.getOrCreateContext();
Holder previous = map.remove(k);
if (previous != null) {
if (previous.expires()) {
vertx.cancelTimer(previous.timerId);
}
return ctx.succeededFuture(previous.value);
} else {
return ctx.succeededFuture();
}
}
private static class Holder {
final V value;
final long timerId;
final long ttl;
final long timestamp;
Holder(V value) {
Objects.requireNonNull(value);
this.value = value;
timestamp = ttl = timerId = 0;
}
Holder(V value, long timerId, long ttl, long timestamp) {
Objects.requireNonNull(value);
if (ttl < 1) {
throw new IllegalArgumentException("ttl must be positive: " + ttl);
}
this.value = value;
this.timerId = timerId;
this.ttl = ttl;
this.timestamp = timestamp;
}
boolean expires() {
return ttl > 0;
}
boolean hasNotExpired() {
return !expires() || MILLISECONDS.convert(System.nanoTime() - timestamp, NANOSECONDS) < ttl;
}
@Override
public String toString() {
return "Holder{" + "value=" + value + ", timerId=" + timerId + ", ttl=" + ttl + ", timestamp=" + timestamp + '}';
}
}
}