Permalink
Browse files

Performance improvements

  • Loading branch information...
magnusnordlander committed Sep 18, 2012
1 parent f7f6a73 commit dfc7d6dc1cf2ea9d7747160b2895cc6a238ae9c4
View
@@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/.composer/autoload.php"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Gittern Test Suite">
@@ -34,7 +34,7 @@ public function __construct(Repository $repo, $commitish)
}
}
public function read($key)
protected function getGitObjectForKey($key)
{
$components = explode('/', $key);
@@ -56,6 +56,18 @@ public function read($key)
break;
}
if ($object)
{
return $object;
}
return null;
}
public function read($key)
{
$object = $this->getGitObjectForKey($key);
if ($object instanceof Blob)
{
return $object->getContents();
@@ -71,7 +83,7 @@ public function write($key, $content, array $metadata = null)
public function exists($key)
{
return array_search($key, $this->keys()) !== false;
return $this->getGitObjectForKey($key) instanceOf Blob;
}
public function keys()
@@ -88,8 +100,14 @@ public function mtime($key)
public function checksum($key)
{
$file = $this->read($key);
return md5($file);
$object = $this->getGitObjectForKey($key);
if ($object instanceof Blob)
{
return $object->getSha();
}
throw new \RuntimeException(sprintf('Could not read the \'%s\' file.', $key));
}
public function delete($key)
@@ -100,8 +100,14 @@ public function mtime($key)
public function checksum($key)
{
$file = $this->read($key);
return md5($file);
$entry = $this->getIndex()->getEntryNamed($key);
if ($entry)
{
return $entry->getBlob()->getSha();
}
throw new \RuntimeException(sprintf('Could not read the \'%s\' file.', $key));
}
public function delete($key)
@@ -24,7 +24,7 @@ public function __load()
{
if (!$this->blob)
{
$this->blob = $this->repo->getObject($this->sha);
$this->blob = $this->repo->getObjectBySha($this->sha);
}
}
@@ -26,7 +26,7 @@ public function __load()
{
if (!$this->commit)
{
$this->commit = $this->repo->getObject($this->sha);
$this->commit = $this->repo->getObjectBySha($this->sha);
}
}
@@ -25,7 +25,7 @@ public function __load()
{
if (!$this->tree)
{
$this->tree = $this->repo->getObject($this->sha);
$this->tree = $this->repo->getObjectBySha($this->sha);
}
}
View
@@ -161,8 +161,18 @@ public function getObject($treeish)
throw new \RuntimeException(sprintf("Couldn't find an object with identifier %s", $treeish));
}
return $this->getObjectBySha($sha);
}
public function getObjectBySha($sha)
{
$raw_object = $this->transport->fetchRawObject($sha);
if (!$raw_object)
{
throw new \RuntimeException(sprintf("Couldn't fetch object with identifier %s", $sha));
}
$hydrator = $this->getHydratorForType($raw_object->getType());
if (!$hydrator)
@@ -56,11 +56,18 @@ public function testCanGetKeys()
public function testCanCheckIfKeyExists()
{
$iter = new \RecursiveArrayIterator(array('foo' => array('foo/bar' => 1, 'foo/baz' => 2), 'quux' => 3));
$foo_tree_mock = M::mock('Gittern\Entity\GitObject\Tree');
$foo_node_mock = M::mock(array('getRelatedObject' => $foo_tree_mock));
$root_tree_mock = M::mock('Gittern\Entity\GitObject\Tree');
$root_tree_mock->shouldReceive('getNodeNamed')->with('foo')->andReturn($foo_node_mock);
$foo_tree_mock->shouldReceive('getNodeNamed')->with('bar')->andReturn(M::mock(array('getRelatedObject' => M::mock('Gittern\Entity\GitObject\Blob'))));
$root_tree_mock->shouldReceive('getNodeNamed')->with('quux')->andReturn(M::mock(array('getRelatedObject' => M::mock('Gittern\Entity\GitObject\Blob'))));
$rp = new \ReflectionProperty('Gittern\Gaufrette\GitternCommitishReadOnlyAdapter', 'tree');
$rp->setAccessible(true);
$rp->setValue($this->adapter, $iter);
$rp->setValue($this->adapter, $root_tree_mock);
$this->assertTrue($this->adapter->exists('foo/bar'));
$this->assertTrue($this->adapter->exists('quux'));
@@ -99,9 +106,9 @@ public function testCanChecksumFile()
$this->tree_mock->shouldReceive('getNodeNamed')->with('foo')->andReturn($this->tree_mock);
$this->tree_mock->shouldReceive('getRelatedObject')->andReturn($foo_mock);
$foo_mock->shouldReceive('getContents')->andReturn('Foobar');
$foo_mock->shouldReceive('getSha')->andReturn('f00bar');
$this->assertEquals(md5('Foobar'), $this->adapter->checksum('foo/bar'));
$this->assertEquals('f00bar', $this->adapter->checksum('foo/bar'));
}
public function testMtimeReturnsCommitTime()
@@ -44,7 +44,7 @@ public function getProxiedMethods()
public function testMethodIsProperlyDecorated($method_name)
{
$blob_mock = M::mock('Gittern\Entity\GitObject\Blob');
$this->repo_mock->shouldReceive('getObject')->with($this->sha)->andReturn($blob_mock);
$this->repo_mock->shouldReceive('getObjectBySha')->with($this->sha)->andReturn($blob_mock);
$return_value = uniqid();
@@ -56,7 +56,7 @@ public function getProxiedMethods()
public function testMethodIsProperlyDecorated($method_name)
{
$commit_mock = M::mock('Gittern\Entity\GitObject\Commit');
$this->repo_mock->shouldReceive('getObject')->with($this->sha)->andReturn($commit_mock);
$this->repo_mock->shouldReceive('getObjectBySha')->with($this->sha)->andReturn($commit_mock);
$return_value = uniqid();
@@ -47,7 +47,7 @@ public function getProxiedMethods()
public function testMethodIsProperlyDecorated($method_name)
{
$tree_mock = M::mock('Gittern\Entity\GitObject\Tree');
$this->repo_mock->shouldReceive('getObject')->with($this->sha)->andReturn($tree_mock);
$this->repo_mock->shouldReceive('getObjectBySha')->with($this->sha)->andReturn($tree_mock);
$return_value = uniqid();

0 comments on commit dfc7d6d

Please sign in to comment.