@Override public OTransaction lockRecord(final OIdentifiable iRecord, final OStorage.LOCKING_STRATEGY lockingStrategy) { final OStorage stg = database.getStorage(); if (!(stg.getUnderlying() instanceof OAbstractPaginatedStorage)) throw new OLockException("Cannot lock record across remote connections"); final ORID rid = new ORecordId(iRecord.getIdentity()); LockedRecordMetadata lockedRecordMetadata = locks.get(rid); boolean addItem = false; if (lockedRecordMetadata == null) { lockedRecordMetadata = new LockedRecordMetadata(lockingStrategy); addItem = true; } else if (lockedRecordMetadata.strategy != lockingStrategy) { assert lockedRecordMetadata.locksCount == 0; lockedRecordMetadata = new LockedRecordMetadata(lockingStrategy); addItem = true; } if (lockingStrategy == OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK) ((OAbstractPaginatedStorage) stg.getUnderlying()).acquireWriteLock(rid); else if (lockingStrategy == OStorage.LOCKING_STRATEGY.SHARED_LOCK) ((OAbstractPaginatedStorage) stg.getUnderlying()).acquireReadLock(rid); else throw new IllegalStateException("Unsupported locking strategy " + lockingStrategy); lockedRecordMetadata.locksCount++; if (addItem) { locks.put(rid, lockedRecordMetadata); } return this; }
private ORawBuffer readRecord(final OCluster clusterSegment, final ORecordId rid, final boolean prefetchRecords) { checkOpenness(); if (!rid.isPersistent()) { throw new ORecordNotFoundException(rid, "Cannot read record " + rid + " since the position is invalid in database '" + name + '\''); } if (transaction.get() != null) { // Disabled this assert have no meaning anymore // assert iLockingStrategy.equals(LOCKING_STRATEGY.DEFAULT); return doReadRecord(clusterSegment, rid, prefetchRecords); } stateLock.acquireReadLock(); try { if (pessimisticLock) { acquireReadLock(rid); } checkOpenness(); return doReadRecord(clusterSegment, rid, prefetchRecords); } finally { try { if (pessimisticLock) { releaseReadLock(rid); } } finally { stateLock.releaseReadLock(); } } }
private ORawBuffer readRecordIfNotLatest(final OCluster cluster, final ORecordId rid, final int recordVersion) throws ORecordNotFoundException { checkOpenness(); if (!rid.isPersistent()) { throw new ORecordNotFoundException(rid, "Cannot read record " + rid + " since the position is invalid in database '" + name + '\''); } if (transaction.get() != null) { return doReadRecordIfNotLatest(cluster, rid, recordVersion); } stateLock.acquireReadLock(); try { if (pessimisticLock) { acquireReadLock(rid); } final ORawBuffer buff; checkOpenness(); buff = doReadRecordIfNotLatest(cluster, rid, recordVersion); return buff; } finally { try { if (pessimisticLock) { releaseReadLock(rid); } } finally { stateLock.releaseReadLock(); } } }