~gz/ubuntu/wily/steam/new_rel_udev_rules

« back to all changes in this revision

Viewing changes to services/fulltext.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
inherit Service.Service;
2
 
 
3
 
Thread.Queue oQueue = Thread.Queue();
4
 
Thread.Mutex mBuffer = Thread.Mutex();
5
 
string sStripBuffer;
6
 
 
7
 
object tStripDemon;
8
 
mapping mStripFilter = ([  ]);
9
 
 
10
 
#include <events.h>
11
 
#include <attributes.h>
12
 
#include <configure.h>
13
 
 
14
 
class Doc {
15
 
  int content_id;
16
 
  int id;
17
 
  object doc;
18
 
  string mime;
19
 
  string name;
20
 
  
21
 
  void create(object o) { 
22
 
    doc = o; 
23
 
    mime = o->query_attribute(DOC_MIME_TYPE);
24
 
    name = o->get_identifier();
25
 
    werror("Mimetype for %s is %O\n", name, mime);
26
 
    id = o->get_object_id();
27
 
    content_id = o->get_content_id();
28
 
  }
29
 
}
30
 
 
31
 
void notify(mixed args)
32
 
{
33
 
    werror("EVENT_UPLOAD in fulltext.pike [%O]\n" , args);
34
 
    if (!tStripDemon)
35
 
        tStripDemon = thread_create(strip_demon);
36
 
    Doc doc = Doc(args[0]);
37
 
    oQueue->write(doc);
38
 
}
39
 
 
40
 
void call_service(object user, mixed args, int|void id)
41
 
{
42
 
    werror("Service called with %O\n", args);
43
 
}
44
 
 
45
 
static void run()
46
 
{
47
 
}
48
 
 
49
 
void strip_demon() {
50
 
    werror("Content Strip Service Demon started.\n");
51
 
    while (1) {
52
 
        string query;
53
 
 
54
 
        Doc oDocument = oQueue->read();
55
 
        werror("Indexing Document !\n");
56
 
        string sMime = oDocument->mime;
57
 
        string sStripHandler;
58
 
        
59
 
        sStripHandler = mStripFilter[sMime];
60
 
        
61
 
        if ( sStripHandler )
62
 
        {
63
 
            int iContentID = oDocument->content_id;
64
 
            int iObID = oDocument->id;
65
 
            Stdio.File temp = Stdio.File("buffer.file", "rwct");
66
 
            Sql.Sql handle = Sql.Sql(serverCfg["database"]);
67
 
            Sql.sql_result res =
68
 
                handle->big_query("select rec_data from doc_data where "+
69
 
                                  "doc_id = "+iContentID+
70
 
                                  " order by rec_order");
71
 
            while (mixed data = res->fetch_row())
72
 
                temp->write(data[0]);
73
 
            temp->close();
74
 
            Stdio.File infile = Stdio.File("buffer.file");
75
 
 
76
 
            if ( strlen(sStripHandler) > 0 ) {
77
 
              Stdio.File outfile = Stdio.File("strip.file","wct");
78
 
              mixed err = catch {
79
 
                int PCode = Process.create_process(
80
 
                                                   ({ sStripHandler }),
81
 
                                                   ([ "stdin" : infile,
82
 
                                                      "stdout" : outfile]))->wait();
83
 
                werror("Fulltext - stripped content length is %d\n",
84
 
                       Stdio.file_size("strip.file"));
85
 
              };
86
 
              if (err)
87
 
                werror("Error during content stripping:\n"+
88
 
                       master()->describe_backtrace(err));
89
 
              outfile->close();
90
 
              outfile->open("strip.file","r");
91
 
              query = "replace into doc_ft values("+iObID+","+
92
 
                iContentID+",\""+
93
 
              handle->quote(outfile->read())+"\")";
94
 
            }
95
 
            else {
96
 
              string cbuffer = Stdio.read_file("buffer.file");
97
 
              werror("Updating indexof %d: %d bytes...\n", 
98
 
                     iObID,
99
 
                     strlen(cbuffer));
100
 
              query = "replace into doc_ft values("+iObID+","+
101
 
                iContentID+",\""+
102
 
                handle->quote(cbuffer)+"\")";
103
 
            }
104
 
            handle->big_query(query);
105
 
        }
106
 
        else
107
 
          werror("No Striphandler configured for %O (%O)\n", sMime,
108
 
                 sStripHandler);
109
 
    }
110
 
}
111
 
 
112
 
