From a3fcf88a37aac86d61f3d9eeee9c0e16de10a604 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Sun, 24 May 2015 04:43:13 +0200 Subject: [PATCH] Very early write (add) support for file-based stack indexes. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- inc/classes/main/index/class_BaseIndex.php | 15 +++++++++ .../index/file_stack/class_FileStackIndex.php | 32 ++++++++++++++++++- .../main/stacker/file/class_BaseFileStack.php | 5 ++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/inc/classes/main/index/class_BaseIndex.php b/inc/classes/main/index/class_BaseIndex.php index e28faccd..d853bca0 100644 --- a/inc/classes/main/index/class_BaseIndex.php +++ b/inc/classes/main/index/class_BaseIndex.php @@ -27,6 +27,21 @@ class BaseIndex extends BaseFrameworkSystem { */ const INDEX_MAGIC = 'INDEXv0.1'; + /** + * Separator group->hash + */ + const SEPARATOR_GROUP_HASH = 0x01; + + /** + * Separator hash->gap position + */ + const SEPARATOR_HASH_GAP_POSITION = 0x02; + + /** + * Separator gap position->length + */ + const SEPARATOR_GAP_LENGTH = 0x03; + /** * Protected constructor * diff --git a/inc/classes/main/index/file_stack/class_FileStackIndex.php b/inc/classes/main/index/file_stack/class_FileStackIndex.php index 307d3996..9075e5c2 100644 --- a/inc/classes/main/index/file_stack/class_FileStackIndex.php +++ b/inc/classes/main/index/file_stack/class_FileStackIndex.php @@ -57,7 +57,37 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { * @return void */ public function addHashToIndex ($groupId, array $data) { - $this->partialStub('groupId=' . $groupId . ',data=' . print_r($data, TRUE)); + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gap=%d,length=%d - CALLED!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION], $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH])); + + // Raw data been written to the file + $rawData = sprintf('%s%s%s%s%s%s%s', + $groupId, + self::SEPARATOR_GROUP_HASH, + hex2bin($data[BaseFileStack::ARRAY_INDEX_HASH]), + self::SEPARATOR_HASH_GAP_POSITION, + $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION], + self::SEPARATOR_GAP_LENGTH, + $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH] + ); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%s', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData))); + + // Search for next free gap + $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData)); + + // Gap position cannot be smaller than header length + 1 + assert($gapPosition > $this->getIteratorInstance()->getHeaderSize()); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $gapPosition)); + + // Then write the data at that gap + $this->getIteratorInstance()->writeData($gapPosition, $rawData); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%s - EXIT!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData))); } /** diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index 14972d73..826156fd 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -234,7 +234,10 @@ class BaseFileStack extends BaseStacker { assert(!is_object($value)); assert(!is_resource($value)); - // Now add the value to the file stack (which returns a hash) + /* + * Now add the value to the file stack which returns gap position, a + * hash and length of the raw data. + */ $data = $this->getIteratorInstance()->writeValueToFile($stackerName, $value); // Add the hash and gap position to the index -- 2.39.2