ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-tags.ext
(Generate patch)

Comparing deliantra/server/ext/map-tags.ext (file contents):
Revision 1.17 by root, Fri Oct 23 03:08:34 2009 UTC vs.
Revision 1.27 by root, Wed May 5 23:23:29 2010 UTC

2 2
3our $SCHEDULE_INTERVAL = $cf::CFG{extractor_schedule_interval} || 3600; 3our $SCHEDULE_INTERVAL = $cf::CFG{extractor_schedule_interval} || 3600;
4 4
5use JSON::XS; 5use JSON::XS;
6 6
7our $db_mapinfo = cf::sync_job { cf::db_table "tag-mapinfo" }; # info/cache for maps 7our $db_mapinfo = cf::db_table "tag-mapinfo"; # info/cache for maps
8our $db_target = cf::sync_job { cf::db_table "tag-target" }; # tag => maps 8our $db_target = cf::db_table "tag-target"; # tag => maps
9 9
10sub remove_tag_target { 10sub remove_tag_target {
11 my ($txn, $tag, $target) = @_; 11 my ($txn, $tag, $target) = @_;
12 # - U O 12 # - U O
13 13
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
84 warn "tag-updated $file (= $key) <@tags>\n" 75 cf::debug "tag-updated $file (= $key) <@tags>\n"
85 if @tags; 76 if @tags;
86} 77}
87 78
88sub scan_static { 79sub 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
95sub reload { 86sub 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
135 126
136 # now hunt for all per-player maps 127 # now hunt for all per-player maps
137# scan_dir $cf::PLAYERDIR 128# scan_dir $cf::PLAYERDIR
138# for my $login (@{ cf::player::list_logins or [] }) { 129# for my $login (@{ cf::player::list_logins or [] }) {
139# for my $path (@{ cf::player::maps $login or [] }) { 130# for my $path (@{ cf::player::maps $login or [] }) {
140# cf::cede_to_tick;
141# 131#
142# $path =~ /^~[^\/]+(\/.*)$/ 132# $path =~ /^~[^\/]+(\/.*)$/
143# or next; # doh 133# or next; # doh
144# 134#
145# my $base = cf::map::find $1; 135# my $base = cf::map::find $1;
158# delete $map->{deny_reset}; 148# delete $map->{deny_reset};
159# } 149# }
160# } 150# }
161# } 151# }
162 152
163 warn sprintf "map-tag scan (%fs)", EV::time - $start; 153 cf::info sprintf "map-tag scan finished (%fs)\n", AE::time - $start;
164} 154}
165 155
166our $RELOAD_SCHEDULER = cf::periodic $SCHEDULE_INTERVAL, Coro::unblock_sub { 156our $RELOAD_SCHEDULER = cf::periodic $SCHEDULE_INTERVAL, Coro::unblock_sub {
167 $Coro::current->prio (Coro::PRIO_MIN); 157 $Coro::current->prio (Coro::PRIO_MIN);
168 $Coro::current->desc ("map-tag scanner"); 158 $Coro::current->desc ("map-tag scanner");
185 grep $_, 175 grep $_,
186 map { cf::map::find $_ } 176 map { cf::map::find $_ }
187 grep s/^s//, 177 grep s/^s//,
188 split /\x00/, $data 178 split /\x00/, $data
189} 179}
180
181sub unload {
182 my $guard = cf::lock_acquire "map-tags::reload";
183
184 BDB::db_close $db_target;
185 BDB::db_close $db_mapinfo;
186}
187

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines