Skip to content

Commit

Permalink
format segment name, close #365 (#366)
Browse files Browse the repository at this point in the history
* format segment name, close #365

---------

Co-authored-by: Vlastimil Pečínka <vlastimil.pecinka@firma.seznam.cz>
  • Loading branch information
vpecinka and Vlastimil Pečínka authored Sep 25, 2023
1 parent cba8dc5 commit 42e66d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
34 changes: 28 additions & 6 deletions src/ObjectStore/v1/Models/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ public function objectExists(string $name): bool
}
}

/**
* Verifies if provied segment index format for DLOs is valid.
*
* @param string $fmt The format of segment index name, e.g. %05d for 00001, 00002, etc.
*
* @return bool TRUE if the format is valid, FALSE if it is not
*/
public function isValidSegmentIndexFormat($fmt)
{
$testValue1 = sprintf($fmt, 1);
$testValue2 = sprintf($fmt, 10);

// Test if different results of the same string length
return ($testValue1 !== $testValue2) && (strlen($testValue1) === strlen($testValue2));
}

/**
* Creates a single object according to the values provided.
*
Expand All @@ -197,11 +213,12 @@ public function createObject(array $data): StorageObject
* container. When this completes, a manifest file is uploaded which references the prefix of the segments,
* allowing concatenation when a request is executed against the manifest.
*
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject}
* @param int $data['segmentSize'] The size in Bytes of each segment
* @param string $data['segmentContainer'] The container to which each segment will be uploaded
* @param string $data['segmentPrefix'] The prefix that will come before each segment. If omitted, a default
* is used: name/timestamp/filesize
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject}
* @param int $data['segmentSize'] The size in Bytes of each segment
* @param string $data['segmentContainer'] The container to which each segment will be uploaded
* @param string $data['segmentPrefix'] The prefix that will come before each segment. If omitted, a default
* is used: name/timestamp/filesize
* @param string $data['segmentIndexFormat'] The format of segment index name, default %05d - 00001, 00002, etc.
*/
public function createLargeObject(array $data): StorageObject
{
Expand All @@ -213,6 +230,11 @@ public function createLargeObject(array $data): StorageObject
$segmentPrefix = isset($data['segmentPrefix'])
? $data['segmentPrefix']
: sprintf('%s/%s/%d', $data['name'], microtime(true), $stream->getSize());
$segmentIndexFormat = isset($data['segmentIndexFormat']) ? $data['segmentIndexFormat'] : '%05d';

if (!$this->isValidSegmentIndexFormat($segmentIndexFormat)) {
throw new \InvalidArgumentException('The provided segmentIndexFormat is not valid.');
}

/** @var \OpenStack\ObjectStore\v1\Service $service */
$service = $this->getService();
Expand All @@ -226,7 +248,7 @@ public function createLargeObject(array $data): StorageObject

while (!$stream->eof() && $count < $totalSegments) {
$promises[] = $this->model(StorageObject::class)->createAsync([
'name' => sprintf('%s/%d', $segmentPrefix, ++$count),
'name' => sprintf('%s/'.$segmentIndexFormat, $segmentPrefix, ++$count),
'stream' => new LimitStream($stream, $segmentSize, ($count - 1) * $segmentSize),
'containerName' => $segmentContainer,
]);
Expand Down
15 changes: 12 additions & 3 deletions tests/unit/ObjectStore/v1/Models/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ public function test_other_exceptions_are_thrown()
$this->container->objectExists('bar');
}

public function test_valid_segment_index_format()
{
self::assertTrue($this->container->isValidSegmentIndexFormat("%03d"));
self::assertTrue($this->container->isValidSegmentIndexFormat("%05d"));
self::assertFalse($this->container->isValidSegmentIndexFormat("%d"));
self::assertFalse($this->container->isValidSegmentIndexFormat("d"));
}

public function test_it_chunks_according_to_provided_segment_size()
{
$stream = function_exists('\GuzzleHttp\Psr7\stream_for')
Expand All @@ -220,6 +228,7 @@ public function test_it_chunks_according_to_provided_segment_size()
'segmentSize' => 10,
'segmentPrefix' => 'objectPrefix',
'segmentContainer' => 'segments',
'segmentIndexFormat' => '%03d',
];

// check container creation
Expand All @@ -235,9 +244,9 @@ public function test_it_chunks_according_to_provided_segment_size()
$this->setupMock('PUT', 'segments', null, [], new Response(201));

// The stream has size 24 so we expect three segments.
$this->setupMock('PUT', 'segments/objectPrefix/1', $stream->read(10), [], new Response(201));
$this->setupMock('PUT', 'segments/objectPrefix/2', $stream->read(10), [], new Response(201));
$this->setupMock('PUT', 'segments/objectPrefix/3', $stream->read(10), [], new Response(201));
$this->setupMock('PUT', 'segments/objectPrefix/001', $stream->read(10), [], new Response(201));
$this->setupMock('PUT', 'segments/objectPrefix/002', $stream->read(10), [], new Response(201));
$this->setupMock('PUT', 'segments/objectPrefix/003', $stream->read(10), [], new Response(201));
$this->setupMock('PUT', 'test/object', null, ['X-Object-Manifest' => 'segments/objectPrefix'], new Response(201));

$stream->rewind();
Expand Down

0 comments on commit 42e66d8

Please sign in to comment.