From 33a077568f330392f1a57f953cc8d94d89b6f949 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 19 May 2014 21:56:23 +0200 Subject: [PATCH] Added basic classes (and unimplemented) for file-based indexes. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- inc/classes/interfaces/index/.htaccess | 1 + inc/classes/interfaces/index/class_Indexable | 28 +++++++ .../interfaces/index/class_Indexable.php | 28 +++++++ inc/classes/interfaces/index/stack/.htaccess | 1 + .../index/stack/class_IndexableStack.php | 28 +++++++ inc/classes/main/factories/index/.htaccess | 1 + .../index/class_FileStackIndexFactory.php | 51 +++++++++++++ inc/classes/main/index/.htaccess | 1 + inc/classes/main/index/class_ | 51 +++++++++++++ inc/classes/main/index/class_BaseIndex.php | 38 ++++++++++ inc/classes/main/index/file_stack/.htaccess | 1 + .../index/file_stack/class_FileStackIndex.php | 51 +++++++++++++ .../main/stacker/file/class_BaseFileStack.php | 75 +++++++++++++++++-- inc/config.php | 3 + 14 files changed, 351 insertions(+), 7 deletions(-) create mode 100644 inc/classes/interfaces/index/.htaccess create mode 100644 inc/classes/interfaces/index/class_Indexable create mode 100644 inc/classes/interfaces/index/class_Indexable.php create mode 100644 inc/classes/interfaces/index/stack/.htaccess create mode 100644 inc/classes/interfaces/index/stack/class_IndexableStack.php create mode 100644 inc/classes/main/factories/index/.htaccess create mode 100644 inc/classes/main/factories/index/class_FileStackIndexFactory.php create mode 100644 inc/classes/main/index/.htaccess create mode 100644 inc/classes/main/index/class_ create mode 100644 inc/classes/main/index/class_BaseIndex.php create mode 100644 inc/classes/main/index/file_stack/.htaccess create mode 100644 inc/classes/main/index/file_stack/class_FileStackIndex.php diff --git a/inc/classes/interfaces/index/.htaccess b/inc/classes/interfaces/index/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/interfaces/index/.htaccess @@ -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 index 00000000..994de03d --- /dev/null +++ b/inc/classes/interfaces/index/class_Indexable @@ -0,0 +1,28 @@ + + * @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 . + */ +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 index 00000000..4d109610 --- /dev/null +++ b/inc/classes/interfaces/index/class_Indexable.php @@ -0,0 +1,28 @@ + + * @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 . + */ +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 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/interfaces/index/stack/.htaccess @@ -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 index 00000000..b6ba4b02 --- /dev/null +++ b/inc/classes/interfaces/index/stack/class_IndexableStack.php @@ -0,0 +1,28 @@ + + * @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 . + */ +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 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/factories/index/.htaccess @@ -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 index 00000000..ca1fb7cc --- /dev/null +++ b/inc/classes/main/factories/index/class_FileStackIndexFactory.php @@ -0,0 +1,51 @@ + + * @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 . + */ +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 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/index/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/index/class_ b/inc/classes/main/index/class_ new file mode 100644 index 00000000..2f51d77c --- /dev/null +++ b/inc/classes/main/index/class_ @@ -0,0 +1,51 @@ + + * @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 . + */ +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 index 00000000..25853eab --- /dev/null +++ b/inc/classes/main/index/class_BaseIndex.php @@ -0,0 +1,38 @@ + + * @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 . + */ +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 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/index/file_stack/.htaccess @@ -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 index 00000000..faa79b06 --- /dev/null +++ b/inc/classes/main/index/file_stack/class_FileStackIndex.php @@ -0,0 +1,51 @@ + + * @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 . + */ +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] +?> diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index 8d5fe13b..f5050899 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -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(); } } diff --git a/inc/config.php b/inc/config.php index de13385e..264b36cc 100644 --- a/inc/config.php +++ b/inc/config.php @@ -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] ?> -- 2.39.5