/
search-lookup
executable file
·68 lines (59 loc) · 1.99 KB
/
search-lookup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
# This code was forked from the LiveJournal project owned and operated
# by Live Journal, Inc. The code has been modified and expanded by
# Dreamwidth Studios, LLC. These files were originally licensed under
# the terms of the license supplied by Live Journal, Inc, which can
# currently be found at:
#
# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
#
# In accordance with the original license, this code and all its
# modifications are provided under the GNU General Public License.
# A copy of that license can be found in the LICENSE file included as
# part of this distribution.
use strict;
use lib "$ENV{LJHOME}/cgi-bin";
BEGIN {
require "ljlib.pl";
}
use Gearman::Worker;
use Storable;
use LJ::Worker::Gearman;
use LJ::Directory::Search;
use LJ::UserSearch;
use LJ::UserSearch::MetaUpdater;
my $mtime = init_memory_datastructure();
gearman_decl("directory_search" => \&dir_search);
gearman_set_idle_handler(\&idle);
gearman_work(save_result => 1);
sub idle {
$mtime = LJ::UserSearch::MetaUpdater::update_users($mtime);
}
sub dir_search {
my $job = shift;
my $args = eval { Storable::thaw($job->arg) } || [];
die "Got error: $@" if $@;
my $dir = $args;
my @constraints = $dir->constraints;
return undef unless scalar @constraints;
my $results = $dir->search_no_dispatch($job);
return Storable::nfreeze($results);
}
sub init_memory_datastructure {
my $file = $LJ::USERSEARCH_METAFILE_PATH;
my $size = -s $file;
my $filemtime = (stat($file))[9];
die "Refusing to start until '$file' exists and has nonzero size" unless $size;
LJ::UserSearch::reset_usermeta($size);
open (my $fh, $file) or die;
my $buf;
my $pushed = 0;
my $rv;
while ($rv = sysread($fh, $buf, 256*1024)) {
LJ::UserSearch::add_usermeta($buf, $rv);
$pushed += $rv;
}
die "Error reading file: $!" unless defined $rv;
die "Didn't read all file" unless $pushed == $size;
return $filemtime;
}