1
2

More than 5 years have passed since last update.

GroongaのC APIでオブジェクトの中身を調べるのはgrn_inspect/grn_pが便利

Last updated at Posted at 2015-11-09

Groongaの C APIではほとんどのオブジェクトがgrn_objで抽象化されており、何者かよくわかりません。

そんなときは以下のようにしてgrn_inspectを使うのが便利です。


    {
      grn_obj buf;
      GRN_TEXT_INIT(&buf, 0);
      GRN_BULK_REWIND(&buf);
      grn_inspect(ctx, &buf, obj);
      printf("inspect=%.*s", GRN_TEXT_LEN(&buf), GRN_TEXT_VALUE(&buf));
      grn_obj_unlink(ctx, &buf);
    }

これでgrn_objの中身が何者か調べることができます。さらに、lib/util.cの各inspect関数を見れば、そのgrn_objからデータを取り出す方法までわかっちゃいます。

追記

printfするだけなら、以下でも行けるようです。バッファの確保もやってくれて楽チン

  grn_p(ctx, obj);
[#<element record:#<record:pat:Tags id:3 key:"Groonga" index:1 synonym:#<record:pat:Tags id:0(nonexistent)>>, weight:0>]
[#<element record:#<record:pat:Tags id:3 key:"Groonga" index:4 synonym:#<record:pat:Tags id:0(nonexistent)>>, weight:0>, #<element record:#<record:pat:Tags id:2 key:"Rroonga" index:1 synonym:#<record:pat:Tags id:0(nonexistent)>>, weight:0>]
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2