aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/FilesMetadata/Service/IndexRequestService.php
blob: 91bd9f0b11e3a935e8a39eb40391eff617b6eb49 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OC\FilesMetadata\Service;

use OCP\DB\Exception as DbException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
use OCP\FilesMetadata\Exceptions\FilesMetadataTypeException;
use OCP\FilesMetadata\Model\IFilesMetadata;
use OCP\FilesMetadata\Model\IMetadataValueWrapper;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

/**
 * manage sql request to the metadata_index table
 */
class IndexRequestService {
	public const TABLE_METADATA_INDEX = 'files_metadata_index';

	public function __construct(
		private IDBConnection $dbConnection,
		private LoggerInterface $logger,
	) {
	}

	/**
	 * update the index for a specific metadata key
	 *
	 * @param IFilesMetadata $filesMetadata metadata
	 * @param string $key metadata key to update
	 *
	 * @throws DbException
	 */
	public function updateIndex(IFilesMetadata $filesMetadata, string $key): void {
		$fileId = $filesMetadata->getFileId();
		try {
			$metadataType = $filesMetadata->getType($key);
		} catch (FilesMetadataNotFoundException $e) {
			return;
		}

		/**
		 * might look harsh, but a lot simpler than comparing current indexed data, as we can expect
		 * conflict with a change of types.
		 * We assume that each time one random metadata were modified we can drop all index for this
		 * key and recreate them.
		 * To make it slightly cleaner, we'll use transaction
		 */
		$this->dbConnection->beginTransaction();
		try {
			$this->dropIndex($fileId, $key);
			match ($metadataType) {
				IMetadataValueWrapper::TYPE_STRING => $this->insertIndexString($fileId, $key, $filesMetadata->getString($key)),
				IMetadataValueWrapper::TYPE_INT => $this->insertIndexInt($fileId, $key, $filesMetadata->getInt($key)),
				IMetadataValueWrapper::TYPE_BOOL => $this->insertIndexBool($fileId, $key, $filesMetadata->getBool($key)),
				IMetadataValueWrapper::TYPE_STRING_LIST => $this->insertIndexStringList($fileId, $key, $filesMetadata->getStringList($key)),
				IMetadataValueWrapper::TYPE_INT_LIST => $this->insertIndexIntList($fileId, $key, $filesMetadata->getIntList($key))
			};
		} catch (FilesMetadataNotFoundException|FilesMetadataTypeException|DbException $e) {
			$this->dbConnection->rollBack();
			$this->logger->warning('issue while updateIndex', ['exception' => $e, 'fileId' => $fileId, 'key' => $key]);
		}

		$this->dbConnection->commit();
	}

	/**
	 * insert a new entry in the metadata_index table for a string value
	 *
	 * @param int $fileId file id
	 * @param string $key metadata key
	 * @param string $value metadata value
	 *
	 * @throws DbException
	 */
	private function insertIndexString(int $fileId, string $key, string $value): void {
		$qb = $this->dbConnection->getQueryBuilder();
		$qb->insert(self::TABLE_METADATA_INDEX)
			->setValue('meta_key', $qb->createNamedParameter($key))
			->setValue('meta_value_string', $qb->createNamedParameter($value))
			->setValue('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
		$qb->executeStatement();
	}

	/**
	 * insert a new entry in the metadata_index table for an int value
	 *
	 * @param int $fileId file id
	 * @param string $key metadata key
	 * @param int $value metadata value
	 *
	 * @throws DbException
	 */
	public function insertIndexInt(int $fileId, string $key, int $value): void {
		$qb = $this->dbConnection->getQueryBuilder();
		$qb->insert(self::TABLE_METADATA_INDEX)
			->setValue('meta_key', $qb->createNamedParameter($key))
			->setValue('meta_value_int', $qb->createNamedParameter($value, IQueryBuilder::PARAM_INT))
			->setValue('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
		$qb->executeStatement();
	}

	/**
	 * insert a new entry in the metadata_index table for a bool value
	 *
	 * @param int $fileId file id
	 * @param string $key metadata key
	 * @param bool $value metadata value
	 *
	 * @throws DbException
	 */
	public function insertIndexBool(int $fileId, string $key, bool $value): void {
		$qb = $this->dbConnection->getQueryBuilder();
		$qb->insert(self::TABLE_METADATA_INDEX)
			->setValue('meta_key', $qb->createNamedParameter($key))
			->setValue('meta_value_int', $qb->createNamedParameter(($value) ? '1' : '0', IQueryBuilder::PARAM_INT))
			->setValue('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
		$qb->executeStatement();
	}

	/**
	 * insert entries in the metadata_index table for list of string
	 *
	 * @param int $fileId file id
	 * @param string $key metadata key
	 * @param string[] $values metadata values
	 *
	 * @throws DbException
	 */
	public function insertIndexStringList(int $fileId, string $key, array $values): void {
		foreach ($values as $value) {
			$this->insertIndexString($fileId, $key, $value);
		}
	}

	/**
	 * insert entries in the metadata_index table for list of int
	 *
	 * @param int $fileId file id
	 * @param string $key metadata key
	 * @param int[] $values metadata values
	 *
	 * @throws DbException
	 */
	public function insertIndexIntList(int $fileId, string $key, array $values): void {
		foreach ($values as $value) {
			$this->insertIndexInt($fileId, $key, $value);
		}
	}

	/**
	 * drop indexes related to a file id
	 * if a key is specified, only drop entries related to it
	 *
	 * @param int $fileId file id
	 * @param string $key metadata key
	 *
	 * @throws DbException
	 */
	public function dropIndex(int $fileId, string $key = ''): void {
		$qb = $this->dbConnection->getQueryBuilder();
		$expr = $qb->expr();
		$qb->delete(self::TABLE_METADATA_INDEX)
			->where($expr->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));

		if ($key !== '') {
			$qb->andWhere($expr->eq('meta_key', $qb->createNamedParameter($key)));
		}

		$qb->executeStatement();
	}
}