@Override public Object get(String componentName, String key) { if (!data.containsKey(componentName)) { return null; } return data.get(componentName).get(key); }
@Override public void put(String componentName, String key, Object value) { Map<String, Object> componentData = data.get(componentName); if (null == componentData) { data.putIfAbsent(componentName, new ConcurrentHashMap<String, Object>()); componentData = data.get(componentName); } componentData.put(key, value); }
@Override public void remove(String componentName, String key) { if (!data.containsKey(componentName)) { return; } data.get(componentName).remove(key); }
static Semaphore getLock(ConcurrentMap<String, Semaphore> locks2, String key) { Semaphore lock = locks2.get(key); if (lock == null) { Semaphore newLock = new Semaphore(1, true); locks2.get(null); locks2.put(null, null); locks2.remove(null); locks2.containsKey(null); locks2.containsValue(null); locks2.putIfAbsent(null, null); locks2.remove(null, null); locks2.replace(null, null); locks2.replace(null, null, null); lock = locks2.putIfAbsent(key, lock); // value, being null, will *always* throw NullPointerException if (lock == null) lock = newLock; } return lock; } // static Semaphore getLock(ConcurrentMap<String, Semaphore> locks2,
private void addJob(Object jobId, StandardScannerExecutor executor) { for (Map.Entry<Object,StandardScannerExecutor> jobs : runningJobs.entrySet()) { StandardScannerExecutor exe = jobs.getValue(); if (exe.isDone() || exe.isCancelled()) { runningJobs.remove(jobs.getKey(),exe); } } runningJobs.putIfAbsent(jobId,executor); Preconditions.checkArgument(runningJobs.get(jobId)==executor,"Another job with the same id is already running: %s",jobId); }
/** * Hold in resource manager channels. * * @param resourceId the resource id * @param clientPort the client port */ public void holdInResourceManagerChannels(String resourceId, Integer clientPort) { if (null == this.clientRMHolderMap) { this.clientRMHolderMap = new ConcurrentHashMap<String, ConcurrentMap<Integer, RpcContext>>(); } clientRMHolderMap.putIfAbsent(resourceId, new ConcurrentHashMap<Integer, RpcContext>()); ConcurrentMap<Integer, RpcContext> portMap = clientRMHolderMap.get(resourceId); portMap.put(clientPort, this); }
private Resolver<?, ?, ?> getResolver(Class<?> cls, Class<? extends Resolver> resolverClass, Annotation anno) { if (!providerCache.containsKey(resolverClass)) { try { providerCache.putIfAbsent(resolverClass, resolverClass.newInstance()); } catch (Exception ex) { throw new RuntimeException(ex); } } return providerCache.get(resolverClass); }
@Override public EntityGraph getOrCreateEmptyEntityGraph(final Class<?> entityClass, final boolean forWriter) { final ConcurrentMap<Class<?>, EntityGraph> classToGraph = forWriter ? writerClassToGraph : readerClassToGraph; if (!classToGraph.containsKey(entityClass) || !(classToGraph.get(entityClass) instanceof EmptyEntityGraphImpl)) { classToGraph.put(entityClass, new EmptyEntityGraphImpl(entityClass)); } return classToGraph.get(entityClass); }
/** * Create a new {@code Message.Builder} instance for the given class. * <p>This method uses a ConcurrentHashMap for caching method lookups. */ private static Message.Builder getMessageBuilder(Class<?> clazz) throws Exception { Method method = methodCache.get(clazz); if (method == null) { method = clazz.getMethod("newBuilder"); methodCache.put(clazz, method); } return (Message.Builder) method.invoke(clazz); }
public synchronized boolean remove(K k, K v) { List<V> list = cache.get(k); if (list == null) { return false; } if (list.isEmpty()) { cache.remove(k); return false; } boolean removed = list.remove(v); if (removed) { if (list.isEmpty()) { cache.remove(k); } else { list = new ArrayList<V>(list); cache.put(k, list); } } return removed; }
@Override public void removeChildListener(String path, ChildListener listener) { ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.get(path); if (listeners != null) { TargetChildListener targetListener = listeners.remove(listener); if (targetListener != null) { removeTargetChildListener(path, targetListener); } } }
@Override protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName(); ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key); if (map == null) { methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<String, WeightedRoundRobin>()); map = methodWeightMap.get(key); for (Invoker<T> invoker : invokers) { String identifyString = invoker.getUrl().toIdentityString(); WeightedRoundRobin weightedRoundRobin = map.get(identifyString); int weight = getWeight(invoker, invocation); weightedRoundRobin = new WeightedRoundRobin(); weightedRoundRobin.setWeight(weight); map.putIfAbsent(identifyString, weightedRoundRobin); methodWeightMap.put(key, newMap); } finally { updateLock.set(false);
private void createColumnFamily(final String name) throws RocksDBException { COLUMN_FAMILY_LOCKS.putIfAbsent(name, new ReentrantLock()); final Lock l = COLUMN_FAMILY_LOCKS.get(name); l.lock(); try { if(!COLUMN_FAMILIES.containsKey(name)) { final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions().optimizeLevelStyleCompaction(); final ColumnFamilyHandle cfHandle = rocksDb.createColumnFamily( new ColumnFamilyDescriptor(name.getBytes(UTF_8), cfOptions) ); COLUMN_FAMILIES.put(name, new ColumnFamily(cfHandle, cfOptions)); } } finally { l.unlock(); } }
@Override public boolean isAllowable(URL url, Invocation invocation) { int rate = url.getParameter(Constants.TPS_LIMIT_RATE_KEY, -1); long interval = url.getParameter(Constants.TPS_LIMIT_INTERVAL_KEY, Constants.DEFAULT_TPS_LIMIT_INTERVAL); String serviceKey = url.getServiceKey(); if (rate > 0) { StatItem statItem = stats.get(serviceKey); if (statItem == null) { stats.putIfAbsent(serviceKey, new StatItem(serviceKey, rate, interval)); statItem = stats.get(serviceKey); } return statItem.isAllowable(); } else { StatItem statItem = stats.get(serviceKey); if (statItem != null) { stats.remove(serviceKey); } } return true; }
private void update(BrokerGroupInfo group, double factor, int maxWeight) { Integer weight = weights.get(group); if (weight == null) { weights.putIfAbsent(group, DEFAULT_WEIGHT); weight = weights.get(group); } weight = Math.min(Math.max((int) (weight * factor), MIN_WEIGHT), maxWeight); weights.put(group, weight); } }
private Resolver<?, ?, ?> getResolver(Class<?> cls, Class<? extends Resolver> resolverClass, Annotation anno) { if (!providerCache.containsKey(resolverClass)) { try { providerCache.putIfAbsent(resolverClass, resolverClass.newInstance()); } catch (Exception ex) { throw new RuntimeException(ex); } } return providerCache.get(resolverClass); }
NamespaceTableAndRegionInfo addNamespace(String namespace) { if (!nsStateCache.containsKey(namespace)) { NamespaceTableAndRegionInfo a1 = new NamespaceTableAndRegionInfo(namespace); nsStateCache.put(namespace, a1); } return nsStateCache.get(namespace); }
/** * Create a new {@code Message.Builder} instance for the given class. * <p>This method uses a ConcurrentHashMap for caching method lookups. */ private static Message.Builder getMessageBuilder(Class<?> clazz) throws Exception { Method method = methodCache.get(clazz); if (method == null) { method = clazz.getMethod("newBuilder"); methodCache.put(clazz, method); } return (Message.Builder) method.invoke(clazz); }
private JAXBContext getJaxbContext(Class<?> clazz) throws JAXBException { Assert.notNull(clazz, "Class must not be null"); JAXBContext jaxbContext = this.jaxbContexts.get(clazz); if (jaxbContext == null) { jaxbContext = JAXBContext.newInstance(clazz); this.jaxbContexts.putIfAbsent(clazz, jaxbContext); } return jaxbContext; }
@Override public void peerSyncReplicationStateChange(String peerId, SyncReplicationState from, SyncReplicationState to, int stage) { if (from == SyncReplicationState.ACTIVE) { if (stage == 0) { Lock lock = createLock.acquireLock(peerId); try { Optional<DualAsyncFSWAL> opt = peerId2WAL.get(peerId); if (opt != null) { opt.ifPresent(w -> w.skipRemoteWAL(to == SyncReplicationState.STANDBY)); } else { // add a place holder to tell the getWAL caller do not use DualAsyncFSWAL any more. peerId2WAL.put(peerId, Optional.empty()); } } finally { lock.unlock(); } } else if (stage == 1) { peerId2WAL.remove(peerId).ifPresent(this::safeClose); } } }