Very early write (add) support for file-based stack indexes.
authorRoland Haeder <roland@mxchange.org>
Sun, 24 May 2015 02:43:13 +0000 (04:43 +0200)
committerRoland Haeder <roland@mxchange.org>
Sun, 24 May 2015 02:43:13 +0000 (04:43 +0200)
Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/classes/main/index/class_BaseIndex.php
inc/classes/main/index/file_stack/class_FileStackIndex.php
inc/classes/main/stacker/file/class_BaseFileStack.php

index e28facc..d853bca 100644 (file)
@@ -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
         *
index 307d399..9075e5c 100644 (file)
@@ -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)));
        }
 
        /**
index 14972d7..826156f 100644 (file)
@@ -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