milkode 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/Gemfile +17 -4
  2. data/HISTORY.ja.rdoc +27 -0
  3. data/HISTORY.rdoc +27 -0
  4. data/LICENSE.txt +23 -1
  5. data/Rakefile +1 -16
  6. data/VERSION +1 -1
  7. data/bin/gmilk +1 -1
  8. data/bin/milk +1 -1
  9. data/lib/milkode/cdstk/cdstk.rb +171 -168
  10. data/lib/milkode/cdstk/milkode_yaml.rb +1 -1
  11. data/lib/milkode/cdweb/app.rb +11 -3
  12. data/lib/milkode/cdweb/lib/command.rb +43 -0
  13. data/lib/milkode/cdweb/lib/database.rb +58 -210
  14. data/lib/milkode/cdweb/lib/mkurl.rb +10 -1
  15. data/lib/milkode/cdweb/lib/package_list.rb +61 -0
  16. data/lib/milkode/cdweb/lib/search_files.rb +2 -1
  17. data/lib/milkode/cdweb/public/css/bootstrap-responsive.min.css +9 -0
  18. data/lib/milkode/cdweb/public/css/bootstrap.min.css +9 -0
  19. data/lib/milkode/cdweb/public/css/milkode.css +44 -9
  20. data/lib/milkode/cdweb/public/img/glyphicons-halflings-white.png +0 -0
  21. data/lib/milkode/cdweb/public/img/glyphicons-halflings.png +0 -0
  22. data/lib/milkode/cdweb/public/js/bootstrap.min.js +6 -0
  23. data/lib/milkode/cdweb/views/index.haml +52 -16
  24. data/lib/milkode/cdweb/views/packages.haml +11 -0
  25. data/lib/milkode/cli.rb +8 -0
  26. data/lib/milkode/common/array_diff.rb +18 -0
  27. data/lib/milkode/common/util.rb +16 -0
  28. data/lib/milkode/database/document_record.rb +64 -0
  29. data/lib/milkode/database/document_table.rb +341 -0
  30. data/lib/milkode/database/groonga_database.rb +105 -0
  31. data/lib/milkode/database/package_table.rb +98 -0
  32. data/lib/milkode/findgrep/findgrep.rb +20 -124
  33. data/lib/milkode/grep/cli_grep.rb +3 -1
  34. data/milkode.gemspec +32 -2
  35. data/test/data/.gitignore +4 -0
  36. data/test/data/c_project/a.txt +1 -0
  37. data/test/data/c_project/abc.c +2 -0
  38. data/test/data/c_project/abc.h +3 -0
  39. data/test/data/c_project/b.txt +1 -0
  40. data/test/data/c_project/c.txt +1 -0
  41. data/test/data/c_project/cc.txt +1 -0
  42. data/test/data/c_project/to/file.rb +1 -0
  43. data/test/test_cdstk.rb +4 -4
  44. data/test/test_cli.rb +9 -2
  45. data/test/test_database.rb +5 -14
  46. data/test/test_document_record.rb +77 -0
  47. data/test/test_document_table.rb +317 -0
  48. data/test/test_groonga_database.rb +58 -0
  49. data/test/test_mkurl.rb +2 -0
  50. data/test/test_package_list.rb +21 -0
  51. data/test/test_package_table.rb +215 -0
  52. data/test/test_util.rb +8 -0
  53. metadata +57 -5
data/Gemfile CHANGED
@@ -1,11 +1,24 @@
1
1
  source "http://rubygems.org"
2
+
2
3
  # Add dependencies required to use your gem here.
3
4
  # Example:
4
5
  # gem "activesupport", ">= 2.3.5"
5
6
  # gemspec
7
+ gem 'termcolor','>= 1.2.0'
8
+ gem 'rroonga','>= 1.1.0'
9
+ gem 'rack','>=1.3.4'
10
+ gem 'sinatra', '>=1.2.6'
11
+ gem 'launchy', '>=0.3.7'
12
+ gem 'coderay', '>=1.0.5'
13
+ gem 'thin', '>=1.2.10'
14
+ gem 'archive-zip', '>=0.4.0'
15
+ gem 'haml', '>=3.1.2'
16
+ gem 'sass', '>=3.1.3'
17
+ gem 'thor', '~> 0.15.0'
18
+
6
19
  # Add dependencies to develop your gem here.
7
20
  # Include everything needed to run rake, tests, features, etc.
8
- # group :development do
9
- # gem "bundler"
10
- # gem "jeweler"
11
- # end
21
+ group :development do
22
+ gem "bundler"
23
+ gem "jeweler"
24
+ end
@@ -1,3 +1,30 @@
1
+ === 0.8.0 2012/06/20
2
+
3
+ * milk web
4
+ * ホームから 最近使った、追加、更新、お気に入り のパッケージに直接ジャンプ出来るように
5
+ * パッケージ一覧のソート種類を指定可能に
6
+ * Twitter bootstrap に対応
7
+
8
+ * database
9
+ * groongaデータベースのアクセスを一元化
10
+ * DocumentTable
11
+ * shorpath -> restpath
12
+ * Add package column
13
+ * 拡張子の検索は完全一致に
14
+ * PackageTable
15
+ * 新規追加
16
+
17
+ * milk
18
+ * 'milk fav' コマンドを追加。お気に入りを設定可能に
19
+ * milk update 時に cleanup も行うように
20
+ * invoke_task を再定義して help option を処理 (thanks tomykaira)
21
+ * milk add 出来るスキーマを追加
22
+ * ssh:foo@bar/baz.git (Support gitolite, thanks hazy_moon)
23
+ * git@example.com:foo/bar.git
24
+
25
+ * build
26
+ * 依存関係の記述をGemfileに移動
27
+
1
28
  === 0.7.0 2012/05/05
2
29
 
3
30
  * milk
@@ -1,3 +1,30 @@
1
+ === 0.8.0 2012/06/20
2
+
3
+ * milk web
4
+ * Can jump to package from home. [Recent, add, update, favorite]
5
+ * Can specify the package list sorting type
6
+ * Support Twitter bootstrap
7
+
8
+ * database
9
+ * Centralized groonga database access
10
+ * DocumentTable
11
+ * shorpath -> restpath
12
+ * Add package column
13
+ * Extension searches exact matches
14
+ * PackageTable
15
+ * New
16
+
17
+ * milk
18
+ * Add 'milk fav' command
19
+ * Also cleanup do the milk update
20
+ * Redefining invoke_task and handle the help option (thanks tomykaira)
21
+ * Add milk add 'schema'
22
+ * ssh:foo@bar/baz.git (Support gitolite, thanks hazy_moon)
23
+ * git@example.com:foo/bar.git
24
+
25
+ * build
26
+ * Move description of the dependencies to the Gemfile
27
+
1
28
  === 0.7.0 2012/05/05
2
29
 
3
30
  * milk
@@ -1,4 +1,8 @@
1
- Copyright (c) 2011 ongaeshi
1
+ =================
2
+ Milkode
3
+ =================
4
+
5
+ Copyright (c) 2011, 2012 ongaeshi
2
6
 
3
7
  Permission is hereby granted, free of charge, to any person obtaining
