Class: CodeStock::Cdstk

Inherits:
Object
  • Object
show all
Defined in:
lib/cdstk/cdstk.rb

Constant Summary collapse

DB_FILE_PATH =
'db/grendb.db'
DEFAULT_TOKENIZER =

記号・アルファベット・数字もバイグラムでトークナイズする。

"TokenBigramSplitSymbolAlphaDigit"

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout, db_dir = ".") ⇒ Cdstk

Returns a new instance of Cdstk.



25
26
27
28
29
30
31
32
# File 'lib/cdstk/cdstk.rb', line 25

def initialize(io = $stdout, db_dir = ".")
  @db_dir = db_dir
  @out = io
  @file_count = 0
  @add_count = 0
  @update_count = 0
  @start_time = Time.now
end

Instance Method Details

#add(*content) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cdstk/cdstk.rb', line 61

def add(*content)
  # 絶対パスに変換
  content.map!{|v|File.expand_path(v)}

  # YAML更新
  yaml = yaml_load
  yaml.add(*content)
  yaml.save

  # 部分アップデート
  db_open(db_file)
  content.each do |dir|
    update_dir(dir)
  end
end

#dumpObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cdstk/cdstk.rb', line 99

def dump
  db_open(db_file)
  
  documents = Groonga::Context.default["documents"]
  records = documents.select
  records.each do |record|
    @out.puts record.inspect
    @out.puts "path : #{record.path}"
    @out.puts "shortpath : #{record.shortpath}"
    @out.puts "suffix : #{record.suffix}"
    @out.puts "timestamp : #{record.timestamp.strftime('%Y/%m/%d %H:%M:%S')}"
    @out.puts "content :", record.content ? record.content[0..64] : nil
    @out.puts
  end
end

#initObject



34
35
36
37
38
39
40
41
42
# File 'lib/cdstk/cdstk.rb', line 34

def init
  if Dir.entries(@db_dir) == [".", ".."]
    CdstkYaml.create(@db_dir)
    @out.puts "create     : #{yaml_file}"
    db_create(db_file)
  else
    @out.puts "Can't create Grendb Database (Not empty) in #{@db_dir}"
  end
end

#listObject



89
90
91
# File 'lib/cdstk/cdstk.rb', line 89

def list
  @out.puts yaml_load.list
end

#rebuildObject



93
94
95
96
97
# File 'lib/cdstk/cdstk.rb', line 93

def rebuild
  db_delete(db_file)
  db_create(db_file)
  update
end

#remove(*content) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cdstk/cdstk.rb', line 77

def remove(*content)
  # 絶対パスに変換
  content.map!{|v|File.expand_path(v)}

  # YAML更新
  yaml = yaml_load
  yaml.remove(*content)
  yaml.save

  # @todo 削除したコンテンツをインデックスから削除
end

#updateObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/cdstk/cdstk.rb', line 44

def update
  print_result do 
    yaml = yaml_load
    db_open(db_file)

    yaml.contents.each do |content|
      update_dir_in(content["directory"])
    end
  end
end

#update_dir(dir) ⇒ Object



55
56
57
58
59
# File 'lib/cdstk/cdstk.rb', line 55

def update_dir(dir)
  print_result do 
    update_dir_in(dir)
  end
end