PageRenderTime 27ms CodeModel.GetById 8ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 132 lines | 96 code | 17 blank | 19 comment | 27 complexity | bdf74c9809327d68ad115cc36e67a45a 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.impl.base.DistributedLock;
 20import com.hazelcast.impl.concurrentmap.ValueHolder;
 21import com.hazelcast.nio.Data;
 22
 23import static com.hazelcast.nio.IOUtil.toObject;
 24
 25@SuppressWarnings("SynchronizeOnThis")
 26public final class DefaultRecord extends AbstractRecord {
 27
 28    private volatile Object valueObject ;
 29    private volatile Data value;
 30
 31    public DefaultRecord(CMap cmap, int blockId, Data key, Data value, long ttl, long maxIdleMillis, long id) {
 32        super(cmap, blockId, key, ttl, maxIdleMillis, id);
 33        this.value = value;
 34    }
 35
 36    public Record copy() {
 37        Record recordCopy = new DefaultRecord(cmap, blockId, key, value, getRemainingTTL(), getRemainingIdle(), id);
 38        if (optionalInfo != null) {
 39            recordCopy.setIndexes(getOptionalInfo().indexes, getOptionalInfo().indexTypes);
 40            recordCopy.setMultiValues(getOptionalInfo().lsMultiValues);
 41        }
 42        final DistributedLock dl = lock;
 43        if (dl != null) {
 44            recordCopy.setLock(new DistributedLock(dl));
 45        }
 46        recordCopy.setVersion(getVersion());
 47        return recordCopy;
 48    }
 49
 50    public Data getValueData() {
 51        return value;
 52    }
 53
 54    public Object getValue() {
 55        if (cmap.isCacheValue()) {
 56            final Object currentValue = valueObject;
 57            if (currentValue != null) {
 58                return currentValue;
 59            }
 60            synchronized (this) {
 61                if (valueObject != null) {
 62                    return valueObject;
 63                }
 64                final Object v = toObject(value);
 65                valueObject = v;
 66                return v;
 67            }
 68
 69        } else {
 70            return toObject(value);
 71        }
 72    }
 73
 74    public Object setValue(Object value) {
 75        Object oldValue = getValue();
 76        valueObject = value;
 77        return oldValue;
 78    }
 79
 80    protected void invalidateValueCache() {
 81        if (cmap.isCacheValue()) {
 82            valueObject = null;
 83        }
 84    }
 85
 86    public void setValueData(Data value) {
 87        this.value = value;
 88        // invalidation should be called after value is set!
 89        // otherwise a call to getValue() from another thread
 90        // may cause stale data to be read when cacheValue is true.
 91        invalidateValueCache();
 92    }
 93
 94    public int valueCount() {
 95        int count = 0;
 96        if (hasValueData()) {
 97            count = 1;
 98        } else if (getMultiValues() != null) {
 99            count = getMultiValues().size();
100        }
101        return count;
102    }
103
104    public long getCost() {
105        long cost = 0;
106        // avoid race condition with local references
107        final Data dataValue = getValueData();
108        final Data dataKey = getKeyData();
109        if (dataValue != null) {
110            cost = dataValue.size();
111            if (valueObject != null) {
112                cost += dataValue.size();
113            }
114        } else if (getMultiValues() != null && getMultiValues().size() > 0) {
115            for (ValueHolder valueHolder : getMultiValues()) {
116                if (valueHolder != null) {
117                    cost += valueHolder.getData().size();
118                }
119            }
120        }
121        return cost + dataKey.size() + 312;
122    }
123
124    public boolean hasValueData() {
125        return value != null;
126    }
127
128    public void invalidate() {
129        value = null;
130        invalidateValueCache();
131    }
132}