mixed search_documents(string pattern)
113
 
{
114
 
    object handle = Sql.Sql(serverCfg["database"]);
115
 
    object result =  handle->big_query("select ob_id, match(doc_data) against(%s) from doc_ft where match(doc_data) against(%s)", pattern, pattern);
116
 
    array res = ({});
117
 
    mixed row;
118
 
    while (row = result->fetch_row())
119
 
        res += ({ row });
120
 
 
121
 
    return res;
122
 
}
123
 
 
124
 
 
125
 
static void create_table(string dbhandle)
126
 
{
127
 
    Sql.Sql handle = Sql.Sql(dbhandle);
128
 
    handle->query("create table if not exists doc_ft (ob_id int, "+
129
 
                  "doc_id int, doc_data TEXT, FULLTEXT(doc_data))");
130
 
}
131
 
 
132
 
 
133
 
/*
134
 
 * create temporary table ft_id (ob_id int primary key, count int);
135
 
 * insert into ft_id select ob_id, 0 from ob_data where ob_attr='DOC_MIME_TYPE' and ob_data='"text/html"';
136
 
 * replace into ft_id select ob_id, 1 from doc_ft;
137
 
 * delete from ft_id where count = 1;
138
 
 */
139
 
static void check_ft_integrity()
140
 
{
141
 
    Sql.Sql handle = Sql.Sql(serverCfg["database"]);
142
 
    handle->query("create temporary table ft_id "+
143
 
                  "(ob_id int primary key, count int)");
144
 
    handle->query("insert into ft_id select distinct "+
145
 
                  "ob_id,0 from ob_data where ob_attr='DOC_MIME_TYPE'"+
146
 
                  "and ob_data='\""+
147
 
                  indices(mStripFilter)*"\"' or ob_data='\""+"\"'");
148
 
    handle->query("replace into ft_id select distinct ob_id, 1 from doc_ft");
149
 
    handle->query("delete from ft_id where count =1");
150
 
    array missing = handle->query("select distinct ob_id from ft_id");
151
 
    handle->query("drop table ft_id");
152
 
    foreach (missing, mixed a)
153
 
    {
154
 
        object o = connection->find_object((int)a["ob_id"]);
155
 
        if (objectp(o))
156
 
            oQueue->write(Doc(o));
157
 
    }
158
 
}
159
 
 
160
 
static private void got_kill(int sig)
161
 
{
162
 
    _exit(1);  
163
 
}
164
 
 
165
 
int main(int argc, array argv)
166
 
{
167
 
    init( "fulltext", argv + ({ "--eid="+EVENT_UPLOAD }) );
168
 
 
169
 
    if (catch{mStripFilter = read_config_file(CONFIG_DIR+"/services/fulltext.cfg");})
170
 
      mStripFilter = ([ "text/html" : "html2text" ]);
171
 
    
172
 
 
173
 
    if ( !mStripFilter["text/plain"]) 
174
 
      mStripFilter["text/plain"] = "";
175
 
    signal(signum("QUIT"), got_kill);
176
 
    
177
 
    mixed err = catch{
178
 
        create_table(serverCfg["database"]);
179
 
        start();
180
 
    };
181
 
 
182
 
    tStripDemon = thread_create(strip_demon);
183
 
    thread_create(check_ft_integrity);
184
 
 
185
 
    if (err)
186
 
        werror("Startup of fulltext service failed.\n"+
187
 
               master()->describe_backtrace(err)+"\n");
188
 
    return -17;
189
 
}
190
 
 
191
 
mapping read_config_file(string fname)
192
 
{
193
 
    Parser.XML.Tree.Node node = Parser.XML.Tree.parse_file(fname);
194
 
    if (!objectp(node))
195
 
        error("Failed to parse config file %s\n", fname);
196
 
 
197
 
    mapping data = ([]);
198
 
    node = node->get_first_element("config");
199
 
    foreach(node->get_elements(), Parser.XML.Tree.Node n)
200
 
    {
201
 
        data[n->get_attributes()["mime"]] = n->get_last_child()->get_text();
202
 
    }
203
 
 
204
 
    return data;
205
 
}