… | |
… | |
40 | Coro::AIO::aio_stat $file |
40 | Coro::AIO::aio_stat $file |
41 | and next; |
41 | and next; |
42 | |
42 | |
43 | my $hash = join ",", 1, (stat _)[7,9], $file; |
43 | my $hash = join ",", 1, (stat _)[7,9], $file; |
44 | |
44 | |
45 | my $old_tags; |
|
|
46 | |
|
|
47 | my $txn = $cf::DB_ENV->txn_begin; |
45 | my $txn = $cf::DB_ENV->txn_begin; |
48 | |
46 | |
49 | utf8::encode $key; |
47 | utf8::encode $key; |
50 | BDB::db_get $db_mapinfo, $txn, $key, my $data; |
48 | BDB::db_get $db_mapinfo, $txn, $key, my $data; |
51 | |
49 | |
52 | unless ($!) { |
50 | unless ($!) { |
53 | $data = decode_json $data; |
51 | $data = decode_json $data; |
54 | return if $data->{hash} eq $hash; |
52 | return if $data->{hash} eq $hash; |
55 | $old_tags = $data->{tags}; |
53 | |
|
|
54 | # remove all old tags unconditionally |
|
|
55 | remove_tag_target $txn, $_, $key |
|
|
56 | for @{ $data->{tags} }; |
56 | } |
57 | } |
57 | |
|
|
58 | $old_tags ||= []; |
|
|
59 | |
58 | |
60 | my $f = new_from_file cf::object::thawer $file |
59 | my $f = new_from_file cf::object::thawer $file |
61 | or return; |
60 | or return; |
62 | |
61 | |
63 | my @tags = sort $f->extract_tags; |
62 | my @tags = sort $f->extract_tags; |
64 | $data = encode_json { hash => $hash, tags => \@tags }; |
63 | $data = encode_json { hash => $hash, tags => \@tags }; |
65 | |
64 | |
66 | BDB::db_put $db_mapinfo, $txn, $key, $data; |
65 | BDB::db_put $db_mapinfo, $txn, $key, $data; |
67 | |
66 | |
68 | # 1. remove tags no longer existing |
67 | # add all tags |
69 | for my $tag (@$old_tags) { |
|
|
70 | next if grep $_ eq $tag, @tags; |
|
|
71 | remove_tag_target $txn, $tag, $key; |
|
|
72 | } |
|
|
73 | |
|
|
74 | # 2. add tags that are new |
|
|
75 | for my $tag (@tags) { |
|
|
76 | next if grep $_ eq $tag, @$old_tags; |
|
|
77 | add_tag_target $txn, $tag, $key; |
68 | add_tag_target $txn, $_, $key |
78 | } |
69 | for @tags; |
79 | |
70 | |
80 | # we don't actually care if it succeeds or not, as we |
71 | # we don't actually care if it succeeds or not, as we |
81 | # will just retry an hour later |
72 | # will just retry an hour later |
82 | BDB::db_txn_finish $txn; |
73 | BDB::db_txn_finish $txn; |
83 | |
74 | |
… | |
… | |
86 | } |
77 | } |
87 | |
78 | |
88 | sub scan_static { |
79 | sub scan_static { |
89 | my $maps = cf::map::static_maps; |
80 | my $maps = cf::map::static_maps; |
90 | |
81 | |
91 | scan_map "s$_", "$cf::MAPDIR$_.map"; |
82 | scan_map "s$_", "$cf::MAPDIR$_.map" |
92 | for @$maps; |
83 | for @$maps; |
93 | } |
84 | } |
94 | |
85 | |
95 | sub reload { |
86 | sub reload { |
96 | my $guard = cf::lock_acquire "map-tags::reload"; |
87 | my $guard = cf::lock_acquire "map-tags::reload"; |
… | |
… | |
185 | grep $_, |
176 | grep $_, |
186 | map { cf::map::find $_ } |
177 | map { cf::map::find $_ } |
187 | grep s/^s//, |
178 | grep s/^s//, |
188 | split /\x00/, $data |
179 | split /\x00/, $data |
189 | } |
180 | } |
|
|
181 | |
|
|
182 | sub unload { |
|
|
183 | my $guard = cf::lock_acquire "map-tags::reload"; |
|
|
184 | |
|
|
185 | BDB::db_close $db_target; |
|
|
186 | BDB::db_close $db_mapinfo; |
|
|
187 | } |