Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Margolin committed May 29, 2014
1 parent 2c090e0 commit 7fc3060
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 44 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
language: php

php:
- 5.5
- 5.4
- 5.3
- hhvm
- hhvm

before_script:
- composer install
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Change Log
==========
### 29/05/2014 - 1.5
* Reduced some internal complexity
* Fixed gzip compression
* Unit tests now running against all options
* Removed `setOptions` method, must be passed into the `load` method

### 11/03/2014 - 1.4
* Now using Composer
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The easiest way to install Flintstone is via [composer](http://getcomposer.org/)
```json
{
"require": {
"fire015/flintstone": "*"
"fire015/flintstone": "1.*"
}
}
```
Expand Down
64 changes: 33 additions & 31 deletions src/Flintstone/FlintstoneDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class FlintstoneDB {
*/
const FILE_WRITE = 2;

/**
* File append flag
* @access public
* @var integer
*/
const FILE_APPEND = 3;

/**
* Database data
* @access private
Expand All @@ -39,10 +46,10 @@ class FlintstoneDB {
* - boolean $cache store get() results in memory
* - integer $swap_memory_limit write out each line to a temporary file and swap if database is larger than limit (0 to always do this)
*
* @access public
* @access private
* @var array
*/
public $options = array(
private $options = array(
'dir' => '',
'ext' => '.dat',
'gzip' => false,
Expand All @@ -65,22 +72,13 @@ public function __construct($database, $options) {

// Set options
if (!empty($options)) {
$this->setOptions($options);
$this->options = array_merge($this->options, $options);
}

// Setup database
$this->setupDatabase($database);
}

/**
* Set flintstone options
* @param array $options an array of options
* @return void
*/
public function setOptions($options) {
$this->options = array_merge($this->options, $options);
}

/**
* Setup the database and perform pre-flight checks
* @param string $database the database name
Expand Down Expand Up @@ -143,21 +141,27 @@ private function openFile($file, $mode) {
$file = 'compress.zlib://' . $file;
}

// Open in read or write mode
// Open in read, write or append mode
if ($mode == self::FILE_READ) {
$fp = @fopen($file, 'rb');
$mode = 'rb';
$operation = LOCK_SH;
}
elseif ($mode == self::FILE_WRITE) {
$mode = 'wb';
$operation = LOCK_EX;
}
else {
$fp = @fopen($file, 'ab');
$mode = 'ab';
$operation = LOCK_EX;
}

$fp = @fopen($file, $mode);

if (!$fp) {
throw new FlintstoneException('Could not open file ' . $file);
}

if (!@flock($fp, $operation)) {
if (($this->options['gzip'] === false) && (!@flock($fp, $operation))) {
throw new FlintstoneException('Could not lock file ' . $file);
}

Expand All @@ -170,7 +174,7 @@ private function openFile($file, $mode) {
* @return void
*/
private function closeFile($fp) {
if (!@flock($fp, LOCK_UN)) {
if (($this->options['gzip'] === false) && (!@flock($fp, LOCK_UN))) {
throw new FlintstoneException('Could not unlock file');
}

Expand Down Expand Up @@ -261,7 +265,7 @@ private function replaceKey($key, $data) {

// Create a copy of data to push into cache
if ($this->options['cache'] === true) {
$orig_data = $data;
$origData = $data;
}

// Preserve new lines
Expand All @@ -273,7 +277,7 @@ private function replaceKey($key, $data) {

// Open tmp file
if ($swap) {
$tp = $this->openFile($this->data['file_tmp'], self::FILE_WRITE);
$tp = $this->openFile($this->data['file_tmp'], self::FILE_APPEND);
}

// Open file
Expand All @@ -296,7 +300,7 @@ private function replaceKey($key, $data) {

// Save to cache
if ($this->options['cache'] === true) {
$this->data['cache'][$key] = $orig_data;
$this->data['cache'][$key] = $origData;
}
}

Expand Down Expand Up @@ -332,11 +336,6 @@ private function replaceKey($key, $data) {
// Open file
$fp = $this->openFile($this->data['file'], self::FILE_WRITE);

// Truncate
if (!@ftruncate($fp, 0)) {
throw new FlintstoneException('Could not truncate file ' . $this->data['file']);
}

// Write contents
if (@fwrite($fp, $contents) === false) {
throw new FlintstoneException('Could not write to file ' . $this->data['file']);
Expand Down Expand Up @@ -380,7 +379,7 @@ private function setKey($key, $data) {
$line = $key . "=" . $data . "\n";

// Open file
$fp = $this->openFile($this->data['file'], self::FILE_WRITE);
$fp = $this->openFile($this->data['file'], self::FILE_APPEND);

// Write line
if (@fwrite($fp, $line) === false) {
Expand Down Expand Up @@ -427,11 +426,6 @@ private function flushDatabase() {
// Open file
$fp = $this->openFile($this->data['file'], self::FILE_WRITE);

// Truncate
if (!@ftruncate($fp, 0)) {
throw new FlintstoneException('Could not truncate file ' . $this->data['file']);
}

// Close file
$this->closeFile($fp);

Expand Down Expand Up @@ -608,4 +602,12 @@ public function flush() {
public function getKeys() {
return $this->getAllKeys();
}

/**
* Get the database file
* @return string file path
*/
public function getFile() {
return $this->data['file'];
}
}
4 changes: 2 additions & 2 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function testFlush() {
$this->assertTrue($this->db->set('a', '1'));
$this->assertTrue($this->db->set('b', 2));
$this->assertTrue($this->db->flush());
$this->assertFalse($this->db->get('a'));
$this->assertFalse($this->db->get('b'));
$keys = $this->db->getKeys();
$this->assertEquals(0, count($keys));
}

/**
Expand Down
50 changes: 41 additions & 9 deletions tests/TestFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* Flintstone Unit Tests
*/

require __DIR__ . '/../src/Flintstone/Flintstone.php';
require __DIR__ . '/../src/Flintstone/FlintstoneDB.php';
require __DIR__ . '/../src/Flintstone/FlintstoneException.php';
require __DIR__ . '/../vendor/autoload.php';

use Flintstone\Flintstone;

Expand All @@ -20,18 +18,52 @@ class TestFixture extends \PHPUnit_Framework_TestCase {
protected $db;

/**
* Load the test database
* Flintstone database name
* @access protected
* @var string
*/
protected $dbName = 'test';

/**
* Run the feature test multiple times with different options
*/
public function setUp() {
$this->db = Flintstone::load('test', array('dir' => __DIR__));
public function run(PHPUnit_Framework_TestResult $result = null) {
if ($result === null) {
$result = $this->createResult();
}

// Default options
$this->db = Flintstone::load($this->dbName, array('dir' => __DIR__));
$result->run($this);

// With no cache
$this->db = Flintstone::load($this->dbName, array('dir' => __DIR__, 'cache' => false));
$result->run($this);

// With no cache and file swap
$this->db = Flintstone::load($this->dbName, array('dir' => __DIR__, 'cache' => false, 'swap_memory_limit' => 0));
$result->run($this);

// With gzip compression
$this->db = Flintstone::load($this->dbName, array('dir' => __DIR__, 'gzip' => true));
$result->run($this);

// With gzip compression and no cache
$this->db = Flintstone::load($this->dbName, array('dir' => __DIR__, 'gzip' => true, 'cache' => false));
$result->run($this);

// With gzip compression, no cache and file swap
$this->db = Flintstone::load($this->dbName, array('dir' => __DIR__, 'gzip' => true, 'cache' => false, 'swap_memory_limit' => 0));
$result->run($this);

return $result;
}

/**
* Unload the test database and remove
*/
public function tearDown() {
Flintstone::unload('test');
$file = __DIR__ . '/test.dat';
unlink($file);
Flintstone::unload($this->dbName);
unlink($this->db->getFile());
}
}

0 comments on commit 7fc3060

Please sign in to comment.