simpletools / db-indexedfile
高吞吐量非并发NoSQL基于文件的键值数据库,具有内存支持索引。
1.0.9
2023-11-28 17:04 UTC
Requires
- php: >=7.0.0
README
IndexedFile是一个数据库系统,允许通过利用内存作为索引来非常快速地处理和存储大量数据,同时保持其他所有数据在磁盘上,具有高吞吐量的读写引擎。
为了简化下面的代码片段,假设以下命名空间在use
中
use \Simpletools\Db\IndexedFile;
初始化临时数据库
启动一个新临时数据库,脚本结束时将被删除
$indexedFile = new IndexedFile\File();
从现有数据库初始化
启动可能已经存在或脚本结束时应该持久化的数据库
$indexedFile = new IndexedFile\File('/path/to/my/db.jsonl');
设置自定义IndexStore
你可以编写自己的IndexStore,它实现了IndexStoreInterface
,并通过以下静态方法进行预设
//the default IndexStore IndexedFile\File::indexStoreClass('Simpletools\Db\IndexedFile\IndexStore\ArrayIndexStore');
存储数据
通过键插入/替换数据
$indexedFile->insert('key',["foo"=>"bar"]);
如果不存在则存储数据
如果键已存在则忽略插入
$indexedFile->insertIgnore('key',["foo"=>"bar"]);
读取数据
通过键读取
$value = $indexedFile->read('key');
遍历所有条目
遍历所有条目
foreach($indexedFile->iterate() as $key => $value) { var_dump($key,$value); }
更新插入数据
更新/插入数据
$indexedFile->upsert('key',function($row){ if($row) { $row->counter++; return $row; } else { return [ 'counter' => 0 ]; } });
删除数据
删除键
$indexedFile->remove('key');
截断数据库
删除所有条目
$indexedFile->truncate();
或在启动时
$indexedFile = new IndexedFile\File('/path/to/my/db.jsonl',true);
排序
声明文本排序
$indexedFile->sort('title','ASC', 'string');
声明整数排序
$indexedFile->sort('count','DESC', 'int');
includeSortStats
标志将具有position
在排序中的位置和占总数的百分比(仅整数)的_sort
元字段添加到索引对象中
$indexedFile->sort('count','DESC', 'int', true);
自定输入和输出文件
$indexedFile->sort('title','ASC', 'string',true, __DIR__.'/unsorted.csv', __DIR__.'/sorted.csv');
按价格排序并使用迭代器的使用示例
$indexedFile->sort('price','DESC', 'int'); $indexedFile->insertIgnore($data['id'],[ 'title' => $data['title'], 'price' => $data['price'] ]); $indexedFile->runSort(); foreach ($indexedFile->sortIterate() as $groupId => $row) { echo 'Position: '. $row->_sort->position.' Percent: '.$row->_sort->position.' Index key: '. $groupId .' Price: '. $row->price."\n"; }