Search in sources :

Example 6 with OModifiableInteger

use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.

the class OSBTreeRidBag method updateSize.

/**
   * Recalculates real bag size.
   *
   * @return real size
   */
private int updateSize() {
    int size = 0;
    if (collectionPointer != null) {
        final OSBTreeBonsai<OIdentifiable, Integer> tree = loadTree();
        try {
            size = tree.getRealBagSize(changes);
        } finally {
            releaseTree();
        }
    } else {
        for (Change change : changes.values()) {
            size += change.applyTo(0);
        }
    }
    for (OModifiableInteger diff : newEntries.values()) {
        size += diff.getValue();
    }
    this.size = size;
    return size;
}
Also used : OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger) OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger) Change(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag.Change)

Example 7 with OModifiableInteger

use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.

the class OReadersWriterSpinLock method releaseReadLock.

public void releaseReadLock() {
    final OModifiableInteger lHolds = lockHolds.get();
    final int holds = lHolds.intValue();
    if (holds > 1) {
        lHolds.decrement();
        return;
    } else if (holds < 0) {
        // write lock was acquired before, do nothing
        return;
    }
    distributedCounter.decrement();
    lHolds.decrement();
    assert lHolds.intValue() == 0;
}
Also used : OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger)

Example 8 with OModifiableInteger

use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.

the class OReadersWriterSpinLock method acquireWriteLock.

public void acquireWriteLock() {
    final OModifiableInteger lHolds = lockHolds.get();
    if (lHolds.intValue() < 0) {
        lHolds.decrement();
        return;
    }
    final WNode node = myNode.get();
    node.locked = true;
    final WNode pNode = tail.getAndSet(myNode.get());
    predNode.set(pNode);
    while (pNode.locked) {
        pNode.waitingWriter = Thread.currentThread();
        if (pNode.locked)
            LockSupport.park(this);
    }
    pNode.waitingWriter = null;
    final long beginTime = System.currentTimeMillis();
    while (!distributedCounter.isEmpty()) {
        // IN THE WORST CASE CPU CAN BE 100% FOR MAXIMUM 1 SECOND
        if (System.currentTimeMillis() - beginTime > 1000)
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                break;
            }
    }
    setExclusiveOwnerThread(Thread.currentThread());
    lHolds.decrement();
    assert lHolds.intValue() == -1;
}
Also used : OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger)

Example 9 with OModifiableInteger

use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.

the class OReadersWriterSpinLock method releaseWriteLock.

public void releaseWriteLock() {
    final OModifiableInteger lHolds = lockHolds.get();
    if (lHolds.intValue() < -1) {
        lHolds.increment();
        return;
    }
    setExclusiveOwnerThread(null);
    final WNode node = myNode.get();
    node.locked = false;
    final Thread waitingWriter = node.waitingWriter;
    if (waitingWriter != null)
        LockSupport.unpark(waitingWriter);
    Thread waitingReader;
    while ((waitingReader = node.waitingReaders.poll()) != null) {
        LockSupport.unpark(waitingReader);
    }
    myNode.set(predNode.get());
    predNode.set(null);
    lHolds.increment();
    assert lHolds.intValue() == 0;
}
Also used : OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger)

Example 10 with OModifiableInteger

use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.

the class OSBTreeRidBag method add.

public void add(final OIdentifiable identifiable) {
    if (identifiable == null)
        throw new NullPointerException("Impossible to add a null identifiable in a ridbag");
    if (identifiable.getIdentity().isValid()) {
        Change counter = changes.get(identifiable);
        if (counter == null)
            changes.put(identifiable, new DiffChange(1));
        else {
            if (counter.isUndefined()) {
                counter = getAbsoluteValue(identifiable);
                changes.put(identifiable, counter);
            }
            counter.increment();
        }
    } else {
        final OModifiableInteger counter = newEntries.get(identifiable);
        if (counter == null)
            newEntries.put(identifiable, new OModifiableInteger(1));
        else
            counter.increment();
    }
    if (size >= 0)
        size++;
    if (this.owner != null)
        ORecordInternal.track(this.owner, identifiable);
    if (updateOwner)
        fireCollectionChangedEvent(new OMultiValueChangeEvent<OIdentifiable, OIdentifiable>(OMultiValueChangeEvent.OChangeType.ADD, identifiable, identifiable, null, false));
}
Also used : OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger) Change(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag.Change)

Aggregations

OModifiableInteger (com.orientechnologies.common.types.OModifiableInteger)17 Change (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag.Change)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OSBTreeRidBag (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag)1 OBonsaiBucketPointer (com.orientechnologies.orient.core.index.sbtreebonsai.local.OBonsaiBucketPointer)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)1 ORecordSerializationContext (com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext)1 ORidBagUpdateSerializationOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.ORidBagUpdateSerializationOperation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1