阅读前提
阅读本文需要有Caché面向对象建模和SQL的基本知识。
简介
Caché提供工具让用户自己定义存储Schema (Storage) ,而且用户定义的Storage不影响面向对象、SQL访问,也就是说,不需要修改任何代码。
Caché提供3种Storage Schema:Caché storage、Caché SQL storage、 Custom storage。
我们以一个简单的类Person和其继承类Patient为例来讲解Storage:
Class User.Person Extends %Persistent
{
Property Name As %String;
Property Gender As %String;
Property DOB As %Date;
}
Class User.Patient Extends User.Person
{
Property PID As %String;
}
Caché storage
Caché storage是Caché默认的Storage schema。它产生一个基于列表(list)的多维数组结构。持久化存储的类编译后会自动生成Caché Storage schema。
查看Storage定义:
有两种方式可以查看Storage定义:通过Studio的Storage>Data nodes 编辑器、Storage XML定义。
通过Data nodes 编辑器
在Studio>Inspector里选中Storage,会看到名为Default的Storage定义:
双击Default,然后点击Data Nodes后面的 ,会弹出Data nodes 编辑器:
看到其中只有一个Node,名为”PersonDefaultData”,双击该Node会显示其具体的定义。可以看到默认情况下,所有的非流类型属性组成一个list被放在ID为下标的节点下:
^User.PersonD(ID)=$LB(%%CLASSNAME,Name,Gender,DOB)
通过Data nodes 编辑器
通过Caché Studio的View>View Storage来查看其Storage定义,例如下面我们查看User.Person的Storage定义:
<Storage name="Default">
<Data name="PersonDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Name</Value>
</Value>
<Value name="3">
<Value>Gender</Value>
</Value>
<Value name="4">
<Value>DOB</Value>
</Value>
</Data>
<DataLocation>^User.PersonD</DataLocation>
<DefaultData>PersonDefaultData</DefaultData>
<ExtentSize>100000</ExtentSize>
<IdLocation>^User.PersonD</IdLocation>
<IndexLocation>^User.PersonI</IndexLocation>
<StreamLocation>^User.PersonS</StreamLocation>
<Type>%Library.CacheStorage</Type>
</Storage>
解读Storage schema定义:
Storage定义是基于XML的,以<Storage>标签开始和结束;
<Data>标签定义非流类型属性数据如何存储;
< DataLocation >标签定义存储数据的Global名称;
<DefaultData>定义用哪个存储方案;
<IdLocation>定义对象ID序列计数器存储的位置;
<IndexLocation>定义存储索引的Global名称;
<StreamLocation>定义存储流类型属性的Global名称;
<Type>定义该Storage是什么类型的。
所以上面名为” PersonDefaultData”的Storage定义是将属性组成列表保存,其顺序是:
$lb(%%CLASSNAME, Name, Gender, DOB)
查看数据存储:
通过[Home] > [Globals] > [View Global Data]查看多维存储的数据,可以看到类似
^User.PersonD(1)=$lb("","Levinson,David Z.","M",36676)
^User.PersonD(2)=$lb("~Patient~","Umansky,Kyra T.","M",33468)
^User.PersonD(2,"Patient")=$lb("C7108")
修改Caché storage定义:
有两种方式可以修改默认的Storage定义:通过Studio的Storage>Data nodes 编辑器、修改Storage XML定义。
假设期望Person的Name属性放在”Name”第二级维度下,而其它属性组成$LB(%%CLASSNAME, Gender, DOB)放在ID第一级维度下,如:
^User.PersonD(1)=$lb("","M",32940)
^User.PersonD(1,"Name")="Gallant,Rhonda B."
通过Data nodes 编辑器
在Studio>Inspector里选中Storage,找到名为Default的Storage定义,删除原PersonDefaultData节点(Node)。
然后新建NameNode,将Additional subscripts设置为”Name”,并选中”One Property”为Name:
再建立OtherNode,并选中”Multiple Properties”,按顺序选中%%CLASSNAME, Gender, DOB:
保存并编译,这时Storage XML数据更新为:
<Storage name="Default">
<Data name="NameNode">
<Attribute>Name</Attribute>
<Structure>node</Structure>
<Subscript>"Name"</Subscript>
</Data>
<Data name="OtherNode">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Gender</Value>
</Value>
<Value name="3">
<Value>DOB</Value>
</Value>
</Data>
<DataLocation>^User.PersonD</DataLocation>
<DefaultData>PersonDefaultData</DefaultData>
<ExtentSize>100000</ExtentSize>
<IdLocation>^User.PersonD</IdLocation>
<IndexLocation>^User.PersonI</IndexLocation>
<StreamLocation>^User.PersonS</StreamLocation>
<Type>%Library.CacheStorage</Type>
</Storage>
产生测试数据,并查看Global,将看到类似于如下的结果:
^User.PersonD(1)=$lb("","V600",32940)
^User.PersonD(1,"Name")="Gallant,Rhonda B."