use of com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation in project orientdb by orientechnologies.
the class OPaginatedCluster method updateRecord.
public void updateRecord(final long clusterPosition, byte[] content, final int recordVersion, final byte recordType) throws IOException {
startOperation();
OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
if (statistic != null)
statistic.startRecordUpdateTimer();
try {
content = compression.compress(content);
content = encryption.encrypt(content);
OAtomicOperation atomicOperation = startAtomicOperation(true);
acquireExclusiveLock();
try {
final OClusterPositionMapBucket.PositionEntry positionEntry = clusterPositionMap.get(clusterPosition, 1);
if (positionEntry == null) {
endAtomicOperation(false, null);
return;
}
int nextRecordPosition = positionEntry.getRecordPosition();
long nextPageIndex = positionEntry.getPageIndex();
int newRecordPosition = -1;
long newPageIndex = -1;
long prevPageIndex = -1;
int prevRecordPosition = -1;
long nextEntryPointer = -1;
int from = 0;
int to;
long sizeDiff = 0;
byte[] updateEntry = null;
do {
final int entrySize;
final int updatedEntryPosition;
if (updateEntry == null) {
if (from == 0) {
entrySize = Math.min(getEntryContentLength(content.length), OClusterPage.MAX_RECORD_SIZE);
to = entrySize - (2 * OByteSerializer.BYTE_SIZE + OIntegerSerializer.INT_SIZE + OLongSerializer.LONG_SIZE);
} else {
entrySize = Math.min(content.length - from + OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE, OClusterPage.MAX_RECORD_SIZE);
to = from + entrySize - (OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE);
}
updateEntry = new byte[entrySize];
int entryPosition = 0;
if (from == 0) {
updateEntry[entryPosition] = recordType;
entryPosition++;
OIntegerSerializer.INSTANCE.serializeNative(content.length, updateEntry, entryPosition);
entryPosition += OIntegerSerializer.INT_SIZE;
}
System.arraycopy(content, from, updateEntry, entryPosition, to - from);
entryPosition += to - from;
if (nextPageIndex == positionEntry.getPageIndex())
updateEntry[entryPosition] = 1;
entryPosition++;
OLongSerializer.INSTANCE.serializeNative(-1, updateEntry, entryPosition);
if (to < content.length) {
assert entrySize == OClusterPage.MAX_RECORD_SIZE;
}
} else {
entrySize = updateEntry.length;
if (from == 0) {
to = entrySize - (2 * OByteSerializer.BYTE_SIZE + OIntegerSerializer.INT_SIZE + OLongSerializer.LONG_SIZE);
} else {
to = from + entrySize - (OByteSerializer.BYTE_SIZE + OLongSerializer.LONG_SIZE);
}
}
int freePageIndex = -1;
if (nextPageIndex < 0) {
FindFreePageResult findFreePageResult = findFreePage(fileId, pinnedStateEntryIndex, entrySize, atomicOperation);
nextPageIndex = findFreePageResult.pageIndex;
freePageIndex = findFreePageResult.freePageIndex;
}
boolean isNew = false;
OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, nextPageIndex, false);
if (cacheEntry == null) {
cacheEntry = addPage(atomicOperation, fileId);
isNew = true;
}
cacheEntry.acquireExclusiveLock();
try {
final OClusterPage localPage = new OClusterPage(cacheEntry, isNew, getChanges(atomicOperation, cacheEntry));
final int pageFreeSpace = localPage.getFreeSpace();
if (freePageIndex < 0)
freePageIndex = calculateFreePageIndex(localPage);
else
assert isNew || freePageIndex == calculateFreePageIndex(localPage);
if (nextRecordPosition >= 0) {
if (localPage.isDeleted(nextRecordPosition))
throw new OPaginatedClusterException("Record with rid " + new ORecordId(id, clusterPosition) + " was deleted", this);
int currentEntrySize = localPage.getRecordSize(nextRecordPosition);
nextEntryPointer = localPage.getRecordLongValue(nextRecordPosition, currentEntrySize - OLongSerializer.LONG_SIZE);
if (currentEntrySize == entrySize) {
localPage.replaceRecord(nextRecordPosition, updateEntry, recordVersion);
updatedEntryPosition = nextRecordPosition;
} else {
localPage.deleteRecord(nextRecordPosition);
if (localPage.getMaxRecordSize() >= entrySize) {
updatedEntryPosition = localPage.appendRecord(recordVersion, updateEntry);
if (updatedEntryPosition < 0) {
throw new IllegalStateException("Page " + cacheEntry.getPageIndex() + " does not have enough free space to add record content");
}
} else {
updatedEntryPosition = -1;
}
}
if (nextEntryPointer >= 0) {
nextRecordPosition = getRecordPosition(nextEntryPointer);
nextPageIndex = getPageIndex(nextEntryPointer);
} else {
nextPageIndex = -1;
nextRecordPosition = -1;
}
} else {
assert localPage.getFreeSpace() >= entrySize;
updatedEntryPosition = localPage.appendRecord(recordVersion, updateEntry);
if (updatedEntryPosition < 0) {
throw new IllegalStateException("Page " + cacheEntry.getPageIndex() + " does not have enough free space to add record content");
}
nextPageIndex = -1;
nextRecordPosition = -1;
}
sizeDiff += pageFreeSpace - localPage.getFreeSpace();
} finally {
cacheEntry.releaseExclusiveLock();
releasePage(atomicOperation, cacheEntry);
}
updateFreePagesIndex(fileId, pinnedStateEntryIndex, freePageIndex, cacheEntry.getPageIndex(), atomicOperation);
if (updatedEntryPosition >= 0) {
if (from == 0) {
newPageIndex = cacheEntry.getPageIndex();
newRecordPosition = updatedEntryPosition;
}
from = to;
if (prevPageIndex >= 0) {
OCacheEntry prevCacheEntry = loadPage(atomicOperation, fileId, prevPageIndex, false);
prevCacheEntry.acquireExclusiveLock();
try {
OClusterPage prevPage = new OClusterPage(prevCacheEntry, false, getChanges(atomicOperation, prevCacheEntry));
prevPage.setRecordLongValue(prevRecordPosition, -OLongSerializer.LONG_SIZE, createPagePointer(cacheEntry.getPageIndex(), updatedEntryPosition));
} finally {
prevCacheEntry.releaseExclusiveLock();
releasePage(atomicOperation, prevCacheEntry);
}
}
prevPageIndex = cacheEntry.getPageIndex();
prevRecordPosition = updatedEntryPosition;
updateEntry = null;
}
} while (to < content.length || updateEntry != null);
// clear unneeded pages
while (nextEntryPointer >= 0) {
nextPageIndex = getPageIndex(nextEntryPointer);
nextRecordPosition = getRecordPosition(nextEntryPointer);
final int freePagesIndex;
final int freeSpace;
OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, nextPageIndex, false);
cacheEntry.acquireExclusiveLock();
try {
final OClusterPage localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
freeSpace = localPage.getFreeSpace();
freePagesIndex = calculateFreePageIndex(localPage);
nextEntryPointer = localPage.getRecordLongValue(nextRecordPosition, -OLongSerializer.LONG_SIZE);
localPage.deleteRecord(nextRecordPosition);
sizeDiff += freeSpace - localPage.getFreeSpace();
} finally {
cacheEntry.releaseExclusiveLock();
releasePage(atomicOperation, cacheEntry);
}
updateFreePagesIndex(fileId, pinnedStateEntryIndex, freePagesIndex, nextPageIndex, atomicOperation);
}
assert newPageIndex >= 0;
assert newRecordPosition >= 0;
if (newPageIndex != positionEntry.getPageIndex() || newRecordPosition != positionEntry.getRecordPosition()) {
clusterPositionMap.update(clusterPosition, new OClusterPositionMapBucket.PositionEntry(newPageIndex, newRecordPosition));
}
updateClusterState(fileId, pinnedStateEntryIndex, 0, sizeDiff, atomicOperation);
addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
endAtomicOperation(false, null);
} catch (RuntimeException e) {
endAtomicOperation(true, e);
throw OException.wrapException(new OPaginatedClusterException("Error during record update", this), e);
} finally {
releaseExclusiveLock();
}
} finally {
if (statistic != null)
statistic.stopRecordUpdateTimer();
completeOperation();
}
}
use of com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation in project orientdb by orientechnologies.
the class OPaginatedCluster method open.
@Override
public void open() throws IOException {
startOperation();
try {
acquireExclusiveLock();
try {
final OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
fileId = openFile(atomicOperation, getFullName());
final OCacheEntry pinnedStateEntry = loadPage(atomicOperation, fileId, 0, false);
try {
pinPage(atomicOperation, pinnedStateEntry);
pinnedStateEntryIndex = pinnedStateEntry.getPageIndex();
} finally {
releasePage(atomicOperation, pinnedStateEntry);
}
clusterPositionMap.open();
} finally {
releaseExclusiveLock();
}
} finally {
completeOperation();
}
}
use of com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation in project orientdb by orientechnologies.
the class OPaginatedCluster method delete.
@Override
public void delete() throws IOException {
startOperation();
try {
final OAtomicOperation atomicOperation = startAtomicOperation(false);
acquireExclusiveLock();
try {
deleteFile(atomicOperation, fileId);
clusterPositionMap.delete();
endAtomicOperation(false, null);
} catch (IOException ioe) {
endAtomicOperation(true, ioe);
throw ioe;
} catch (Exception e) {
endAtomicOperation(true, e);
throw OException.wrapException(new OPaginatedClusterException("Error during deletion of cluster " + getName(), this), e);
} finally {
releaseExclusiveLock();
}
} finally {
completeOperation();
}
}
use of com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation in project orientdb by orientechnologies.
the class OPaginatedCluster method readRecordBuffer.
private ORawBuffer readRecordBuffer(long clusterPosition, int pageCount, OClusterPositionMapBucket.PositionEntry positionEntry) throws IOException {
final int recordPosition = positionEntry.getRecordPosition();
final long pageIndex = positionEntry.getPageIndex();
final OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
if (getFilledUpTo(atomicOperation, fileId) <= pageIndex)
return null;
int recordVersion;
final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false, pageCount);
cacheEntry.acquireSharedLock();
try {
final OClusterPage localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
if (localPage.isDeleted(recordPosition))
return null;
recordVersion = localPage.getRecordVersion(recordPosition);
} finally {
cacheEntry.releaseSharedLock();
releasePage(atomicOperation, cacheEntry);
}
final byte[] fullContent = readFullEntry(clusterPosition, pageIndex, recordPosition, atomicOperation, pageCount);
if (fullContent == null)
return null;
int fullContentPosition = 0;
final byte recordType = fullContent[fullContentPosition];
fullContentPosition++;
final int readContentSize = OIntegerSerializer.INSTANCE.deserializeNative(fullContent, fullContentPosition);
fullContentPosition += OIntegerSerializer.INT_SIZE;
byte[] recordContent = compression.uncompress(fullContent, fullContentPosition, readContentSize);
recordContent = encryption.decrypt(recordContent);
return new ORawBuffer(recordContent, recordVersion, recordType);
}
use of com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation in project orientdb by orientechnologies.
the class OPaginatedCluster method createRecord.
public OPhysicalPosition createRecord(byte[] content, final int recordVersion, final byte recordType, OPhysicalPosition allocatedPosition) throws IOException {
startOperation();
OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
if (statistic != null)
statistic.startRecordCreationTimer();
try {
content = compression.compress(content);
content = encryption.encrypt(content);
OAtomicOperation atomicOperation = startAtomicOperation(true);
acquireExclusiveLock();
try {
final RecordCreationResult recordCreationResult = createDataRecord(fileId, pinnedStateEntryIndex, content, recordVersion, recordType, atomicOperation);
final long clusterPosition;
if (allocatedPosition != null) {
clusterPositionMap.update(allocatedPosition.clusterPosition, new OClusterPositionMapBucket.PositionEntry(recordCreationResult.pageIndex, recordCreationResult.recordPosition));
clusterPosition = allocatedPosition.clusterPosition;
} else
clusterPosition = clusterPositionMap.add(recordCreationResult.pageIndex, recordCreationResult.recordPosition);
addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
endAtomicOperation(false, null);
return createPhysicalPosition(recordType, clusterPosition, recordCreationResult.recordVersion);
} finally {
releaseExclusiveLock();
}
} finally {
if (statistic != null)
statistic.stopRecordCreationTimer();
completeOperation();
}
}
Aggregations