]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/binary/index/class_IndexFile.php
Refacuring / possible WIP:
[core.git] / framework / main / classes / file_directories / binary / index / class_IndexFile.php
index 010927136fd069a19bc37b933c2b75278462cc7f..473ba3140cc03818613df5d1c0d8ccf820d2a279 100644 (file)
@@ -6,7 +6,7 @@ namespace Org\Mxchange\CoreFramework\Filesystem\Index;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Filesystem\Index\IndexableFile;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
-use Org\Mxchange\CoreFramework\Index\Stack\IndexableStack;
+use Org\Mxchange\CoreFramework\Index\File\Stack\Indexable;
 
 // Import SPL stuff
 use \SplFileInfo;
@@ -48,16 +48,16 @@ class IndexFile extends BaseBinaryFile implements IndexableFile {
         * Creates an instance of this File class and prepares it for usage
         *
         * @param       $fileInfoInstance       An instance of a SplFileInfo class
-        * @param       $indexInstance  An instance of a IndexableStack class
+        * @param       $indexInstance  An instance of a Indexable class
         * @return      $indexFileInstance      An instance of an IndexableFile class
         */
-       public final static function createIndexFile (SplFileInfo $fileInfoInstance, IndexableStack $indexInstance) {
+       public final static function createIndexFile (SplFileInfo $fileInfoInstance, Indexable $indexInstance) {
                // Get a new instance
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: fileInfoInstance[%s]=%s,indexInstance=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $indexInstance->__toString()));
                $indexFileInstance = new IndexFile();
 
                // Set file instance here for callbacks
-               $indexFileInstance->setIndexableStackInstance($indexInstance);
+               $indexFileInstance->setIndexInstance($indexInstance);
 
                // Expand file name with .idx
                $indexInfoInstance = new SplFileInfo(sprintf('%s.idx', $fileInfoInstance->__toString()));
@@ -73,27 +73,49 @@ class IndexFile extends BaseBinaryFile implements IndexableFile {
        /**
         * Writes given value to the file and returns a hash and gap position for it
         *
-        * @param       $groupId        Group identifier
+        * @param       $stackName      Group identifier
         * @param       $value          Value to be added to the stack
         * @return      $data           Hash and gap position
-        * @throws      UnsupportedOperationException   If this method is called
+        * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       public function writeValueToFile (string $groupId, $value) {
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: groupId=' . $groupId . ',value[' . gettype($value) . ']=' . print_r($value, true));
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       public function writeValueToFile (string $stackName, $value) {
+               // Validate parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
+               if (empty($stackName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "stackName" is empty');
+               } elseif (is_object($value) || is_resource($value)) {
+                       // Not wanted here
+                       throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
+               }
+
+               // Encode/convert the value into a "binary format"
+               $encoded = StringUtils::encodeData($value);
+
+               // Get a strong hash for the "encoded" data
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: encoded=%s', $encoded));
+               $hash = self::hash($encoded);
+
+               // Then write it to the next free gap
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: hash=%s', $hash));
+               $data = $this->getIndexInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
+
+               // Return info
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: data[]=%s - EXIT!', gettype($data)));
+               return $data;
        }
 
        /**
         * Writes given raw data to the file and returns a gap position and length
         *
-        * @param       $groupId        Group identifier
+        * @param       $stackName      Group identifier
         * @param       $hash           Hash from encoded value
         * @param       $encoded        Encoded value to be written to the file
         * @return      $data           Gap position and length of the raw data
         * @throws      UnsupportedOperationException   If this method is called
         */
-       public function writeDataToFreeGap (string $groupId, string $hash, string $encoded) {
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: groupId=' . $groupId . ',encoded()=' . strlen($encoded));
+       public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('INDEX-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
                throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }