1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?php
namespace phorkie;
class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
{
public function __construct()
{
$this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
}
public function addRepo(Repository $repo, $crdate = null, $modate = null)
{
if ($crdate == null) {
$crdate = time();
}
if ($modate == null) {
$modate = time();
}
$this->updateRepo($repo, $crdate, $modate);
}
public function updateRepo(Repository $repo, $crdate = null, $modate = null)
{
//add repository
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo/' . $repo->id,
\HTTP_Request2::METHOD_PUT
);
$repoData = array(
'id' => $repo->id,
'description' => $repo->getDescription(),
'tstamp' => gmdate('c', time()),
);
if ($crdate == null) {
$crdate = $this->getCrDate($repo);
}
if ($crdate !== null) {
$repoData['crdate'] = gmdate('c', $crdate);
}
if ($modate == null) {
$modate = $this->getMoDate($repo);
}
if ($modate !== null) {
$repoData['modate'] = gmdate('c', $modate);
}
$r->setBody(json_encode((object)$repoData));
$r->send();
//add files
//clean up before adding files; files might have been deleted
$this->deleteRepoFiles($repo);
foreach ($repo->getFiles() as $file) {
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'file/?parent=' . $repo->id,
\HTTP_Request2::METHOD_POST
);
$r->setBody(
json_encode(
(object)array(
'name' => $file->getFilename(),
'extension' => $file->getExt(),
'content' => $file->isText() ? $file->getContent() : '',
)
)
);
$r->send();
}
}
/**
* When updating the repository, we don't have a creation date.
* We need to keep it, but elasticsearch does not have a simple way
* to update some fields only (without using a custom script).
*
* @return integer Unix timestamp
*/
protected function getCrDate(Repository $repo)
{
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo/' . $repo->id,
\HTTP_Request2::METHOD_GET
);
$json = json_decode($r->send()->getBody());
if (!isset($json->_source->crdate)) {
return null;
}
return strtotime($json->_source->crdate);
}
/**
* When updating the repository, we don't have a modification date.
* We need to keep it, but elasticsearch does not have a simple way
* to update some fields only (without using a custom script).
*
* @return integer Unix timestamp
*/
protected function getMoDate(Repository $repo)
{
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo/' . $repo->id,
\HTTP_Request2::METHOD_GET
);
$json = json_decode($r->send()->getBody());
if (!isset($json->_source->modate)) {
return null;
}
return strtotime($json->_source->modate);
}
public function deleteAllRepos()
{
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo',
\HTTP_Request2::METHOD_DELETE
);
$r->send();
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'file',
\HTTP_Request2::METHOD_DELETE
);
$r->send();
}
public function deleteRepo(Repository $repo)
{
//delete repository from index
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo/' . $repo->id,
\HTTP_Request2::METHOD_DELETE
);
$r->allow404 = true;
$r->send();
$this->deleteRepoFiles($repo);
}
protected function deleteRepoFiles(Repository $repo)
{
//delete files of that repository
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'file/_query'
. '?q=_parent:' . $repo->id,
\HTTP_Request2::METHOD_DELETE
);
$r->send();
}
}
?>
|