下面列出了java.util.concurrent.ConcurrentNavigableMap#isEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void deleteRange(final byte[] startKey, final byte[] endKey, final KVStoreClosure closure) {
final Timer.Context timeCtx = getTimeContext("DELETE_RANGE");
try {
final ConcurrentNavigableMap<byte[], byte[]> subMap = this.defaultDB.subMap(startKey, endKey);
if (!subMap.isEmpty()) {
subMap.clear();
}
setSuccess(closure, Boolean.TRUE);
} catch (final Exception e) {
LOG.error("Fail to [DELETE_RANGE], ['[{}, {})'], {}.", BytesUtil.toHex(startKey), BytesUtil.toHex(endKey),
StackTraceUtil.stackTrace(e));
setCriticalError(closure, "Fail to [DELETE_RANGE]", e);
} finally {
timeCtx.stop();
}
}
/**
* Remove implementation mapping for the given adapter type.
* @param adapterTypeName Adapter type name
* @param implTypeName Implementation type name
*/
public void remove(String adapterTypeName, String implTypeName) {
String key = adapterTypeName;
if (StringUtils.equals(adapterTypeName, implTypeName)) {
modelClasses.remove(key);
}
else {
// although we already use a ConcurrentMap synchronize explicitly because we apply non-atomic operations on it
synchronized (adapterImplementations) {
ConcurrentNavigableMap<String,ModelClass<?>> implementations = adapterImplementations.get(key);
if (implementations != null) {
implementations.remove(implTypeName);
if (implementations.isEmpty()) {
adapterImplementations.remove(key);
}
}
}
}
}
/**
* @param adapterType the type to check
* @return {@code true} in case the given type is a model (may be with a different adapter class)
*/
@SuppressWarnings("unchecked")
public <ModelType> boolean isModelClass(Class<ModelType> adapterType) {
String key = adapterType.getName();
// lookup in cache for models without adapter classes
ModelClass<ModelType> modelClass = (ModelClass<ModelType>)modelClasses.get(key);
if (modelClass!=null) {
return true;
}
// not found? look in cache with adapter classes
ConcurrentNavigableMap<String,ModelClass<?>> implementations = adapterImplementations.get(key);
if (implementations==null || implementations.isEmpty()) {
return false;
}
return true;
}
@Override
public Optional<ServerAddress> resolveByUuid(UUID uuid) {
Assert.notNull(uuid);
if (circle.isEmpty()) {
return Optional.empty();
}
Long hash = hashFunction.newHasher().putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits())
.hash().asLong();
if (!circle.containsKey(hash)) {
ConcurrentNavigableMap<Long, ServerInstance> tailMap = circle.tailMap(hash);
hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
}
ServerInstance result = circle.get(hash);
if (!currentServer.equals(result)) {
return Optional.of(result.getServerAddress());
} else {
return Optional.empty();
}
}
public DeepPageContainer lookup(DeepPageKey key, int skipTo) {
LOG.debug("Looking up DeepPageContainer for skipTo [{0}] with key hashcode [{1}] and key [{2}]", skipTo,
key.hashCode(), key);
DeepPageKeyPlusPosition k = new DeepPageKeyPlusPosition(skipTo, key);
DeepPageContainer deepPageContainer = _lruCache.get(k);
if (deepPageContainer != null) {
_hits.mark();
return deepPageContainer;
}
ConcurrentNavigableMap<DeepPageKeyPlusPosition, DeepPageContainer> headMap = _positionCache.headMap(k, true);
if (headMap == null || headMap.isEmpty()) {
_misses.mark();
return null;
}
Entry<DeepPageKeyPlusPosition, DeepPageContainer> firstEntry = headMap.lastEntry();
DeepPageKeyPlusPosition dpkpp = firstEntry.getKey();
if (dpkpp._deepPageKey.equals(key)) {
_hits.mark();
return firstEntry.getValue();
}
_misses.mark();
return null;
}
@Override
public byte[] jumpOver(final byte[] startKey, final long distance) {
final Timer.Context timeCtx = getTimeContext("JUMP_OVER");
try {
final byte[] realStartKey = BytesUtil.nullToEmpty(startKey);
final ConcurrentNavigableMap<byte[], byte[]> tailMap = this.defaultDB.tailMap(realStartKey);
if (tailMap.isEmpty()) {
return null;
}
long approximateKeys = 0;
byte[] lastKey = null;
for (final byte[] key : tailMap.keySet()) {
lastKey = key;
if (++approximateKeys >= distance) {
break;
}
}
if (lastKey == null) {
return null;
}
final byte[] endKey = new byte[lastKey.length];
System.arraycopy(lastKey, 0, endKey, 0, lastKey.length);
return endKey;
} finally {
timeCtx.stop();
}
}
/**
* Lookup the best-matching implementation for the given adapter type by enquiring the {@link ImplementationPicker} services.
* @param adapterType Adapter type
* @param adaptable Adaptable for reference
* @return Implementation type or null if none detected
*/
@SuppressWarnings("unchecked")
public <ModelType> ModelClass<ModelType> lookup(Class<ModelType> adapterType, Object adaptable) {
String key = adapterType.getName();
// lookup in cache for models without adapter classes
ModelClass<ModelType> modelClass = (ModelClass<ModelType>)modelClasses.get(key);
if (modelClass!=null) {
return modelClass;
}
// not found? look in cache with adapter classes
ConcurrentNavigableMap<String,ModelClass<?>> implementations = adapterImplementations.get(key);
if (implementations==null || implementations.isEmpty()) {
return null;
}
Collection<ModelClass<?>> implementationsCollection = implementations.values();
ModelClass<?>[] implementationWrappersArray = implementationsCollection.toArray(new ModelClass<?>[implementationsCollection.size()]);
// prepare array for implementation picker
Class<?>[] implementationsArray = new Class<?>[implementationsCollection.size()];
for (int i=0; i<implementationWrappersArray.length; i++) {
implementationsArray[i] = implementationWrappersArray[i].getType();
}
for (ImplementationPicker picker : this.sortedImplementationPickers) {
Class<?> implementation = picker.pick(adapterType, implementationsArray, adaptable);
if (implementation != null) {
for (int i=0; i<implementationWrappersArray.length; i++) {
if (implementation==implementationWrappersArray[i].getType()) {
return (ModelClass<ModelType>)implementationWrappersArray[i];
}
}
}
}
return null;
}
/**
* @return Optional list of HRegionLocations for meta replica(s), null if the cache is empty.
*
*/
public Optional<List<HRegionLocation>> getMetaRegionLocations() {
ConcurrentNavigableMap<Integer, HRegionLocation> snapshot =
cachedMetaLocations.tailMap(cachedMetaLocations.firstKey());
if (snapshot.isEmpty()) {
// This could be possible if the master has not successfully initialized yet or meta region
// is stuck in some weird state.
return Optional.empty();
}
List<HRegionLocation> result = new ArrayList<>();
// Explicitly iterate instead of new ArrayList<>(snapshot.values()) because the underlying
// ArrayValueCollection does not implement toArray().
snapshot.values().forEach(location -> result.add(location));
return Optional.of(result);
}