Passed
Push — master ( 57ecf5...cc5e26 )
by Robin
16:29 queued 51s
created
lib/private/Files/Cache/StorageGlobal.php 2 patches
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -38,81 +38,81 @@
 block discarded – undo
38 38
  * @package OC\Files\Cache
39 39
  */
40 40
 class StorageGlobal {
41
-	/** @var IDBConnection */
42
-	private $connection;
41
+    /** @var IDBConnection */
42
+    private $connection;
43 43
 
44
-	/** @var array<string, array> */
45
-	private $cache = [];
46
-	/** @var array<int, array> */
47
-	private $numericIdCache = [];
44
+    /** @var array<string, array> */
45
+    private $cache = [];
46
+    /** @var array<int, array> */
47
+    private $numericIdCache = [];
48 48
 
49
-	public function __construct(IDBConnection $connection) {
50
-		$this->connection = $connection;
51
-	}
49
+    public function __construct(IDBConnection $connection) {
50
+        $this->connection = $connection;
51
+    }
52 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)));
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 61
 
62
-		$result = $query->execute();
63
-		while ($row = $result->fetch()) {
64
-			$this->cache[$row['id']] = $row;
65
-		}
66
-		$result->closeCursor();
67
-	}
62
+        $result = $query->execute();
63
+        while ($row = $result->fetch()) {
64
+            $this->cache[$row['id']] = $row;
65
+        }
66
+        $result->closeCursor();
67
+    }
68 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)));
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 79
 
80
-			$result = $query->execute();
81
-			$row = $result->fetch();
82
-			$result->closeCursor();
80
+            $result = $query->execute();
81
+            $row = $result->fetch();
82
+            $result->closeCursor();
83 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
-	}
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 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)));
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 102
 
103
-			$result = $query->execute();
104
-			$row = $result->fetch();
105
-			$result->closeCursor();
103
+            $result = $query->execute();
104
+            $row = $result->fetch();
105
+            $result->closeCursor();
106 106
 
107
-			if ($row) {
108
-				$this->numericIdCache[$numericId] = $row;
109
-				$this->cache[$row['id']] = $row;
110
-			}
111
-		}
112
-		return $this->numericIdCache[$numericId] ?? null;
113
-	}
107
+            if ($row) {
108
+                $this->numericIdCache[$numericId] = $row;
109
+                $this->cache[$row['id']] = $row;
110
+            }
111
+        }
112
+        return $this->numericIdCache[$numericId] ?? null;
113
+    }
114 114
 
115
-	public function clearCache() {
116
-		$this->cache = [];
117
-	}
115
+    public function clearCache() {
116
+        $this->cache = [];
117
+    }
118 118
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@
 block discarded – undo
83 83
 
84 84
 			if ($row) {
85 85
 				$this->cache[$storageId] = $row;
86
-				$this->numericIdCache[(int)$row['numeric_id']] = $row;
86
+				$this->numericIdCache[(int) $row['numeric_id']] = $row;
87 87
 			}
88 88
 		}
89 89
 		return $this->cache[$storageId] ?? null;
Please login to merge, or discard this patch.
lib/private/Files/Cache/Storage.php 1 patch
Indentation   +207 added lines, -207 removed lines patch added patch discarded remove patch
@@ -45,211 +45,211 @@
 block discarded – undo
45 45
  * @package OC\Files\Cache
46 46
  */
47 47
 class Storage {
48
-	/** @var StorageGlobal|null */
49
-	private static $globalCache = null;
50
-	private $storageId;
51
-	private $numericId;
52
-
53
-	/**
54
-	 * @return StorageGlobal
55
-	 */
56
-	public static function getGlobalCache() {
57
-		if (is_null(self::$globalCache)) {
58
-			self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
59
-		}
60
-		return self::$globalCache;
61
-	}
62
-
63
-	/**
64
-	 * @param \OC\Files\Storage\Storage|string $storage
65
-	 * @param bool $isAvailable
66
-	 * @throws \RuntimeException
67
-	 */
68
-	public function __construct($storage, $isAvailable = true) {
69
-		if ($storage instanceof IStorage) {
70
-			$this->storageId = $storage->getId();
71
-		} else {
72
-			$this->storageId = $storage;
73
-		}
74
-		$this->storageId = self::adjustStorageId($this->storageId);
75
-
76
-		if ($row = self::getStorageById($this->storageId)) {
77
-			$this->numericId = (int)$row['numeric_id'];
78
-		} else {
79
-			$connection = \OC::$server->getDatabaseConnection();
80
-			$available = $isAvailable ? 1 : 0;
81
-			if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
82
-				$this->numericId = $connection->lastInsertId('*PREFIX*storages');
83
-			} else {
84
-				if ($row = self::getStorageById($this->storageId)) {
85
-					$this->numericId = (int)$row['numeric_id'];
86
-				} else {
87
-					throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
88
-				}
89
-			}
90
-		}
91
-	}
92
-
93
-	/**
94
-	 * @param string $storageId
95
-	 * @return array
96
-	 */
97
-	public static function getStorageById($storageId) {
98
-		return self::getGlobalCache()->getStorageInfo($storageId);
99
-	}
100
-
101
-	/**
102
-	 * Adjusts the storage id to use md5 if too long
103
-	 * @param string $storageId storage id
104
-	 * @return string unchanged $storageId if its length is less than 64 characters,
105
-	 * else returns the md5 of $storageId
106
-	 */
107
-	public static function adjustStorageId($storageId) {
108
-		if (strlen($storageId) > 64) {
109
-			return md5($storageId);
110
-		}
111
-		return $storageId;
112
-	}
113
-
114
-	/**
115
-	 * Get the numeric id for the storage
116
-	 *
117
-	 * @return int
118
-	 */
119
-	public function getNumericId() {
120
-		return $this->numericId;
121
-	}
122
-
123
-	/**
124
-	 * Get the string id for the storage
125
-	 *
126
-	 * @param int $numericId
127
-	 * @return string|null either the storage id string or null if the numeric id is not known
128
-	 */
129
-	public static function getStorageId(int $numericId): ?string {
130
-		$storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId);
131
-		return $storage['id'] ?? null;
132
-	}
133
-
134
-	/**
135
-	 * Get the numeric of the storage with the provided string id
136
-	 *
137
-	 * @param $storageId
138
-	 * @return int|null either the numeric storage id or null if the storage id is not knwon
139
-	 */
140
-	public static function getNumericStorageId($storageId) {
141
-		$storageId = self::adjustStorageId($storageId);
142
-
143
-		if ($row = self::getStorageById($storageId)) {
144
-			return (int)$row['numeric_id'];
145
-		} else {
146
-			return null;
147
-		}
148
-	}
149
-
150
-	/**
151
-	 * @return array [ available, last_checked ]
152
-	 */
153
-	public function getAvailability() {
154
-		if ($row = self::getStorageById($this->storageId)) {
155
-			return [
156
-				'available' => (int)$row['available'] === 1,
157
-				'last_checked' => $row['last_checked']
158
-			];
159
-		} else {
160
-			return [
161
-				'available' => true,
162
-				'last_checked' => time(),
163
-			];
164
-		}
165
-	}
166
-
167
-	/**
168
-	 * @param bool $isAvailable
169
-	 * @param int $delay amount of seconds to delay reconsidering that storage further
170
-	 */
171
-	public function setAvailability($isAvailable, int $delay = 0) {
172
-		$available = $isAvailable ? 1 : 0;
173
-		if (!$isAvailable) {
174
-			\OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
175
-		}
176
-
177
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
178
-		$query->update('storages')
179
-			->set('available', $query->createNamedParameter($available))
180
-			->set('last_checked', $query->createNamedParameter(time() + $delay))
181
-			->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
182
-		$query->execute();
183
-	}
184
-
185
-	/**
186
-	 * Check if a string storage id is known
187
-	 *
188
-	 * @param string $storageId
189
-	 * @return bool
190
-	 */
191
-	public static function exists($storageId) {
192
-		return !is_null(self::getNumericStorageId($storageId));
193
-	}
194
-
195
-	/**
196
-	 * remove the entry for the storage
197
-	 *
198
-	 * @param string $storageId
199
-	 */
200
-	public static function remove($storageId) {
201
-		$storageId = self::adjustStorageId($storageId);
202
-		$numericId = self::getNumericStorageId($storageId);
203
-
204
-		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
205
-		$query->delete('storages')
206
-			->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
207
-		$query->execute();
208
-
209
-		if (!is_null($numericId)) {
210
-			$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
211
-			$query->delete('filecache')
212
-				->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
213
-			$query->execute();
214
-		}
215
-	}
216
-
217
-	/**
218
-	 * remove the entry for the storage by the mount id
219
-	 *
220
-	 * @param int $mountId
221
-	 */
222
-	public static function cleanByMountId(int $mountId) {
223
-		$db = \OC::$server->getDatabaseConnection();
224
-
225
-		try {
226
-			$db->beginTransaction();
227
-
228
-			$query = $db->getQueryBuilder();
229
-			$query->select('storage_id')
230
-				->from('mounts')
231
-				->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
232
-			$storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
233
-
234
-			$query = $db->getQueryBuilder();
235
-			$query->delete('filecache')
236
-				->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
237
-			$query->executeStatement();
238
-
239
-			$query = $db->getQueryBuilder();
240
-			$query->delete('storages')
241
-				->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
242
-			$query->executeStatement();
243
-
244
-			$query = $db->getQueryBuilder();
245
-			$query->delete('mounts')
246
-				->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
247
-			$query->executeStatement();
248
-
249
-			$db->commit();
250
-		} catch (\Exception $e) {
251
-			$db->rollBack();
252
-			throw $e;
253
-		}
254
-	}
48
+    /** @var StorageGlobal|null */
49
+    private static $globalCache = null;
50
+    private $storageId;
51
+    private $numericId;
52
+
53
+    /**
54
+     * @return StorageGlobal
55
+     */
56
+    public static function getGlobalCache() {
57
+        if (is_null(self::$globalCache)) {
58
+            self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
59
+        }
60
+        return self::$globalCache;
61
+    }
62
+
63
+    /**
64
+     * @param \OC\Files\Storage\Storage|string $storage
65
+     * @param bool $isAvailable
66
+     * @throws \RuntimeException
67
+     */
68
+    public function __construct($storage, $isAvailable = true) {
69
+        if ($storage instanceof IStorage) {
70
+            $this->storageId = $storage->getId();
71
+        } else {
72
+            $this->storageId = $storage;
73
+        }
74
+        $this->storageId = self::adjustStorageId($this->storageId);
75
+
76
+        if ($row = self::getStorageById($this->storageId)) {
77
+            $this->numericId = (int)$row['numeric_id'];
78
+        } else {
79
+            $connection = \OC::$server->getDatabaseConnection();
80
+            $available = $isAvailable ? 1 : 0;
81
+            if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
82
+                $this->numericId = $connection->lastInsertId('*PREFIX*storages');
83
+            } else {
84
+                if ($row = self::getStorageById($this->storageId)) {
85
+                    $this->numericId = (int)$row['numeric_id'];
86
+                } else {
87
+                    throw new \RuntimeException('Storage could neither be inserted nor be selected from the database: ' . $this->storageId);
88
+                }
89
+            }
90
+        }
91
+    }
92
+
93
+    /**
94
+     * @param string $storageId
95
+     * @return array
96
+     */
97
+    public static function getStorageById($storageId) {
98
+        return self::getGlobalCache()->getStorageInfo($storageId);
99
+    }
100
+
101
+    /**
102
+     * Adjusts the storage id to use md5 if too long
103
+     * @param string $storageId storage id
104
+     * @return string unchanged $storageId if its length is less than 64 characters,
105
+     * else returns the md5 of $storageId
106
+     */
107
+    public static function adjustStorageId($storageId) {
108
+        if (strlen($storageId) > 64) {
109
+            return md5($storageId);
110
+        }
111
+        return $storageId;
112
+    }
113
+
114
+    /**
115
+     * Get the numeric id for the storage
116
+     *
117
+     * @return int
118
+     */
119
+    public function getNumericId() {
120
+        return $this->numericId;
121
+    }
122
+
123
+    /**
124
+     * Get the string id for the storage
125
+     *
126
+     * @param int $numericId
127
+     * @return string|null either the storage id string or null if the numeric id is not known
128
+     */
129
+    public static function getStorageId(int $numericId): ?string {
130
+        $storage = self::getGlobalCache()->getStorageInfoByNumericId($numericId);
131
+        return $storage['id'] ?? null;
132
+    }
133
+
134
+    /**
135
+     * Get the numeric of the storage with the provided string id
136
+     *
137
+     * @param $storageId
138
+     * @return int|null either the numeric storage id or null if the storage id is not knwon
139
+     */
140
+    public static function getNumericStorageId($storageId) {
141
+        $storageId = self::adjustStorageId($storageId);
142
+
143
+        if ($row = self::getStorageById($storageId)) {
144
+            return (int)$row['numeric_id'];
145
+        } else {
146
+            return null;
147
+        }
148
+    }
149
+
150
+    /**
151
+     * @return array [ available, last_checked ]
152
+     */
153
+    public function getAvailability() {
154
+        if ($row = self::getStorageById($this->storageId)) {
155
+            return [
156
+                'available' => (int)$row['available'] === 1,
157
+                'last_checked' => $row['last_checked']
158
+            ];
159
+        } else {
160
+            return [
161
+                'available' => true,
162
+                'last_checked' => time(),
163
+            ];
164
+        }
165
+    }
166
+
167
+    /**
168
+     * @param bool $isAvailable
169
+     * @param int $delay amount of seconds to delay reconsidering that storage further
170
+     */
171
+    public function setAvailability($isAvailable, int $delay = 0) {
172
+        $available = $isAvailable ? 1 : 0;
173
+        if (!$isAvailable) {
174
+            \OC::$server->get(LoggerInterface::class)->info('Storage with ' . $this->storageId . ' marked as unavailable', ['app' => 'lib']);
175
+        }
176
+
177
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
178
+        $query->update('storages')
179
+            ->set('available', $query->createNamedParameter($available))
180
+            ->set('last_checked', $query->createNamedParameter(time() + $delay))
181
+            ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
182
+        $query->execute();
183
+    }
184
+
185
+    /**
186
+     * Check if a string storage id is known
187
+     *
188
+     * @param string $storageId
189
+     * @return bool
190
+     */
191
+    public static function exists($storageId) {
192
+        return !is_null(self::getNumericStorageId($storageId));
193
+    }
194
+
195
+    /**
196
+     * remove the entry for the storage
197
+     *
198
+     * @param string $storageId
199
+     */
200
+    public static function remove($storageId) {
201
+        $storageId = self::adjustStorageId($storageId);
202
+        $numericId = self::getNumericStorageId($storageId);
203
+
204
+        $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
205
+        $query->delete('storages')
206
+            ->where($query->expr()->eq('id', $query->createNamedParameter($storageId)));
207
+        $query->execute();
208
+
209
+        if (!is_null($numericId)) {
210
+            $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
211
+            $query->delete('filecache')
212
+                ->where($query->expr()->eq('storage', $query->createNamedParameter($numericId)));
213
+            $query->execute();
214
+        }
215
+    }
216
+
217
+    /**
218
+     * remove the entry for the storage by the mount id
219
+     *
220
+     * @param int $mountId
221
+     */
222
+    public static function cleanByMountId(int $mountId) {
223
+        $db = \OC::$server->getDatabaseConnection();
224
+
225
+        try {
226
+            $db->beginTransaction();
227
+
228
+            $query = $db->getQueryBuilder();
229
+            $query->select('storage_id')
230
+                ->from('mounts')
231
+                ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
232
+            $storageIds = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
233
+
234
+            $query = $db->getQueryBuilder();
235
+            $query->delete('filecache')
236
+                ->where($query->expr()->in('storage', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
237
+            $query->executeStatement();
238
+
239
+            $query = $db->getQueryBuilder();
240
+            $query->delete('storages')
241
+                ->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)));
242
+            $query->executeStatement();
243
+
244
+            $query = $db->getQueryBuilder();
245
+            $query->delete('mounts')
246
+                ->where($query->expr()->eq('mount_id', $query->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
247
+            $query->executeStatement();
248
+
249
+            $db->commit();
250
+        } catch (\Exception $e) {
251
+            $db->rollBack();
252
+            throw $e;
253
+        }
254
+    }
255 255
 }
Please login to merge, or discard this patch.