Very early write (add) support for file-based stack indexes.
[core.git] / inc / classes / main / index / file_stack / class_FileStackIndex.php
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)));
        }
 
        /**