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.1 by root, Mon Sep 10 18:30:30 2007 UTC vs.
Revision 1.14 by root, Mon Sep 22 05:42:41 2008 UTC

1#! perl # mandatory 1#! perl # mandatory
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 7my $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 8my $db_target = cf::sync_job { cf::db_table "tag-target" }; # tag => maps
48 48
49 utf8::encode $key; 49 utf8::encode $key;
50 BDB::db_get $db_mapinfo, $txn, $key, my $data; 50 BDB::db_get $db_mapinfo, $txn, $key, my $data;
51 51
52 unless ($!) { 52 unless ($!) {
53 $data = from_json $data; 53 $data = decode_json $data;
54 return if $data->{hash} eq $hash; 54 return if $data->{hash} eq $hash;
55 $old_tags = $data->{tags}; 55 $old_tags = $data->{tags};
56 } 56 }
57 57
58 $old_tags ||= []; 58 $old_tags ||= [];
59 59
60 my $f = new_from_file cf::object::thawer $file 60 my $f = new_from_file cf::object::thawer $file
61 or return; 61 or return;
62 62
63 my @tags = sort $f->extract_tags; 63 my @tags = sort $f->extract_tags;
64 $data = to_json { hash => $hash, tags => \@tags }; 64 $data = encode_json { hash => $hash, tags => \@tags };
65 65
66 BDB::db_put $db_mapinfo, $txn, $key, $data; 66 BDB::db_put $db_mapinfo, $txn, $key, $data;
67 67
68 # 1. remove tags no longer existing 68 # 1. remove tags no longer existing
69 for my $tag (@$old_tags) { 69 for my $tag (@$old_tags) {
77 add_tag_target $txn, $tag, $key; 77 add_tag_target $txn, $tag, $key;
78 } 78 }
79 79
80 # we don't actually care if it succeeds or not, as we 80 # we don't actually care if it succeeds or not, as we
81 # will just retry an hour later 81 # will just retry an hour later
82 BDB::db_txn_commit $txn; 82 BDB::db_txn_finish $txn;
83 83
84# warn "tag-updated $file (= $key) $hash\n";#d# 84 warn "tag-updated $file (= $key) <@tags>\n"
85 if @tags;
85} 86}
86 87
87sub scan_static { 88sub scan_static {
88 my ($dir, $map) = @_; 89 my ($dir, $map) = @_;
89 90
100 101
101 &scan_static ("$dir/$_", "$map$_/") 102 &scan_static ("$dir/$_", "$map$_/")
102 for @$dirs; 103 for @$dirs;
103} 104}
104 105
105cf::async_ext { 106sub reload {
106 $Coro::current->prio (Coro::PRIO_MIN); 107 my $guard = cf::lock_acquire "map-tags::reload";
107 108
108 while () {
109 my $start = Event::time; 109 my $start = EV::time;
110 110
111 # 1. check for maps no longer existing 111 # 1. check for maps no longer existing
112 {
113 my @delkeys;
112 114
113 # 2. scan all static maps 115 my $cursor = $db_mapinfo->cursor;
114 scan_static $cf::MAPDIR, "/"; 116 for (;;) {
117 BDB::db_c_get $cursor, my $key, my $data, BDB::NEXT;
118 last if $!;
115 119
120 my $data = JSON::XS::decode_json $data;
121 my ($ver, undef, undef, $path) = split /,/, $data->{hash}, 4;
122 push @delkeys, [$key, $data->{tags}]
123 if $ver != 1 || Coro::AIO::aio_stat $path;
124 }
125 BDB::db_c_close $cursor;
126
127 for (@delkeys) {
128 my ($key, $tags) = @$_;
129 my $txn = $cf::DB_ENV->txn_begin;
130 BDB::db_del $db_mapinfo, $txn, $key;
131 for my $tag (@{ $tags || [] }) {
132 remove_tag_target $txn, $tag, $key;
133 }
134 BDB::db_txn_finish $txn;
135 }
136 }
137
138 # 2. scan all static maps
139 scan_static $cf::MAPDIR, "/";
140
116 # 3. scan all dynamic maps 141 # 3. scan all dynamic maps
117 for my $path (@{ cf::map::tmp_maps or [] }, @{ cf::map::random_maps or [] }) { 142 for my $path (@{ cf::map::tmp_maps or [] }, @{ cf::map::random_maps or [] }) {
118# my $map = cf::map::find $path; 143# my $map = cf::map::find $path;
119# extract_map_tags "t/$map", $path; 144# extract_map_tags "t/$map", $path;
120 } 145 }
121 146
122 # now hunt for all per-player maps 147 # now hunt for all per-player maps
123# scan_dir $cf::PLAYERDIR 148# scan_dir $cf::PLAYERDIR
124# for my $login (@{ cf::player::list_logins or [] }) { 149# for my $login (@{ cf::player::list_logins or [] }) {
125# for my $path (@{ cf::player::maps $login or [] }) { 150# for my $path (@{ cf::player::maps $login or [] }) {
126# cf::cede_to_tick; 151# cf::cede_to_tick;
144# delete $map->{deny_reset}; 169# delete $map->{deny_reset};
145# } 170# }
146# } 171# }
147# } 172# }
148 173
149 warn sprintf "map-tag scan (%fs)", Event::time - $start; 174 warn sprintf "map-tag scan (%fs)", EV::time - $start;
150 Coro::Timer::sleep $SCHEDULE_INTERVAL; 175}
151 } 176
177our $RELOAD_SCHEDULER = cf::periodic $SCHEDULE_INTERVAL, Coro::unblock_sub {
178 $Coro::current->prio (Coro::PRIO_MIN);
179 $Coro::current->desc ("map-tag scanner");
180 reload;
152}; 181};
153 182
183$RELOAD_SCHEDULER->invoke (0); # force at startup
154 184
185# find all objects with the given tag, or at least try to
186sub find($) {
187 my ($tag) = @_;
188
189 utf8::encode (my $key = $tag);
190 BDB::db_get $db_target, undef, $key, my $data;
191 utf8::decode $data;
192
193 map { $_->load; $_->find_tagged_objects ($tag) }
194 grep $_,
195 map { cf::map::find $_ }
196 grep s/^s//,
197 split /\x00/, $data
198}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines