|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mattbit\Flat\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Mattbit\Flat\Model\DocumentInterface; |
|
6
|
|
|
use Mattbit\Flat\Storage\EncoderInterface; |
|
7
|
|
|
use Mattbit\Flat\Exception\DuplicateKeyException; |
|
8
|
|
|
use Mattbit\Flat\Storage\Filesystem\FilesystemCursor; |
|
9
|
|
|
|
|
10
|
|
|
class DocumentStore |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var EngineInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $engine; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $namespace; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create a DocumentStore instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @param EngineInterface $engine |
|
26
|
|
|
* @param string $namespace |
|
27
|
|
|
*/ |
|
28
|
13 |
|
public function __construct(EngineInterface $engine, $namespace) |
|
29
|
|
|
{ |
|
30
|
13 |
|
$this->engine = $engine; |
|
31
|
13 |
|
$this->namespace = $namespace; |
|
32
|
13 |
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function getNamespace() |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->namespace; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getEngine() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->engine; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function truncate() |
|
45
|
|
|
{ |
|
46
|
1 |
|
return $this->engine->clear($this->namespace); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
public function insert(DocumentInterface $document) |
|
50
|
|
|
{ |
|
51
|
3 |
|
if (!$id = $document->getId()) { |
|
52
|
1 |
|
$id = $this->generateId(); |
|
53
|
1 |
|
$document->setId($id); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
if ($this->engine->has($id, $this->namespace)) { |
|
57
|
1 |
|
throw new DuplicateKeyException("Cannot insert document with duplicate key: {$id}"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
$this->engine->put($document, $id, $this->namespace); |
|
61
|
|
|
|
|
62
|
2 |
|
return $id; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
public function update(DocumentInterface $document) |
|
66
|
|
|
{ |
|
67
|
2 |
|
if (!$id = $document->getId()) { |
|
68
|
1 |
|
throw new \Exception("Cannot update a document without _id!"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return $this->engine->put($document, $id, $this->namespace); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function remove($id) |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->engine->delete($id, $this->namespace); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function find($id) |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->engine->get($id, $this->namespace); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
public function scan(callable $filter = null, $limit = null) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$documents = []; |
|
87
|
|
|
|
|
88
|
3 |
|
$index = 0; |
|
89
|
3 |
|
foreach ($this->engine->all($this->namespace) as $document) { |
|
90
|
3 |
|
if ($limit && $index >= $limit) { |
|
91
|
1 |
|
break; |
|
92
|
|
|
} |
|
93
|
3 |
|
$index += 1; |
|
94
|
|
|
|
|
95
|
3 |
|
if (!$filter || call_user_func($filter, $document)) { |
|
96
|
3 |
|
$documents[] = $document; |
|
97
|
3 |
|
} |
|
98
|
3 |
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
return $documents; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
protected function generateId() |
|
104
|
|
|
{ |
|
105
|
|
|
// A simple uniqid should have enough entropy. |
|
106
|
|
|
// @todo: evaluate the usage of ramsey/uuid |
|
107
|
1 |
|
return uniqid(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|