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.8 by root, Mon Sep 17 11:45:41 2007 UTC vs.
Revision 1.19 by root, Sat Oct 24 00:25:26 2009 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
7my $db_mapinfo = cf::sync_job { cf::db_table "tag-mapinfo" }; # info/cache for maps 7our $db_mapinfo = cf::sync_job { cf::db_table "tag-mapinfo" }; # info/cache for maps
8my $db_target = cf::sync_job { cf::db_table "tag-target" }; # tag => maps 8our $db_target = cf::sync_job { 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 = from_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 = to_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 warn "tag-updated $file (= $key) <@tags>\n"
85 if @tags; 76 if @tags;
86} 77}
87 78
88sub scan_static { 79sub scan_static {
89 my ($dir, $map) = @_; 80 my $maps = cf::map::static_maps;
90 81
91 my ($dirs, $files) = Coro::AIO::aio_scandir $dir, 2 82 scan_map "s$_", "$cf::MAPDIR$_.map"
92 or return;
93
94 for my $file (@$files) {
95 my $name = $file;
96 next unless $name =~ s/\.map$//;
97 utf8::decode $name;
98
99 scan_map "s$map$name", "$dir/$file";
100 }
101
102 &scan_static ("$dir/$_", "$map$_/")
103 for @$dirs; 83 for @$maps;
104} 84}
105 85
106sub reload { 86sub reload {
107 my $guard = cf::lock_acquire "map-tags::reload"; 87 my $guard = cf::lock_acquire "map-tags::reload";
108 88
109 my $start = Event::time; 89 my $start = EV::time;
110 90
111 # 1. check for maps no longer existing 91 # 1. check for maps no longer existing
112 { 92 {
113 my @delkeys; 93 my @delkeys;
114 94
115 my $cursor = $db_mapinfo->cursor; 95 my $cursor = $db_mapinfo->cursor;
116 for (;;) { 96 for (;;) {
117 BDB::db_c_get $cursor, my $key, my $data, BDB::NEXT; 97 BDB::db_c_get $cursor, my $key, my $data, BDB::NEXT;
118 last if $!; 98 last if $!;
119 99
120 my $data = JSON::XS::from_json $data; 100 my $data = JSON::XS::decode_json $data;
121 my ($ver, undef, undef, $path) = split /,/, $data->{hash}, 4; 101 my ($ver, undef, undef, $path) = split /,/, $data->{hash}, 4;
122 push @delkeys, [$key, $data->{tags}] 102 push @delkeys, [$key, $data->{tags}]
123 if $ver != 1 || Coro::AIO::aio_stat $path; 103 if $ver != 1 || Coro::AIO::aio_stat $path;
124 } 104 }
125 BDB::db_c_close $cursor; 105 BDB::db_c_close $cursor;
169# delete $map->{deny_reset}; 149# delete $map->{deny_reset};
170# } 150# }
171# } 151# }
172# } 152# }
173 153
174 warn sprintf "map-tag scan (%fs)", Event::time - $start; 154 warn sprintf "map-tag scan (%fs)", EV::time - $start;
175} 155}
176 156
177our $RELOAD_SCHEDULER = Event->timer ( 157our $RELOAD_SCHEDULER = cf::periodic $SCHEDULE_INTERVAL, Coro::unblock_sub {
178 after => 0,
179 interval => $SCHEDULE_INTERVAL,
180 data => cf::WF_AUTOCANCEL,
181 cb => Coro::unblock_sub {
182 $Coro::current->prio (Coro::PRIO_MIN); 158 $Coro::current->prio (Coro::PRIO_MIN);
159 $Coro::current->desc ("map-tag scanner");
183 reload; 160 reload;
184 }, 161};
185); 162
163cf::post_init {
164 $RELOAD_SCHEDULER->invoke (0); # force at startup
165};
186 166
187# find all objects with the given tag, or at least try to 167# find all objects with the given tag, or at least try to
188sub find($) { 168sub find($) {
189 my ($tag) = @_; 169 my ($tag) = @_;
190 170

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines