Passed
Push — master ( 57ecf5...cc5e26 )
by Robin
16:29 queued 51s
created

StorageGlobal   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 77
rs 10
c 1
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStorageInfoByNumericId() 0 17 3
A getStorageInfo() 0 17 3
A __construct() 0 2 1
A loadForStorageIds() 0 11 2
A clearCache() 0 2 1
1
<?php
2
/**
3
 * @copyright Robin Appelman <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OC\Files\Cache;
25
26
use OCP\DB\QueryBuilder\IQueryBuilder;
27
use OCP\IDBConnection;
28
29
/**
30
 * Handle the mapping between the string and numeric storage ids
31
 *
32
 * Each storage has 2 different ids
33
 *    a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
34
 *    and a numeric storage id which is referenced in the file cache
35
 *
36
 * A mapping between the two storage ids is stored in the database and accessible trough this class
37
 *
38
 * @package OC\Files\Cache
39
 */
40
class StorageGlobal {
41
	/** @var IDBConnection */
42
	private $connection;
43
44
	/** @var array<string, array> */
45
	private $cache = [];
46
	/** @var array<int, array> */
47
	private $numericIdCache = [];
48
49
	public function __construct(IDBConnection $connection) {
50
		$this->connection = $connection;
51
	}
52
53
	/**
54
	 * @param string[] $storageIds
55
	 */
56
	public function loadForStorageIds(array $storageIds) {
57
		$builder = $this->connection->getQueryBuilder();
58
		$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
59
			->from('storages')
60
			->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY)));
61
62
		$result = $query->execute();
63
		while ($row = $result->fetch()) {
64
			$this->cache[$row['id']] = $row;
65
		}
66
		$result->closeCursor();
67
	}
68
69
	/**
70
	 * @param string $storageId
71
	 * @return array|null
72
	 */
73
	public function getStorageInfo(string $storageId): ?array {
74
		if (!isset($this->cache[$storageId])) {
75
			$builder = $this->connection->getQueryBuilder();
76
			$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
77
				->from('storages')
78
				->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));
79
80
			$result = $query->execute();
81
			$row = $result->fetch();
82
			$result->closeCursor();
83
84
			if ($row) {
85
				$this->cache[$storageId] = $row;
86
				$this->numericIdCache[(int)$row['numeric_id']] = $row;
87
			}
88
		}
89
		return $this->cache[$storageId] ?? null;
90
	}
91
92
	/**
93
	 * @param int $numericId
94
	 * @return array|null
95
	 */
96
	public function getStorageInfoByNumericId(int $numericId): ?array {
97
		if (!isset($this->numericIdCache[$numericId])) {
98
			$builder = $this->connection->getQueryBuilder();
99
			$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
100
				->from('storages')
101
				->where($builder->expr()->eq('numeric_id', $builder->createNamedParameter($numericId)));
102
103
			$result = $query->execute();
104
			$row = $result->fetch();
105
			$result->closeCursor();
106
107
			if ($row) {
108
				$this->numericIdCache[$numericId] = $row;
109
				$this->cache[$row['id']] = $row;
110
			}
111
		}
112
		return $this->numericIdCache[$numericId] ?? null;
113
	}
114
115
	public function clearCache() {
116
		$this->cache = [];
117
	}
118
}
119