[Sedna] collection を使ってみる

Sedna は高速な XMLDBなのだそうで,インデックスをはったりできてなんだか楽しそうなので,ちょっと勉強してみた.


さわっているうちに,複数の XML ドキュメントにまとめて XQuery をかけるのはどうやってやるんだろうか,と思って FAQ を眺めているとやっぱりあった.

Q. 
I've inserted several documents into my database, each containing XML
data almost of the same structure. I want to run an XQuery query
against all of those documents. How can I do that?

A.
If you have a number of documents that have similar structure (the
structure must not be exactly the same) and you want to query through
the documents, you should store them in a collection. Collections are
optimized for querying through documents.
How to create and use collection see "Managing Collections" section of
the "Sedna Programmer's Guide".

ようするに collection とかいう機能を使いましょう,ということらしい.


実験してみた.

XML を準備

下記を a.xml とする

<a>
  <b/>
  <c/>
</a>

Sedna を起動する

 $ se_gov

DB を作成

ここでは testdb という名前で作る.

 $ se_cdb testdb

DB を起動

 $ se_sm testdb

(基本) Sedna での XQuery の発行方法

XQuery をファイルに書いて

 $ se_term -file ファイル名 DB名

もしくは

 $ se_term -query クエリ DB名

以下では後者で発行する.

collection "col" を作る

 $ se_term -query "CREATE COLLECTION 'col'" testdb

a.xml をたくさんロード

 $ se_term -query "LOAD 'a.xml' 'a1' 'col'" testdb
 $ se_term -query "LOAD 'a.xml' 'a2' 'col'" testdb
 $ se_term -query "LOAD 'a.xml' 'a3' 'col'" testdb
 $ se_term -query "LOAD 'a.xml' 'a4' 'col'" testdb

collection を使って XQuery を発行してみる

for 文が使いたいのだけれども,-query でどう書くか,というか,for 文を
一行でどう書くのか知らないのでファイルに保存して -file でかけることに
する.

(: a.xquery :)
for $i in 
collection('col')//b 
return <x>{$i}</x>
 $ se_term -file a.xquery testdb


結果

<x>
 <b/>
</x>
<x>
 <b/>
</x>
<x>
 <b/>
</x>
<x>
 <b/>
</x>

まとめ

XQuery を書くこと自体始めてだったので,例がしょぼいけど,きっと今後役
に立つはず.