Created
September 18, 2012 01:09
-
-
Save tlhunter/3740684 to your computer and use it in GitHub Desktop.
SofaDB PHP CouchDB alternative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
SofaDB, a pure PHP CouchDB alternative | |
Developed by Thomas Hunter | |
Released under the LGPL | |
May 1st, 2010 | |
Version 0.0.1 | |
This was something I started as a joke, basically a document storage system which | |
stores files on disk in the form of ID.json. It was never heavily tested... | |
*/ | |
define('SOFA_DIR', './sofadb/'); | |
define('SOFA_EXT', '.json'); | |
define('SOFA_IND', 'index'); | |
class SofaDB { | |
private $auto_increment; | |
private $error_message; | |
public function __construct() { | |
$filename = SOFA_DIR . SOFA_IND; | |
if (file_exists($filename)) { | |
$this->auto_increment = file_get_contents($filename) + 0; | |
return true; | |
} else { | |
$this->error_message = "Index file does not exist."; | |
return false; | |
} | |
} | |
public function getNext() { | |
return $this->auto_increment + 1; | |
} | |
public function getError() { | |
return $this->$error_message; | |
} | |
public function repair() { | |
# re-determine auto increment value | |
# clean up database files? | |
} | |
public function select($id) { | |
$id += 0; | |
$filename = SOFA_DIR . $id . SOFA_EXT; | |
if (file_exists($filename)) { | |
$data_string = file_get_contents($filename); | |
$data = json_decode($data_string); | |
return $data; | |
} else { | |
$this->error_message = "Record does not exist."; | |
return false; | |
} | |
} | |
public function update($id, $data) { | |
$id += 0; | |
$filename = SOFA_DIR . $id . SOFA_EXT; | |
$data_string = json_encode($data); | |
$handle_data = fopen($filename, 'w'); | |
fwrite($handle_data, $data_string); | |
fclose($handle_data); | |
return true; | |
} | |
public function insert($data) { | |
$data_string = json_encode($data); | |
$this->auto_increment++; | |
$handle_ai = fopen(SOFA_DIR . SOFA_IND, 'w'); | |
fwrite($handle_ai, $this->auto_increment); | |
fclose($handle_ai); | |
$handle_data = fopen(SOFA_DIR . $this->auto_increment . SOFA_EXT, 'w'); | |
fwrite($handle_data, $data_string); | |
fclose($handle_data); | |
return $this->auto_increment; | |
} | |
public function delete($id) { | |
$id += 0; | |
$filename = SOFA_DIR . $id . SOFA_EXT; | |
if (file_exists($filename)) { | |
unlink($filename); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function truncate() { | |
$handle = opendir(SOFA_DIR); | |
$i = 0; | |
while (false !== ($filename = readdir($handle))) { | |
if (strpos($filename, SOFA_EXT)) { | |
unlink(SOFA_DIR . $filename); | |
$i++; | |
} | |
} | |
$handle_ai = fopen(SOFA_DIR . SOFA_IND, 'w'); | |
fwrite($handle_ai, '0'); | |
fclose($handle_ai); | |
return $i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment