Debian Codegraph @

plus/xspex-ja.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
6
7 <head>
8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
9 <meta http-equiv="Content-Style-Type" content="text/css" />
10 <meta name="author" content="Mikio Hirabayashi" />
11 <meta name="keywords" content="QDBM, C++, Depot, Curia, Villa" />
12 <meta name="description" content="specifications of C++ API of QDBM" />
13 <link rel="contents" href="./" />
14 <link rel="alternate" href="xspex.html" hreflang="en" title="the English version" />
15 <link rev="made" href="mailto:[email protected]" />
16 <title>Specifications of QDBM for C++ (Japanese)</title>
17 <style type="text/css">html { margin: 0em 0em; padding: 0em 0em; background: #eeeeee none; }
18 body { margin: 2em 2em; padding: 0em 0em;
19 background: #eeeeee none; color: #111111;
20 font-style: normal; font-weight: normal; }
21 h1 { margin-top: 1.8em; margin-bottom: 1.3em; font-weight: bold; }
22 h2 { margin-top: 1.8em; margin-bottom: 1.1em; font-weight: bold;
23 border-left: solid 0.6em #445555; border-bottom: solid 1pt #bbbbbb;
24 padding: 0.5em 0.5em; width: 60%; }
25 h3 { margin-top: 1.8em; margin-bottom: 0.8em; font-weight: bold; }
26 hr { margin-top: 2.5em; margin-bottom: 1.5em; height: 1pt;
27 color: #999999; background-color: #999999; border: none; }
28 div.note,div.navi { text-align: right; }
29 div.logo { text-align: center; margin: 3em 0em; }
30 div.logo img { border: inset 2pt #ccccdd; }
31 p { margin: 0.8em 0em; line-height: 140%; }
32 p,dd { text-indent: 0.8em; }
33 div,pre { margin-left: 1.7em; margin-right: 1.7em; }
34 pre { background-color: #ddddee; padding: 0.2em; border: 1pt solid #bbbbcc; font-size: smaller; }
35 kbd { color: #111111; font-style: normal; font-weight: bold; }
36 a { color: #0022aa; text-decoration: none; }
37 a:hover,a:focus { color: #0033ee; text-decoration: underline; }
38 a.head { color: #111111; text-decoration: none; }
39 table { padding: 1pt 2pt 1pt 2pt; border: none; margin-left: 1.7em; border-collapse: collapse; }
40 th { padding: 1pt 4pt 1pt 4pt; border-style: none;
41 text-align: left; vertical-align: bottom; }
42 td { padding: 1pt 4pt 1pt 4pt; border: 1pt solid #333333;
43 text-align: left; vertical-align: top; }
44 ul,ol,dl { line-height: 140%; }
45 dt { margin-left: 1.2em; }
46 dd { margin-left: 2.0em; }
47 ul.lines { list-style-type: none; }
48 @media print {
49 html,body { margin: 0em 0em; background-color: #ffffff; color: #000000; }
50 h1 { padding: 8em 0em 0.5em 0em; text-align: center; }
51 h2 { page-break-before: always; }
52 div.note { text-align: center; }
53 div.navi,div.logo { display: none }
54 hr { display: none; }
55 pre { margin: 0.8em 0.8em; background-color: #ffffff;
56 border: 1pt solid #aaaaaa; font-size: smaller; }
57 a,kbd { color: #000000; text-decoration: none; }
58 h1,h2,h3 { font-family: sans-serif; }
59 p,div,li,dt,dd { font-family: serif; }
60 pre,kbd { font-family: monospace; }
61 dd { font-size: smaller; }
62 }
63 </style>
64 </head>
65
66 <body>
67
68 <h1>QDBM付属C++用API仕様書</h1>
69
70 <div class="note">Copyright (C) 2000-2006 Mikio Hirabayashi</div>
71 <div class="note">Last Update: Thu, 26 Oct 2006 15:00:20 +0900</div>
72 <div class="navi">[<a href="xapidoc/index.html">API</a>] [<a href="xspex.html" hreflang="en">English</a>]</div>
73
74 <hr />
75
76 <h2>目次</h2>
77
78 <ol>
79 <li><a href="#overview">概要</a></li>
80 <li><a href="#installation">インストール</a></li>
81 <li><a href="#examples">サンプルコード</a></li>
82 <li><a href="#bugs">バグ</a></li>
83 </ol>
84
85 <hr />
86
87 <h2><a name="overview" id="overview" class="head">概要</a></h2>
88
89 <p>QDBMにはC++言語用のAPIがある。QDBMの基本APIと拡張APIと上級APIの関数群をC++のクラス機構を用いてカプセル化し、かつスレッドセーフにしたものである。</p>
90
91 <p>基本APIはファイルを用いてハッシュデータベースを実現する。クラス `Depot' のコンストラクタによってデータベースファイルが開かれる。データベースを閉じるにはメンバ関数 `close' を呼ぶ。明示的にデータベースを閉じないでインスタンスが破棄される場合は、デストラクタによってデータベースが閉じられる。メンバ関数 `put' はレコードを追加するために用いる。メンバ関数 `out' はレコードを削除するために用いる。メンバ関数 `get' はレコードを検索するために用いる。その他にも、C言語の基本APIとほぼ同じ操作を利用することができる。各メンバ関数はエラー時にクラス `Depot_error' のインスタンスを投げる。</p>
92
93 <p>拡張APIはディレクトリと複数のファイルを用いてハッシュデータベースを実現する。クラス `Curia' のコンストラクタによってデータベースディレクトリが開かれる。データベースを閉じるにはメンバ関数 `close' を呼ぶ。明示的にデータベースを閉じないでインスタンスが破棄される場合は、デストラクタによってデータベースが閉じられる。メンバ関数 `put' はレコードを追加するために用いる。メンバ関数 `out' はレコードを削除するために用いる。メンバ関数 `get' はレコードを検索するために用いる。ラージオブジェクトを扱うこともできる。その他にも、C言語の拡張APIとほぼ同じ操作を利用することができる。各メンバ関数はエラー時にクラス `Curia_error' のインスタンスを投げる。</p>
94
95 <p>上級APIはファイルを用いてB+木データベースを実現する。クラス `Villa' のコンストラクタによってデータベースファイルが開かれる。データベースを閉じるにはメンバ関数 `close' を呼ぶ。明示的にデータベースを閉じないでインスタンスが破棄される場合は、デストラクタによってデータベースが閉じられる。メンバ関数 `put' はレコードを追加するために用いる。メンバ関数 `out' はレコードを削除するために用いる。メンバ関数 `get' はレコードを検索するために用いる。その他にも、C言語の上級APIとほぼ同じ操作を利用することができる。各メンバ関数はエラー時にクラス `Villa_error' のインスタンスを投げる。</p>
96
97 <p>`Depot' と `Curia' と `Villa' はクラス `ADBM' の派生クラスである。このクラスはUNIX標準のDBMと同様の機能を持つデータベースマネージャを抽象化したインタフェースであり、純粋仮想関数の宣言のみを行う。各メンバ関数はエラー時にクラス `DBM_error' のインスタンスを投げる。このフレームワークでは、レコードのキーや値はクラス `Datum' として表現される。`Datum' のインスタンスはデータ領域のポインタとそのサイズを持つ。四つのAPIから適切なものを選択する際には、実行効率を重視するなら `Depot' を、スケーラビリティを重視するなら `Curia' を、順序に基づく参照が必要なら `Villa' を、エレガンスと保守性を重視するなら `ADBM' を選ぶべきであろう。データベースファイルは各APIの間で互換性がない。</p>
98
99 <p>アプリケーションのソースファイルにおいて、`Depot' を用いるには `xdepot.h' を、`Curia' を用いるには `xcuria.h' を、`Villa' を用いるには `xvilla.h' を、`ADBM' を用いるには `xadbm.h' をインクルードする必要がある。各クラスは名前空間 `qdbm' にパッケージされている。</p>
100
101 <p>C言語のAPIは、スレッド間でデータベースハンドルを共有しない限りはスレッドセーフである。C++のAPIでは、複数のスレッドが同じハンドルにアクセスしてもスレッドセーフである。このAPIはPOSIXスレッドパッケージを前提として実装されている。別のスレッドパッケージを用いる場合は、独自のC++用ラッパを書くべきである。</p>
102
103 <p>`put' で既存のレコードの上書きがキャンセルされた際や `get' で存在しないレコードが検索された際には例外によって操作の失敗が通知されるが、それが鬱陶しい場合は `silent' フラグを真にするとよい。その場合は失敗が戻り値によって通知される。</p>
104
105 <p>APIの詳細に関しては、サブディレクトリ `<a href="xapidoc/">xapidoc</a>' の文書と各ヘッダファイルを参照すること。</p>
106
107 <hr />
108
109 <h2><a name="installation" id="installation" class="head">インストール</a></h2>
110
111 <h3>準備</h3>
112
113 <p>GCCの3.3以降のバージョンがインストールされ、QDBMが `/usr/local' 以下にインストールされていることが必要である。</p>
114
115 <p>インストール作業は、サブディレクトリ `plus' をカレントディレクトリにして行う。</p>
116
117 <pre>cd plus
118 </pre>
119
120 <h3>普通の手順</h3>
121
122 <p>ビルド環境を設定する。</p>
123
124 <pre>./configure
125 </pre>
126
127 <p>プログラムをビルドする。</p>
128
129 <pre>make
130 </pre>
131
132 <p>プログラムの自己診断テストを行う。</p>
133
134 <pre>make check
135 </pre>
136
137 <p>プログラムをインストールする。作業は `root' ユーザで行う。</p>
138
139 <pre>make install
140 </pre>
141
142 <p>一連の作業が終ると、ヘッダファイル `xdepot.h' と `xcuria.h' と `xvilla.h' と `xadbm.h' が `/usr/local/include' に、ライブラリ `libxqdbm.so' 等が `/usr/local/lib' に、コマンド `xdptest' と `xcrtest' と `xvltest' が `/usr/local/bin' にインストールされる。</p>
143
144 <p>アンインストールするには、`./configure' をした後の状態で以下のコマンドを実行する。作業は `root' ユーザで行う。</p>
145
146 <pre>make uninstall
147 </pre>
148
149 <h3>Windowsの場合</h3>
150
151 <p>Windows(Cygwin)にインストールする場合、以下の手順に従う。</p>
152
153 <p>ビルド環境を設定する。</p>
154
155 <pre>./configure
156 </pre>
157
158 <p>プログラムをビルドする。</p>
159
160 <pre>make win
161 </pre>
162
163 <p>プログラムの自己診断テストを行う。</p>
164
165 <pre>make check-win
166 </pre>
167
168 <p>プログラムをインストールする。なお、アンインストールする場合は `make uninstall-win' とする。</p>
169
170 <pre>make install-win
171 </pre>
172
173 <p>Windowsでは、静的ライブラリ `libxqdbm.a' の代わりにインポートライブラリ `libxqdbm.dll.a' が生成され、さらにダイナミックリンクライブラリ `xqdbm.dll' が生成される。`xqdbm.dll' は `/usr/local/bin' にインストールされる。</p>
174
175 <p>C++用のAPIではMinGWを用いたビルドはサポートされない。</p>
176
177 <h3>Mac OS Xの場合</h3>
178
179 <p>Mac OS X(Darwin)にインストールする場合、以下の手順に従う。</p>
180
181 <p>ビルド環境を設定する。</p>
182
183 <pre>./configure
184 </pre>
185
186 <p>プログラムをビルドする。</p>
187
188 <pre>make mac
189 </pre>
190
191 <p>プログラムの自己診断テストを行う。</p>
192
193 <pre>make check-mac
194 </pre>
195
196 <p>プログラムをインストールする。なお、アンインストールする場合は `make uninstall-mac' とする。</p>
197
198 <pre>make install-mac
199 </pre>
200
201 <p>Mac OS Xでは、`libxqdbm.so' 等の代わりに `libxqdbm.dylib' 等が生成される。</p>
202
203 <h3>HP-UXの場合</h3>
204
205 <p>HP-UXにインストールする場合、以下の手順に従う。</p>
206
207 <p>ビルド環境を設定する。</p>
208
209 <pre>./configure
210 </pre>
211
212 <p>プログラムをビルドする。</p>
213
214 <pre>make hpux
215 </pre>
216
217 <p>プログラムの自己診断テストを行う。</p>
218
219 <pre>make check-hpux
220 </pre>
221
222 <p>プログラムをインストールする。なお、アンインストールする場合は `make uninstall-hpux' とする。</p>
223
224 <pre>make install-hpux
225 </pre>
226
227 <p>HP-UXでは、`libxqdbm.so' 等の代わりに `libxqdbm.sl' が生成される。</p>
228
229 <hr />
230
231 <h2><a name="examples" id="examples" class="head">サンプルコード</a></h2>
232
233 <p>名前と対応させて電話番号を格納し、それを検索するアプリケーションのサンプルコードを以下に示す。</p>
234
235 <pre>#include &lt;xdepot.h&gt;
236 #include &lt;cstdlib&gt;
237 #include &lt;iostream&gt;
238
239 using namespace std;
240 using namespace qdbm;
241
242 const char* NAME = "mikio";
243 const char* NUMBER = "000-1234-5678";
244 const char* DBNAME = "book";
245
246 int main(int argc, char** argv){
247 try {
248
249 // データベースを開く
250 Depot depot(DBNAME, Depot::OWRITER | Depot::OCREAT);
251
252 // レコードを格納する
253 depot.put(NAME, -1, NUMBER, -1);
254
255 // レコードを取得する
256 char* val = depot.get(NAME, -1);
257 cout &lt;&lt; "Name: " &lt;&lt; NAME &lt;&lt; endl;
258 cout &lt;&lt; "Number: " &lt;&lt; val &lt;&lt; endl;
259 free(val);
260
261 // データベースを閉じる
262 depot.close();
263
264 } catch(Depot_error&amp; e){
265 cerr &lt;&lt; e &lt;&lt; endl;
266 return 1;
267 }
268 return 0;
269 }
270 </pre>
271
272 <p>上記の例を `ADBM' クラスを用いて書き直した例を以下に示す。</p>
273
274 <pre>#include &lt;xadbm.h&gt;
275 #include &lt;xdepot.h&gt;
276 #include &lt;iostream&gt;
277
278 using namespace std;
279 using namespace qdbm;
280
281 const char* NAME = "mikio";
282 const char* NUMBER = "000-1234-5678";
283 const char* DBNAME = "book";
284
285 int main(int argc, char** argv){
286 try {
287
288 // データベースを開く
289 Depot depot(DBNAME, Depot::OWRITER | Depot::OCREAT);
290 ADBM&amp; dbm = depot;
291
292 // レコードを準備する
293 Datum key(NAME);
294 Datum val(NUMBER);
295
296 // レコードを格納する
297 dbm.storerec(key, val);
298
299 // レコードを取得する
300 const Datum&amp; res = dbm.fetchrec(key);
301 cout &lt;&lt; "Name: " &lt;&lt; NAME &lt;&lt; endl;
302 cout &lt;&lt; "Number: " &lt;&lt; res &lt;&lt; endl;
303
304 // データベースを閉じる
305 dbm.close();
306
307 } catch(DBM_error&amp; e){
308 cerr &lt;&lt; e &lt;&lt; endl;
309 return 1;
310 }
311 return 0;
312 }
313 </pre>
314
315 <p>`Villa' クラスを用いて文字列の前方一致検索を行う例を以下に示す。</p>
316
317 <pre>#include &lt;xvilla.h&gt;
318 #include &lt;cstdlib&gt;
319 #include &lt;cstring&gt;
320 #include &lt;iostream&gt;
321
322 using namespace std;
323 using namespace qdbm;
324
325 const char* DBNAME = "words";
326 const char* PREFIX = "apple";
327
328 int main(int argc, char** argv){
329 try {
330
331 // データベースを開く
332 Villa villa(DBNAME, Villa::OWRITER | Villa::OCREAT);
333
334 // レコードを格納する
335 villa.put("applet", -1, "little application", -1, Villa::DDUP);
336 villa.put("aurora", -1, "polar wonderwork", -1, Villa::DDUP);
337 villa.put("apple", -1, "delicious fruit", -1, Villa::DDUP);
338 villa.put("amigo", -1, "good friend", -1, Villa::DDUP);
339 villa.put("apple", -1, "big city", -1, Villa::DDUP);
340
341 try {
342
343 // カーソルを候補の先頭に置く
344 villa.curjump(PREFIX, -1);
345
346 // カーソルを走査する
347 for(;;){
348 char* key = villa.curkey();
349 if(strstr(key, PREFIX) != key){
350 free(key);
351 break;
352 }
353 char* val = villa.curval();
354 cout &lt;&lt; key &lt;&lt; ": " &lt;&lt; val &lt;&lt; endl;
355 free(key);
356 free(val);
357 villa.curnext();
358 }
359
360 } catch(Villa_error&amp; e){
361 if(e.code() != Villa::ENOITEM) throw e;
362 }
363
364 // データベースを閉じる
365 villa.close();
366
367 } catch(Villa_error&amp; e){
368 cerr &lt;&lt; e &lt;&lt; endl;
369 return 1;
370 }
371 return 0;
372 }
373 </pre>
374
375 <p>C++用APIを利用したプログラムをビルドするには、ライブラリ `xqdbm' と `qdbm' をリンク対象に加える必要がある。例えば、`sample.cc' から `sample' を作るには、以下のようにビルドを行う。</p>
376
377 <pre>g++ -I/usr/local/include -o sample sample.cc -L/usr/local/lib -lxqdbm -lqdbm
378 </pre>
379
380 <hr />
381
382 <h2><a name="bugs" id="bugs" class="head">バグ</a></h2>
383
384 <p>ひとつのプロセスで複数のデータベースファイルを同時に利用することは可能であるが、同じデータベースファイルの複数のハンドルを利用してはならない。ひとつのデータベースを複数のスレッドで利用する場合には、メインスレッドで生成したハンドルを他のスレッドに渡せばよい。</p>
385
386 <p>一般的なオブジェクトを直列化してデータベースに格納する仕組みはないので、必要ならば、`Datum' クラスを拡張してその仕組みを作ってほしい。また、`Villa' の比較関数で、直列化したオブジェクトを復元した上で比較するようにすれば、よりオブジェクト指向データベースらしくなるだろう。</p>
387
388 <hr />
389
390 </body>
391
392 </html>
393
394 <!-- END OF FILE -->