… | |
… | |
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 | |
… | |
… | |
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"; |
97 | |
88 | |
98 | my $start = EV::time; |
89 | my $start = AE::time; |
99 | |
90 | |
100 | # 1. check for maps no longer existing |
91 | # 1. check for maps no longer existing |
101 | { |
92 | { |
102 | my @delkeys; |
93 | my @delkeys; |
103 | |
94 | |
… | |
… | |
158 | # delete $map->{deny_reset}; |
149 | # delete $map->{deny_reset}; |
159 | # } |
150 | # } |
160 | # } |
151 | # } |
161 | # } |
152 | # } |
162 | |
153 | |
163 | warn sprintf "map-tag scan (%fs)", EV::time - $start; |
154 | warn sprintf "map-tag scan finished (%fs)\n", AE::time - $start; |
164 | } |
155 | } |
165 | |
156 | |
166 | our $RELOAD_SCHEDULER = cf::periodic $SCHEDULE_INTERVAL, Coro::unblock_sub { |
157 | our $RELOAD_SCHEDULER = cf::periodic $SCHEDULE_INTERVAL, Coro::unblock_sub { |
167 | $Coro::current->prio (Coro::PRIO_MIN); |
158 | $Coro::current->prio (Coro::PRIO_MIN); |
168 | $Coro::current->desc ("map-tag scanner"); |
159 | $Coro::current->desc ("map-tag scanner"); |
… | |
… | |
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 | } |