Class: Flist::Dbutil::DirzMgr

Inherits:
Object
  • Object
show all
Includes:
Ykutils::DebugUtils
Defined in:
lib/flist/dbutil/dirzmgr.rb

Overview

ディレクトリ情報マネージャ

Instance Method Summary collapse

Constructor Details

#initialize(encx, db_encoding, register_time) ⇒ DirzMgr

初期化



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flist/dbutil/dirzmgr.rb', line 13

def initialize(encx, db_encoding, register_time)
  # 指定エンコーディングの基づいたパス操作
  @encx = encx
  # DBのエンコーディング
  @db_encoding = db_encoding
  # DB接続時のタイムスタンプ
  @register_time = register_time
  # DBに登録・更新したファイル情報をのハッシュ
  # キーはパス
  @hs_by_path = {}
  # DBに登録・更新したファイル情報のハッシュ
  # キーはファイル情報のobject_id
  @hs_by_id = {}
  # DBに登録・更新したディレクトリ情報に対応するパス
  @set_of_dir_path = Set.new
end

Instance Method Details

#add(path) ⇒ Object

ディレクトリ情報追加・更新 DBに未登録の時は追加し、登録済みの時は更新する



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flist/dbutil/dirzmgr.rb', line 32

def add(path)
  # pathをDBのエンコーディングに変換する
  path_conv = @encx.convert(path, @db_encoding)
  # パスが表すファイル情報をハッシュから得る
  dirz = @hs_by_path[path]
  # パスに対応するファイル情報が見つかれば何もしない
  return dirz if dirz

  # DirzのViewから、パスで指定したディレクトリ情報を得る
  cur_dirz = Dbutil::Currentdirz.where(path: path_conv).limit(1)
  if cur_dirz.size.zero?
    # 見つからなければ、新たにファイル情報をDBに登録する
    begin
      d_puts '#### DirzMgr'
      d_puts "path=#{path_conv}"
      d_puts '#### FlistzMgr End'
      dirz = Dirz.create(path: path_conv, start_datetime: @register_time)
    rescue StandardError => e
      p e.class
      p e.message
      pp e.backtrace

      dirz = nil
    end
  else
    # DBから見つかれば、最初に見つけたファイル情報を更新する
    # current_curz = cur_dirz.first.dirz
    current_dirz = cur_dirz[0].dirz
    # hs = { atime: atime, ctime: ctime, mtime: mtime, repo: repo,
    #        project: project, desc: desc, comment: comment }
    # value_hs = hs.each_with_object({}) do |item, hsx|
    #  hsx[item[0]] = item[1] if current_flistz[item[0]] != item[1]
    # end
    # current_flistz.update(value_hs) if value_hs.size.positive?
    dirz = current_dirz
  end
  # DBに登録・更新したファイル情報をハッシュに格納
  if dirz
    @hs_by_path[path] = dirz
    @hs_by_id[dirz.object_id] = dirz
    @set_of_dir_path.add(path)
  end
  dirz
end

#post_processObject

後処理



78
79
80
81
82
# File 'lib/flist/dbutil/dirzmgr.rb', line 78

def post_process
  @set_of_dir_path.each do |path|
    post_process_for_dir_path(path)
  end
end

#post_process_for_dir_path(path) ⇒ Object

後処理(パス指定)



85
86
87
88
89
90
91
92
93
94
# File 'lib/flist/dbutil/dirzmgr.rb', line 85

def post_process_for_dir_path(path)
  h_ids = Currentdirz.where(path: path).pluck(:org_id)
  t_ids = @hs_by_id.keys
  ids = h_ids - t_ids
  return unless ids.size.positive?

  ids.each do |idx|
    Invaliddirz.create(org_id: idx, end_datetime: @register_time)
  end
end