/hazelcast/src/main/java/com/hazelcast/impl/MProxyImpl.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 1060 lines · 877 code · 168 blank · 15 comment · 72 complexity · dc347526e71c3f4b43db56e55d50b0c1 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl;
  17. import com.hazelcast.core.*;
  18. import com.hazelcast.impl.ConcurrentMapManager.*;
  19. import com.hazelcast.impl.base.FactoryAwareNamedProxy;
  20. import com.hazelcast.impl.concurrentmap.AddMapIndex;
  21. import com.hazelcast.impl.monitor.LocalMapStatsImpl;
  22. import com.hazelcast.impl.monitor.MapOperationsCounter;
  23. import com.hazelcast.monitor.LocalMapStats;
  24. import com.hazelcast.nio.Data;
  25. import com.hazelcast.nio.DataSerializable;
  26. import com.hazelcast.query.Expression;
  27. import com.hazelcast.query.Predicate;
  28. import com.hazelcast.query.Predicates;
  29. import com.hazelcast.util.Clock;
  30. import java.lang.reflect.InvocationHandler;
  31. import java.lang.reflect.InvocationTargetException;
  32. import java.lang.reflect.Method;
  33. import java.lang.reflect.Proxy;
  34. import java.util.Collection;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import java.util.concurrent.CountDownLatch;
  38. import java.util.concurrent.Future;
  39. import java.util.concurrent.TimeUnit;
  40. import java.util.concurrent.TimeoutException;
  41. import static com.hazelcast.impl.ClusterOperation.CONCURRENT_MAP_ITERATE_KEYS;
  42. import static com.hazelcast.impl.Util.toMillis;
  43. import static com.hazelcast.nio.IOUtil.toData;
  44. public class MProxyImpl extends FactoryAwareNamedProxy implements MProxy, DataSerializable {
  45. private transient MProxy mproxyReal = null;
  46. private transient ConcurrentMapManager concurrentMapManager = null;
  47. private transient ListenerManager listenerManager = null;
  48. private volatile transient MProxy dynamicProxy;
  49. public MProxyImpl() {
  50. }
  51. private class DynamicInvoker implements InvocationHandler {
  52. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  53. beforeCall();
  54. try {
  55. return method.invoke(mproxyReal, args);
  56. } catch (Throwable e) {
  57. if (e instanceof InvocationTargetException) {
  58. InvocationTargetException ite = (InvocationTargetException) e;
  59. throw ite.getCause();
  60. }
  61. if (e instanceof OutOfMemoryError) {
  62. OutOfMemoryErrorDispatcher.onOutOfMemory((OutOfMemoryError) e);
  63. }
  64. throw e;
  65. } finally {
  66. afterCall();
  67. }
  68. }
  69. }
  70. MProxyImpl(String name, FactoryImpl factory) {
  71. setName(name);
  72. setHazelcastInstance(factory);
  73. mproxyReal = new MProxyReal();
  74. }
  75. public MapOperationsCounter getMapOperationCounter() {
  76. return mproxyReal.getMapOperationCounter();
  77. }
  78. @Override
  79. public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
  80. super.setHazelcastInstance(hazelcastInstance);
  81. this.concurrentMapManager = factory.node.concurrentMapManager;
  82. this.listenerManager = factory.node.listenerManager;
  83. ClassLoader cl = MProxy.class.getClassLoader();
  84. dynamicProxy = (MProxy) Proxy.newProxyInstance(cl, new Class[]{MProxy.class}, new DynamicInvoker());
  85. }
  86. private void beforeCall() {
  87. factory.initialChecks();
  88. if (mproxyReal == null) {
  89. mproxyReal = (MProxy) factory.getOrCreateProxyByName(name);
  90. }
  91. }
  92. private void afterCall() {
  93. }
  94. public Object get(Object key) {
  95. beforeCall();
  96. try {
  97. return mproxyReal.get(key);
  98. } catch (Throwable e) {
  99. Util.throwUncheckedException(e);
  100. return null;
  101. } finally {
  102. afterCall();
  103. }
  104. }
  105. public Object put(Object key, Object value) {
  106. return put(key, value, 0, TimeUnit.SECONDS);
  107. }
  108. public Future getAsync(Object key) {
  109. beforeCall();
  110. final MProxyImpl mProxy = MProxyImpl.this;
  111. final Data dataKey = toData(key);
  112. AsyncCall call = new ClassLoaderAwareAsyncCall() {
  113. @Override
  114. protected void call() {
  115. setResult(mProxy.get(dataKey));
  116. }
  117. };
  118. factory.node.executorManager.executeAsync(call);
  119. return call;
  120. }
  121. public Future putAsync(Object key, Object value) {
  122. beforeCall();
  123. final MProxyImpl mProxy = MProxyImpl.this;
  124. final Data dataKey = toData(key);
  125. final Data dataValue = toData(value);
  126. AsyncCall call = new ClassLoaderAwareAsyncCall() {
  127. @Override
  128. protected void call() {
  129. setResult(mProxy.put(dataKey, dataValue));
  130. }
  131. };
  132. factory.node.executorManager.executeAsync(call);
  133. return call;
  134. }
  135. public Future removeAsync(Object key) {
  136. beforeCall();
  137. final MProxyImpl mProxy = MProxyImpl.this;
  138. final Data dataKey = toData(key);
  139. AsyncCall call = new ClassLoaderAwareAsyncCall() {
  140. @Override
  141. protected void call() {
  142. setResult(mProxy.remove(dataKey));
  143. }
  144. };
  145. factory.node.executorManager.executeAsync(call);
  146. return call;
  147. }
  148. public Object put(Object key, Object value, long ttl, TimeUnit timeunit) {
  149. beforeCall();
  150. try {
  151. return mproxyReal.put(key, value, ttl, timeunit);
  152. } catch (Throwable e) {
  153. Util.throwUncheckedException(e);
  154. return null;
  155. } finally {
  156. afterCall();
  157. }
  158. }
  159. public Object remove(Object key) {
  160. beforeCall();
  161. try {
  162. return mproxyReal.remove(key);
  163. } catch (Throwable e) {
  164. Util.throwUncheckedException(e);
  165. return null;
  166. } finally {
  167. afterCall();
  168. }
  169. }
  170. public Object tryRemove(Object key, long time, TimeUnit timeunit) throws TimeoutException {
  171. beforeCall();
  172. try {
  173. return mproxyReal.tryRemove(key, time, timeunit);
  174. } catch (Throwable e) {
  175. if (e instanceof TimeoutException) {
  176. throw (TimeoutException) e;
  177. }
  178. Util.throwUncheckedException(e);
  179. return null;
  180. } finally {
  181. afterCall();
  182. }
  183. }
  184. public void putAndUnlock(Object key, Object value) {
  185. dynamicProxy.putAndUnlock(key, value);
  186. }
  187. public Object tryLockAndGet(Object key, long time, TimeUnit timeunit) throws TimeoutException {
  188. return dynamicProxy.tryLockAndGet(key, time, timeunit);
  189. }
  190. public Map getAll(Set keys) {
  191. return dynamicProxy.getAll(keys);
  192. }
  193. public void flush() {
  194. dynamicProxy.flush();
  195. }
  196. public void putForSync(Object key, Object value) {
  197. dynamicProxy.putForSync(key, value);
  198. }
  199. public void removeForSync(Object key) {
  200. dynamicProxy.removeForSync(key);
  201. }
  202. public void putTransient(Object key, Object value, long time, TimeUnit timeunit) {
  203. dynamicProxy.putTransient(key, value, time, timeunit);
  204. }
  205. public boolean putFromLoad(Object key, Object value) {
  206. return dynamicProxy.putFromLoad(key, value);
  207. }
  208. public boolean tryPut(Object key, Object value, long time, TimeUnit timeunit) {
  209. return dynamicProxy.tryPut(key, value, time, timeunit);
  210. }
  211. public void set(Object key, Object value, long time, TimeUnit timeunit) {
  212. dynamicProxy.set(key, value, time, timeunit);
  213. }
  214. public Object putIfAbsent(Object key, Object value, long ttl, TimeUnit timeunit) {
  215. return dynamicProxy.putIfAbsent(key, value, ttl, timeunit);
  216. }
  217. public Object putIfAbsent(Object key, Object value) {
  218. return dynamicProxy.putIfAbsent(key, value);
  219. }
  220. public LocalMapStats getLocalMapStats() {
  221. return dynamicProxy.getLocalMapStats();
  222. }
  223. public void addIndex(String attribute, boolean ordered) {
  224. dynamicProxy.addIndex(attribute, ordered);
  225. }
  226. public void addIndex(Expression expression, boolean ordered) {
  227. dynamicProxy.addIndex(expression, ordered);
  228. }
  229. public Object getId() {
  230. return dynamicProxy.getId();
  231. }
  232. @Override
  233. public String toString() {
  234. return "Map [" + getName() + "] ";
  235. }
  236. @Override
  237. public boolean equals(Object o) {
  238. if (this == o) return true;
  239. if (o == null || getClass() != o.getClass()) return false;
  240. MProxyImpl mProxy = (MProxyImpl) o;
  241. return name.equals(mProxy.name);
  242. }
  243. @Override
  244. public int hashCode() {
  245. return name != null ? name.hashCode() : 0;
  246. }
  247. public void destroy() {
  248. dynamicProxy.destroy();
  249. }
  250. public InstanceType getInstanceType() {
  251. return dynamicProxy.getInstanceType();
  252. }
  253. public boolean removeKey(Object key) {
  254. return dynamicProxy.removeKey(key);
  255. }
  256. public int size() {
  257. return dynamicProxy.size();
  258. }
  259. public boolean isEmpty() {
  260. return dynamicProxy.isEmpty();
  261. }
  262. public boolean containsKey(Object key) {
  263. return dynamicProxy.containsKey(key);
  264. }
  265. public boolean containsValue(Object value) {
  266. return dynamicProxy.containsValue(value);
  267. }
  268. public MapEntry getMapEntry(Object key) {
  269. return dynamicProxy.getMapEntry(key);
  270. }
  271. public void putAll(Map t) {
  272. dynamicProxy.putAll(t);
  273. }
  274. public void clear() {
  275. dynamicProxy.clear();
  276. }
  277. public int valueCount(Object key) {
  278. return dynamicProxy.valueCount(key);
  279. }
  280. public Set allKeys() {
  281. return dynamicProxy.allKeys();
  282. }
  283. public Set localKeySet() {
  284. return dynamicProxy.localKeySet();
  285. }
  286. public Set localKeySet(Predicate predicate) {
  287. return dynamicProxy.localKeySet(predicate);
  288. }
  289. public Set keySet() {
  290. return dynamicProxy.keySet();
  291. }
  292. public Collection values() {
  293. return dynamicProxy.values();
  294. }
  295. public Set entrySet() {
  296. return dynamicProxy.entrySet();
  297. }
  298. public Set keySet(Predicate predicate) {
  299. return dynamicProxy.keySet(predicate);
  300. }
  301. public Collection values(Predicate predicate) {
  302. return dynamicProxy.values(predicate);
  303. }
  304. public Set entrySet(Predicate predicate) {
  305. return dynamicProxy.entrySet(predicate);
  306. }
  307. public boolean remove(Object key, Object value) {
  308. return dynamicProxy.remove(key, value);
  309. }
  310. public boolean replace(Object key, Object oldValue, Object newValue) {
  311. return dynamicProxy.replace(key, oldValue, newValue);
  312. }
  313. public Object replace(Object key, Object value) {
  314. return dynamicProxy.replace(key, value);
  315. }
  316. public String getName() {
  317. return name.substring(Prefix.MAP.length());
  318. }
  319. public boolean lockMap(long time, TimeUnit timeunit) {
  320. return dynamicProxy.lockMap(time, timeunit);
  321. }
  322. public void unlockMap() {
  323. dynamicProxy.unlockMap();
  324. }
  325. public void lock(Object key) {
  326. dynamicProxy.lock(key);
  327. }
  328. public boolean isLocked(Object key) {
  329. return dynamicProxy.isLocked(key);
  330. }
  331. public boolean tryLock(Object key) {
  332. return dynamicProxy.tryLock(key);
  333. }
  334. public boolean tryLock(Object key, long time, TimeUnit timeunit) {
  335. return dynamicProxy.tryLock(key, time, timeunit);
  336. }
  337. public void unlock(Object key) {
  338. dynamicProxy.unlock(key);
  339. }
  340. public void forceUnlock(Object key) {
  341. dynamicProxy.forceUnlock(key);
  342. }
  343. public String getLongName() {
  344. return name;
  345. }
  346. public void addGenericListener(Object listener, Object key, boolean includeValue,
  347. InstanceType instanceType) {
  348. dynamicProxy.addGenericListener(listener, key, includeValue, instanceType);
  349. }
  350. public void removeGenericListener(Object listener, Object key) {
  351. dynamicProxy.removeGenericListener(listener, key);
  352. }
  353. public void addLocalEntryListener(EntryListener entryListener) {
  354. dynamicProxy.addLocalEntryListener(entryListener);
  355. }
  356. public void addEntryListener(EntryListener listener, boolean includeValue) {
  357. dynamicProxy.addEntryListener(listener, includeValue);
  358. }
  359. public void addEntryListener(EntryListener listener, Object key, boolean includeValue) {
  360. dynamicProxy.addEntryListener(listener, key, includeValue);
  361. }
  362. public void removeEntryListener(EntryListener listener) {
  363. dynamicProxy.removeEntryListener(listener);
  364. }
  365. public void removeEntryListener(EntryListener listener, Object key) {
  366. dynamicProxy.removeEntryListener(listener, key);
  367. }
  368. public boolean containsEntry(Object key, Object value) {
  369. return dynamicProxy.containsEntry(key, value);
  370. }
  371. public boolean putMulti(Object key, Object value) {
  372. return dynamicProxy.putMulti(key, value);
  373. }
  374. public boolean removeMulti(Object key, Object value) {
  375. return dynamicProxy.removeMulti(key, value);
  376. }
  377. public boolean add(Object value) {
  378. return dynamicProxy.add(value);
  379. }
  380. public boolean evict(Object key) {
  381. return dynamicProxy.evict(key);
  382. }
  383. private static void check(Object obj) {
  384. Util.checkSerializable(obj);
  385. }
  386. private class MProxyReal implements MProxy {
  387. private final transient MapOperationsCounter mapOperationCounter = new MapOperationsCounter();
  388. public MProxyReal() {
  389. super();
  390. }
  391. @Override
  392. public String toString() {
  393. return MProxyImpl.this.toString();
  394. }
  395. public InstanceType getInstanceType() {
  396. return InstanceType.MAP;
  397. }
  398. public Object getId() {
  399. return name;
  400. }
  401. @Override
  402. public boolean equals(Object o) {
  403. return MProxyImpl.this.equals(o);
  404. }
  405. @Override
  406. public int hashCode() {
  407. return MProxyImpl.this.hashCode();
  408. }
  409. public String getLongName() {
  410. return name;
  411. }
  412. public String getName() {
  413. return MProxyImpl.this.getName();
  414. }
  415. public void addIndex(final String attribute, final boolean ordered) {
  416. addIndex(Predicates.get(attribute), ordered);
  417. }
  418. public void addIndex(final Expression expression, final boolean ordered) {
  419. final CountDownLatch latch = new CountDownLatch(1);
  420. concurrentMapManager.enqueueAndReturn(new Processable() {
  421. public void process() {
  422. AddMapIndex addMapIndexProcess = new AddMapIndex(name, expression, ordered);
  423. concurrentMapManager.sendProcessableToAll(addMapIndexProcess, true);
  424. latch.countDown();
  425. }
  426. });
  427. try {
  428. latch.await();
  429. } catch (InterruptedException ignored) {
  430. }
  431. }
  432. public void flush() {
  433. concurrentMapManager.flush(name);
  434. }
  435. public MapEntry getMapEntry(Object key) {
  436. long begin = Clock.currentTimeMillis();
  437. check(key);
  438. MGetMapEntry mgetMapEntry = concurrentMapManager.new MGetMapEntry();
  439. MapEntry mapEntry = mgetMapEntry.get(name, key);
  440. mapOperationCounter.incrementGets(Clock.currentTimeMillis() - begin);
  441. return mapEntry;
  442. }
  443. public boolean putMulti(Object key, Object value) {
  444. long begin = Clock.currentTimeMillis();
  445. check(key);
  446. check(value);
  447. MPutMulti mput = concurrentMapManager.new MPutMulti();
  448. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  449. return mput.put(name, key, value);
  450. }
  451. public Object put(Object key, Object value) {
  452. return put(key, value, 0, TimeUnit.SECONDS);
  453. }
  454. public void putForSync(Object key, Object value) {
  455. long begin = Clock.currentTimeMillis();
  456. check(key);
  457. check(value);
  458. MPut mput = ThreadContext.get().getCallCache(factory).getMPut();
  459. mput.putForSync(name, key, value);
  460. mput.clearRequest();
  461. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  462. }
  463. public void removeForSync(Object key) {
  464. long begin = Clock.currentTimeMillis();
  465. check(key);
  466. MRemove mremove = ThreadContext.get().getCallCache(factory).getMRemove();
  467. mremove.removeForSync(name, key);
  468. mremove.clearRequest();
  469. mapOperationCounter.incrementRemoves(Clock.currentTimeMillis() - begin);
  470. }
  471. public Map getAll(Set keys) {
  472. if (keys == null) {
  473. throw new NullPointerException();
  474. }
  475. return concurrentMapManager.getAll(name, keys);
  476. }
  477. public Future getAsync(Object key) {
  478. throw new UnsupportedOperationException();
  479. }
  480. public Future putAsync(Object key, Object value) {
  481. throw new UnsupportedOperationException();
  482. }
  483. public Future removeAsync(Object key) {
  484. throw new UnsupportedOperationException();
  485. }
  486. public Object put(Object key, Object value, long ttl, TimeUnit timeunit) {
  487. if (ttl < 0) {
  488. throw new IllegalArgumentException("ttl value cannot be negative. " + ttl);
  489. }
  490. if (ttl == 0) {
  491. ttl = -1;
  492. } else {
  493. ttl = toMillis(ttl, timeunit);
  494. }
  495. return put(key, value, ttl);
  496. }
  497. public void putTransient(Object key, Object value, long ttl, TimeUnit timeunit) {
  498. if (ttl < 0) {
  499. throw new IllegalArgumentException("ttl value cannot be negative. " + ttl);
  500. }
  501. if (ttl == 0) {
  502. ttl = -1;
  503. } else {
  504. ttl = toMillis(ttl, timeunit);
  505. }
  506. mapOperationCounter.incrementOtherOperations();
  507. concurrentMapManager.putTransient(name, key, value, ttl);
  508. }
  509. public boolean putFromLoad(Object key, Object value) {
  510. mapOperationCounter.incrementOtherOperations();
  511. return concurrentMapManager.putFromLoad(name, key, value);
  512. }
  513. public Object put(Object key, Object value, long ttl) {
  514. long begin = Clock.currentTimeMillis();
  515. check(key);
  516. check(value);
  517. MPut mput = ThreadContext.get().getCallCache(factory).getMPut();
  518. Object result = mput.put(name, key, value, ttl);
  519. mput.clearRequest();
  520. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  521. return result;
  522. }
  523. public void set(Object key, Object value, long ttl, TimeUnit timeunit) {
  524. long begin = Clock.currentTimeMillis();
  525. if (ttl < 0) {
  526. throw new IllegalArgumentException("ttl value cannot be negative. " + ttl);
  527. }
  528. if (ttl == 0) {
  529. ttl = -1;
  530. } else {
  531. ttl = toMillis(ttl, timeunit);
  532. }
  533. check(key);
  534. check(value);
  535. MPut mput = ThreadContext.get().getCallCache(factory).getMPut();
  536. mput.set(name, key, value, ttl);
  537. mput.clearRequest();
  538. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  539. }
  540. public boolean tryPut(Object key, Object value, long timeout, TimeUnit timeunit) {
  541. long begin = Clock.currentTimeMillis();
  542. if (timeout < 0) {
  543. throw new IllegalArgumentException("timeout value cannot be negative. " + timeout);
  544. }
  545. timeout = toMillis(timeout, timeunit);
  546. check(key);
  547. check(value);
  548. MPut mput = ThreadContext.get().getCallCache(factory).getMPut();
  549. Boolean result = mput.tryPut(name, key, value, timeout, -1);
  550. mput.clearRequest();
  551. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  552. return result;
  553. }
  554. public Object tryLockAndGet(Object key, long timeout, TimeUnit timeunit) throws TimeoutException {
  555. long begin = Clock.currentTimeMillis();
  556. if (timeout < 0) {
  557. throw new IllegalArgumentException("timeout value cannot be negative. " + timeout);
  558. }
  559. timeout = toMillis(timeout, timeunit);
  560. check(key);
  561. Object result = concurrentMapManager.tryLockAndGet(name, key, timeout);
  562. mapOperationCounter.incrementGets(Clock.currentTimeMillis() - begin);
  563. return result;
  564. }
  565. public boolean lockMap(long time, TimeUnit timeunit) {
  566. if (factory.locksMapProxy.tryLock("map_lock_" + name, time, timeunit)) {
  567. MLockMap mLockMap = concurrentMapManager.new MLockMap(name, true);
  568. mLockMap.call();
  569. return true;
  570. }
  571. return false;
  572. }
  573. public void unlockMap() {
  574. MLockMap mLockMap = concurrentMapManager.new MLockMap(name, false);
  575. mLockMap.call();
  576. factory.locksMapProxy.unlock("map_lock_" + name);
  577. }
  578. public void lock(Object key) {
  579. check(key);
  580. mapOperationCounter.incrementOtherOperations();
  581. concurrentMapManager.lock(name, key, -1);
  582. }
  583. public boolean isLocked(Object key) {
  584. check(key);
  585. mapOperationCounter.incrementOtherOperations();
  586. MLock mlock = concurrentMapManager.new MLock();
  587. return mlock.isLocked(name, key);
  588. }
  589. public boolean tryLock(Object key) {
  590. check(key);
  591. mapOperationCounter.incrementOtherOperations();
  592. return concurrentMapManager.lock(name, key, 0);
  593. }
  594. public boolean tryLock(Object key, long time, TimeUnit timeunit) {
  595. check(key);
  596. if (time < 0)
  597. throw new IllegalArgumentException("Time cannot be negative. time = " + time);
  598. mapOperationCounter.incrementOtherOperations();
  599. long timeoutMillis = toMillis(time, timeunit);
  600. return concurrentMapManager.lock(name, key, timeoutMillis);
  601. }
  602. public void unlock(Object key) {
  603. check(key);
  604. mapOperationCounter.incrementOtherOperations();
  605. MLock mlock = concurrentMapManager.new MLock();
  606. if (!mlock.unlock(name, key, 0)) {
  607. throw new IllegalMonitorStateException("Current thread is not owner of the lock!");
  608. }
  609. }
  610. public void forceUnlock(Object key) {
  611. check(key);
  612. mapOperationCounter.incrementOtherOperations();
  613. MLock mlock = concurrentMapManager.new MLock();
  614. mlock.forceUnlock(name, key);
  615. }
  616. public void putAndUnlock(Object key, Object value) {
  617. long begin = Clock.currentTimeMillis();
  618. check(key);
  619. check(value);
  620. concurrentMapManager.putAndUnlock(name, key, value);
  621. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  622. }
  623. public Object putIfAbsent(Object key, Object value) {
  624. return putIfAbsent(key, value, -1);
  625. }
  626. public Object putIfAbsent(Object key, Object value, long ttl, TimeUnit timeunit) {
  627. if (ttl < 0) {
  628. throw new IllegalArgumentException("ttl value cannot be negative. " + ttl);
  629. }
  630. if (ttl == 0) {
  631. ttl = -1;
  632. } else {
  633. ttl = toMillis(ttl, timeunit);
  634. }
  635. return putIfAbsent(key, value, ttl);
  636. }
  637. private Object putIfAbsent(Object key, Object value, long ttl) {
  638. long begin = Clock.currentTimeMillis();
  639. check(key);
  640. check(value);
  641. MPut mput = concurrentMapManager.new MPut();
  642. Object result = mput.putIfAbsent(name, key, value, ttl);
  643. mput.clearRequest();
  644. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  645. return result;
  646. }
  647. public Object get(Object key) {
  648. check(key);
  649. long begin = Clock.currentTimeMillis();
  650. MGet mget = ThreadContext.get().getCallCache(factory).getMGet();
  651. Object result = mget.get(name, key, -1);
  652. mget.clearRequest();
  653. mapOperationCounter.incrementGets(Clock.currentTimeMillis() - begin);
  654. return result;
  655. }
  656. public Object remove(Object key) {
  657. long begin = Clock.currentTimeMillis();
  658. check(key);
  659. MRemove mremove = ThreadContext.get().getCallCache(factory).getMRemove();
  660. Object result = mremove.remove(name, key);
  661. mremove.clearRequest();
  662. mapOperationCounter.incrementRemoves(Clock.currentTimeMillis() - begin);
  663. return result;
  664. }
  665. public Object tryRemove(Object key, long timeout, TimeUnit timeunit) throws TimeoutException {
  666. long begin = Clock.currentTimeMillis();
  667. check(key);
  668. MRemove mremove = ThreadContext.get().getCallCache(factory).getMRemove();
  669. Object result = mremove.tryRemove(name, key, toMillis(timeout, timeunit));
  670. mremove.clearRequest();
  671. mapOperationCounter.incrementRemoves(Clock.currentTimeMillis() - begin);
  672. return result;
  673. }
  674. public int size() {
  675. mapOperationCounter.incrementOtherOperations();
  676. return concurrentMapManager.size(name);
  677. }
  678. public int valueCount(Object key) {
  679. int count;
  680. mapOperationCounter.incrementOtherOperations();
  681. MValueCount mcount = concurrentMapManager.new MValueCount();
  682. count = ((Number) mcount.count(name, key, -1)).intValue();
  683. return count;
  684. }
  685. public boolean removeMulti(Object key, Object value) {
  686. long begin = Clock.currentTimeMillis();
  687. check(key);
  688. check(value);
  689. MRemoveMulti mremove = concurrentMapManager.new MRemoveMulti();
  690. mapOperationCounter.incrementRemoves(Clock.currentTimeMillis() - begin);
  691. return mremove.remove(name, key, value);
  692. }
  693. public boolean remove(Object key, Object value) {
  694. long begin = Clock.currentTimeMillis();
  695. check(key);
  696. check(value);
  697. MRemove mremove = concurrentMapManager.new MRemove();
  698. boolean result = mremove.removeIfSame(name, key, value);
  699. mapOperationCounter.incrementRemoves(Clock.currentTimeMillis() - begin);
  700. return result;
  701. }
  702. public Object replace(Object key, Object value) {
  703. long begin = Clock.currentTimeMillis();
  704. check(key);
  705. check(value);
  706. MPut mput = concurrentMapManager.new MPut();
  707. Object result = mput.replace(name, key, value);
  708. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  709. return result;
  710. }
  711. public boolean replace(Object key, Object oldValue, Object newValue) {
  712. long begin = Clock.currentTimeMillis();
  713. check(key);
  714. check(oldValue);
  715. check(newValue);
  716. MPut mput = concurrentMapManager.new MPut();
  717. Boolean result = mput.replace(name, key, oldValue, newValue);
  718. mapOperationCounter.incrementPuts(Clock.currentTimeMillis() - begin);
  719. return result;
  720. }
  721. public LocalMapStats getLocalMapStats() {
  722. mapOperationCounter.incrementOtherOperations();
  723. LocalMapStatsImpl localMapStats = concurrentMapManager.getLocalMapStats(name);
  724. localMapStats.setOperationStats(mapOperationCounter.getPublishedStats());
  725. return localMapStats;
  726. }
  727. public void addGenericListener(Object listener, Object key, boolean includeValue,
  728. InstanceType instanceType) {
  729. if (listener == null)
  730. throw new IllegalArgumentException("Listener cannot be null");
  731. listenerManager.addListener(name, listener, key, includeValue, instanceType);
  732. }
  733. public void removeGenericListener(Object listener, Object key) {
  734. if (listener == null)
  735. throw new IllegalArgumentException("Listener cannot be null");
  736. listenerManager.removeListener(name, listener, key);
  737. }
  738. public void addLocalEntryListener(EntryListener listener) {
  739. if (listener == null)
  740. throw new IllegalArgumentException("Listener cannot be null");
  741. listenerManager.addLocalListener(name, listener, getInstanceType());
  742. }
  743. public void addEntryListener(EntryListener listener, boolean includeValue) {
  744. if (listener == null)
  745. throw new IllegalArgumentException("Listener cannot be null");
  746. addGenericListener(listener, null, includeValue, getInstanceType());
  747. }
  748. public void addEntryListener(EntryListener listener, Object key, boolean includeValue) {
  749. if (listener == null)
  750. throw new IllegalArgumentException("Listener cannot be null");
  751. check(key);
  752. addGenericListener(listener, key, includeValue, getInstanceType());
  753. }
  754. public void removeEntryListener(EntryListener listener) {
  755. if (listener == null)
  756. throw new IllegalArgumentException("Listener cannot be null");
  757. removeGenericListener(listener, null);
  758. }
  759. public void removeEntryListener(EntryListener listener, Object key) {
  760. if (listener == null)
  761. throw new IllegalArgumentException("Listener cannot be null");
  762. check(key);
  763. removeGenericListener(listener, key);
  764. }
  765. public boolean containsEntry(Object key, Object value) {
  766. check(key);
  767. check(value);
  768. mapOperationCounter.incrementOtherOperations();
  769. TransactionImpl txn = ThreadContext.get().getCallContext().getTransaction();
  770. if (txn != null && txn.has(name, key)) {
  771. if (txn.containsEntry(name, key, value)) {
  772. return true;
  773. }
  774. }
  775. MContainsKey mContainsKey = concurrentMapManager.new MContainsKey();
  776. return mContainsKey.containsEntry(name, key, value);
  777. }
  778. public boolean containsKey(Object key) {
  779. check(key);
  780. mapOperationCounter.incrementOtherOperations();
  781. TransactionImpl txn = ThreadContext.get().getCallContext().getTransaction();
  782. if (txn != null) {
  783. if (txn.has(name, key)) {
  784. Data value = txn.get(name, key);
  785. return value != null;
  786. }
  787. }
  788. MContainsKey mContainsKey = concurrentMapManager.new MContainsKey();
  789. return mContainsKey.containsKey(name, key);
  790. }
  791. public boolean containsValue(Object value) {
  792. check(value);
  793. mapOperationCounter.incrementOtherOperations();
  794. TransactionImpl txn = ThreadContext.get().getCallContext().getTransaction();
  795. if (txn != null) {
  796. if (txn.containsValue(name, value))
  797. return true;
  798. }
  799. MContainsValue mContainsValue = concurrentMapManager.new MContainsValue(name, value);
  800. return mContainsValue.call();
  801. }
  802. public boolean isEmpty() {
  803. mapOperationCounter.incrementOtherOperations();
  804. MEmpty mempty = concurrentMapManager.new MEmpty();
  805. return mempty.isEmpty(name);
  806. }
  807. public void putAll(Map map) {
  808. Set<Entry> entries = map.entrySet();
  809. TransactionImpl txn = ThreadContext.get().getCallContext().getTransaction();
  810. if (txn != null && txn.getStatus() == Transaction.TXN_STATUS_ACTIVE) {
  811. for (final Entry entry : entries) {
  812. put(entry.getKey(), entry.getValue());
  813. }
  814. } else {
  815. concurrentMapManager.doPutAll(name, map);
  816. }
  817. }
  818. public boolean add(Object value) {
  819. check(value);
  820. Object old = putIfAbsent(value, toData(Boolean.TRUE));
  821. return old == null;
  822. }
  823. public boolean removeKey(Object key) {
  824. long begin = Clock.currentTimeMillis();
  825. check(key);
  826. MRemoveItem mRemoveItem = concurrentMapManager.new MRemoveItem();
  827. boolean result = mRemoveItem.removeItem(name, key);
  828. mapOperationCounter.incrementRemoves(Clock.currentTimeMillis() - begin);
  829. return result;
  830. }
  831. public void clear() {
  832. final CMap cMap = concurrentMapManager.getMap(name);
  833. if (cMap != null && cMap.isClearQuick()) {
  834. mapOperationCounter.incrementOtherOperations();
  835. MClearQuick mClearQuick = concurrentMapManager.new MClearQuick(name);
  836. mClearQuick.call();
  837. } else {
  838. Set keys = keySet();
  839. for (Object key : keys) {
  840. removeKey(key);
  841. }
  842. }
  843. }
  844. public Set localKeySet() {
  845. return localKeySet(null);
  846. }
  847. public Set localKeySet(Predicate predicate) {
  848. mapOperationCounter.incrementOtherOperations();
  849. return concurrentMapManager.queryLocal(name, CONCURRENT_MAP_ITERATE_KEYS, predicate);
  850. }
  851. public Set entrySet(Predicate predicate) {
  852. return (Set) query(ClusterOperation.CONCURRENT_MAP_ITERATE_ENTRIES, predicate);
  853. }
  854. public Set keySet(Predicate predicate) {
  855. return (Set) query(ClusterOperation.CONCURRENT_MAP_ITERATE_KEYS, predicate);
  856. }
  857. public Collection values(Predicate predicate) {
  858. return query(ClusterOperation.CONCURRENT_MAP_ITERATE_VALUES, predicate);
  859. }
  860. public Set entrySet() {
  861. return (Set) query(ClusterOperation.CONCURRENT_MAP_ITERATE_ENTRIES, null);
  862. }
  863. public Set keySet() {
  864. return (Set) query(ClusterOperation.CONCURRENT_MAP_ITERATE_KEYS, null);
  865. }
  866. public Set allKeys() {
  867. return (Set) query(ClusterOperation.CONCURRENT_MAP_ITERATE_KEYS_ALL, null);
  868. }
  869. public MapOperationsCounter getMapOperationCounter() {
  870. return mapOperationCounter;
  871. }
  872. public Collection values() {
  873. return query(ClusterOperation.CONCURRENT_MAP_ITERATE_VALUES, null);
  874. }
  875. private Collection query(ClusterOperation iteratorType, Predicate predicate) {
  876. mapOperationCounter.incrementOtherOperations();
  877. return concurrentMapManager.query(name, iteratorType, predicate);
  878. }
  879. public void destroy() {
  880. factory.destroyInstanceClusterWide(name, null);
  881. }
  882. public boolean evict(Object key) {
  883. mapOperationCounter.incrementOtherOperations();
  884. MEvict mevict = ThreadContext.get().getCallCache(factory).getMEvict();
  885. boolean result = mevict.evict(name, key);
  886. mevict.clearRequest();
  887. return result;
  888. }
  889. public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
  890. }
  891. }
  892. }