1313
1414namespace sweetrdf \InMemoryStoreSqlite \Store \QueryHandler ;
1515
16- use sweetrdf \InMemoryStoreSqlite \KeyValueBag ;
1716use sweetrdf \InMemoryStoreSqlite \Log \Logger ;
1817use sweetrdf \InMemoryStoreSqlite \Store \InMemoryStoreSqlite ;
1918
2019use function sweetrdf \InMemoryStoreSqlite \getNormalizedValue ;
2120
2221class InsertQueryHandler extends QueryHandler
2322{
24- /**
25- * If true, store assumes one or more insert into SPARQL queries and will
26- * skip certain DB operations to speed up insertion process.
27- */
28- private bool $ bulkLoadModeIsActive = false ;
29-
3023 /**
3124 * Is used if $bulkLoadModeIsActive is true. Determines next term ID for
3225 * entries in id2val, s2val and o2val.
3326 */
3427 private int $ bulkLoadModeNextTermId = 1 ;
3528
36- /**
37- * When set it is used to store term information to speed up insert into operations.
38- */
39- private KeyValueBag $ rowCache ;
40-
4129 /**
4230 * Is being used for blank nodes to generate a hash which is not only dependent on
4331 * blank node ID and graph, but also on a random value.
@@ -48,25 +36,6 @@ class InsertQueryHandler extends QueryHandler
4836 public function __construct (InMemoryStoreSqlite $ store , Logger $ logger )
4937 {
5038 parent ::__construct ($ store , $ logger );
51-
52- // default value; will be overridden when running inside InMemoryStoreSqlite
53- $ this ->rowCache = new KeyValueBag ();
54- }
55-
56- public function activateBulkLoadMode (int $ bulkLoadModeNextTermId ): void
57- {
58- $ this ->bulkLoadModeIsActive = true ;
59- $ this ->bulkLoadModeNextTermId = $ bulkLoadModeNextTermId ;
60- }
61-
62- public function getBulkLoadModeNextTermId (): int
63- {
64- return $ this ->bulkLoadModeNextTermId ;
65- }
66-
67- public function setRowCache (KeyValueBag $ rowCache ): void
68- {
69- $ this ->rowCache = $ rowCache ;
7039 }
7140
7241 public function runQuery (array $ infos )
@@ -100,7 +69,7 @@ private function addTripleToGraph(array $triple, string $graph): void
10069 $ graphId = $ this ->getIdOfExistingTerm ($ graph , 'id ' );
10170 if (null == $ graphId ) {
10271 $ graphId = $ this ->store ->getDBObject ()->insert ('id2val ' , [
103- 'id ' => $ this ->getMaxTermId (),
72+ 'id ' => $ this ->getNextMaxTermId (),
10473 'val ' => $ graph ,
10574 'val_type ' => 0 , // = uri
10675 ]);
@@ -111,7 +80,7 @@ private function addTripleToGraph(array $triple, string $graph): void
11180 */
11281 $ subjectId = $ this ->getIdOfExistingTerm ($ triple ['s ' ], 'subject ' );
11382 if (null == $ subjectId ) {
114- $ subjectId = $ this ->getMaxTermId ();
83+ $ subjectId = $ this ->getNextMaxTermId ();
11584 $ this ->store ->getDBObject ()->insert ('s2val ' , [
11685 'id ' => $ subjectId ,
11786 'val ' => $ triple ['s ' ],
@@ -124,7 +93,7 @@ private function addTripleToGraph(array $triple, string $graph): void
12493 */
12594 $ predicateId = $ this ->getIdOfExistingTerm ($ triple ['p ' ], 'id ' );
12695 if (null == $ predicateId ) {
127- $ predicateId = $ this ->getMaxTermId ();
96+ $ predicateId = $ this ->getNextMaxTermId ();
12897 $ this ->store ->getDBObject ()->insert ('id2val ' , [
12998 'id ' => $ predicateId ,
13099 'val ' => $ triple ['p ' ],
@@ -137,7 +106,7 @@ private function addTripleToGraph(array $triple, string $graph): void
137106 */
138107 $ objectId = $ this ->getIdOfExistingTerm ($ triple ['o ' ], 'object ' );
139108 if (null == $ objectId ) {
140- $ objectId = $ this ->getMaxTermId ();
109+ $ objectId = $ this ->getNextMaxTermId ();
141110 $ this ->store ->getDBObject ()->insert ('o2val ' , [
142111 'id ' => $ objectId ,
143112 'val ' => $ triple ['o ' ],
@@ -163,7 +132,7 @@ private function addTripleToGraph(array $triple, string $graph): void
163132
164133 $ oLangDtId = $ this ->getIdOfExistingTerm ($ oLangDt , 'id ' );
165134 if (null == $ oLangDtId ) {
166- $ oLangDtId = $ this ->getMaxTermId ();
135+ $ oLangDtId = $ this ->getNextMaxTermId ();
167136 $ this ->store ->getDBObject ()->insert ('id2val ' , [
168137 'id ' => $ oLangDtId ,
169138 'val ' => $ oLangDt ,
@@ -250,32 +219,13 @@ private function prepareTriple(array $triple, string $graph): array
250219 }
251220
252221 /**
253- * Generates the next valid ID based on latest values in id2val, s2val and o2val .
222+ * Generates the next valid ID.
254223 *
255224 * @return int returns 1 or higher
256225 */
257- private function getMaxTermId (): int
226+ private function getNextMaxTermId (): int
258227 {
259- if (true === $ this ->bulkLoadModeIsActive ) {
260- return $ this ->bulkLoadModeNextTermId ++;
261- } else {
262- $ sql = '' ;
263- foreach (['id2val ' , 's2val ' , 'o2val ' ] as $ table ) {
264- $ sql .= !empty ($ sql ) ? ' UNION ' : '' ;
265- $ sql .= 'SELECT MAX(id) as id FROM ' .$ table ;
266- }
267- $ result = 0 ;
268-
269- $ rows = $ this ->store ->getDBObject ()->fetchList ($ sql );
270-
271- if (\is_array ($ rows )) {
272- foreach ($ rows as $ row ) {
273- $ result = ($ result < $ row ['id ' ]) ? $ row ['id ' ] : $ result ;
274- }
275- }
276-
277- return $ result + 1 ;
278- }
228+ return $ this ->bulkLoadModeNextTermId ++;
279229 }
280230
281231 /**
@@ -290,15 +240,7 @@ private function getIdOfExistingTerm(string $value, string $quadPart): ?int
290240 if ('id ' == $ quadPart ) {
291241 $ sql = 'SELECT id, val FROM id2val WHERE val = ? ' ;
292242
293- $ hashKey = md5 ($ sql .json_encode ([$ value ]));
294- if (false === $ this ->rowCache ->has ($ hashKey )) {
295- $ row = $ this ->store ->getDBObject ()->fetchRow ($ sql , [$ value ]);
296- if (\is_array ($ row )) {
297- $ this ->rowCache ->set ($ hashKey , $ row );
298- }
299- }
300-
301- $ entry = $ this ->rowCache ->get ($ hashKey );
243+ $ entry = $ this ->store ->getDBObject ()->fetchRow ($ sql , [$ value ]);
302244
303245 // entry found, use its ID
304246 if (\is_array ($ entry )) {
@@ -312,15 +254,7 @@ private function getIdOfExistingTerm(string $value, string $quadPart): ?int
312254 $ sql = 'SELECT id, val FROM ' .$ table .' WHERE val_hash = ? ' ;
313255 $ params = [$ this ->getValueHash ($ value )];
314256
315- $ hashKey = md5 ($ sql .json_encode ($ params ));
316- if (false === $ this ->rowCache ->has ($ hashKey )) {
317- $ row = $ this ->store ->getDBObject ()->fetchRow ($ sql , $ params );
318- if (\is_array ($ row )) {
319- $ this ->rowCache ->set ($ hashKey , $ row );
320- }
321- }
322-
323- $ entry = $ this ->rowCache ->get ($ hashKey );
257+ $ entry = $ this ->store ->getDBObject ()->fetchRow ($ sql , $ params );
324258
325259 // entry found, use its ID
326260 if (isset ($ entry ['val ' ]) && $ entry ['val ' ] == $ value ) {
0 commit comments