|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mattbit\Flat\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Mattbit\Flat\Document\Document; |
|
6
|
|
|
use Mattbit\Flat\Document\Identifiable; |
|
7
|
|
|
|
|
8
|
|
|
class DocumentStore |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Engine |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $engine; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var EncoderInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $encoder; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $namespace; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create a RecordStore instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Engine $engine |
|
29
|
|
|
* @param string $namespace |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Engine $engine, $namespace) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->engine = $engine; |
|
34
|
|
|
$this->namespace = $namespace; |
|
35
|
|
|
$this->filesystem = $this->engine->getFilesystem(); |
|
|
|
|
|
|
36
|
|
|
$this->encoder = $this->engine->getEncoder(); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function truncate() |
|
40
|
|
|
{ |
|
41
|
|
|
$files = $this->filesystem->listContents($this->namespace); |
|
42
|
|
|
|
|
43
|
|
|
foreach ($files as $file) { |
|
44
|
|
|
$this->filesystem->delete($file['path']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function insertDocument(Identifiable $document) |
|
51
|
|
|
{ |
|
52
|
|
|
$id = $document->getId() ?: $this->generateId(); |
|
53
|
|
|
$path = $this->path($id); |
|
54
|
|
|
$data = $this->encoder->encode($document); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if ($this->filesystem->has($this->path($id))) { |
|
57
|
|
|
throw new \Exception("Duplicate _id: {$id}"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$this->filesystem->put($path, $data); |
|
62
|
|
|
|
|
63
|
|
|
return $id; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function insertDocuments(array $documents) |
|
67
|
|
|
{ |
|
68
|
|
|
$ids = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($documents as $document) { |
|
71
|
|
|
$ids[] = $this->insertDocument($document); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $ids; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function updateDocument(Identifiable $document) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$id = $document->getId()) { |
|
80
|
|
|
throw new \Exception("Cannot update a document without _id!"); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$data = $this->encoder->encode($document); |
|
|
|
|
|
|
84
|
|
|
$path = $this->path($id); |
|
85
|
|
|
|
|
86
|
|
|
return $this->filesystem->put($path, $data); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function removeDocument($documentId) |
|
90
|
|
|
{ |
|
91
|
|
|
$path = $this->path($documentId); |
|
92
|
|
|
|
|
93
|
|
|
return $this->filesystem->delete($path); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function removeDocuments(array $documentIds) |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($documentIds as $documentId) { |
|
99
|
|
|
$this->removeDocument($documentId); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function findDocument($documentId) |
|
106
|
|
|
{ |
|
107
|
|
|
$path = $this->path($documentId); |
|
108
|
|
|
$data = $this->filesystem->read($path); |
|
109
|
|
|
|
|
110
|
|
|
return $this->encoder->decode($data); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function scanDocuments(callable $filter = null, $limit = null) |
|
114
|
|
|
{ |
|
115
|
|
|
$files = $this->filesystem->listContents($this->namespace); |
|
116
|
|
|
$documents = []; |
|
117
|
|
|
|
|
118
|
|
|
foreach ($files as $index => $file) { |
|
119
|
|
|
if ($limit && $index > $limit) { |
|
120
|
|
|
break; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$data = $this->filesystem->read($file['path']); |
|
124
|
|
|
$document = $this->encoder->decode($data); |
|
125
|
|
|
|
|
126
|
|
|
if (!$filter || call_user_func($filter, $document)) { |
|
127
|
|
|
$documents[] = $document; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $documents; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
protected function path($id) |
|
135
|
|
|
{ |
|
136
|
|
|
return sprintf('%s/%s.%s', $this->namespace, $id, $this->encoder->getExtension()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function extractId($record) |
|
140
|
|
|
{ |
|
141
|
|
|
if (isset($record['_id'])) { |
|
142
|
|
|
return $record['_id']; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
protected function generateId() |
|
147
|
|
|
{ |
|
148
|
|
|
// A simple uniqid should have enough entropy. |
|
149
|
|
|
return uniqid(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
protected function newDocument($attributes) |
|
153
|
|
|
{ |
|
154
|
|
|
return new Document($attributes); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: