Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add Riak storage

  • Loading branch information...
commit 2c1a79a3eae1926e482d1ff81e5db72ebe1da54c 1 parent 995d06f
Markus Bachmann authored
2  README.md
Source Rendered
@@ -26,7 +26,7 @@ Following vendors are targeted:
26 26
 * Amazon DynamoDB
27 27
 * CouchDB
28 28
 * MongoDB
29  
-* Riak
  29
+* Riak (Implemented)
30 30
 
31 31
 We happily accept contributions for any of the drivers.
32 32
 
3  composer.json
@@ -3,6 +3,9 @@
3 3
     "require": {
4 4
         "doctrine/common": "*"
5 5
     },
  6
+    "require-dev": {
  7
+        "riak/riak-client": "*"
  8
+    },
6 9
     "description": "Simple Key-Value Store Abstraction Layer that maps to PHP objects, allowing for many backends.",
7 10
     "license": "MIT",
8 11
     "autoload": {
29  composer.lock
... ...
@@ -1,9 +1,30 @@
1 1
 {
2  
-    "hash": "09b80610c08784c764e1df9edca9b53f",
  2
+    "hash": "23665765c3ec49f9478cd201eaf36803",
3 3
     "packages": [
4 4
         {
5  
-            "package": "doctrine\/common",
6  
-            "version": "master-dev"
  5
+            "package": "doctrine/common",
  6
+            "version": "dev-master",
  7
+            "alias-pretty-version": "2.3.x-dev",
  8
+            "alias-version": "2.3.9999999.9999999-dev"
  9
+        },
  10
+        {
  11
+            "package": "doctrine/common",
  12
+            "version": "dev-master",
  13
+            "source-reference": "f0b548aa55bb7dac36d79061270a085dd38635b4"
  14
+        }
  15
+    ],
  16
+    "packages-dev": [
  17
+        {
  18
+            "package": "riak/riak-client",
  19
+            "version": "dev-master",
  20
+            "source-reference": "cea446f04d759caaef44020a2064a38b41cbdfab"
7 21
         }
  22
+    ],
  23
+    "aliases": [
  24
+
  25
+    ],
  26
+    "minimum-stability": "dev",
  27
+    "stability-flags": [
  28
+
8 29
     ]
9  
-}
  30
+}
175  lib/Doctrine/KeyValueStore/Storage/RiakStorage.php
... ...
@@ -0,0 +1,175 @@
  1
+<?php
  2
+/*
  3
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14
+ *
  15
+ * This software consists of voluntary contributions made by many individuals
  16
+ * and is licensed under the MIT license. For more information, see
  17
+ * <http://www.doctrine-project.org>.
  18
+ */
  19
+
  20
+
  21
+namespace Doctrine\KeyValueStore\Storage;
  22
+
  23
+use Doctrine\KeyValueStore\NotFoundException;
  24
+
  25
+use Riak\Client;
  26
+
  27
+/**
  28
+ * @author Markus Bachmann <markus.bachmann@bachi.biz>
  29
+ */
  30
+class RiakStorage implements Storage
  31
