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 e28faccd035b2f44b430192ebf5dea765e0d36ea..d853bca0eb0882e00461a1f416c7ab593831d6be 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 307d39963ea64829cd974a1fed28c1283b98edb0..9075e5c21ee3c8460eb7bd3fc9b9babdfc0d8eac 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 14972d7316d8580fefe7f1329f94d03686d65bf9..826156fde8ce80c6613f7194bc1d6ae7f756321d 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