二. 層次數據集的結構 層次數據集可以表示為一個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