Added basic classes (and unimplemented) for file-based indexes.
authorRoland Haeder <roland@mxchange.org>
Mon, 19 May 2014 19:56:23 +0000 (21:56 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 19 May 2014 19:56:23 +0000 (21:56 +0200)
Signed-off-by: Roland H├Ąder <roland@mxchange.org>
14 files changed:
inc/classes/interfaces/index/.htaccess [new file with mode: 0644]
inc/classes/interfaces/index/class_Indexable [new file with mode: 0644]
inc/classes/interfaces/index/class_Indexable.php [new file with mode: 0644]
inc/classes/interfaces/index/stack/.htaccess [new file with mode: 0644]
inc/classes/interfaces/index/stack/class_IndexableStack.php [new file with mode: 0644]
inc/classes/main/factories/index/.htaccess [new file with mode: 0644]
inc/classes/main/factories/index/class_FileStackIndexFactory.php [new file with mode: 0644]
inc/classes/main/index/.htaccess [new file with mode: 0644]
inc/classes/main/index/class_ [new file with mode: 0644]
inc/classes/main/index/class_BaseIndex.php [new file with mode: 0644]
inc/classes/main/index/file_stack/.htaccess [new file with mode: 0644]
inc/classes/main/index/file_stack/class_FileStackIndex.php [new file with mode: 0644]
inc/classes/main/stacker/file/class_BaseFileStack.php
inc/config.php

diff --git a/inc/classes/interfaces/index/.htaccess b/inc/classes/interfaces/index/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/interfaces/index/class_Indexable b/inc/classes/interfaces/index/class_Indexable
new file mode 100644 (file)
index 0000000..994de03
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for Indexable classes
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface Indexable??? extends Indexable {
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/interfaces/index/class_Indexable.php b/inc/classes/interfaces/index/class_Indexable.php
new file mode 100644 (file)
index 0000000..4d10961
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for Indexable classes
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface Indexable extends FrameworkInterface {
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/interfaces/index/stack/.htaccess b/inc/classes/interfaces/index/stack/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/interfaces/index/stack/class_IndexableStack.php b/inc/classes/interfaces/index/stack/class_IndexableStack.php
new file mode 100644 (file)
index 0000000..b6ba4b0
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for Indexable stack classes
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+interface IndexableStack extends Indexable {
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/main/factories/index/.htaccess b/inc/classes/main/factories/index/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/main/factories/index/class_FileStackIndexFactory.php b/inc/classes/main/factories/index/class_FileStackIndexFactory.php
new file mode 100644 (file)
index 0000000..ca1fb7c
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * A factory class for file-based stack indexes
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class FileStackIndexFactory extends ObjectFactory {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Returns a singleton (registry-based) StackableFile instance
+        *
+        * @param       $stackName                      Name of the stack's file
+        * @return      $indexInstance          An instance of a IndexableStack class
+        */
+       public static final function createFileStackIndexInstance ($fileName) {
+               // Call parent factory
+               $indexInstance self::createObjectByConfiguredName('file_stack_index_class', array($fileName));
+
+               // Return the instance
+               return $indexInstance;
+       }
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/main/index/.htaccess b/inc/classes/main/index/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/main/index/class_ b/inc/classes/main/index/class_
new file mode 100644 (file)
index 0000000..2f51d77
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * A ??? index class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class ???Index extends BaseIndex implements Indexable {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this Index class and prepares it for usage
+        *
+        * @param       $fileName               Name of the file stack to create an index file for
+        * @return      $indexInstance  An instance of this Index class
+        */
+       public final static function create???Index ($fileName) {
+               // Get a new instance
+               $indexInstance = new ???Index();
+
+               // Return the prepared instance
+               return $indexInstance;
+       }
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/main/index/class_BaseIndex.php b/inc/classes/main/index/class_BaseIndex.php
new file mode 100644 (file)
index 0000000..25853ea
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * A general index class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+class BaseIndex extends BaseFrameworkSystem {
+       /**
+        * Protected constructor
+        *
+        * @param       $className      Name of the class
+        * @return      void
+        */
+       protected function __construct ($className) {
+               // Call parent constructor
+               parent::__construct($className);
+       }
+}
+
+// [EOF]
+?>
diff --git a/inc/classes/main/index/file_stack/.htaccess b/inc/classes/main/index/file_stack/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/main/index/file_stack/class_FileStackIndex.php b/inc/classes/main/index/file_stack/class_FileStackIndex.php
new file mode 100644 (file)
index 0000000..faa79b0
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/**
+ * A FileStack index class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class FileStackIndex extends BaseIndex implements IndexableStack {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this Index class and prepares it for usage
+        *
+        * @param       $fileName               Name of the file stack to create an index file for
+        * @return      $indexInstance  An instance of this Index class
+        */
+       public final static function createFileStackIndex ($fileName) {
+               // Get a new instance
+               $indexInstance = new FileStackIndex();
+
+               // Return the prepared instance
+               return $indexInstance;
+       }
+}
+
+// [EOF]
+?>
index 8d5fe13..f505089 100644 (file)
@@ -77,6 +77,16 @@ class BaseFileStack extends BaseStacker {
         */
        private $header = array();
 
+       /**
+        * Seek positions for gaps ("fragmentation")
+        */
+       private $gaps = array();
+
+       /**
+        * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
+        */
+       private $damagedEntries = array();
+
        /**
         * Protected constructor
         *
@@ -96,6 +106,27 @@ class BaseFileStack extends BaseStacker {
                        self::LENGTH_POSITION +
                        strlen(self::SEPARATOR_HEADER_ENTRIES)
                );
+
+               // Init counters and gaps array
+               $this->initCountersGapsArray();
+       }
+
+       /**
+        * Initializes counter for valid entries, arrays for damaged entries and
+        * an array for gap seek positions. If you call this method on your own,
+        * please re-analyze the file structure. So you are better to call
+        * analyzeStackFile() instead of this method.
+        *
+        * @return      void
+        */
+       private function initCountersGapsArray () {
+               // Init counter and seek position
+               $this->setCounter(0);
+               $this->setSeekPosition(0);
+
+               // Init arrays
+               $this->gaps = array();
+               $this->damagedEntries = array();
        }
 
        /**
@@ -385,6 +416,28 @@ class BaseFileStack extends BaseStacker {
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
 
+       /**
+        * Analyzes entries in stack file. This will count all found (and valid)
+        * entries, mark invalid as damaged and count gaps ("fragmentation"). If
+        * only gaps are found, the file is considered as "virgin" (no entries).
+        *
+        * @return      void
+        */
+       private function analyzeStackFile () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+
+               // Make sure the file is initialized
+               assert($this->isFileInitialized());
+
+               // Init counters and gaps array
+               $this->initCountersGapsArray();
+
+               // Output message (as this may take some time)
+               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
+
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+       }
+
        /**
         * Initializes this file-based stack.
         *
@@ -392,7 +445,7 @@ class BaseFileStack extends BaseStacker {
         * @return      void
         */
        protected function initFileStack ($fileName) {
-               // Get a file i/o pointer instance
+               // Get a file i/o pointer instance for stack file
                $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
 
                // Get iterator instance
@@ -415,6 +468,18 @@ class BaseFileStack extends BaseStacker {
 
                // Load the file header
                $this->readFileHeader();
+
+               // Count all entries in file
+               $this->analyzeStackFile();
+
+               /*
+                * Get stack index instance. This can be used for faster
+                * "defragmentation" and startup.
+                */
+               $indexInstance = FileStackIndexFactory::createFileStackIndex($fileName);
+
+               // And set it here
+               $this->setIndexInstance($indexInstance);
        }
 
        /**
@@ -588,12 +653,8 @@ class BaseFileStack extends BaseStacker {
         * @return      $count                  Size of stack (array count)
         */
        public function getStackCount ($stackerName) {
-               // Now, count the array of entries
-               $this->partialStub('stackerName=' . $stackerName);
-               $count = 0;
-
-               // Return result
-               return $count;
+               // Now, simply return the found count value, this must be up-to-date then!
+               return $this->getCounter();
        }
 }
 
index de13385..264b36c 100644 (file)
@@ -377,5 +377,8 @@ $cfg->setConfigEntry('file_stack_pre_allocate_enabled', 'Y');
 // CFG: FILE-STACK-PRE-ALLOCATE-COUNT
 $cfg->setConfigEntry('file_stack_pre_allocate_count', 10000);
 
+// CFG: FILE-STACK-INDEX-CLASS
+$cfg->setConfigEntry('file_stack_index_class', 'FileStackIndex');
+
 // [EOF]
 ?>