Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
storage.php
1<?php
8
9class Storage
10{
11 protected $iblockId = 0;
12 protected static $exists = array();
13
14 const PRICE = 1;
15 const DICTIONARY = 2;
16 const STRING = 3;
17 const NUMERIC = 4;
18 const DATETIME = 5;
19
23 public function __construct($iblockId)
24 {
25 $this->iblockId = intval($iblockId);
26 }
27
33 public function getIblockId()
34 {
35 return $this->iblockId;
36 }
37
43 public function getTableName()
44 {
45 return "b_iblock_".$this->iblockId."_index";
46 }
47
54 public function isExists()
55 {
56 if (!array_key_exists($this->iblockId, self::$exists))
57 {
58 $connection = \Bitrix\Main\Application::getConnection();
59 self::$exists[$this->iblockId] = $connection->isTableExists($this->getTableName());
60 }
61
62 return self::$exists[$this->iblockId];
63 }
64
71 public function create()
72 {
73 $connection = \Bitrix\Main\Application::getConnection();
74
75 $connection->createTable($this->getTableName(), array(
76 "SECTION_ID" => new \Bitrix\Main\Entity\IntegerField("SECTION_ID", array(
77 'required' => true,
78 )),
79 "ELEMENT_ID" => new \Bitrix\Main\Entity\IntegerField("ELEMENT_ID", array(
80 'required' => true,
81 )),
82 "FACET_ID" => new \Bitrix\Main\Entity\IntegerField("FACET_ID", array(
83 'required' => true,
84 )),
85 "VALUE" => new \Bitrix\Main\Entity\IntegerField("VALUE", array(
86 'required' => true,
87 )),
88 "VALUE_NUM" => new \Bitrix\Main\Entity\FloatField("VALUE_NUM", array(
89 'required' => true,
90 )),
91 "INCLUDE_SUBSECTIONS" => new \Bitrix\Main\Entity\BooleanField("INCLUDE_SUBSECTIONS", array(
92 'required' => true,
93 'values' => array(0, 1),
94 )),
95 ), array("SECTION_ID", "FACET_ID", "VALUE", "VALUE_NUM", "ELEMENT_ID"));
96
97 $connection->createIndex($this->getTableName(), 'IX_'.$this->getTableName().'_0', array("SECTION_ID", "FACET_ID", "VALUE_NUM", "VALUE", "ELEMENT_ID"));
98 $connection->createIndex($this->getTableName(), 'IX_'.$this->getTableName().'_1', array("ELEMENT_ID", "SECTION_ID", "FACET_ID"));
99
100 self::$exists[$this->iblockId] = true;
101 }
102
109 public function drop()
110 {
111 $connection = \Bitrix\Main\Application::getConnection();
112
113 $connection->dropTable($this->getTableName());
114
115 self::$exists[$this->iblockId] = false;
116 }
117
123 public function getLastStoredElementId()
124 {
125 $connection = \Bitrix\Main\Application::getConnection();
126
127 $max = $connection->queryScalar("select max(ELEMENT_ID) ELEMENT_MAX from ".$this->getTableName());
128
129 return $max > 0? $max: 0;
130 }
131
144 public function addIndexEntry($sectionId, $elementId, $facetId, $value, $valueNum, $includeSubsections)
145 {
146 $connection = \Bitrix\Main\Application::getConnection();
147
148 try
149 {
150 $connection->query("
151 INSERT INTO ".$this->getTableName()." (
152 SECTION_ID
153 ,ELEMENT_ID
154 ,FACET_ID
155 ,VALUE
156 ,VALUE_NUM
157 ,INCLUDE_SUBSECTIONS
158 ) VALUES (
159 ".intval($sectionId)."
160 ,".intval($elementId)."
161 ,".intval($facetId)."
162 ,".intval($value)."
163 ,".doubleval($valueNum)."
164 ,".($includeSubsections > 0? 1: 0)."
165 )
166 ");
167 }
168 catch (\Bitrix\Main\DB\SqlException $e)
169 {
170 return false;
171 }
172
173 return true;
174 }
175
176 protected $insertBuffer = array();
177 protected $insertLength = 0;
178 protected $insertMax = 1024000;
179
192 public function queueIndexEntry($sectionId, $elementId, $facetId, $value, $valueNum, $includeSubsections)
193 {
194 $connection = \Bitrix\Main\Application::getConnection();
195 $sqlHelper = $connection->getSqlHelper();
196
197 $values = "("
198 .intval($sectionId).","
199 .intval($elementId).","
200 .intval($facetId).","
201 .intval($value).","
202 .doubleval($valueNum).","
203 .($includeSubsections > 0? 1: 0)
204 .")";
205 $this->insertBuffer[] = $values;
206 $this->insertLength += mb_strlen($values);
207 if ($this->insertLength > $this->insertMax)
208 {
209 return $this->flushIndexEntries();
210 }
211
212 return true;
213 }
214
220 public function flushIndexEntries()
221 {
222 $connection = \Bitrix\Main\Application::getConnection();
223 if ($this->insertBuffer)
224 {
225 try
226 {
227 $insertQuery = "
228 INSERT INTO ".$this->getTableName()."
229 (SECTION_ID ,ELEMENT_ID ,FACET_ID ,VALUE ,VALUE_NUM ,INCLUDE_SUBSECTIONS)
230 VALUES ".implode(',', $this->insertBuffer)."
231 ";
232 $connection->query($insertQuery);
233 $this->insertBuffer = array();
234 $this->insertLength = 0;
235 }
236 catch (\Bitrix\Main\DB\SqlException $e)
237 {
238 return false;
239 }
240 }
241
242 return true;
243 }
244
252 public function deleteIndexElement($elementId)
253 {
254 $connection = \Bitrix\Main\Application::getConnection();
255
256 $connection->query("DELETE from ".$this->getTableName()." WHERE ELEMENT_ID = ".intval($elementId));
257
258 return true;
259 }
260
267 public static function propertyIdToFacetId($propertyId)
268 {
269 return intval($propertyId * 2);
270 }
271
278 public static function priceIdToFacetId($priceId)
279 {
280 return intval($priceId * 2 + 1);
281 }
282
290 public static function isPriceId($facetId)
291 {
292 return ($facetId % 2) != 0;
293 }
294
302 public static function isPropertyId($facetId)
303 {
304 return ($facetId % 2) == 0;
305 }
306
314 public static function facetIdToPropertyId($facetId)
315 {
316 return intval($facetId / 2);
317 }
318
326 public static function facetIdToPriceId($facetId)
327 {
328 return intval(($facetId - 1) / 2);
329 }
330}
static propertyIdToFacetId($propertyId)
Definition storage.php:267
queueIndexEntry($sectionId, $elementId, $facetId, $value, $valueNum, $includeSubsections)
Definition storage.php:192
addIndexEntry($sectionId, $elementId, $facetId, $value, $valueNum, $includeSubsections)
Definition storage.php:144
static facetIdToPropertyId($facetId)
Definition storage.php:314