+{
  32
+    /**
  33
+     * @var \Riak\Client
  34
+     */
  35
+    protected $client;
  36
+
  37
+    /**
  38
+     * @var string
  39
+     */
  40
+    protected $bucketName;
  41
+
  42
+    /**
  43
+     * Constructor
  44
+     *
  45
+     * @param \Riak\Client $riak
  46
+     * @param string       $bucketName
  47
+     */
  48
+    public function __construct(Client $riak, $bucketName)
  49
+    {
  50
+        $this->client = $riak;
  51
+        $this->bucketName = $bucketName;
  52
+    }
  53
+
  54
+    /**
  55
+     * Determine if the storage supports updating only a subset of properties,
  56
+     * or if all properties have to be set, even if only a subset of properties
  57
+     * changed.
  58
+     *
  59
+     * @return bool
  60
+     */
  61
+    public function supportsPartialUpdates()
  62
+    {
  63
+        return false;
  64
+    }
  65
+
  66
+    /**
  67
+     * Does this storage support composite primary keys?
  68
+     *
  69
+     * @return bool
  70
+     */
  71
+    function supportsCompositePrimaryKeys()
  72
+    {
  73
+        return false;
  74
+    }
  75
+
  76
+    /**
  77
+     * Does this storage require composite primary keys?
  78
+     *
  79
+     * @return bool
  80
+     */
  81
+    function requiresCompositePrimaryKeys()
  82
+    {
  83
+        return false;
  84
+    }
  85
+
  86
+    /**
  87
+     * Insert data into the storage key specified.
  88
+     *
  89
+     * @param string $storageName
  90
+     * @param array|string $key
  91
+     * @param array $data
  92
+     * @return void
  93
+     */
  94
+    public function insert($storageName, $key, array $data)
  95
+    {
  96
+        $bucket = $this->client->bucket($this->bucketName);
  97
+        $object = $bucket->newObject($key, $data);
  98
+        $object->store();
  99
+    }
  100
+
  101
+    /**
  102
+     * Update data into the given key.
  103
+     *
  104
+     * @param string $storageName
  105
+     * @param array|string $key
  106
+     * @param array $data
  107
+     * @return void
  108
+     */
  109
+    public function update($storageName, $key, array $data)
  110
+    {
  111
+        $bucket = $this->client->bucket($this->bucketName);
  112
+        /** @var $object \Riak\Object */
  113
+        $object = $bucket->get($key);
  114
+
  115
+        $object->setData($data);
  116
+        $object->store();
  117
+    }
  118
+
  119
+    /**
  120
+     * Delete data at key
  121
+     *
  122
+     * @param string $storageName
  123
+     * @param array|string $key
  124
+     * @return void
  125
+     */
  126
+    public function delete($storageName, $key)
  127
+    {
  128
+        $bucket = $this->client->bucket($this->bucketName);
  129
+
  130
+        /** @var $object \Riak\Object */
  131
+        $object = $bucket->get($key);
  132
+
  133
+        if (!$object->exists()) {
  134
+            // object does not exist, do nothing
  135
+            return;
  136
+        }
  137
+
  138
+        $object->delete();
  139
+    }
  140
+
  141
+    /**
  142
+     * Find data at key
  143
+     *
  144
+     * Important note: The returned array does contain the identifier (again)!
  145
+     *
  146
+     * @throws Doctrine\KeyValueStore\NotFoundException When data with key is not found.
  147
+     *
  148
+     * @param string $storageName
  149
+     * @param array|string $key
  150
+     * @return array
  151
+     */
  152
+    public function find($storageName, $key)
  153
+    {
  154
+        $bucket = $this->client->bucket($this->bucketName);
  155
+
  156
+        /** @var $object \Riak\Object */
  157
+        $object = $bucket->get($key);
  158
+
  159
+        if (!$object->exists()) {
  160
+            throw new NotFoundException;
  161
+        }
  162
+
  163
+        return $object->getData();
  164
+    }
  165
+
  166
+    /**
  167
+     * Return a name of the underlying storage.
  168
+     *
  169
+     * @return string
  170
+     */
  171
+    public function getName()
  172
+    {
  173
+        return 'riak';
  174
+    }
  175
+}
233  tests/Doctrine/Tests/KeyValueStore/Storage/RiakStorageTest.php
... ...
@@ -0,0 +1,233 @@
  1
+<?php
  2
+
  3
+namespace Doctrine\Tests\KeyValueStore\Storage;
  4
+
  5
+use Doctrine\KeyValueStore\Storage\RiakStorage;
  6
+
  7
+/**
  8
+ * @author Markus Bachmann <markus.bachmann@bachi.biz>
  9
+ */
  10
+class RiakStorageTest extends \PHPUnit_Framework_TestCase
  11
