Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #7 from e-butik/performance-improvments
Performance improvements
  • Loading branch information
magnusnordlander committed Sep 18, 2012
2 parents f7f6a73 + dfc7d6d commit 8d5af4e
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Expand Up @@ -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">
Expand Down
26 changes: 22 additions & 4 deletions src/Gittern/Gaufrette/GitternCommitishReadOnlyAdapter.php
Expand Up @@ -34,7 +34,7 @@ public function __construct(Repository $repo, $commitish)
}
}

public function read($key)
protected function getGitObjectForKey($key)
{
$components = explode('/', $key);

Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions src/Gittern/Gaufrette/GitternIndexAdapter.php
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Gittern/Proxy/BlobProxy.php
Expand Up @@ -24,7 +24,7 @@ public function __load()
{
if (!$this->blob)
{
$this->blob = $this->repo->getObject($this->sha);
$this->blob = $this->repo->getObjectBySha($this->sha);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gittern/Proxy/CommitProxy.php
Expand Up @@ -26,7 +26,7 @@ public function __load()
{
if (!$this->commit)
{
$this->commit = $this->repo->getObject($this->sha);
$this->commit = $this->repo->getObjectBySha($this->sha);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gittern/Proxy/TreeProxy.php
Expand Up @@ -25,7 +25,7 @@ public function __load()
{
if (!$this->tree)
{
$this->tree = $this->repo->getObject($this->sha);
$this->tree = $this->repo->getObjectBySha($this->sha);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Gittern/Repository.php
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions tests/Gittern/Gaufrette/GitternCommitishReadOnlyAdapterTest.php
Expand Up @@ -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'));
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/Gittern/Proxy/BlobProxyTest.php
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Gittern/Proxy/CommitProxyTest.php
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/Gittern/Proxy/TreeProxyTest.php
Expand Up @@ -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();

Expand Down

0 comments on commit 8d5af4e

Please sign in to comment.