三日で書くGroonga関数
- 10. とりあえず版 関数 proc.c
static grn_obj *func_distinct(grn_ctx *ctx, int nargs, grn_obj
**args, grn_user_data *user_data) {
int result = 0;
grn_obj *obj;
if (nargs == 1) {
grn_obj *item_key = args[0];
result = _distinct(GRN_TEXT_VALUE(item_key),
GRN_TEXT_LEN(item_key));
if ((obj = GRN_PROC_ALLOC(GRN_DB_UINT32, 0))) {
GRN_UINT32_SET(ctx, obj, result);
}
}
return obj;
}
- 11. とりあえず版 中身 distinct.cc
#include <string>
#include <set>
typedef std::set<std::string> Map;
static Map _map; // 格納済キーの保存庫
extern "C" int _distinct(const char *name, size_t length) {
std::string _key(name, length); // std::string に変換
Map::iterator it = _map.find(_key); // キーを探す
if (it != _map.end()) return 0; // 格納済なら 0 を返す
_map.insert(key); // 格納する
return 1;
}
- 12. 適当にビルドしてみる
lib/ に distinct.cc を追加して make
よくわからないエラー
../lib/.libs/libgroonga.so: undefined
reference to `__cxa_begin_catch‘
g++ 経由でリンクしないと駄目
- 13. 正式なやり方
configure.ac に AC_PROG_CXX を追加
lib/Makefile.am の
libgroonga_la_SOURCES に distinct.cc
を追加
autoreconf で configure を再生成
- 16. デモ
商品テーブル item
ShortText 型 description
3 件
在庫テーブル stock
item 型 i
Time 型 date
Int32 型 price
9 件
- 18. distinct あり
select --table stock --filter
"distinct(i._key)" --output_columns
i._key,price
→ i._key が重複しない