/hazelcast/src/main/java/com/hazelcast/core/IMap.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 900 lines · 62 code · 55 blank · 783 comment · 0 complexity · 887998ab5e8382844a1f06d624561eb4 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.core;
  17. import com.hazelcast.monitor.LocalMapStats;
  18. import com.hazelcast.query.Expression;
  19. import com.hazelcast.query.Predicate;
  20. import java.util.Collection;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.concurrent.ConcurrentMap;
  24. import java.util.concurrent.Future;
  25. import java.util.concurrent.TimeUnit;
  26. import java.util.concurrent.TimeoutException;
  27. /**
  28. * Concurrent, distributed, observable and queryable map.
  29. * <p/>
  30. * <p><b>This class is <i>not</i> a general-purpose <tt>ConcurrentMap</tt> implementation! While this class implements
  31. * the <tt>Map</tt> interface, it intentionally violates <tt>Map's</tt> general contract, which mandates the
  32. * use of the <tt>equals</tt> method when comparing objects. Instead of the <tt>equals</tt> method this implementation
  33. * compares the serialized byte version of the objects.</b>
  34. * <p/>
  35. * <p>
  36. * <b>Gotchas:</b>
  37. * <ul>
  38. * <li>
  39. * Methods, including but not limited to <tt>get</tt>, <tt>containsKey</tt>,
  40. * <tt>containsValue</tt>, <tt>evict</tt>, <tt>remove</tt>, <tt>put</tt>,
  41. * <tt>putIfAbsent</tt>, <tt>replace</tt>, <tt>lock</tt>,
  42. * <tt>unlock</tt>, do not use <tt>hashCode</tt> and <tt>equals</tt> implementations of keys,
  43. * instead they use <tt>hashCode</tt> and <tt>equals</tt> of binary (serialized) forms of the objects.
  44. * </li>
  45. * <li>
  46. * <tt>get</tt> method returns a clone of original values, modifying the returned value does not change
  47. * the actual value in the map. One should put modified value back to make changes visible to all nodes.
  48. * For additional info see {@link IMap#get(Object)}.
  49. * </li>
  50. * </li>
  51. * <li>
  52. * Methods, including but not limited to <tt>keySet</tt>, <tt>values</tt>, <tt>entrySet</tt>,
  53. * return a collection clone of the values. The collection is <b>NOT</b> backed by the map,
  54. * so changes to the map are <b>NOT</b> reflected in the collection, and vice-versa.
  55. * </li>
  56. * </ul>
  57. * </p>
  58. *
  59. * @param <K> key
  60. * @param <V> value
  61. * @see java.util.concurrent.ConcurrentMap
  62. * @see java.util.IdentityHashMap
  63. */
  64. public interface IMap<K, V> extends ConcurrentMap<K, V>, Instance {
  65. /**
  66. * {@inheritDoc}
  67. * <p/>
  68. * <p><b>Warning:</b></p>
  69. * <p>
  70. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  71. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  72. * defined in <tt>key</tt>'s class.
  73. * </p>
  74. */
  75. boolean containsKey(Object key);
  76. /**
  77. * {@inheritDoc}
  78. */
  79. boolean containsValue(Object value);
  80. /**
  81. * {@inheritDoc}
  82. * <p/>
  83. * <p><b>Warning:</b></p>
  84. * <p>
  85. * This method returns a clone of original value, modifying the returned value does not change
  86. * the actual value in the map. One should put modified value back to make changes visible to all nodes.
  87. * <pre>
  88. * V value = map.get(key);
  89. * value.updateSomeProperty();
  90. * map.put(key, value);
  91. * </pre>
  92. * </p>
  93. * <p/>
  94. * <p><b>Warning-2:</b></p>
  95. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  96. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  97. * defined in <tt>key</tt>'s class.
  98. * <p/>
  99. * <p><b>Warning-3:</b></p>
  100. * <p>
  101. * If <tt>cache-value</tt> is true (default is true), this method returns a clone of original value
  102. * but also caches that value for fast access in local. Modifications done to this cached value without
  103. * putting it back to map will be visible to only local node, not entire cluster,
  104. * successive <tt>get</tt> calls will return the same cached value.
  105. * To reflect modifications to distributed map, one should put modified value back into map.
  106. * </p>
  107. */
  108. V get(Object key);
  109. /**
  110. * {@inheritDoc}
  111. * <p/>
  112. * <p><b>Warning:</b></p>
  113. * <p>
  114. * This method returns a clone of previous value, not the original (identically equal) value
  115. * previously put into map.
  116. * </p>
  117. * <p/>
  118. * <p><b>Warning-2:</b></p>
  119. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  120. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  121. * defined in <tt>key</tt>'s class.
  122. */
  123. V put(K key, V value);
  124. /**
  125. * {@inheritDoc}
  126. * <p/>
  127. * <p><b>Warning:</b></p>
  128. * <p>
  129. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  130. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  131. * defined in <tt>key</tt>'s class.
  132. * </p>
  133. * <p/>
  134. * <p><b>Warning-2:</b></p>
  135. * <p>
  136. * This method returns a clone of previous value, not the original (identically equal) value
  137. * previously put into map.
  138. * </p>
  139. */
  140. V remove(Object key);
  141. /**
  142. * {@inheritDoc}
  143. * <p/>
  144. * <p><b>Warning:</b></p>
  145. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  146. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  147. * defined in <tt>key</tt>'s class.
  148. */
  149. boolean remove(Object key, Object value);
  150. /**
  151. * If this map has a MapStore and write-delay-seconds is
  152. * bigger than 0 (write-behind) then this method flushes
  153. * all the local dirty entries by calling MapStore.storeAll() and/or MapStore.deleteAll()
  154. */
  155. void flush();
  156. /**
  157. * Returns the name of this map
  158. *
  159. * @return name of this map
  160. */
  161. String getName();
  162. /**
  163. * Returns the entries for the given keys.
  164. * <p/>
  165. * <p><b>Warning:</b></p>
  166. * The returned map is <b>NOT</b> backed by the original map,
  167. * so changes to the original map are <b>NOT</b> reflected in the returned map, and vice-versa.
  168. * <p/>
  169. * <p><b>Warning-2:</b></p>
  170. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  171. * the <tt>keys</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  172. * defined in <tt>key</tt>'s class.
  173. *
  174. * @param keys keys to get
  175. * @return map of entries
  176. */
  177. Map<K, V> getAll(Set<K> keys);
  178. /**
  179. * Asynchronously gets the given key.
  180. * <code>
  181. * Future future = map.getAsync(key);
  182. * // do some other stuff, when ready get the result
  183. * Object value = future.get();
  184. * </code>
  185. * Future.get() will block until the actual map.get() completes.
  186. * If the application requires timely response,
  187. * then Future.get(timeout, timeunit) can be used.
  188. * <code>
  189. * try{
  190. * Future future = map.getAsync(key);
  191. * Object value = future.get(40, TimeUnit.MILLISECOND);
  192. * }catch (TimeoutException t) {
  193. * // time wasn't enough
  194. * }
  195. * </code>
  196. * ExecutionException is never thrown.
  197. * <p/>
  198. * <p><b>Warning:</b></p>
  199. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  200. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  201. * defined in <tt>key</tt>'s class.
  202. *
  203. * @param key the key of the map entry
  204. * @return Future from which the value of the key can be retrieved.
  205. * @see java.util.concurrent.Future
  206. */
  207. Future<V> getAsync(K key);
  208. /**
  209. * Asynchronously puts the given key and value.
  210. * <code>
  211. * Future future = map.putAsync(key, value);
  212. * // do some other stuff, when ready get the result
  213. * Object oldValue = future.get();
  214. * </code>
  215. * Future.get() will block until the actual map.get() completes.
  216. * If the application requires timely response,
  217. * then Future.get(timeout, timeunit) can be used.
  218. * <code>
  219. * try{
  220. * Future future = map.putAsync(key, newValue);
  221. * Object oldValue = future.get(40, TimeUnit.MILLISECOND);
  222. * }catch (TimeoutException t) {
  223. * // time wasn't enough
  224. * }
  225. * </code>
  226. * ExecutionException is never thrown.
  227. * <p/>
  228. * <p><b>Warning:</b></p>
  229. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  230. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  231. * defined in <tt>key</tt>'s class.
  232. *
  233. * @param key the key of the map entry
  234. * @param value the new value of the map entry
  235. * @return Future from which the old value of the key can be retrieved.
  236. * @see java.util.concurrent.Future
  237. */
  238. Future<V> putAsync(K key, V value);
  239. /**
  240. * Asynchronously removes the given key.
  241. * <p/>
  242. * <p><b>Warning:</b></p>
  243. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  244. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  245. * defined in <tt>key</tt>'s class.
  246. *
  247. * @param key The key of the map entry to remove.
  248. * @return A {@link java.util.concurrent.Future} from which the value
  249. * removed from the map can be retrieved.
  250. */
  251. Future<V> removeAsync(K key);
  252. /**
  253. * Tries to remove the entry with the given key from this map
  254. * within specified timeout value. If the key is already locked by another
  255. * thread and/or member, then this operation will wait timeout
  256. * amount for acquiring the lock.
  257. * <p/>
  258. * <p><b>Warning:</b></p>
  259. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  260. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  261. * defined in <tt>key</tt>'s class.
  262. * <p/>
  263. * <p><b>Warning-2:</b></p>
  264. * <p>
  265. * This method returns a clone of previous value, not the original (identically equal) value
  266. * previously put into map.
  267. * </p>
  268. *
  269. * @param key key of the entry
  270. * @param timeout maximum time to wait for acquiring the lock
  271. * for the key
  272. * @param timeunit time unit for the timeout
  273. * @return removed value of the entry
  274. * @throws java.util.concurrent.TimeoutException
  275. * if lock cannot be acquired for the given key within timeout
  276. */
  277. Object tryRemove(K key, long timeout, TimeUnit timeunit) throws TimeoutException;
  278. /**
  279. * Tries to put the given key, value into this map within specified
  280. * timeout value. If this method returns false, it means that
  281. * the caller thread couldn't acquire the lock for the key within
  282. * timeout duration, thus put operation is not successful.
  283. * <p/>
  284. * <p><b>Warning:</b></p>
  285. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  286. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  287. * defined in <tt>key</tt>'s class.
  288. *
  289. * @param key key of the entry
  290. * @param value value of the entry
  291. * @param timeout maximum time to wait
  292. * @param timeunit time unit for the timeout
  293. * @return <tt>true</tt> if the put is successful, <tt>false</tt>
  294. * otherwise.
  295. */
  296. boolean tryPut(K key, V value, long timeout, TimeUnit timeunit);
  297. /**
  298. * Puts an entry into this map with a given ttl (time to live) value.
  299. * Entry will expire and get evicted after the ttl. If ttl is 0, then
  300. * the entry lives forever.
  301. * <p/>
  302. * <p><b>Warning:</b></p>
  303. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  304. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  305. * defined in <tt>key</tt>'s class.
  306. * <p/>
  307. * <p><b>Warning-2:</b></p>
  308. * <p>
  309. * This method returns a clone of previous value, not the original (identically equal) value
  310. * previously put into map.
  311. * </p>
  312. *
  313. * @param key key of the entry
  314. * @param value value of the entry
  315. * @param ttl maximum time for this entry to stay in the map
  316. * 0 means infinite.
  317. * @param timeunit time unit for the ttl
  318. * @return old value of the entry
  319. */
  320. V put(K key, V value, long ttl, TimeUnit timeunit);
  321. /**
  322. * Same as {@link #put(K, V, long, TimeUnit)} but MapStore, if defined,
  323. * will not be called to store/persist the entry. If ttl is 0, then
  324. * the entry lives forever.
  325. * <p/>
  326. * <p><b>Warning:</b></p>
  327. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  328. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  329. * defined in <tt>key</tt>'s class.
  330. *
  331. * @param key key of the entry
  332. * @param value value of the entry
  333. * @param ttl maximum time for this entry to stay in the map.
  334. * 0 means infinite.
  335. * @param timeunit time unit for the ttl
  336. */
  337. void putTransient(K key, V value, long ttl, TimeUnit timeunit);
  338. /**
  339. * {@inheritDoc}
  340. * <p/>
  341. * <p><b>Warning:</b></p>
  342. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  343. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  344. * defined in <tt>key</tt>'s class.
  345. * <p/>
  346. * <p><b>Warning-2:</b></p>
  347. * <p>
  348. * This method returns a clone of previous value, not the original (identically equal) value
  349. * previously put into map.
  350. * </p>
  351. */
  352. V putIfAbsent(K key, V value);
  353. /**
  354. * Puts an entry into this map with a given ttl (time to live) value
  355. * if the specified key is not already associated with a value.
  356. * Entry will expire and get evicted after the ttl.
  357. * <p/>
  358. * <p><b>Warning:</b></p>
  359. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  360. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  361. * defined in <tt>key</tt>'s class.
  362. * <p/>
  363. * <p><b>Warning-2:</b></p>
  364. * <p>
  365. * This method returns a clone of previous value, not the original (identically equal) value
  366. * previously put into map.
  367. * </p>
  368. *
  369. * @param key key of the entry
  370. * @param value value of the entry
  371. * @param ttl maximum time for this entry to stay in the map
  372. * @param timeunit time unit for the ttl
  373. * @return old value of the entry
  374. */
  375. V putIfAbsent(K key, V value, long ttl, TimeUnit timeunit);
  376. /**
  377. * {@inheritDoc}
  378. * <p/>
  379. * <p><b>Warning:</b></p>
  380. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  381. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  382. * defined in <tt>key</tt>'s class.
  383. */
  384. boolean replace(K key, V oldValue, V newValue);
  385. /**
  386. * {@inheritDoc}
  387. * <p/>
  388. * <p><b>Warning:</b></p>
  389. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  390. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  391. * defined in <tt>key</tt>'s class.
  392. * <p/>
  393. * <p><b>Warning-2:</b></p>
  394. * <p>
  395. * This method returns a clone of previous value, not the original (identically equal) value
  396. * previously put into map.
  397. * </p>
  398. */
  399. V replace(K key, V value);
  400. /**
  401. * Puts an entry into this map with a given ttl (time to live) value.
  402. * Entry will expire and get evicted after the ttl. If ttl is 0, then
  403. * the entry lives forever. Similar to put operation except that set
  404. * doesn't return the old value which is more efficient.
  405. * <p/>
  406. * <p><b>Warning:</b></p>
  407. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  408. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  409. * defined in <tt>key</tt>'s class.
  410. *
  411. * @param key key of the entry
  412. * @param value value of the entry
  413. * @param ttl maximum time for this entry to stay in the map
  414. * 0 means infinite.
  415. * @param timeunit time unit for the ttl
  416. * @return old value of the entry
  417. */
  418. void set(K key, V value, long ttl, TimeUnit timeunit);
  419. /**
  420. * Tries to acquire the lock for the specified key and returns
  421. * the value of the key if lock is required in time.
  422. * <p>If the lock is not available then
  423. * the current thread becomes disabled for thread scheduling
  424. * purposes and lies dormant until one of two things happens:
  425. * <ul>
  426. * <li>The lock is acquired by the current thread; or
  427. * <li>The specified waiting time elapses
  428. * </ul>
  429. * <p/>
  430. * <p><b>Warning:</b></p>
  431. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  432. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  433. * defined in <tt>key</tt>'s class.
  434. * <p/>
  435. * <p><b>Warning:</b></p>
  436. * <p>
  437. * This method returns a clone of original value, modifying the returned value does not change
  438. * the actual value in the map. One should put modified value back to make changes visible to all nodes.
  439. * <pre>
  440. * V value = map.get(key);
  441. * value.updateSomeProperty();
  442. * map.put(key, value);
  443. * </pre>
  444. * </p>
  445. *
  446. * @param key key of the entry
  447. * @param time maximum time to wait for the lock
  448. * @param timeunit time unit of the <tt>time</tt> argument.
  449. * @return value of the key in this map
  450. * @throws java.util.concurrent.TimeoutException
  451. * if lock cannot be acquired in time.
  452. */
  453. V tryLockAndGet(K key, long time, TimeUnit timeunit) throws TimeoutException;
  454. /**
  455. * Puts the key and value into this map and unlocks the key
  456. * if the calling thread owns the lock.
  457. * <p/>
  458. * <p><b>Warning:</b></p>
  459. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  460. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  461. * defined in <tt>key</tt>'s class.
  462. *
  463. * @param key key of the entry
  464. * @param value value of the entry
  465. */
  466. void putAndUnlock(K key, V value);
  467. /**
  468. * Acquires the lock for the specified key.
  469. * <p>If the lock is not available then
  470. * the current thread becomes disabled for thread scheduling
  471. * purposes and lies dormant until the lock has been acquired.
  472. * <p/>
  473. * Scope of the lock is this map only.
  474. * Acquired lock is only for the key in this map.
  475. * <p/>
  476. * Locks are re-entrant so if the key is locked N times then
  477. * it should be unlocked N times before another thread can acquire it.
  478. * <p/>
  479. * <p><b>Warning:</b></p>
  480. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  481. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  482. * defined in <tt>key</tt>'s class.
  483. *
  484. * @param key key to lock.
  485. */
  486. void lock(K key);
  487. /**
  488. * Checks the lock for the specified key.
  489. * <p>If the lock is acquired then returns true, else false.
  490. * <p/>
  491. * <p><b>Warning:</b></p>
  492. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  493. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  494. * defined in <tt>key</tt>'s class.
  495. *
  496. * @param key key to lock to be checked.
  497. * @return <tt>true</tt> if lock is acquired, <tt>false</tt> otherwise.
  498. */
  499. boolean isLocked(K key);
  500. /**
  501. * Tries to acquire the lock for the specified key.
  502. * <p>If the lock is not available then the current thread
  503. * doesn't wait and returns false immediately.
  504. * <p/>
  505. * <p><b>Warning:</b></p>
  506. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  507. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  508. * defined in <tt>key</tt>'s class.
  509. *
  510. * @param key key to lock.
  511. * @return <tt>true</tt> if lock is acquired, <tt>false</tt> otherwise.
  512. */
  513. boolean tryLock(K key);
  514. /**
  515. * Tries to acquire the lock for the specified key.
  516. * <p>If the lock is not available then
  517. * the current thread becomes disabled for thread scheduling
  518. * purposes and lies dormant until one of two things happens:
  519. * <ul>
  520. * <li>The lock is acquired by the current thread; or
  521. * <li>The specified waiting time elapses
  522. * </ul>
  523. * <p/>
  524. * <p><b>Warning:</b></p>
  525. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  526. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  527. * defined in <tt>key</tt>'s class.
  528. *
  529. * @param key key to lock in this map
  530. * @param time maximum time to wait for the lock
  531. * @param timeunit time unit of the <tt>time</tt> argument.
  532. * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
  533. * if the waiting time elapsed before the lock was acquired.
  534. */
  535. boolean tryLock(K key, long time, TimeUnit timeunit);
  536. /**
  537. * Releases the lock for the specified key. It never blocks and
  538. * returns immediately.
  539. *
  540. * <p>If the current thread is the holder of this lock then the hold
  541. * count is decremented. If the hold count is now zero then the lock
  542. * is released. If the current thread is not the holder of this
  543. * lock then {@link IllegalMonitorStateException} is thrown.
  544. *
  545. * <p/>
  546. * <p><b>Warning:</b></p>
  547. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  548. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  549. * defined in <tt>key</tt>'s class.
  550. *
  551. * @param key key to lock.
  552. * @throws IllegalMonitorStateException if the current thread does not hold this lock
  553. */
  554. void unlock(K key);
  555. /**
  556. * Releases the lock for the specified key regardless of the lock owner.
  557. * It always successfully unlocks the key, never blocks
  558. * and returns immediately.
  559. * <p/>
  560. * <p><b>Warning:</b></p>
  561. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  562. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  563. * defined in <tt>key</tt>'s class.
  564. *
  565. * @param key key to lock.
  566. */
  567. void forceUnlock(K key);
  568. /**
  569. * Tries to acquire the lock for the entire map.
  570. * The thread that locks the map can do all the operations
  571. * but other threads in the cluster cannot operate on the map.
  572. * <p>If the lock is not available then
  573. * the current thread becomes disabled for thread scheduling
  574. * purposes and lies dormant until one of two things happens:
  575. * <ul>
  576. * <li>The lock is acquired by the current thread; or
  577. * <li>The specified waiting time elapses
  578. * </ul>
  579. *
  580. * @param time maximum time to wait for the lock
  581. * @param timeunit time unit of the <tt>time</tt> argument.
  582. * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
  583. * if the waiting time elapsed before the lock was acquired.
  584. */
  585. boolean lockMap(long time, TimeUnit timeunit);
  586. /**
  587. * Unlocks the map. It never blocks and
  588. * returns immediately.
  589. */
  590. void unlockMap();
  591. /**
  592. * Adds a local entry listener for this map. Added listener will be only
  593. * listening for the events (add/remove/update/evict) of the locally owned entries.
  594. * <p/>
  595. * Note that entries in distributed map are partitioned across
  596. * the cluster members; each member owns and manages the some portion of the
  597. * entries. Owned entries are called local entries. This
  598. * listener will be listening for the events of local entries. Let's say
  599. * your cluster has member1 and member2. On member2 you added a local listener and from
  600. * member1, you call <code>map.put(key2, value2)</code>.
  601. * If the key2 is owned by member2 then the local listener will be
  602. * notified for the add/update event. Also note that entries can migrate to
  603. * other nodes for load balancing and/or membership change.
  604. *
  605. * @param listener entry listener
  606. * @see #localKeySet()
  607. */
  608. void addLocalEntryListener(EntryListener<K, V> listener);
  609. /**
  610. * Adds an entry listener for this map. Listener will get notified
  611. * for all map add/remove/update/evict events.
  612. *
  613. * @param listener entry listener
  614. * @param includeValue <tt>true</tt> if <tt>EntryEvent</tt> should
  615. * contain the value.
  616. */
  617. void addEntryListener(EntryListener<K, V> listener, boolean includeValue);
  618. /**
  619. * Removes the specified entry listener
  620. * Returns silently if there is no such listener added before.
  621. *
  622. * @param listener entry listener
  623. */
  624. void removeEntryListener(EntryListener<K, V> listener);
  625. /**
  626. * Adds the specified entry listener for the specified key.
  627. * The listener will get notified for all
  628. * add/remove/update/evict events of the specified key only.
  629. * <p/>
  630. * <p><b>Warning:</b></p>
  631. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  632. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  633. * defined in <tt>key</tt>'s class.
  634. *
  635. * @param listener entry listener
  636. * @param key key to listen
  637. * @param includeValue <tt>true</tt> if <tt>EntryEvent</tt> should
  638. * contain the value.
  639. */
  640. void addEntryListener(EntryListener<K, V> listener, K key, boolean includeValue);
  641. /**
  642. * Removes the specified entry listener for the specified key.
  643. * Returns silently if there is no such listener added before for
  644. * the key.
  645. * <p/>
  646. * <p><b>Warning:</b></p>
  647. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  648. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  649. * defined in <tt>key</tt>'s class.
  650. *
  651. * @param listener
  652. * @param key
  653. */
  654. void removeEntryListener(EntryListener<K, V> listener, K key);
  655. /**
  656. * Returns the <tt>MapEntry</tt> for the specified key.
  657. * <p/>
  658. * <p><b>Warning:</b></p>
  659. * <p>
  660. * This method returns a clone of original mapping, modifying the returned value does not change
  661. * the actual value in the map. One should put modified value back to make changes visible to all nodes.
  662. * </p>
  663. * <p/>
  664. * <p><b>Warning-2:</b></p>
  665. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  666. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  667. * defined in <tt>key</tt>'s class.
  668. *
  669. * @param key key of the entry
  670. * @return <tt>MapEntry</tt> of the specified key
  671. * @see MapEntry
  672. */
  673. MapEntry<K, V> getMapEntry(K key);
  674. /**
  675. * Evicts the specified key from this map. If
  676. * a <tt>MapStore</tt> defined for this map, then the entry is not
  677. * deleted from the underlying <tt>MapStore</tt>, evict only removes
  678. * the entry from the memory.
  679. * <p/>
  680. * <p><b>Warning:</b></p>
  681. * This method uses <tt>hashCode</tt> and <tt>equals</tt> of binary form of
  682. * the <tt>key</tt>, not the actual implementations of <tt>hashCode</tt> and <tt>equals</tt>
  683. * defined in <tt>key</tt>'s class.
  684. *
  685. * @param key key to evict
  686. * @return <tt>true</tt> if the key is evicted, <tt>false</tt> otherwise.
  687. */
  688. boolean evict(Object key);
  689. /**
  690. * Returns a set clone of the keys contained in this map.
  691. * The set is <b>NOT</b> backed by the map,
  692. * so changes to the map are <b>NOT</b> reflected in the set, and vice-versa.
  693. *
  694. * @return a set clone of the keys contained in this map
  695. */
  696. Set<K> keySet();
  697. /**
  698. * Returns a collection clone of the values contained in this map.
  699. * The collection is <b>NOT</b> backed by the map,
  700. * so changes to the map are <b>NOT</b> reflected in the collection, and vice-versa.
  701. *
  702. * @return a collection clone of the values contained in this map
  703. */
  704. Collection<V> values();
  705. /**
  706. * Returns a {@link Set} clone of the mappings contained in this map.
  707. * The set is <b>NOT</b> backed by the map,
  708. * so changes to the map are <b>NOT</b> reflected in the set, and vice-versa.
  709. *
  710. * @return a set clone of the keys mappings in this map
  711. */
  712. Set<Map.Entry<K, V>> entrySet();
  713. /**
  714. * Queries the map based on the specified predicate and
  715. * returns the keys of matching entries.
  716. * <p/>
  717. * Specified predicate runs on all members in parallel.
  718. * <p/>
  719. * <p><b>Warning:</b></p>
  720. * The set is <b>NOT</b> backed by the map,
  721. * so changes to the map are <b>NOT</b> reflected in the set, and vice-versa.
  722. *
  723. * @param predicate query criteria
  724. * @return result key set of the query
  725. */
  726. Set<K> keySet(Predicate predicate);
  727. /**
  728. * Queries the map based on the specified predicate and
  729. * returns the matching entries.
  730. * <p/>
  731. * Specified predicate runs on all members in parallel.
  732. * <p/>
  733. * <p><b>Warning:</b></p>
  734. * The set is <b>NOT</b> backed by the map,
  735. * so changes to the map are <b>NOT</b> reflected in the set, and vice-versa.
  736. *
  737. * @param predicate query criteria
  738. * @return result entry set of the query
  739. */
  740. Set<Map.Entry<K, V>> entrySet(Predicate predicate);
  741. /**
  742. * Queries the map based on the specified predicate and
  743. * returns the values of matching entries.
  744. * <p/>
  745. * Specified predicate runs on all members in parallel.
  746. * <p/>
  747. * <p><b>Warning:</b></p>
  748. * The collection is <b>NOT</b> backed by the map,
  749. * so changes to the map are <b>NOT</b> reflected in the collection, and vice-versa.
  750. *
  751. * @param predicate query criteria
  752. * @return result value collection of the query
  753. */
  754. Collection<V> values(Predicate predicate);
  755. /**
  756. * Returns the locally owned set of keys.
  757. * <p/>
  758. * Each key in this map is owned and managed by a specific
  759. * member in the cluster.
  760. * <p/>
  761. * Note that ownership of these keys might change over time
  762. * so that key ownerships can be almost evenly distributed
  763. * in the cluster.
  764. * <p/>
  765. * <p><b>Warning:</b></p>
  766. * The set is <b>NOT</b> backed by the map,
  767. * so changes to the map are <b>NOT</b> reflected in the set, and vice-versa.
  768. *
  769. * @return locally owned keys.
  770. */
  771. Set<K> localKeySet();
  772. /**
  773. * Returns the keys of matching locally owned entries.
  774. * <p/>
  775. * Each key in this map is owned and managed by a specific
  776. * member in the cluster.
  777. * <p/>
  778. * Note that ownership of these keys might change over time
  779. * so that key ownerships can be almost evenly distributed
  780. * in the cluster.
  781. * <p/>
  782. * <p><b>Warning:</b></p>
  783. * The set is <b>NOT</b> backed by the map,
  784. * so changes to the map are <b>NOT</b> reflected in the set, and vice-versa.
  785. *
  786. * @param predicate query criteria
  787. * @return keys of matching locally owned entries.
  788. */
  789. Set<K> localKeySet(Predicate predicate);
  790. /**
  791. * Adds an index to this map for the specified entries so
  792. * that queries can run faster.
  793. * <p/>
  794. * Let's say your map values are Employee objects.
  795. * <pre>
  796. * public class Employee implements Serializable {
  797. * private boolean active = false;
  798. * private int age;
  799. * private String name = null;
  800. * // other fields.
  801. *
  802. * // getters setter
  803. *
  804. * }
  805. * </pre>
  806. * <p/>
  807. * If you are querying your values mostly based on age and active then
  808. * you should consider indexing these fields.
  809. * <pre>
  810. * IMap imap = Hazelcast.getMap("employees");
  811. * imap.addIndex("age", true); // ordered, since we have ranged queries for this field
  812. * imap.addIndex("active", false); // not ordered, because boolean field cannot have range
  813. * </pre>
  814. * <p/>
  815. * Index attribute should either have a getter method or be public.
  816. * You should also make sure to add the indexes before adding
  817. * entries to this map.
  818. *
  819. * @param attribute attribute of value
  820. * @param ordered <tt>true</tt> if index should be ordered,
  821. * <tt>false</tt> otherwise.
  822. */
  823. void addIndex(String attribute, boolean ordered);
  824. /**
  825. * Adds an index to this map based on the provided expression.
  826. *
  827. * @param expression expression for the index.
  828. * @param ordered <tt>true</tt> if index should be ordered,
  829. * <tt>false</tt> otherwise.
  830. */
  831. void addIndex(Expression<?> expression, boolean ordered);
  832. /**
  833. * Returns LocalMapStats for this map.
  834. * LocalMapStats is the statistics for the local portion of this
  835. * distributed map and contains information such as ownedEntryCount
  836. * backupEntryCount, lastUpdateTime, lockedEntryCount.
  837. * <p/>
  838. * Since this stats are only for the local portion of this map, if you
  839. * need the cluster-wide MapStats then you need to get the LocalMapStats
  840. * from all members of the cluster and combine them.
  841. *
  842. * @return this map's local statistics.
  843. */
  844. LocalMapStats getLocalMapStats();
  845. }