commitThreshold et mode wal par défaut
This commit is contained in:
parent
2fa83097ba
commit
bf4c39bd94
|
@ -7,13 +7,16 @@ namespace nur\sery\db;
|
||||||
class CapacitorChannel {
|
class CapacitorChannel {
|
||||||
const NAME = null;
|
const NAME = null;
|
||||||
|
|
||||||
|
const EACH_COMMIT_THRESHOLD = 100;
|
||||||
|
|
||||||
static function verifix_name(?string $name): string {
|
static function verifix_name(?string $name): string {
|
||||||
if ($name === null) $name = "default";
|
if ($name === null) $name = "default";
|
||||||
return strtolower($name);
|
return strtolower($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct(?string $name=null) {
|
function __construct(?string $name=null, ?int $eachCommitThreshold=null) {
|
||||||
$this->name = self::verifix_name($name ?? static::NAME);
|
$this->name = self::verifix_name($name ?? static::NAME);
|
||||||
|
$this->eachCommitThreshold = $eachCommitThreshold ?? static::EACH_COMMIT_THRESHOLD;
|
||||||
$this->created = false;
|
$this->created = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +27,17 @@ class CapacitorChannel {
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ?int nombre maximum de modifications dans une transaction avant un
|
||||||
|
* commit automatique dans {@link Capacitor::each()}. Utiliser null pour
|
||||||
|
* désactiver la fonctionnalité.
|
||||||
|
*/
|
||||||
|
protected $eachCommitThreshold;
|
||||||
|
|
||||||
|
function getEachCommitThreshold(): ?int {
|
||||||
|
return $this->eachCommitThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
function getTableName(): string {
|
function getTableName(): string {
|
||||||
return $this->name."_channel";
|
return $this->name."_channel";
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ class Sqlite {
|
||||||
"file" => $sqlite->file,
|
"file" => $sqlite->file,
|
||||||
"flags" => $sqlite->flags,
|
"flags" => $sqlite->flags,
|
||||||
"encryption_key" => $sqlite->encryptionKey,
|
"encryption_key" => $sqlite->encryptionKey,
|
||||||
|
"allow_wal" => $sqlite->allowWal,
|
||||||
"config" => $sqlite->config,
|
"config" => $sqlite->config,
|
||||||
"migrate" => $sqlite->migration,
|
"migrate" => $sqlite->migration,
|
||||||
], $params));
|
], $params));
|
||||||
|
@ -30,19 +31,30 @@ class Sqlite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static function config_enableExceptions(self $sqlite) {
|
static function config_enableExceptions(self $sqlite): void {
|
||||||
$sqlite->db->enableExceptions(true);
|
$sqlite->db->enableExceptions(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function config_enableWalIfAllowed(self $sqlite): void {
|
||||||
|
if ($sqlite->isWalAllowed()) {
|
||||||
|
$sqlite->db->exec("PRAGMA journal_mode=WAL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ALLOW_WAL = null;
|
||||||
|
|
||||||
const CONFIG = [
|
const CONFIG = [
|
||||||
[self::class, "config_enableExceptions"],
|
[self::class, "config_enableExceptions"],
|
||||||
|
[self::class, "config_enableWalIfAllowed"],
|
||||||
];
|
];
|
||||||
|
|
||||||
const MIGRATE = null;
|
const MIGRATE = null;
|
||||||
|
|
||||||
const SCHEMA = [
|
const SCHEMA = [
|
||||||
"file" => ["string", ""],
|
"file" => ["string", ""],
|
||||||
"flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE],
|
"flags" => ["int", SQLITE3_OPEN_READWRITE + SQLITE3_OPEN_CREATE],
|
||||||
"encryption_key" => ["string", ""],
|
"encryption_key" => ["string", ""],
|
||||||
|
"allow_wal" => ["?bool"],
|
||||||
"config" => ["?array|callable"],
|
"config" => ["?array|callable"],
|
||||||
"migrate" => ["?array|string|callable"],
|
"migrate" => ["?array|string|callable"],
|
||||||
"auto_open" => ["bool", true],
|
"auto_open" => ["bool", true],
|
||||||
|
@ -51,21 +63,25 @@ class Sqlite {
|
||||||
function __construct(?string $file=null, ?array $params=null) {
|
function __construct(?string $file=null, ?array $params=null) {
|
||||||
if ($file !== null) $params["file"] = $file;
|
if ($file !== null) $params["file"] = $file;
|
||||||
##schéma
|
##schéma
|
||||||
$default_file = self::SCHEMA["file"][1];
|
$defaultFile = self::SCHEMA["file"][1];
|
||||||
$this->file = strval($params["file"] ?? $default_file);
|
$this->file = $file = strval($params["file"] ?? $defaultFile);
|
||||||
|
$inMemory = $file === ":memory:";
|
||||||
#
|
#
|
||||||
$default_flags = self::SCHEMA["flags"][1];
|
$defaultFlags = self::SCHEMA["flags"][1];
|
||||||
$this->flags = intval($params["flags"] ?? $default_flags);
|
$this->flags = intval($params["flags"] ?? $defaultFlags);
|
||||||
#
|
#
|
||||||
$default_encryptionKey = self::SCHEMA["encryption_key"][1];
|
$defaultEncryptionKey = self::SCHEMA["encryption_key"][1];
|
||||||
$this->encryptionKey = strval($params["encryption_key"] ?? $default_encryptionKey);
|
$this->encryptionKey = strval($params["encryption_key"] ?? $defaultEncryptionKey);
|
||||||
|
#
|
||||||
|
$defaultAllowWal = static::ALLOW_WAL ?? !$inMemory;
|
||||||
|
$this->allowWal = $params["allow_wal"] ?? $defaultAllowWal;
|
||||||
# configuration
|
# configuration
|
||||||
$this->config = $params["config"] ?? static::CONFIG;
|
$this->config = $params["config"] ?? static::CONFIG;
|
||||||
# migrations
|
# migrations
|
||||||
$this->migration = $params["migrate"] ?? static::MIGRATE;
|
$this->migration = $params["migrate"] ?? static::MIGRATE;
|
||||||
#
|
#
|
||||||
$default_autoOpen = self::SCHEMA["auto_open"][1];
|
$defaultAutoOpen = self::SCHEMA["auto_open"][1];
|
||||||
if ($params["auto_open"] ?? $default_autoOpen) {
|
if ($params["auto_open"] ?? $defaultAutoOpen) {
|
||||||
$this->open();
|
$this->open();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,6 +95,14 @@ class Sqlite {
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $encryptionKey;
|
protected $encryptionKey;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $allowWal;
|
||||||
|
|
||||||
|
/** vérifier s'il est autorisé de configurer le mode WAL */
|
||||||
|
function isWalAllowed(): bool {
|
||||||
|
return $this->allowWal;
|
||||||
|
}
|
||||||
|
|
||||||
/** @var array|string|callable */
|
/** @var array|string|callable */
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
|
|
|
@ -196,6 +196,7 @@ class SqliteCapacitor extends CapacitorStorage {
|
||||||
$commited = false;
|
$commited = false;
|
||||||
$count = 0;
|
$count = 0;
|
||||||
$sqlite->beginTransaction();
|
$sqlite->beginTransaction();
|
||||||
|
$commitThreshold = $channel->getEachCommitThreshold();
|
||||||
try {
|
try {
|
||||||
$rows = $sqlite->all([
|
$rows = $sqlite->all([
|
||||||
"select",
|
"select",
|
||||||
|
@ -216,6 +217,13 @@ class SqliteCapacitor extends CapacitorStorage {
|
||||||
"values" => $updates,
|
"values" => $updates,
|
||||||
"where" => ["_id" => $row["_id"]],
|
"where" => ["_id" => $row["_id"]],
|
||||||
]);
|
]);
|
||||||
|
if ($commitThreshold !== null) {
|
||||||
|
$commitThreshold--;
|
||||||
|
if ($commitThreshold == 0) {
|
||||||
|
$sqlite->commit();
|
||||||
|
$commitThreshold = $channel->getEachCommitThreshold();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$count++;
|
$count++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue