PageRenderTime 64ms CodeModel.GetById 2ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 0ms

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