Skip to content

Instantly share code, notes, and snippets.

@ovdojoey
Last active March 23, 2016 20:05
Show Gist options
  • Select an option

  • Save ovdojoey/4afc6818fd1997c31227 to your computer and use it in GitHub Desktop.

Select an option

Save ovdojoey/4afc6818fd1997c31227 to your computer and use it in GitHub Desktop.
<?php
class FlatDB {
public $storageFileLocation = __DIR__ . "/../storage/flatdb";
public $storageFolderLocation = __DIR__ . "/../storage";
public $errors;
public $cacheFile;
public $cacheChanged = false;
/**
* FlatDb
* A small flat file driven database using PHP operations to store and
* access data of string format.
*/
public function __construct() {
// check for storage directory
if ( !file_exists( $this->storageFolderLocation ) ) {
// directory does not exist, try and create it
$makeDirectory = mkdir( $this->storageFolderLocation, 0600 );
if ( $makeDirectory !== true) {
throw new \Exception("FlatDB is unable to create the storage location");
}
}
if ( !file_exists( $this->storageFileLocation ) ) {
try {
file_put_contents( $this->storageFileLocation , '' );
} catch (\Exception $e) {
$this->errors = $e->getMessage();
}
}
// get a cached version upon instantiation
$this->cacheFile = file_get_contents( $this->storageFileLocation );
if ( $this->cacheFile === false ) {
throw new \Exception("FlatDB is unable to load from storage location");
}
}
public function insert($key, $value) {
$key = str_replace(";;", "&#59;&#59;", $key);
$value = str_replace(";;", "&#59;&#59;", $value);
$this->cacheFile .= ";;{$key};;" . $value;
$put = file_put_contents( $this->storageFileLocation , $this->cacheFile );
if ( $put === false ) {
throw new \Exception("Unable to load from storage location");
}
$this->cacheChanged = true;
return $value;
}
public function select($key) {
$valuesFound = array();
if ( $this->cacheChanged ) {
$this->reloadCache();
}
$numTimesKeyWasFound = substr_count ( $this->cacheFile , ";;{$key};;" );
$currentOffset = 0;
for ($x = 0; $x < $numTimesKeyWasFound; $x++) {
$occurance = strpos($this->cacheFile, ";;{$key};;", $currentOffset);
if ( $occurance !== false ) {
// key was found at $occrance;
$keyPosAtEnd = ($occurance + (4 + strlen($key))) ;
$endingPositionOfValue = strpos($this->cacheFile, ";;", $keyPosAtEnd);
$endingPositionOfValue = ( $endingPositionOfValue === 0 ) ? null : $endingPositionOfValue;
$lengthOfValue = (($endingPositionOfValue - $keyPosAtEnd) > 0) ? ($endingPositionOfValue - $keyPosAtEnd) : strlen($this->cacheFile);
$value = str_replace("&#59;&#59;", ";;", substr($this->cacheFile, $keyPosAtEnd, $lengthOfValue));
array_push($valuesFound, $value);
$currentOffset = $keyPosAtEnd;
}
}
if ( sizeof($valuesFound) === 0 ) {
return null;
}
if ( sizeof($valuesFound) === 1 ) {
return $valuesFound[0];
}
// return an array of objects
return $valuesFound;
}
private function reloadCache() {
$getCache = file_get_contents( $this->storageFileLocation );
if ( $getCache === false ) {
throw new \Exception("Unable to load from storage location");
}
$this->cacheFile = $getCache;
$this->cacheChanged = false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment