gren - 次世代grepを目指したコマンドラインツール - 気がつけば0.2.3、groonga, rroongaと連携して超高速検索をサポート!!

gren - gren is a next grep tool.

お久しぶりです、gren 0.2.3 をリリースしました。(大分ブログの更新が滞ってしまいました・・)

今回の売りはgroonga, rroongaを利用して超高速検索モードを搭載したことです。自分のマシン内にファイルデータベースを構築し、それを利用して高速な検索を実現しています。

今までのgrenに加え、新たに

  • mkgrendb ... ファイルデータベースの作成
  • grendb ... ファイルデータベースを利用した検索

という二つのコマンドラインツールが追加されています。

grenは今までと同じように使えます。現在位置を基点に検索したい時等はgren、ざっくりと全体から探したい時はgrendb、というように用途に応じて使い分けることが可能です。

インストール

0.2からrroongaを必要とするようになり、Windowsユーザーの方は一工夫必要です。

Windows

# プラットフォームを指定してWindows用のrroongaをインストールする
gem install rroonga -v 1.0.1 --platform x86-mingw32
# その後、grenをいつも通りインストール
gem install gren

Mac OS X(多分LinuxやBSDも大丈夫?)

sudo gem install gren

使い方

1. 適当な場所に、データベース生成用のyamlファイルを作ります。

私は~/grendb/grendb.yamlとかに置いてます。
yamlファイルの横に~/grendb/grendb.db みたいなファイルがいっぱい出来るので、ディレクトリを掘っておいた方がいいかもしれません。

#
# grendb : データベース作成用YAML
# 

# 検索対象となるディレクトリ
directory:
  - ~/Documents/git-manual-jp/
  - ~/Documents/gren
  - ~/Documents/gren-html/
  - ~/Documents/tidtools
  - ~/Documents/MacOS
  - ~/Documents/iPhone
  - ~/Documents/junk
  - /opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1
2. mkgrendbでデータベース生成

mkgrendbコマンドを実行すると、検索対象となるディレクトリ以下を探索し、データベースが生成されます。
ここまでくれば後一歩!!

% mkgrendb ~/grendb/grendb.yaml
input_yaml : /Users/ongaeshi/grendb/grendb.yaml found.
create     : /Users/ongaeshi/grendb/grendb.db created.
open       : /Users/ongaeshi/grendb/grendb.db open.
add_file   : /Users/ongaeshi/Documents/git-manual-jp/COPYING
add_file   : /Users/ongaeshi/Documents/git-manual-jp/Documentation/asciidoc.conf
.
.
.
add_file   : /opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/test/test-view.rb
add_file   : /opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/text/expression.rdoc
add_file   : /opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/text/TUTORIAL.ja.rdoc

input_yaml : /Users/ongaeshi/grendb/grendb.yaml (6.78sec)
output_db  : /Users/ongaeshi/grendb/grendb.db*
files      : 781
updates    : 0
3. grendbで検索

grendbコマンドで検索出来ます。
データベースを利用するので、ファイルを一つ一つ検索するのとは比べ物にならないスピードです。
私のマシンだと、70342個のファイルからある文字列を検索するのに0.25秒位で検索出来ました。

% grendb --db ~/grendb/grendb.db utf-8 # utf-8というキーワードが含まれる行を検索。
open    : /Users/ongaeshi/grendb/grendb.db open.
Found   : 194 records.
/Users/ongaeshi/Documents/gren/lib/common/display_util.rb:1:# -*- coding: utf-8 -*-
.
.
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/text/TUTORIAL.ja.rdoc:1:# -*- coding: utf-8 -*-

dir   : /Users/ongaeshi/Documents/gren (1.02sec)
files : 194 in 194 (1.86MB in 1.86MB)
match : 97 files, 113 hit

% grendb --db ~/grendb/grendb.db utf-8 -f groonga # -fオプションでファイル名による絞り込み
open    : /Users/ongaeshi/grendb/grendb.db open.
Found   : 10 records.
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/ext/groonga/rb-grn-encoding.c:53: * [<tt>"utf-8"</tt>]
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/ext/groonga/rb-grn-encoding.c:99:	       rb_grn_equal_option(object, "utf-8")) {
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/context.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/expression-builder.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/pagination.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/patricia-trie.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/patricia-trie.rb:37:    #   Groonga::Context.default_options = {:encoding => "utf-8"}
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/record.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/schema.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/view-record.rb:1:# -*- coding: utf-8 -*-
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga.rb:1:# -*- coding: utf-8 -*-

dir   : /Users/ongaeshi/Documents/gren (0.04sec)
files : 10 in 10 (120.3KB in 120.3KB)
match : 9 files, 11 hit
% grendb --db ~/grendb/grendb.db utf-8 -f groonga encoding # and検索も出来るよ
open    : /Users/ongaeshi/grendb/grendb.db open.
Found   : 2 records.
/opt/local/lib/ruby/gems/1.8/gems/rroonga-1.0.1/lib/groonga/patricia-trie.rb:37:    #   Groonga::Context.default_options = {:encoding => "utf-8"}

dir   : /Users/ongaeshi/Documents/gren (0.04sec)
files : 2 in 2 (10.48KB in 10.48KB)
match : 1 files, 1 hit

制約

Windowsではmkgrendb, grendbがまだ使えません。
grenは今まで通り使用可能です。

※1 おそらくWindows用のrroongaが上手く動いていないことによる問題ではないかと推測してます
※2 Windows上で、rroonga gemを自作、インストール出来たら上手くいくのでは無いかなー、誰かやってくれたら超嬉しいです

groonga, rroonga って何?

[http
//groonga.org/:title=groonga - an open-source fulltext search engine and column store.]:オープンソース、国産、高性能な全文検索エンジンです。MySQL等のストレージを必要としないので、ローカル検索に利用することが出来ました。
[http
//groonga.rubyforge.org/:title=Rubyでgroonga使って全文検索 - ラングバ]:groongaをRuby上で使えるようするためのライブラリです。
[http
//rurema.clear-code.com/:title=Rubyリファレンスマニュアル全文検索|るりまサーチ]:groonga, rroongaを利用して実装されています、便利です。

あとがき

デスクトップ検索ってちょっと廃れちゃった感があるけど、あれってネットワークと連携されてインデックスが送られそうな感じとか、どちらかといえばユーザーじゃなくて管理側にフォーカスしちゃったりといった戦略がこけただけで、デスクトップ検索というアイデア自体は良かったと思ってるんですよね。OSXは検索機能自体がOSに内蔵されているのは素晴らしいんだけど、マッチ部分がプレビュー出来ないのが残念・・・。

ってことでgrendbです、自作のソースとか、有名なオープンソースとかローカルに取り込んでがしがし検索してみようと思います。

バグ報告や感想等もらえると嬉しいです。