refactor: change storage tree
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -71,6 +71,6 @@ final class Provider implements ProviderBaseInterface
|
|||||||
|
|
||||||
private function service(string $tenantId): Service
|
private function service(string $tenantId): Service
|
||||||
{
|
{
|
||||||
return new Service($this->rootDir . '/storage/system-store/' . hash('sha256', $tenantId) . '/default');
|
return new Service($this->rootDir . '/storage/' . $tenantId . '/system');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ use KTXF\SystemStore\WriteConflictException;
|
|||||||
|
|
||||||
final class Service implements SystemStoreServiceInterface
|
final class Service implements SystemStoreServiceInterface
|
||||||
{
|
{
|
||||||
|
private const BLOB_EXTENSION = '.blob';
|
||||||
|
private const META_EXTENSION = '.meta';
|
||||||
|
private const LOCK_EXTENSION = '.lock';
|
||||||
|
|
||||||
public function __construct(private readonly string $root)
|
public function __construct(private readonly string $root)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -64,7 +68,7 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
if ($info === null) {
|
if ($info === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$handle = fopen($this->dataPath($key), 'rb');
|
$handle = fopen($this->blobPath($key), 'rb');
|
||||||
if ($handle === false) {
|
if ($handle === false) {
|
||||||
throw new SystemStoreException("Unable to open local blob '{$key}' for reading");
|
throw new SystemStoreException("Unable to open local blob '{$key}' for reading");
|
||||||
}
|
}
|
||||||
@@ -105,10 +109,9 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
$metaTemp = null;
|
$metaTemp = null;
|
||||||
try {
|
try {
|
||||||
$this->assertCondition($this->statUnlocked($key), $condition);
|
$this->assertCondition($this->statUnlocked($key), $condition);
|
||||||
$dataPath = $this->dataPath($key);
|
$dataPath = $this->blobPath($key);
|
||||||
$metaPath = $this->metaPath($key);
|
$metaPath = $this->metaPath($key);
|
||||||
$this->ensureParent($dataPath, $this->dataRoot());
|
$this->ensureParent($dataPath);
|
||||||
$this->ensureParent($metaPath, $this->metaRoot());
|
|
||||||
|
|
||||||
$dataTemp = tempnam(dirname($dataPath), '.write-');
|
$dataTemp = tempnam(dirname($dataPath), '.write-');
|
||||||
if ($dataTemp === false || ($handle = fopen($dataTemp, 'wb')) === false) {
|
if ($dataTemp === false || ($handle = fopen($dataTemp, 'wb')) === false) {
|
||||||
@@ -187,7 +190,7 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
if ($current === null) {
|
if ($current === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!unlink($this->dataPath($key))) {
|
if (!unlink($this->blobPath($key))) {
|
||||||
throw new SystemStoreException("Unable to delete local blob '{$key}'");
|
throw new SystemStoreException("Unable to delete local blob '{$key}'");
|
||||||
}
|
}
|
||||||
$metaPath = $this->metaPath($key);
|
$metaPath = $this->metaPath($key);
|
||||||
@@ -205,7 +208,7 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
if ($prefix !== '') {
|
if ($prefix !== '') {
|
||||||
$prefix = $this->validateKey($prefix, true);
|
$prefix = $this->validateKey($prefix, true);
|
||||||
}
|
}
|
||||||
$root = $this->dataRoot();
|
$root = $this->root;
|
||||||
if (!is_dir($root)) {
|
if (!is_dir($root)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -214,7 +217,12 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
if (!$file->isFile() || $file->isLink()) {
|
if (!$file->isFile() || $file->isLink()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$key = str_replace(DIRECTORY_SEPARATOR, '/', substr($file->getPathname(), strlen($root) + 1));
|
$pathname = $file->getPathname();
|
||||||
|
if (!str_ends_with($pathname, self::BLOB_EXTENSION)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$relative = substr($pathname, strlen($root) + 1, -strlen(self::BLOB_EXTENSION));
|
||||||
|
$key = str_replace(DIRECTORY_SEPARATOR, '/', $relative);
|
||||||
if ($prefix !== '' && !str_starts_with($key, $prefix)) {
|
if ($prefix !== '' && !str_starts_with($key, $prefix)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -227,11 +235,11 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
|
|
||||||
private function statUnlocked(string $key): ?BlobInfo
|
private function statUnlocked(string $key): ?BlobInfo
|
||||||
{
|
{
|
||||||
$path = $this->dataPath($key);
|
$path = $this->blobPath($key);
|
||||||
if (!is_file($path) || is_link($path)) {
|
if (!is_file($path) || is_link($path)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$this->assertExistingPathInside($path, $this->dataRoot());
|
$this->assertExistingPathInside($path, $this->root);
|
||||||
$size = filesize($path);
|
$size = filesize($path);
|
||||||
$modified = filemtime($path);
|
$modified = filemtime($path);
|
||||||
$hash = hash_file('sha256', $path);
|
$hash = hash_file('sha256', $path);
|
||||||
@@ -283,10 +291,10 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
/** @return resource */
|
/** @return resource */
|
||||||
private function lock(string $key, int $operation)
|
private function lock(string $key, int $operation)
|
||||||
{
|
{
|
||||||
$root = $this->root . '/locks';
|
$path = $this->lockPath($key);
|
||||||
$this->ensureDirectory($root);
|
$this->ensureDirectory(dirname($path));
|
||||||
$this->assertExistingPathInside($root, $this->root);
|
$this->assertExistingPathInside(dirname($path), $this->root);
|
||||||
$handle = fopen($root . '/' . hash('sha256', $key) . '.lock', 'c');
|
$handle = fopen($path, 'c');
|
||||||
if ($handle === false || !flock($handle, $operation)) {
|
if ($handle === false || !flock($handle, $operation)) {
|
||||||
if (is_resource($handle)) { fclose($handle); }
|
if (is_resource($handle)) { fclose($handle); }
|
||||||
throw new SystemStoreException("Unable to lock local blob '{$key}'");
|
throw new SystemStoreException("Unable to lock local blob '{$key}'");
|
||||||
@@ -325,15 +333,14 @@ final class Service implements SystemStoreServiceInterface
|
|||||||
return $key;
|
return $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function dataRoot(): string { return $this->root . '/data'; }
|
private function blobPath(string $key): string { return $this->root . '/' . str_replace('/', DIRECTORY_SEPARATOR, $key) . self::BLOB_EXTENSION; }
|
||||||
private function metaRoot(): string { return $this->root . '/meta'; }
|
private function metaPath(string $key): string { return $this->root . '/' . str_replace('/', DIRECTORY_SEPARATOR, $key) . self::META_EXTENSION; }
|
||||||
private function dataPath(string $key): string { return $this->dataRoot() . '/' . str_replace('/', DIRECTORY_SEPARATOR, $key); }
|
private function lockPath(string $key): string { return $this->root . '/' . str_replace('/', DIRECTORY_SEPARATOR, $key) . self::LOCK_EXTENSION; }
|
||||||
private function metaPath(string $key): string { return $this->metaRoot() . '/' . str_replace('/', DIRECTORY_SEPARATOR, $key) . '.json'; }
|
|
||||||
|
|
||||||
private function ensureParent(string $path, string $root): void
|
private function ensureParent(string $path): void
|
||||||
{
|
{
|
||||||
$this->ensureDirectory(dirname($path));
|
$this->ensureDirectory(dirname($path));
|
||||||
$this->assertExistingPathInside(dirname($path), $root);
|
$this->assertExistingPathInside(dirname($path), $this->root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function ensureDirectory(string $path): void
|
private function ensureDirectory(string $path): void
|
||||||
|
|||||||
Reference in New Issue
Block a user