+{
  12
+    /**
  13
+     * @var RiakStorage
  14
+     */
  15
+    private $storage;
  16
+
  17
+    /**
  18
+     * @var \PHPUnit_Framework_MockObject_MockObject
  19
+     */
  20
+    private $riak;
  21
+
  22
+    protected function setup()
  23
+    {
  24
+        $this->riak = $this->getMockBuilder('Riak\\Client')
  25
+                           ->disableOriginalConstructor()
  26
+                           ->getMock();
  27
+
  28
+
  29
+        $this->storage = new RiakStorage($this->riak, 'test');
  30
+    }
  31
+
  32
+    public function testSupportsPartialUpdates()
  33
+    {
  34
+        $this->assertFalse($this->storage->supportsPartialUpdates());
  35
+    }
  36
+
  37
+    public function testSupportsCompositePrimaryKeys()
  38
+    {
  39
+        $this->assertFalse($this->storage->supportsCompositePrimaryKeys());
  40
+    }
  41
+
  42
+    public function testRequiresCompositePrimaryKeys()
  43
+    {
  44
+        $this->assertFalse($this->storage->requiresCompositePrimaryKeys());
  45
+    }
  46
+
  47
+    public function testInsert()
  48
+    {
  49
+        $bucket = $this->getMockBuilder('Riak\Bucket')
  50
+            ->disableOriginalConstructor()
  51
+            ->getMock();
  52
+
  53
+        $this->riak->expects($this->once())
  54
+            ->method('bucket')
  55
+            ->will($this->returnValue($bucket));
  56
+
  57
+        $objectMock = $this->getMockBuilder('Riak\Object')
  58
+                           ->disableOriginalConstructor()
  59
+                           ->getMock();
  60
+
  61
+        $objectMock->expects($this->once())
  62
+                   ->method('store');
  63
+
  64
+
  65
+        $that = $this;
  66
+        $bucket->expects($this->once())
  67
+                   ->method('newObject')
  68
+                   ->will($this->returnCallback(function($key, $data) use ($objectMock, $that) {
  69
+                        $that->assertEquals('foobar', $key);
  70
+                        $that->assertEquals(array('title' => 'Riak test'), $data);
  71
+                        return $objectMock;
  72
+                    }));
  73
+
  74
+        $this->storage->insert('riak-test', 'foobar', array('title' => 'Riak test'));
  75
+    }
  76
+
  77
+    public function testUpdate()
  78
+    {
  79
+        $objectMock = $this->getMockBuilder('Riak\Object')
  80
+                           ->disableOriginalConstructor()
  81
+                           ->getMock();
  82
+
  83
+        $bucket = $this->getMockBuilder('Riak\Bucket')
  84
+            ->disableOriginalConstructor()
  85
+            ->getMock();
  86
+
  87
+        $this->riak->expects($this->once())
  88
+            ->method('bucket')
  89
+            ->will($this->returnValue($bucket));
  90
+
  91
+        $bucket->expects($this->once())
  92
+             ->method('get')
  93
+             ->will($this->returnValue($objectMock));
  94
+
  95
+
  96
+        $that = $this;
  97
+        $objectMock->expects($this->once())
  98
+                   ->method('setData')
  99
+                   ->will($this->returnCallback(function($data) use ($that) {
  100
+                        $that->assertEquals(array('title' => 'Riak cookbook'), $data);
  101
+                   }));
  102
+
  103
+        $objectMock->expects($this->once())
  104
+                   ->method('store');
  105
+
  106
+        $this->storage->update('riak-test', 'foobar', array('title' => 'Riak cookbook'));
  107
+    }
  108
+
  109
+    public function testDelete()
  110
+    {
  111
+        $objectMock = $this->getMockBuilder('Riak\Object')
  112
+            ->disableOriginalConstructor()
  113
+            ->getMock();
  114
+
  115
+        $bucket = $this->getMockBuilder('Riak\Bucket')
  116
+            ->disableOriginalConstructor()
  117
+            ->getMock();
  118
+
  119
+        $this->riak->expects($this->once())
  120
+            ->method('bucket')
  121
+            ->will($this->returnValue($bucket));
  122
+
  123
+        $bucket->expects($this->once())
  124
+            ->method('get')
  125
+            ->with('foobar')
  126
+            ->will($this->returnValue($objectMock));
  127
+
  128
+        $objectMock->expects($this->once())
  129
+                   ->method('exists')
  130
+                   ->will($this->returnValue(true));
  131
+
  132
+        $objectMock->expects($this->once())
  133
+                   ->method('delete');
  134
+
  135
+        $this->storage->delete('riak-test', 'foobar');
  136
+    }
  137
+
  138
+    public function testDeleteWithNotExistKey()
  139
+    {
  140
+        $objectMock = $this->getMockBuilder('Riak\Object')
  141
+            ->disableOriginalConstructor()
  142
+            ->getMock();
  143
+
  144
+        $bucket = $this->getMockBuilder('Riak\Bucket')
  145
+            ->disableOriginalConstructor()
  146
+            ->getMock();
  147
+
  148
+        $this->riak->expects($this->once())
  149
+            ->method('bucket')
  150
+            ->will($this->returnValue($bucket));
  151
+
  152
+        $bucket->expects($this->once())
  153
+            ->method('get')
  154
+            ->with('foobar')
  155
+            ->will($this->returnValue($objectMock));
  156
+
  157
+        $objectMock->expects($this->once())
  158
+            ->method('exists')
  159
+            ->will($this->returnValue(false));
  160
+
  161
+        $objectMock->expects($this->never())
  162
+            ->method('delete');
  163
+
  164
+        $this->storage->delete('riak-test', 'foobar');
  165
+    }
  166
+
  167
+    public function testFind()
  168
+    {
  169
+        $objectMock = $this->getMockBuilder('Riak\Object')
  170
+            ->disableOriginalConstructor()
  171
+            ->getMock();
  172
+
  173
+        $bucket = $this->getMockBuilder('Riak\Bucket')
  174
+            ->disableOriginalConstructor()
  175
+            ->getMock();
  176
+
  177
+        $this->riak->expects($this->once())
  178
+            ->method('bucket')
  179
+            ->will($this->returnValue($bucket));
  180
+
  181
+        $bucket->expects($this->once())
  182
+            ->method('get')
  183
+            ->with('foobar')
  184
+            ->will($this->returnValue($objectMock));
  185
+
  186
+        $objectMock->expects($this->once())
  187
+                   ->method('exists')
  188
+                   ->will($this->returnValue(true));
  189
+
  190
+        $objectMock->expects($this->once())
  191
+                   ->method('getData')
  192
+                   ->will($this->returnValue(array('title' => 'Riak Test')));
  193
+
  194
+        $this->assertEquals(array('title' => 'Riak Test'), $this->storage->find('riaktest', 'foobar'));
  195
+    }
  196
+
  197
+    /**
  198
+     * @expectedException Doctrine\KeyValueStore\NotFoundException
  199
+     */
  200
+    public function testFindWithNotExistKey()
  201
+    {
  202
+        $objectMock = $this->getMockBuilder('Riak\Object')
  203
+            ->disableOriginalConstructor()
  204
+            ->getMock();
  205
+
  206
+        $bucket = $this->getMockBuilder('Riak\Bucket')
  207
+            ->disableOriginalConstructor()
  208
+            ->getMock();
  209
+
  210
+        $this->riak->expects($this->once())
  211
+            ->method('bucket')
  212
+            ->will($this->returnValue($bucket));
  213
+
  214
+        $bucket->expects($this->once())
  215
+            ->method('get')
  216
+            ->with('foobar')
  217
+            ->will($this->returnValue($objectMock));
  218
+
  219
+        $objectMock->expects($this->once())
  220
+            ->method('exists')
  221
+            ->will($this->returnValue(false));
  222
+
  223
+        $objectMock->expects($this->never())
  224
+            ->method('getData');
  225
+
  226
+        $this->storage->find('riak-test', 'foobar');
  227
+    }
  228
+
  229
+    public function testGetName()
  230
+    {
  231
+        $this->assertEquals('riak', $this->storage->getName());
  232
+    }
  233
+}

0 notes on commit 2c1a79a

Please sign in to comment.
Something went wrong with that request. Please try again.