Last active
November 28, 2020 00:44
-
-
Save divinity76/16e30b2aebe16eb0fbc030129c9afde7 to your computer and use it in GitHub Desktop.
sqlite filesystem tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
function doubleRunProtection() | |
{ | |
static $fp = null; | |
if ($fp !== null) { | |
return; | |
} | |
$fp = fopen(__FILE__, 'rb'); | |
if (! flock($fp, LOCK_EX | LOCK_NB)) { | |
die("failed to clock, probably already running..."); | |
} | |
register_shutdown_function(function () use (&$fp) { | |
flock($fp, LOCK_UN); | |
fclose($fp); | |
}); | |
} | |
doubleRunProtection(); | |
class Sqlite_Filesystem | |
{ | |
/** @var \PDO $db */ | |
public $db; | |
private $db_path; | |
private function initPDO(): void | |
{ | |
if ($this->db !== null) { | |
return; | |
} | |
if (empty($this->db_path)) { | |
throw new LogicException("db_path should always be set before this call.."); | |
} | |
$this->db = new \PDO('sqlite:' . $this->db_path, '', '', array( | |
\PDO::ATTR_EMULATE_PREPARES => false, | |
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, | |
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC | |
)); | |
} | |
private function create(string $db_path) | |
{ | |
$sql = 'CREATE TABLE `fs` ( | |
`id` INTEGER PRIMARY KEY /*AUTOINCREMENT*/, | |
`path` TEXT NOT NULL UNIQUE, | |
`content` BLOB | |
);'; | |
$this->initPDO(); | |
$this->db->exec($sql); | |
} | |
function __construct(string $db_path, bool $create_if_missing) | |
{ | |
$this->db_path = $db_path; | |
if (! is_file($db_path)) { | |
if (! $create_if_missing) { | |
throw new \RuntimeException("db path does not exist/is not readable"); | |
} else { | |
self::create($db_path); | |
} | |
} | |
$this->initPDO(); | |
} | |
private function OptimizedLoopExample() | |
{ | |
$db = &$this->db; | |
$files = shell_exec('find /cloudtrail/_original_471361836846/CloudTrail -type f -print0'); | |
$files = rtrim($files, "\x00"); | |
$files = explode("\x00", $files); | |
$stm = $db->prepare("INSERT INTO `fs` (`path`,`content`) VALUES(:path,:content);"); | |
$path = $content = null; | |
$stm->bindParam(':path', $path); | |
$stm->bindParam(':content', $content); | |
$db->beginTransaction(); | |
foreach ($files as $index => $file) { | |
$content = file_get_contents($file); | |
$pretty = substr($file, strlen('/cloudtrail/_original_471361836846/CloudTrail')); | |
$path = $pretty; | |
$stm->execute(); | |
if (($index % 10000) === 0) { | |
echo "."; | |
$db->commit(); | |
$db->beginTransaction(); | |
} | |
} | |
$db->commit(); | |
} | |
} | |
$fs = new Sqlite_Filesystem('sqlite_fs.db3', true); | |
$db = $fs->db; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment