arc / store
无模式JSON存储,具有搜索、分层结构和索引
Requires
- php: >=7
- arc/base: >=3.0.3
- arc/prototype: >=3.0.1
Requires (Dev)
- phpunit/phpunit: 7|8|~9
This package is auto-updated.
Last update: 2024-09-11 21:31:22 UTC
README
arc\store是ARC - 组件库的一部分。
ARC是Ariadne Web应用平台和内容管理系统http://www.ariadne-cms.org/的衍生品。
安装
您可以使用composer安装ARC组件的全套
composer require arc/arc
或者,您可以从arc/arc开始一个新的项目,如下所示
composer create-project arc/arc {$path}
或者,直接使用此包
composer require arc/store
用法
PostgreSQL的示例DSN
$dsn = 'pgsql:host=localhost;dbname=arcstore;user=arcstore;password=arcstore';
MySQL的示例DSN
$dsn = 'mysql:host=localhost;dbname=arcstore;user=arcstore;password=arcstore';
$store = \arc\store::connect($dsn); $store->initialize(); if ($store->save(\arc\prototype::create(["foo" => "bar"]), "/foo/")) { $objects = $store->ls('/'); var_dump($objects); }
这将显示一个包含一个对象数组的数组,该对象具有父级'/'、名称'foo'和单个属性'foo' => 'bar'。
ARC\Store是什么?
ARC\Store是Ariadne-CMS中实现的具有结构化对象存储的简化实现。它以树结构形式存储自由形式对象数据,类似于文件系统。它提供单独的查询、保存和删除方法。查询有其自己的格式和解析器。数据使用JSONB数据块存储在PostgreSQL中,这些数据块是完全索引的。
此解决方案为您提供灵活且快速的数据存储,同时保留使用经过验证的技术(如PostgreSQL)的许多优点。尽管此实现没有它,但添加事务(提交/回滚)以获取原子更新(即使是批量操作)也很容易。
由于其树结构,ARC\Store很好地与其他ARC组件(如ARC\Grants和ARC\Config)集成。
方法
\arc\store::connect
(\arc\store\Store) \arc\store::connect( (string) $dsn, (callable) $resultHandler=null)
此方法创建一个新的PSQLStore实例并将其连接到PostgreSQL数据库。您可以选择传递自己的resultHandler函数。PSQLStore类包含两个用于此的预定义静态函数
- \arc\store\ResultHandlers::getDBHandler
- \arc\store\ResultHandlers::getDBGeneratorHandler 这些函数接受1个参数,即数据库连接,并返回一个result handler函数。结果处理程序用编译的SQL查询where子句和参数调用,必须执行此操作并返回结果。
\arc\store::disconnect
(void) \arc\store::disconnect()
从上下文堆栈(\arc\context)中删除最后一个存储连接。
\arc\store::cd
(\arc\store\PSQLStore) \arc\store::cd($path)
返回一个新的存储实例,其默认路径设置为$path。此调用将始终成功,即使$path在对象存储中不存在。它不会更新存储实例在上下文堆栈(\arc\context)中的路径。要执行此操作,您必须将新的存储实例推送到上下文堆栈
\arc\context::push([ 'arcStore' => \arc\store::cd('/foo/') ]);
\arc\store::find
(mixed) \arc\store::find((string) $query, (string) $path)
将查询编译为SQL,用此调用结果处理程序,并返回结果。查询语法为只读,只能读取数据,不能更新或删除数据。您也可以在存储实例上调用此方法,如下所示
$store = \arc\context::cd('/'); $objects = $store->find("foo='bar'");
查询格式支持以下运算符
<
小于>
大于=
等于<=
小于等于>=
大于等于<>
、!=
不等于~=
类似于,支持%
和?
通配符!~
不类似,支持%
和?
通配符?
对象包含键(属性)
您可以使用and
和or
将多个查询部分组合起来。您可以使用括号来分组它们。并且您可以通过在前面加上not
来否定一个部分。字符串必须用单引号括起来。字符串内的单引号应使用\
转义。
您可以查询对象的任何部分,但有一些元数据属性您可以搜索。
nodes.path
匹配对象在树中的完整路径。nodes.parent
匹配对象在树中的父对象的完整路径。nodes.mtime
匹配对象最后更改的日期和时间。nodes.ctime
匹配对象创建的日期和时间。
示例查询
$results = $store->find("nodes.path ~= '/foo/%'"); $results = $store->find("foo.bar>3"); $results = $store->find("foo.bar>3 and foo.bar<6"); $results = $store->find("foo.bar<2 or foo.bar>8"); $results = $store->find("type='order' and ( total<10 or total>1000 )");
\arc\store::parents
(mixed) \arc\store::parents((string) $path)
返回从根开始到直接父对象的所有父对象列表。
\arc\store::ls
(mixed) \arc\store::ls((string) $path)
返回给定路径的直接子对象列表。
\arc\store::get
(object) \arc\store::get((string) $path)
返回具有给定路径的对象,或null。
\arc\store::exists
(bool) \arc\store::exists((string) $path)
如果存在具有给定路径的对象,则返回true。
\arc\store\Store->save
(bool) $store->save( (object) $data, (string) $path = '')
在给定的路径上保存对象数据。成功时返回true,失败时返回false。
\arc\store\Store->delete
(bool) $store->delete((string) $path = '')
删除给定路径的对象及其所有子对象。它永远不会删除根对象。如果不传递参数,它将使用存储实例中设置的当前路径。成功时返回true,失败时返回false。