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