4
8
  a copy of this software and associated documentation files (the
@@ -18,3 +22,21 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
22
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
23
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
24
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+ =================
27
+ Twitter Bootstrap
28
+ =================
29
+
30
+ Copyright 2012 Twitter, Inc.
31
+
32
+ Licensed under the Apache License, Version 2.0 (the "License");
33
+ you may not use this file except in compliance with the License.
34
+ You may obtain a copy of the License at
35
+
36
+ http://www.apache.org/licenses/LICENSE-2.0
37
+
38
+ Unless required by applicable law or agreed to in writing, software
39
+ distributed under the License is distributed on an "AS IS" BASIS,
40
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
+ See the License for the specific language governing permissions and
42
+ limitations under the License.
data/Rakefile CHANGED
@@ -20,22 +20,7 @@ Jeweler::Tasks.new do |gem|
20
20
  gem.description = %Q{Line based local source code search engine & grep-command & web-app.}
21
21
  gem.email = "ongaeshi0621@gmail.com"
22
22
  gem.authors = ["ongaeshi"]
23
-
24
- # Include your dependencies below. Runtime dependencies are required when using your gem,
25
- gem.add_runtime_dependency 'termcolor','>= 1.2.0'
26
- gem.add_runtime_dependency 'rroonga','>= 1.1.0'
27
- gem.add_runtime_dependency 'rack','>=1.3.4'
28
- gem.add_runtime_dependency 'sinatra', '>=1.2.6'
29
- gem.add_runtime_dependency 'launchy', '>=0.3.7'
30
- gem.add_runtime_dependency 'coderay', '>=1.0.5'
31
- gem.add_runtime_dependency 'thin', '>=1.2.10'
32
- gem.add_runtime_dependency 'archive-zip', '>=0.4.0'
33
- gem.add_runtime_dependency 'haml', '>=3.1.2'
34
- gem.add_runtime_dependency 'sass', '>=3.1.3'
35
- gem.add_runtime_dependency 'thor', '~> 0.15.0'
36
-
37
- # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
38
- # gem.add_development_dependency 'rspec', '> 1.2.3'
23
+ # Dependencies move to 'Gemfile'
39
24
  end
40
25
  Jeweler::RubygemsDotOrgTasks.new
41
26
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.8.0
data/bin/gmilk CHANGED
@@ -6,5 +6,5 @@
6
6
  require 'rubygems'
7
7
  require 'milkode/grep/cli_grep'
8
8
 
9
- Version = "0.7.0"
9
+ Version = "0.8.0"
10
10
  Milkode::CLI_Grep.execute(STDOUT, ARGV)
data/bin/milk CHANGED
@@ -6,5 +6,5 @@
6
6
  require 'rubygems'
7
7
  require 'milkode/cli'
8
8
 
9
- Version = "0.7.0"
9
+ Version = "0.8.0"
10
10
  Milkode::CLI.start(ARGV)
@@ -3,13 +3,11 @@
3
3
  require 'yaml'
4
4
  require 'pathname'
5
5
  require 'rubygems'
6
- require 'groonga'
7
6
  require 'fileutils'
8
7
  require 'pathname'
9
8
  require 'milkode/common/grenfiletest'
10
9
  require 'milkode/common/util'
11
10
  require 'milkode/common/dir'
12
- require 'milkode/findgrep/findgrep'
13
11
  include Milkode
14
12
  require 'kconv'
15
13
  begin
@@ -17,14 +15,14 @@ begin
17
15
  rescue LoadError
18
16
  $no_readline = true
19
17
  end
20
- require 'milkode/cdweb/lib/database'
21
18
  require 'open-uri'
22
-
23
19
  require 'milkode/cdstk/cdstk_command'
24
-
25
20
  require 'milkode/cdstk/yaml_file_wrapper'
26
21
  require 'milkode/cdstk/package'
27
22
  require 'milkode/common/ignore_checker'
23
+ require 'milkode/database/groonga_database'
24
+ require 'milkode/database/document_record'
25
+ require 'milkode/common/array_diff'
28
26
 
29
27
  module Milkode
30
28
  class IgnoreError < RuntimeError ; end
@@ -40,7 +38,6 @@ module Milkode
40
38
 
41
39
  def initialize(io = $stdout, db_dir = ".")
42
40
  @db_dir = db_dir
43
- Database.setup(@db_dir)
44
41
  @out = io
45
42
  # @out = $stdout # 強制出力
46
43
  @is_display_info = false # alert_info の表示
@@ -60,7 +57,7 @@ module Milkode
60
57
  if Dir.emptydir?(@db_dir)
61
58
  @yaml = YamlFileWrapper.create(@db_dir)
62
59
  @out.puts "create : #{yaml_file}"
63
- db_create(db_file)
60
+ db_create
64
61
  setdb([@db_dir], {}) if (options[:setdb])
65
62
  else
66
63
  @out.puts "Can't create milkode db (Because not empty in #{db_dir_expand})"
@@ -68,24 +65,24 @@ module Milkode
68
65
  end
69
66
 
70
67
  def assert_compatible
71
- db_open(db_file)
68
+ db_open
72
69
  end
73
70
 
74
- def update_all
71
+ def update_all(options)
75
72
  print_result do
76
- db_open(db_file)
73
+ db_open
77
74
 
78
75
  @yaml.contents.each do |package|
79
- update_package_in(package)
76
+ update_package_in(package, options)
80
77
  end
81
78
  end
82
79
  end
83
80
 
84
81
  def update(args, options)
85
82
  update_display_info(options)
86
-
83
+
87
84
  if (options[:all])
88
- update_all
85
+ update_all(options)
89
86
  else
90
87
  if (args.empty?)
91
88
  path = File.expand_path('.')
@@ -93,19 +90,19 @@ module Milkode
93
90
 
94
91
  if (package)
95
92
  print_result do
96
- db_open(db_file)
97
- update_package_in(package)
93
+ db_open
94
+ update_package_in(package, options)
98
95
  end
99
96
  else
100
97
  @out.puts "Not registered. If you want to add, 'milk add #{path}'."
101
98
  end
102
99
  else
103
100
  print_result do
104
- db_open(db_file)
101
+ db_open
105
102
  args.each do |name|
106
103
  package = @yaml.find_name(name)
107
104
  if (package)
108
- update_package_in(package)
105
+ update_package_in(package, options)
109
106
  else
110
107
  @out.puts "Not found package '#{name}'."
111
108
  return
@@ -117,7 +114,7 @@ module Milkode
117
114
  end
118
115
 
119
116
  def update_for_grep(dir)
120
- db_open(db_file)
117
+ db_open
121
118
  update_dir_in(dir)
122
119
  end
123
120
 
@@ -126,7 +123,7 @@ module Milkode
126
123
 
127
124
  print_result do
128
125
  # データベースを開く
129
- db_open(db_file)
126
+ db_open
130
127
 
131
128
  # メイン処理
132
129
  begin
@@ -183,7 +180,7 @@ module Milkode
183
180
 
184
181
  def add_dir(dir, no_yaml = false)
185
182
  add_yaml(Package.create(dir)) unless no_yaml
186
- db_open(db_file)
183
+ db_open
187
184
  update_dir_in(dir)
188
185
  end
189
186
 
@@ -204,6 +201,12 @@ module Milkode
204
201
  # YAML更新
205
202
  @yaml.add(package)
206
203
  @yaml.save
204
+
205
+ # データベースを開く
206
+ db_open
207
+
208
+ # yamlファイルと同期する
209
+ @grndb.yaml_sync(@yaml.contents)
207
210
  end
208
211
 
209
212
  def convert_content(src)
@@ -251,7 +254,7 @@ module Milkode
251
254
  end
252
255
 
253
256
  def git_url?(src)
254
- (src =~ /^git:/) != nil
257
+ Util::git_url?(src)
255
258
  end
256
259
  private :git_url?
257
260
 
@@ -294,7 +297,7 @@ module Milkode
294
297
  list([], {:verbose => true})
295
298
 
296
299
  if yes_or_no("Remove #{@yaml.contents.size} contents? (yes/no)")
297
- db_open(db_file)
300
+ db_open
298
301
 
299
302
  @yaml.contents.each do |package|
300
303
  remove_dir(package.directory)
@@ -317,7 +320,7 @@ module Milkode
317
320
 
318
321
  if (package)
319
322
  print_result do
320
- db_open(db_file)
323
+ db_open
321
324
  remove_dir(package.directory)
322
325
  end
323
326
  else
@@ -325,7 +328,7 @@ module Milkode
325
328
  end
326
329
  else
327
330
  print_result do
328
- db_open(db_file)
331
+ db_open
329
332
  args.each do |name|
330
333
  package = @yaml.find_name(name)
331
334
  if (package)
@@ -359,6 +362,11 @@ module Milkode
359
362
  end
360
363
 
361
364
  def list(args, options)
365
+ if options[:check]
366
+ check_integrity
367
+ return
368
+ end
369
+
362
370
  match_p = @yaml.contents.find_all do |p|
363
371
  args.all? {|k| p.name.include? k }
364
372
  end
@@ -386,6 +394,62 @@ module Milkode
386
394
  end
387
395
  end
388
396
 
397
+ def fav(args, options)
398
+ db_open
399
+
400
+ if (args.empty?)
401
+ @out.puts @grndb.packages.favs.map{|r| r.name}
402
+ else
403
+ is_dirty = false
404
+
405
+ args.each do |v|
406
+ # database
407
+ time = options[:delete] ? Time.at(0) : Time.now
408
+
409
+ if @grndb.packages.touch_if(v, :favtime, time)
410
+ # milkode_yaml
411
+ package = @yaml.find_name(v)
412
+ dst = package.options
413
+ unless options[:delete]
414
+ dst[:fav] = true
415
+ else
416
+ dst.delete(:fav)
417
+ end
418
+ package.set_options(dst)
419
+ @yaml.update(package)
420
+ is_dirty = true
421
+ else
422
+ @out.puts "Not found package '#{v}'"
423
+ end
424
+ end
425
+
426
+ @yaml.save if is_dirty
427
+ end
428
+ end
429
+
430
+ def check_integrity
431
+ db_open
432
+
433
+ @out.puts "Check integrity ..."
434
+
435
+ yaml = @yaml.contents.map {|p| p.name}.sort
436
+ grndb = @grndb.packages.map {|p| p.name}.sort
437
+
438
+ d = yaml.diff grndb
439
+
440
+ unless d[0].empty?
441
+ @out.puts "'milkode.yaml' has #{d[0]}, but 'PackageTable' don't have."
442
+ end
443
+
444
+ unless d[1].empty?
445
+ @out.puts "'PackageTable' has #{d[1]}, but 'milkode.yaml' don't have."
446
+ end
447
+
448
+ if d[0].empty? && d[1].empty?
449
+ @out.puts 'ok'
450
+ end
451
+ end
452
+
389
453
  def pwd(options)
390
454
  dir = options[:default] ? Dbdir.default_dir : db_dir_expand
391
455
 
@@ -403,9 +467,6 @@ module Milkode
403
467
  end
404
468
 
405
469
  def cleanup(options)
406
- # 互換性テスト
407
- db_open(db_file)
408
-
409
470
  # cleanup開始
410
471
  if (options[:force] or yes_or_no("cleanup contents? (yes/no)"))
411
472
  print_result do
@@ -416,9 +477,15 @@ module Milkode
416
477
  @package_count += 1
417
478
  end
418
479
  @yaml.save
480
+
481
+ # データベースを開く
482
+ db_open
483
+
484
+ # yamlファイルと同期する
485
+ @grndb.yaml_sync(@yaml.contents)
419
486
 
420
487
  # データベースのクリーンアップ
421
- Database.instance.cleanup do |record|
488
+ @documents.cleanup do |record|
422
489
  alert("rm_record", record.path)
423
490
  @file_count += 1
424
491
  end
@@ -430,9 +497,10 @@ module Milkode
430
497
  update_display_info(options)
431
498
 
432
499
  if (options[:all])
433
- db_delete(db_file)
434
- db_create(db_file)
435
- update_all
500
+ db_delete
501
+ db_create
502
+ @grndb.yaml_sync(@yaml.contents)
503
+ update_all({})
436
504
  else
437
505
  if (args.empty?)
438
506
  path = File.expand_path('.')
@@ -440,7 +508,7 @@ module Milkode
440
508
 
441
509
  if (package)
442
510
  print_result do
443
- db_open(db_file)
511
+ db_open
444
512
  remove_dir(package.directory, true)
445
513
  add_dir(package.directory, true)
446
514
  end
@@ -452,7 +520,7 @@ module Milkode
452
520
  args.each do |name|
453
521
  package = @yaml.find_name(name)
454
522
  if (package)
455
- db_open(db_file)
523
+ db_open
456
524
  remove_dir(package.directory, true)
457
525
  add_dir(package.directory, true)
458
526
  else
@@ -466,11 +534,11 @@ module Milkode
466
534
  end
467
535
 
468
536
  def dump
469
- db_open(db_file)
470
-
471
- documents = Groonga::Context.default["documents"]
472
- records = documents.select
473
- records.each do |record|
537
+ db_open
538
+
539
+ @documents.each do |grnrcd|
540
+ record = DocumentRecord.new grnrcd
541
+
474
542
  @out.puts record.inspect
475
543
  @out.puts "path : #{record.path}"
476
544
  @out.puts "shortpath : #{record.shortpath}"
@@ -479,7 +547,8 @@ module Milkode
479
547
  @out.puts "content :", record.content ? record.content[0..64] : nil
480
548
  @out.puts
481
549
  end
482
- @out.puts "total #{records.size} record."
550
+
551
+ @out.puts "total #{@documents.size} record."
483
552
  end
484
553
 
485
554
  def dir(args, options)
@@ -586,7 +655,7 @@ EOF
586
655
 
587
656
  if options[:dry_run]
588
657
  # Test mode
589
- db_open(db_file)
658
+ db_open
590
659
  @is_display_info = true
591
660
  @is_silent = true
592
661
  update_dir_in(package.directory)
@@ -634,30 +703,31 @@ EOF
634
703
  Dbdir.expand_groonga_path(@db_dir)
635
704
  end
636
705
 
637
- def db_file_expand
638
- File.expand_path(db_file)
639
- end
640
-
641
706
  def db_dir_expand
642
707
  File.expand_path(@db_dir)
643
708
  end
644
709
 
645
- def custom_db?
646
- db_dir_expand != Dbdir.default_dir
647
- end
648
-
649
710
  def yaml_file
650
711
  YamlFileWrapper.yaml_file @db_dir
651
712
  end
652
713
 
653
- def update_package_in(package)
714
+ def update_package_in(package, options)
654
715
  if package.options[:update_with_git_pull]
655
716
  Dir.chdir(package.directory) { system("git pull") }
656
717
  end
657
718
 
719
+ unless options[:no_clean]
720
+ cleanup_package_in(package)
721
+ end
722
+
658
723
  update_dir_in(package.directory)
659
724
  end
660
725
 
726
+ def cleanup_package_in(package)
727
+ db_open
728
+ @documents.cleanup_package_name(package.name)
729
+ end
730
+
661
731
  def update_dir_in(dir)
662
732
  alert("package", File.basename(dir) )
663
733
  @package_count += 1
@@ -669,24 +739,33 @@ EOF
669
739
  elsif (FileTest.directory? dir)
670
740
  db_add_dir(dir)
671
741
  else
672
- db_add_file(STDOUT, dir, File.basename(dir))
742
+ db_add_file(STDOUT, File.dirname(dir), File.basename(dir), File.basename(dir)) # .bashrc/.bashrc のようになる
673
743
  end
674
744
  end
675
745
 
676
746
  def remove_dir(dir, no_yaml = false)
677
- # yamlから削除
678
747
  unless (no_yaml)
679
- @yaml.remove(@yaml.find_dir(dir))
748
+ package = @yaml.find_dir(dir)
749
+
750
+ # yamlから削除
751
+ @yaml.remove(package)
680
752
  @yaml.save
753
+
754
+ # PackageTableから削除
755
+ db_open
756
+ @grndb.packages.remove package.name
681
757
  end
682
-
758
+
759
+ # データベース開く
760
+ db_open
761
+
683
762
  # データベースからも削除
684
763
  # dir = File.expand_path(dir)
685
764
 
686
765
  alert("rm_package", dir)
687
766
  @package_count += 1
688
767
 
689
- Database.instance.remove_fpath(dir) do |record|
768
+ @documents.remove_match_path(dir) do |record|
690
769
  alert_info("rm_record", record.path)
691
770
  @file_count += 1
692
771
  end
@@ -716,17 +795,15 @@ EOF
716
795
  end
717
796
 
718
797
  def milkode_info
719
- alert('*milkode*', "#{@yaml.contents.size} packages, #{Database.instance.totalRecords} records in #{db_file}.")
798
+ db_open
799
+ alert('*milkode*', "#{@yaml.contents.size} packages, #{@documents.size} records in #{db_file}.")
720
800
  end
721
801
 
722
- def db_create(filename)
723
- dbfile = Pathname(File.expand_path(filename))
724
- dbdir = dbfile.dirname
725
- dbdir.mkpath unless dbdir.exist?
802
+ def db_create
803
+ filename = db_file
726
804
 
727
- unless dbfile.exist?
728
- Groonga::Database.create(:path => dbfile.to_s)
729
- db_define
805
+ unless File.exist? filename
806
+ db_open
730
807
  @out.puts "create : #{filename} created."
731
808
  else
732
809
  @out.puts "message : #{filename} already exist."
@@ -734,60 +811,29 @@ EOF
734
811
  end
735
812
 
736
813
  def list_info(packages)
737
- option = FindGrep::FindGrep::DEFAULT_OPTION.dup
738
- option.dbFile = Dbdir.groonga_path(Dbdir.default_dir)
739
- option.isSilent = true
740
- option.packages = packages.map{|p| p.directory}
741
- findGrep = FindGrep::FindGrep.new([], option)
742
- records = findGrep.pickupRecords
743
-
814
+ db_open
815
+ records = @documents.search(:packages => packages.map{|p| p.name})
744
816
  alert('*milk_list*', "#{packages.size} packages, #{records.size} records in #{db_file}.")
745
817
  end
746
818
 
747
- def db_define
748
- Groonga::Schema.define do |schema|
749
- schema.create_table("documents", :type => :hash) do |table|
750
- table.string("path")
751
- table.string("shortpath")
752
- table.text("content")
753
- table.time("timestamp")
754
- table.text("suffix")
755
- end
756
-
757
- schema.create_table("terms",
758
- :type => :patricia_trie,
759
- :key_normalize => true,
760
- :default_tokenizer => DEFAULT_TOKENIZER) do |table|
761
- table.index("documents.path", :with_position => true)
762
- table.index("documents.shortpath", :with_position => true)
763
- table.index("documents.content", :with_position => true)
764
- table.index("documents.suffix", :with_position => true)
765
- end
819
+ def db_open
820
+ if !@grndb || @grndb.closed?
821
+ @grndb = GroongaDatabase.new
822
+ @grndb.open(@db_dir)
823
+ @documents = @grndb.documents
766
824
  end
767
825
  end
768
826
 
769
- def db_open(filename)
770
- dbfile = Pathname(File.expand_path(filename))
771
-
772
- if dbfile.exist?
773
- # データベースを開く
774
- Groonga::Database.open(dbfile.to_s)
775
-
776
- # 互換性テスト
777
- db_compatible?
778
- else
779
- raise "error : #{dbfile.to_s} not found!!"
780
- end
781
- end
827
+ def db_delete
828
+ filename = db_file
782
829
 
783
- def db_delete(filename)
784
830
  raise "Illegal file name : #{filename}." unless filename =~ /\.db$/
831
+
785
832
  Dir.glob("#{filename}*").each do |f|
786
833
  @out.puts "delete : #{f}"
787
834
  FileUtils.rm_r(f)
788
835
  end
789
836
  end
790
- private :db_delete
791
837
 
792
838
  def db_add_dir(dir)
793
839
  @current_package = @yaml.package_root(dir)
@@ -797,57 +843,30 @@ EOF
797
843
  end
798
844
  private :db_add_dir
799
845
 
800
- def db_add_file(stdout, filename, shortpath)
846
+ def db_add_file(stdout, package_dir, restpath, package_name = nil)
801
847
  # サイレントモード
802
848
  return if @is_silent
803
-
804
- # ファイル名を全てUTF-8に変換
805
- filename_utf8 = Util::filename_to_utf8(filename)
806
- shortpath_utf8 = Util::filename_to_utf8(shortpath)
807
- suffix_utf8 = File::extname(filename_utf8)
808
-
809
- # 格納するデータ
810
- values = {
811
- :path => filename_utf8,
812
- :shortpath => shortpath_utf8,
813
- :content => nil,
814
- :timestamp => File.mtime(filename),
815
- :suffix => suffix_utf8,
816
- }
817
-
818
- # 検索するデータベース
819
- documents = Groonga::Context.default["documents"]
820
-
821
- record = documents[ values[:path] ]
822
- isNewFile = false
823
849
 
824
- unless record
825
- document = documents.add(values[:path])
826
- isNewFile = true
827
- else
828
- document = record
829
- end
830
-
831
- # タイムスタンプが新しければデータベースに格納
832
- if (document[:timestamp] < values[:timestamp])
833
- # 実際に使うタイミングでファイルの内容を読み込み
834
- # values[:content] = open(filename).read
835
- # データベース内の文字コードは'utf-8'で統一
836
- values[:content] = Kconv.kconv(File.read(filename), Kconv::UTF8)
837
-
838
- # データベースに格納
839
- values.each do |key, value|
840
- if (key == :path)
841
- if (isNewFile)
842
- @add_count += 1
843
- alert_info("add_record", value)
844
- else
845
- @update_count += 1
846
- alert_info("update", value)
847
- end
848
- end
849
- document[key] = value
850
- end
850
+ # データベースには先頭の'/'を抜いて登録する
851
+ # 最初から'/'を抜いておけば高速化の余地あり?
852
+ # ignore設定との互換性保持が必要
853
+ restpath = restpath.sub(/^\//, "")
854
+
855
+ # パッケージ名を設定
856
+ package_name = package_name || File.basename(package_dir)
857
+
858
+ # レコードの追加
859
+ result = @documents.add(package_dir, restpath, package_name)
860
+
861
+ # メッセージの表示
862
+ case result
863
+ when :newfile
864
+ @add_count += 1
865
+ alert_info("add_record", File.join(package_dir, restpath))
866
+ when :update
867
+ @grndb.packages.touch(package_name, :updatetime)
868
+ @update_count += 1
869
+ alert_info("update", File.join(package_dir, restpath))
851
870
  end
852
871
  end
853
872
 
@@ -875,7 +894,7 @@ EOF
875
894
  searchDirectory(stdout, dirname, packname, next_path, depth + 1)
876
895
  when "file"
877
896
  unless ignoreFile?(fpath, next_path)
878
- db_add_file(stdout, fpath, shortpath)
897
+ db_add_file(stdout, dirname, next_path) # shortpathの先頭に'/'が付いているのが気になる
879
898
  @file_count += 1
880
899
  # @out.puts "file_count : #{@file_count}" if (@file_count % 100 == 0)
881
900
  end
@@ -934,22 +953,6 @@ EOF
934
953
  @out.puts "[fatal] #{msg}"
935
954
  end
936
955
 
937
- def db_compatible?
938
- begin
939
- db_define
940
- rescue Groonga::Schema::Error => e
941
- puts <<EOF
942
- Milkode repository is old -> #{db_dir_expand}.
943
- Please rebuild repository,
944
-
945
- milk rebuild
946
-
947
- See 'milk --help' or http://milkode.ongaeshi.me .
948
- EOF
949
- exit -1
950
- end
951
- end
952
-
953
956
  def update_display_info(options)
954
957
  @is_display_info = true if (options[:verbose])
955
958
  end