|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mihkel |
|
5
|
|
|
* Date: 27.08.14 |
|
6
|
|
|
* Time: 22:34 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace opus\elastic\spooler; |
|
10
|
|
|
|
|
11
|
|
|
use yii\base\BaseObject; |
|
12
|
|
|
use yii\db\Exception; |
|
13
|
|
|
use yii\db\Query; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class operates with spool table |
|
17
|
|
|
* Mass inserts, individual record save, marking processing rows are supported |
|
18
|
|
|
* Class Spooler |
|
19
|
|
|
* |
|
20
|
|
|
* @package opus\elastic |
|
21
|
|
|
*/ |
|
22
|
|
|
class Spooler extends BaseObject |
|
23
|
|
|
{ |
|
24
|
|
|
public static $tableName = 'ym_elastic_spool_item'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $actionCode string ElasticSearch action code (INDEX, DELETE supported) |
|
28
|
|
|
* @param $modelName string ActiveRecord class name |
|
29
|
|
|
* @param $recordId int ActiveRecord record id |
|
30
|
|
|
* @return \yii\db\Command |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function saveItem($actionCode, $modelName, $recordId) |
|
33
|
|
|
{ |
|
34
|
|
|
return (new Query()) |
|
35
|
|
|
->createCommand() |
|
36
|
|
|
->insert( |
|
37
|
|
|
\Yii::$app->elasticsearch->spoolerTableName, |
|
38
|
|
|
[ |
|
39
|
|
|
'action_code' => $actionCode, |
|
40
|
|
|
'model_class' => $modelName, |
|
41
|
|
|
'record_id' => $recordId, |
|
42
|
|
|
'is_processing' => 0, |
|
43
|
|
|
] |
|
44
|
|
|
)->execute(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @param $modelName string |
|
50
|
|
|
* @param $modelTable string |
|
51
|
|
|
* @throws Exception |
|
52
|
|
|
* @throws \Exception |
|
53
|
|
|
* @return \yii\db\Command |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function reindexData($modelName, $modelTable) |
|
56
|
|
|
{ |
|
57
|
|
|
$tableName = \Yii::$app->elasticsearch->spoolerTableName; |
|
58
|
|
|
$modelTable = sprintf('{{%s}}', $modelTable); |
|
59
|
|
|
$rows = 0; |
|
60
|
|
|
$transaction = \Yii::$app->db->beginTransaction(); |
|
61
|
|
|
try { |
|
62
|
|
|
\Yii::$app->db->createCommand()->delete( |
|
63
|
|
|
self::$tableName, |
|
64
|
|
|
['model_class' => $modelName] |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$sql = |
|
68
|
|
|
"INSERT INTO $tableName |
|
69
|
|
|
([[model_class]], [[record_id]], [[action_code]], [[is_processing]]) |
|
70
|
|
|
SELECT :modelName, [[id]], 'INDEX', 0 FROM $modelTable"; |
|
71
|
|
|
|
|
72
|
|
|
$rows = \Yii::$app->db |
|
73
|
|
|
->createCommand($sql, [':modelName' => $modelName]) |
|
74
|
|
|
->execute(); |
|
75
|
|
|
$transaction->commit(); |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
$transaction->rollBack(); |
|
78
|
|
|
throw $e; |
|
79
|
|
|
} |
|
80
|
|
|
return $rows; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return int |
|
85
|
|
|
* @throws \yii\db\Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function deleteProcessingRows() |
|
88
|
|
|
{ |
|
89
|
|
|
return \Yii::$app->db |
|
90
|
|
|
->createCommand() |
|
91
|
|
|
->delete( |
|
92
|
|
|
\Yii::$app->elasticsearch->spoolerTableName, |
|
93
|
|
|
['is_processing' => 1] |
|
94
|
|
|
) |
|
95
|
|
|
->execute(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Marks a batch to is_processing |
|
100
|
|
|
* |
|
101
|
|
|
* @param integer $limit |
|
102
|
|
|
* @param integer $offset |
|
103
|
|
|
* @param string $class |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function setProcessingRows($limit, $offset, $class) |
|
107
|
|
|
{ |
|
108
|
|
|
$tableName = \Yii::$app->elasticsearch->spoolerTableName; |
|
109
|
|
|
return \Yii::$app->db->createCommand( |
|
110
|
|
|
"UPDATE $tableName spool |
|
111
|
|
|
INNER JOIN ( |
|
112
|
|
|
SELECT id FROM $tableName |
|
113
|
|
|
WHERE model_class = :class |
|
114
|
|
|
LIMIT $limit OFFSET $offset |
|
115
|
|
|
) AS spool_join ON spool_join.id = spool.id |
|
116
|
|
|
SET spool.is_processing = 1", |
|
117
|
|
|
[':class' => $class] |
|
118
|
|
|
)->execute(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Provides total count |
|
123
|
|
|
* |
|
124
|
|
|
* @param $className |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function getTotalCount($className) |
|
128
|
|
|
{ |
|
129
|
|
|
return (new Query()) |
|
130
|
|
|
->select('COUNT(id)') |
|
131
|
|
|
->from(\Yii::$app->elasticsearch->spoolerTableName) |
|
132
|
|
|
->where(['model_class' => $className]) |
|
133
|
|
|
->scalar(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Sets all rows to is_processing to 0 |
|
138
|
|
|
* @return int |
|
139
|
|
|
* @throws Exception |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function removeProcessingRows() |
|
142
|
|
|
{ |
|
143
|
|
|
$tableName = \Yii::$app->elasticsearch->spoolerTableName; |
|
144
|
|
|
return \Yii::$app->db->createCommand( |
|
145
|
|
|
"UPDATE $tableName spool SET spool.is_processing = 0" |
|
146
|
|
|
)->execute(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|