Skip to content

Commit

Permalink
Duplicate command performance optimizations (don't create metadata di…
Browse files Browse the repository at this point in the history
…rs if not needed)
  • Loading branch information
ivopetkov committed Jan 4, 2021
1 parent e58e438 commit 984e867
Show file tree
Hide file tree
Showing 2 changed files with 685 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,18 +890,44 @@ public function execute(array $commands): array
$sourceKey = $getProperty('sourceKey', true);
$targetKey = $getProperty('targetKey', true);
$prepareFileForReading($this->objectsDir . $sourceKey, true);
$prepareFileForReading($this->metadataDir . $sourceKey);
$prepareFileForWriting($this->objectsDir . $targetKey);
$prepareFileForWriting($this->metadataDir . $targetKey);
$functions[$index] = function () use ($sourceKey, $targetKey, $getFileContent, $setFileContent, $deleteFile) {
$noSourceMetadata = false;
if (!isset($filePointers[$this->metadataDir . $sourceKey])) { // Not opened for writing and does not exists
if ($logStorageAccess) {
$this->internalStorageAccessLog[] = ['is_file', str_replace([$this->objectsDir, $this->metadataDir], ['OBJECTSDIR/', 'METADATADIR/'], $this->metadataDir . $sourceKey), 'Duplicate command.'];
}
if (!is_file($this->metadataDir . $sourceKey)) {
$noSourceMetadata = true; // Optimization: Do not prepare and delete the source metadata file because it does not exist.
}
}
$noSourceAndNoTargetMetadata = false;
if ($noSourceMetadata) {
if (!isset($filePointers[$this->metadataDir . $targetKey])) { // Not opened for writing and does not exists
if ($logStorageAccess) {
$this->internalStorageAccessLog[] = ['is_file', str_replace([$this->objectsDir, $this->metadataDir], ['OBJECTSDIR/', 'METADATADIR/'], $this->metadataDir . $targetKey), 'Duplicate command.'];
}
if (!is_file($this->metadataDir . $targetKey)) {
$noSourceAndNoTargetMetadata = true; // Optimization: Do not prepare and delete the source metadata file because it does not exist.
}
}
}
if (!$noSourceMetadata) {
$prepareFileForReading($this->metadataDir . $sourceKey);
}
if (!$noSourceAndNoTargetMetadata) {
$prepareFileForWriting($this->metadataDir . $targetKey);
}
$functions[$index] = function () use ($sourceKey, $targetKey, $getFileContent, $setFileContent, $deleteFile, $noSourceMetadata, $noSourceAndNoTargetMetadata) {
$sourceBody = $getFileContent($this->objectsDir . $sourceKey);
if ($sourceBody === null) { // The source file is deleted in previous command
throw new \IvoPetkov\ObjectStorage\ObjectNotFoundException('The source object (' . $sourceKey . ') does not exists!');
} else {
$sourceMetadata = $getFileContent($this->metadataDir . $sourceKey);
$sourceMetadata = $noSourceMetadata ? null : $getFileContent($this->metadataDir . $sourceKey);
$setFileContent($this->objectsDir . $targetKey, $sourceBody);
if ($sourceMetadata === null) {
$deleteFile($this->metadataDir . $targetKey);
if (!$noSourceAndNoTargetMetadata) {
$deleteFile($this->metadataDir . $targetKey);
}
} else {
$setFileContent($this->metadataDir . $targetKey, $sourceMetadata);
}
Expand Down Expand Up @@ -936,9 +962,7 @@ public function execute(array $commands): array
if (!$noSourceMetadata) {
$prepareFileForWriting($this->metadataDir . $sourceKey);
}
if ($noSourceAndNoTargetMetadata) {
// Optimization: Do not prepare the target metadata file.
} else {
if (!$noSourceAndNoTargetMetadata) {
$prepareFileForWriting($this->metadataDir . $targetKey);
}
$functions[$index] = function () use ($sourceKey, $targetKey, $getFileContent, $setFileContent, $deleteFile, $noSourceMetadata, $noSourceAndNoTargetMetadata) {
Expand All @@ -949,9 +973,7 @@ public function execute(array $commands): array
$sourceMetadata = $noSourceMetadata ? null : $getFileContent($this->metadataDir . $sourceKey);
$setFileContent($this->objectsDir . $targetKey, $sourceBody);
if ($sourceMetadata === null) {
if ($noSourceAndNoTargetMetadata) {
// Optimization: Not prepared
} else {
if (!$noSourceAndNoTargetMetadata) {
$deleteFile($this->metadataDir . $targetKey);
}
} else {
Expand Down
Loading

0 comments on commit 984e867

Please sign in to comment.