Skip to content

Commit

Permalink
Improved speed of SQLite with SQL transactions.
Browse files Browse the repository at this point in the history
Now all the logs are stored when level is saving or when the server is stopping.
  • Loading branch information
matcracker committed Jun 15, 2019
1 parent f65df07 commit b156964
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 21 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: BedcoreProtect
main: matcracker\BedcoreProtect\Main
version: 0.2.2
version: 0.2.3
api: [3.4.0]

load: POSTWORLD
Expand Down
8 changes: 8 additions & 0 deletions resources/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ CREATE TABLE IF NOT EXISTS "inventories_log"
);
-- # }
-- # }
-- # {transaction
-- # {begin
BEGIN TRANSACTION;
-- # }
-- # {end
END TRANSACTION;
-- # }
-- # }
-- # {add
-- # {entity
-- # :uuid string
Expand Down
3 changes: 3 additions & 0 deletions src/matcracker/BedcoreProtect/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use matcracker\BedcoreProtect\commands\BCPCommand;
use matcracker\BedcoreProtect\listeners\TrackerListener;
use matcracker\BedcoreProtect\matcracker\BedcoreProtect\listeners\PluginListener;
use matcracker\BedcoreProtect\storage\Database;
use matcracker\BedcoreProtect\utils\ConfigParser;
use pocketmine\plugin\PluginBase;
Expand Down Expand Up @@ -79,6 +80,7 @@ public function onEnable(): void
$this->getServer()->getCommandMap()->register("bedcoreprotect", new BCPCommand($this));

$this->getServer()->getPluginManager()->registerEvents(new TrackerListener($this), $this);
$this->getServer()->getPluginManager()->registerEvents(new PluginListener($this), $this);
}

public function getParsedConfig(): ConfigParser
Expand All @@ -100,6 +102,7 @@ public function getDatabase(): Database
public function onDisable(): void
{
if (($this->database !== null)) {
$this->database->getQueries()->endTransaction();
$this->database->close();
}
Inspector::clearCache();
Expand Down
69 changes: 69 additions & 0 deletions src/matcracker/BedcoreProtect/listeners/PluginListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* ___ __ ___ __ __
* / _ )___ ___/ /______ _______ / _ \_______ / /____ ____/ /_
* / _ / -_) _ / __/ _ \/ __/ -_) ___/ __/ _ \/ __/ -_) __/ __/
* /____/\__/\_,_/\__/\___/_/ \__/_/ /_/ \___/\__/\__/\__/\__/
*
* Copyright (C) 2019
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author matcracker
* @link https://www.github.com/matcracker/BedcoreProtect
*
*/

declare(strict_types=1);

namespace matcracker\BedcoreProtect\matcracker\BedcoreProtect\listeners;

use matcracker\BedcoreProtect\Inspector;
use matcracker\BedcoreProtect\Main;
use pocketmine\event\level\LevelSaveEvent;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\event\player\PlayerQuitEvent;

final class PluginListener implements Listener
{
private $plugin;
private $database;

public function __construct(Main $plugin)
{
$this->plugin = $plugin;
$this->database = $plugin->getDatabase();
}

/**
* @param LevelSaveEvent $event
* @priority HIGH
*/
public function onLevelSave(LevelSaveEvent $event): void
{
$this->database->getQueries()->endTransaction();
$this->database->getQueries()->beginTransaction();
}

/**
* @param PlayerJoinEvent $event
* @priority LOWEST
*/
public function onPlayerJoin(PlayerJoinEvent $event): void
{
$this->database->getQueries()->addEntity($event->getPlayer());
}

/**
* @param PlayerQuitEvent $event
* @priority LOWEST
*/
public function onPlayerQuit(PlayerQuitEvent $event): void
{
Inspector::removeInspector($event->getPlayer());
}
}
20 changes: 0 additions & 20 deletions src/matcracker/BedcoreProtect/listeners/TrackerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
use pocketmine\event\player\PlayerBucketEmptyEvent;
use pocketmine\event\player\PlayerBucketEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\inventory\ContainerInventory;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\level\Position;
Expand Down Expand Up @@ -297,22 +295,4 @@ public function trackInventoryTransaction(InventoryTransactionEvent $event): voi
}
}
}

/**
* @param PlayerJoinEvent $event
* @priority LOWEST
*/
public function onPlayerJoin(PlayerJoinEvent $event): void
{
$this->database->getQueries()->addEntity($event->getPlayer());
}

/**
* @param PlayerQuitEvent $event
* @priority LOWEST
*/
public function onPlayerQuit(PlayerQuitEvent $event): void
{
Inspector::removeInspector($event->getPlayer());
}
}
24 changes: 24 additions & 0 deletions src/matcracker/BedcoreProtect/storage/Queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function init(): void
$this->connector->waitAll();
$this->addDefaultEntities();
$this->addDefaultBlocks();

if ($this->configParser->isSQLite()) {
$this->beginTransaction();
}
}

private function addDefaultEntities(): void
Expand All @@ -73,6 +77,16 @@ private function addDefaultBlocks(): void
$this->addBlock(BlockUtils::createAir());
}

/**
* Can be used only with SQLite
*/
public final function beginTransaction(): void
{
if ($this->configParser->isSQLite()) {
$this->connector->executeGeneric(QueriesConst::BEGIN_TRANSACTION);
}
}

public function requestNearLog(Player $inspector, Position $position, int $near): void
{
$this->requestLog(QueriesConst::GET_NEAR_LOG, $inspector, $position, $near);
Expand Down Expand Up @@ -141,6 +155,16 @@ public function purge(int $time, ?callable $onSuccess = null): void
], $onSuccess);
}

/**
* Can be used only with SQLite
*/
public final function endTransaction(): void
{
if ($this->configParser->isSQLite()) {
$this->connector->executeGeneric(QueriesConst::END_TRANSACTION);
}
}

private function addRawLog(string $uuid, Position $position, int $action): void
{
$this->connector->executeInsert(QueriesConst::ADD_HISTORY_LOG, [
Expand Down
2 changes: 2 additions & 0 deletions src/matcracker/BedcoreProtect/storage/QueriesConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ final class QueriesConst
self::INIT_ENTITIES_LOG, self::INIT_SIGNS_LOG,
self::INIT_INVENTORIES_LOG
];
public const BEGIN_TRANSACTION = "bcp.transaction.begin";
public const END_TRANSACTION = "bcp.transaction.end";
public const ADD_ENTITY = "bcp.add.entity";
public const ADD_BLOCK = "bcp.add.block";
public const ADD_HISTORY_LOG = "bcp.add.log.main";
Expand Down

0 comments on commit b156964

Please sign in to comment.