2014-02-06 17:25:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dataset class providing the results of a select operation on a kolab_storage_folder.
|
|
|
|
*
|
|
|
|
* Can be used as a normal array as well as an iterator in foreach() loops.
|
|
|
|
*
|
|
|
|
* @version @package_version@
|
|
|
|
* @author Thomas Bruederli <bruederli@kolabsys.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-07 09:29:11 +01:00
|
|
|
class kolab_storage_dataset implements Iterator, ArrayAccess, Countable
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
2024-01-24 11:24:41 +01:00
|
|
|
public const CHUNK_SIZE = 25;
|
2022-11-25 14:24:28 +01:00
|
|
|
|
2014-02-06 17:25:16 +01:00
|
|
|
private $cache; // kolab_storage_cache instance to use for fetching data
|
|
|
|
private $memlimit = 0;
|
|
|
|
private $buffer = false;
|
2022-11-25 14:24:28 +01:00
|
|
|
private $index = [];
|
|
|
|
private $data = [];
|
2014-02-07 09:30:08 +01:00
|
|
|
private $iteratorkey = 0;
|
2014-02-10 08:51:51 +01:00
|
|
|
private $error = null;
|
2022-11-25 14:24:28 +01:00
|
|
|
private $chunk = [];
|
2014-02-06 17:25:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*
|
|
|
|
* @param object kolab_storage_cache instance to be used for fetching objects upon access
|
|
|
|
*/
|
|
|
|
public function __construct($cache)
|
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
|
|
|
|
// enable in-memory buffering up until 1/5 of the available memory
|
|
|
|
if (function_exists('memory_get_usage')) {
|
|
|
|
$this->memlimit = parse_bytes(ini_get('memory_limit')) / 5;
|
|
|
|
$this->buffer = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 08:51:51 +01:00
|
|
|
/**
|
|
|
|
* Return error state
|
|
|
|
*/
|
|
|
|
public function is_error()
|
|
|
|
{
|
|
|
|
return !empty($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set error state
|
|
|
|
*/
|
|
|
|
public function set_error($err)
|
|
|
|
{
|
|
|
|
$this->error = $err;
|
|
|
|
}
|
|
|
|
|
2014-02-06 17:25:16 +01:00
|
|
|
|
2014-02-07 09:29:11 +01:00
|
|
|
/*** Implement PHP Countable interface ***/
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function count(): int
|
2014-02-07 09:29:11 +01:00
|
|
|
{
|
|
|
|
return count($this->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-06 17:25:16 +01:00
|
|
|
/*** Implement PHP ArrayAccess interface ***/
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function offsetSet($offset, $value): void
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
2022-11-25 14:24:28 +01:00
|
|
|
if (is_string($value)) {
|
|
|
|
$uid = $value;
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
2022-11-25 14:24:28 +01:00
|
|
|
$uid = !empty($value['_msguid']) ? $value['_msguid'] : $value['uid'];
|
|
|
|
}
|
2014-02-06 17:25:16 +01:00
|
|
|
|
|
|
|
if (is_null($offset)) {
|
|
|
|
$offset = count($this->index);
|
|
|
|
}
|
|
|
|
|
2022-10-06 15:59:53 +02:00
|
|
|
$this->index[$offset] = $uid;
|
|
|
|
|
2014-02-06 17:25:16 +01:00
|
|
|
// keep full payload data in memory if possible
|
2022-10-06 15:59:53 +02:00
|
|
|
if ($this->memlimit && $this->buffer) {
|
2014-02-06 17:25:16 +01:00
|
|
|
$this->data[$offset] = $value;
|
|
|
|
|
|
|
|
// check memory usage and stop buffering
|
|
|
|
if ($offset % 10 == 0) {
|
|
|
|
$this->buffer = memory_get_usage() < $this->memlimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function offsetExists($offset): bool
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
|
|
|
return isset($this->index[$offset]);
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function offsetUnset($offset): void
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
|
|
|
unset($this->index[$offset]);
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
#[ReturnTypeWillChange]
|
2014-02-06 17:25:16 +01:00
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
2022-11-25 14:24:28 +01:00
|
|
|
if (isset($this->chunk[$offset])) {
|
|
|
|
return $this->chunk[$offset] ?: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The item is a string (object's UID), use multiget method to pre-fetch
|
|
|
|
// multiple objects from the server in one request
|
|
|
|
if (isset($this->data[$offset]) && is_string($this->data[$offset]) && method_exists($this->cache, 'multiget')) {
|
|
|
|
$idx = $offset;
|
|
|
|
$uids = [];
|
|
|
|
|
|
|
|
while (isset($this->index[$idx]) && count($uids) < self::CHUNK_SIZE) {
|
2022-11-28 12:28:04 +01:00
|
|
|
if (isset($this->data[$idx]) && !is_string($this->data[$idx])) {
|
|
|
|
// skip objects that had the raw content in the cache (are not empty)
|
2024-01-24 11:24:41 +01:00
|
|
|
} else {
|
2022-12-09 14:39:46 +01:00
|
|
|
$uids[$idx] = $this->index[$idx];
|
2022-11-28 12:28:04 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 14:24:28 +01:00
|
|
|
$idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($uids)) {
|
|
|
|
$this->chunk = $this->cache->multiget($uids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->chunk[$offset])) {
|
|
|
|
return $this->chunk[$offset] ?: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-06 17:25:16 +01:00
|
|
|
if (isset($this->data[$offset])) {
|
|
|
|
return $this->data[$offset];
|
|
|
|
}
|
2022-10-06 15:59:53 +02:00
|
|
|
|
2023-01-18 14:50:31 +01:00
|
|
|
if ($uid = ($this->index[$offset] ?? null)) {
|
2022-10-06 15:59:53 +02:00
|
|
|
return $this->cache->get($uid);
|
2014-02-06 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*** Implement PHP Iterator interface ***/
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
#[ReturnTypeWillChange]
|
2014-02-06 17:25:16 +01:00
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return $this->offsetGet($this->iteratorkey);
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function key(): int
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
|
|
|
return $this->iteratorkey;
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function next(): void
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
|
|
|
$this->iteratorkey++;
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function rewind(): void
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
|
|
|
$this->iteratorkey = 0;
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:54:43 +01:00
|
|
|
public function valid(): bool
|
2014-02-06 17:25:16 +01:00
|
|
|
{
|
|
|
|
return !empty($this->index[$this->iteratorkey]);
|
|
|
|
}
|
|
|
|
}
|