二. 层次数据集的结构 层次数据集可以表示为一个JavaAPI ,XML或者别的格式,而用XML来表示将会更加形象: <AspireDataSet> <!-- A set of key value pairs at the root level --> <key1>val1</key1> <key2>val2</key2> <!-- A set of named loops --> <loop name="loop"> </loop> <loop name="loop2"> </loop> </AspireDataSet>
这是一系列的key/value对.一个给定的key/value可以用在N个独立的loops当中.其实每一个loop 就是一个数据表.loop可以说是table的同义词了.我没有用table这个术语是为了防止人们会不由自主的联想到关系型数据表.已经说过了loop其实上是很多行记录的集合,现在让我们在认真的看loop的结构: <loop name="loopname"> <row> <!-- a set of key value pairs --> <key1>val1</key1> <key2>val2</key2> <!-- a set of named loops --> <loop name="loopname1"> </loop> <!-- a set of named loops --> <loop name="loopname2"> </loop> </row> <row> </row> </loop>
三.Java当中的层次数据的结构 当我把层次数据集以XML的形式展示的时候,你可能会把层次数据集理解为字面上的XML,因此你会先到DOM,接着你甚至会想这样岂不是会占用很大的JVM内存.不必慌张.层次数据集有自己的的Java API二不需要DOM来描述.下面就是一个层次数据集的Java API代码: package com.ai.htmlgen; import com.ai.data.*; /** * RePResents a Hierarchical Data Set. * An hds is a collection of rows. * You can step through the rows using ILoopForwardIterator * You can find out about the columns via IMetaData. * An hds is also a collection loops originated using the current row. */ public interface ihds extends ILoopForwardIterator { /** * Returns the parent if available * Returns null if there is no parent */ public ihds getParent() throws DataException; /** * For the current row return a set of * child loop names. ILoopForwardIteraor determines * what the current row is. * * @see ILoopForwardIterator */ public IIterator getChildNames() throws DataException; /** * Given a child name return the child Java object * represented by ihds again */ public ihds getChild(String childName) throws DataException; /** * returns a column that is similar to SUM, AVG etc of a * set of rows that are children to this row. */ public String getAggregatevalue(String keyname) throws DataException; /** * Returns the column names of this loop or table. * @see IMetaData */ public IMetaData getMetaData() throws DataException; /** * Releases any resources that may be held by this loop of data * or table. */ public void close() throws DataException; }
简单的说来,上面的ihds接口就是一个层次数据集的接口.这个API使你可以递归的访问你的loop结构.这个接口里有一些遍历loop结构是需要的一些选项.它也能假定是前序遍历或者随机遍历.现在我来介绍的是这个API用到的两个附加的接口: ILoopForwardIterator和IMetaData: 如何在IHDS里遍历行记录的接口: ILoopForwardIterator package com.ai.htmlgen; import com.ai.data.*; public interface ILoopForwardIterator { /** * getvalue from the current row matching the key */ public String getvalue(final String key); public void moveToFirst() throws DataException; public void moveToNext() throws DataException; public boolean isAtTheEnd() throws DataException; }
IMetaData: 用于读取列名的接口 package com.ai.data; public interface IMetaData { public IIterator getIterator(); public int getColumnCount(); public int getIndex(final String attributeName) throws FieldNameNotFoundException; }
一个早期的标志性的对Aspire的评论参见:“ For Tomcat Developers, Aspire Comes in a JAR”.配置了初始化一个Aspire就像你定义数据库.调用SQL语句或者存储过程一样. 为你的层次数据集创建定义文件: 一个层次数据集的定义实例: ################################### # ihdsTest data definition: section1 ################################### request.ihdsTest.className=com.ai.htmlgen.DBHashTableFormHandler1 request.ihdsTest.loopNames=works #section2 request.ihdsTest.works.class_request.className=com.ai.htmlgen.GenericTableHandler6 r