Skip to content

Commit

Permalink
Delete command optimizations. Updated exceptions texts.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Nov 13, 2017
1 parent b14d429 commit 7525b26
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
47 changes: 30 additions & 17 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ public function execute(array $commands): array
return;
}
if (is_dir($filename)) {
throw new \IvoPetkov\ObjectStorage\ErrorException('The file ' . $filename . ' is not writable.');
throw new \IvoPetkov\ObjectStorage\ErrorException('The file ' . $filename . ' is not writable (dir with the same name exists).');
}
if ($this->createFileDirIfNotExists($filename) === false) {
throw new \IvoPetkov\ObjectStorage\ErrorException('The file ' . $filename . ' is not writable.');
throw new \IvoPetkov\ObjectStorage\ErrorException('The file ' . $filename . ' is not writable (cannot create dir).');
}
$getFilePointer = function() use ($filename, &$emptyOpenedFiles) {
clearstatcache(false, $filename);
Expand Down Expand Up @@ -372,7 +372,7 @@ public function execute(array $commands): array
}
}
if (!$isParentDirReadable) {
throw new \IvoPetkov\ObjectStorage\ErrorException('The file ' . $filename . ' is not readable.');
throw new \IvoPetkov\ObjectStorage\ErrorException('The file ' . $filename . ' is not readable (parent dir is not readable).');
}
}
};
Expand Down Expand Up @@ -408,7 +408,7 @@ public function execute(array $commands): array
unset($emptyOpenedFiles[$filename]);
}
} else {
throw new \IvoPetkov\ObjectStorage\ErrorException('File ' . $filename . ' is not opened for writing!');
throw new \Exception('Internal error! File ' . $filename . ' is not opened for writing! Should not get here!');
}
};

Expand Down Expand Up @@ -639,11 +639,8 @@ public function execute(array $commands): array
$prepareFileForWriting($this->objectsDir . $key);
}
if ($modifyMetadata && sizeof($metadata) === 1 && isset($metadata['*']) && $metadata['*'] === '') { // Check for setting empty metadata
if (isset($filePointers[$this->metadataDir . $key])) { // Other command opened it and may change change it
} else {
if (!is_file($this->metadataDir . $key)) { // The file does not exist
$modifyMetadata = false;
}
if (!isset($filePointers[$this->metadataDir . $key]) && !is_file($this->metadataDir . $key)) { // Not opened for writing and does not exists
$modifyMetadata = false;
}
}
if ($modifyMetadata) {
Expand Down Expand Up @@ -689,11 +686,27 @@ public function execute(array $commands): array
};
} elseif ($command === 'delete') {
$key = $getProperty('key', true);
$prepareFileForWriting($this->objectsDir . $key);
$prepareFileForWriting($this->metadataDir . $key);
$functions[$index] = function() use ($key, $deleteFile) {
$deleteFile($this->objectsDir . $key);
$deleteFile($this->metadataDir . $key);
$deleteObjectFile = true;
if (!isset($filePointers[$this->objectsDir . $key]) && !is_file($this->objectsDir . $key)) { // Not opened for writing and does not exists
$deleteObjectFile = false;
}
$deleteMetadataFile = true;
if (!isset($filePointers[$this->metadataDir . $key]) && !is_file($this->metadataDir . $key)) { // Not opened for writing and does not exists
$deleteMetadataFile = false;
}
if ($deleteObjectFile) {
$prepareFileForWriting($this->objectsDir . $key);
}
if ($deleteMetadataFile) {
$prepareFileForWriting($this->metadataDir . $key);
}
$functions[$index] = function() use ($key, $deleteObjectFile, $deleteMetadataFile, $deleteFile) {
if ($deleteObjectFile) {
$deleteFile($this->objectsDir . $key);
}
if ($deleteMetadataFile) {
$deleteFile($this->metadataDir . $key);
}
return true;
};
} elseif ($command === 'duplicate') {
Expand All @@ -702,7 +715,7 @@ public function execute(array $commands): array
$prepareFileForReading($this->objectsDir . $sourceKey, true);
$prepareFileForReading($this->metadataDir . $sourceKey);
$prepareFileForWriting($this->objectsDir . $targetKey);
$prepareFileForWriting($this->metadataDir . $targetKey); // todo optimize if source has metadata or current has metadata
$prepareFileForWriting($this->metadataDir . $targetKey);
$functions[$index] = function() use ($sourceKey, $targetKey, $getFileContent, $setFileContent, $deleteFile) {
$sourceBody = $getFileContent($this->objectsDir . $sourceKey);
if ($sourceBody === null) { // The source file is deleted in previous command
Expand All @@ -723,9 +736,9 @@ public function execute(array $commands): array
$targetKey = $getProperty('targetKey', true);
$prepareFileForReading($this->objectsDir . $sourceKey, true);
$prepareFileForWriting($this->objectsDir . $sourceKey);
$prepareFileForWriting($this->metadataDir . $sourceKey); // todo optimizations maybe???
$prepareFileForWriting($this->metadataDir . $sourceKey);
$prepareFileForWriting($this->objectsDir . $targetKey);
$prepareFileForWriting($this->metadataDir . $targetKey); // todo optimizations maybe???
$prepareFileForWriting($this->metadataDir . $targetKey);
$functions[$index] = function() use ($sourceKey, $targetKey, $getFileContent, $setFileContent, $deleteFile) {
$sourceBody = $getFileContent($this->objectsDir . $sourceKey);
if ($sourceBody === null) { // The source file is deleted in previous command
Expand Down
1 change: 1 addition & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ public function testDataState1()
]
]
);

$this->assertTrue($result === array(
0 => true,
1 => true,
Expand Down
13 changes: 4 additions & 9 deletions tests/KeyCollisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function testFileWhenThereIsDirWithTheSameKey()
'body' => 'data'
]
);

//echo "\n\n" . var_export($result) . "\n\n" . getState() . "\n\n";
}

/**
Expand Down Expand Up @@ -69,8 +67,6 @@ public function testDirWhenThereIsFileWithTheSameKey()
'body' => 'data'
]
);

//echo "\n\n" . var_export($result) . "\n\n" . getState() . "\n\n";
}

/**
Expand Down Expand Up @@ -106,14 +102,13 @@ public function testDeleteKeyWhenItsDir()

$this->createFile($this->getDataDir() . '/objects/data1/key1', 'content');

$this->assertTrue($this->checkState('3fb020a8835e135fb296ad20b43ba773
objects/data1/key1: content
'));

$this->setExpectedException('\IvoPetkov\ObjectStorage\ErrorException');
$objectStorage->delete([
'key' => 'data1'
]);

$this->assertTrue($this->checkState('3fb020a8835e135fb296ad20b43ba773
objects/data1/key1: content
'));
}

}

0 comments on commit 7525b26

Please sign in to comment.