// Import SPL stuff
use \InvalidArgumentException;
use \SplFileInfo;
+use \UnexpectedValueException;
/**
* Database backend class for storing objects in locally created files.
*/
private $indexKey = '__idx';
+ /**
+ * Cached file names based on table name to avoid "expensive" invocations
+ * of FrameworkConfiguration->getConfigEntry().
+ */
+ private $pathNames = [];
+
/**
* The protected constructor. Do never instance from outside! You need to
* set a local file path. The class will then validate it.
if (empty($tableName)) {
// Throw IAE
throw new InvalidArgumentException('Parameter "tableName" is empty');
+ } elseif (!isset($this->pathNames[$tableName])) {
+ // "Cache" is not present, so create and assign it
+ $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
}
// The result is null by any errors
$resultData = NULL;
- // Create full path name
- $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
-
/*
* A 'select' query is not that easy on local files, so first try to
* find the 'table' which is in fact a directory on the server
*/
try {
// Get a directory pointer instance
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for pathName=%s ...', $pathName));
- $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: Getting directory_class for this->pathNames[%s]=%s ...', $tableName, $this->pathNames[$tableName]));
+ $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
// Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
$resultData = [
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: data[]=%d', count($dataArray)));
foreach ($dataArray as $key => $value) {
// Found one entry?
- $isFound = (($isFound === true) && ($searchInstance->isCriteriaMatching($key, $value)));
+ $isFound = ($isFound && $searchInstance->isCriteriaMatching($key, $value));
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: key=%s,value[%s]=%s,isFound=%s', $key, gettype($value), $value, intval($isFound)));
}
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: isFound=%d,limitFound=%d,limit=%d', intval($isFound), $limitFound, $searchInstance->getLimit()));
if ($isFound === true) {
// Shall we skip this entry?
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d', $searchInstance->getSkip()));
- if ($searchInstance->getSkip() > 0) {
- // We shall skip some entries
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: skipFound=%s', $skipFound));
- if ($skipFound < $searchInstance->getSkip()) {
- // Skip this entry
- $skipFound++;
- break;
- }
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: searchInstance->skip=%d,skipFound=%d', $searchInstance->getSkip(), $skipFound));
+ if ($searchInstance->getSkip() > 0 && $skipFound < $searchInstance->getSkip()) {
+ // Skip this entry
+ $skipFound++;
+ break;
}
// Set id number
*
* @param $dataSetInstance An instance of a StorableCriteria class
* @return void
+ * @throws UnexpectedValueException If $tableName is empty
* @throws SqlException If an SQL error occurs
*/
public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
- // Create full path name
- $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR;
+ // Get table name
+ $tableName = $dataSetInstance->getTableName();
+
+ // Is "cache" there?
+ if (empty($tableName)) {
+ // Should never be an empty string
+ throw new UnexpectedValueException('Class field dataSetInstance->tableName is empty');
+ } elseif (!isset($this->pathNames[$tableName])) {
+ // "Cache" is not present, so create and assign it
+ $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
+ }
// Try all the requests
try {
// Get a file pointer instance
- $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+ $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
// Initialize limit/skip
$limitFound = 0;
if (empty($tableName)) {
// Throw IAE
throw new InvalidArgumentException('Parameter "tableName" is empty');
+ } elseif (!isset($this->pathNames[$tableName])) {
+ // "Cache" is not present, so create and assign it
+ $this->pathNames[$tableName] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
}
-
- // Create full path name
- $pathName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . $tableName . DIRECTORY_SEPARATOR;
-
// Try all the requests
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-LFDB: pathName=' . $pathName);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LFDB: this->pathNames[%s]=%s', $tableName, $this->pathNames[$tableName]));
try {
// Get a file pointer instance
- $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
+ $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', [$this->pathNames[$tableName]]);
// Initialize counter
$count = 0;