~ubuntu-branches/ubuntu/trusty/ruby-ferret/trusty

« back to all changes in this revision

Viewing changes to ext/q_prefix.c

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2011-07-28 00:02:49 UTC
  • Revision ID: james.westby@ubuntu.com-20110728000249-v0443y69ftcpxwi6
Tags: upstream-0.11.6
ImportĀ upstreamĀ versionĀ 0.11.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string.h>
 
2
#include "search.h"
 
3
 
 
4
/****************************************************************************
 
5
 *
 
6
 * PrefixQuery
 
7
 *
 
8
 ****************************************************************************/
 
9
 
 
10
#define PfxQ(query) ((PrefixQuery *)(query))
 
11
 
 
12
static char *prq_to_s(Query *self, const char *current_field) 
 
13
{
 
14
    char *buffer, *bptr;
 
15
    const char *prefix = PfxQ(self)->prefix;
 
16
    const char *field = PfxQ(self)->field;
 
17
    size_t plen = strlen(prefix);
 
18
    size_t flen = strlen(field);
 
19
 
 
20
    bptr = buffer = ALLOC_N(char, plen + flen + 35);
 
21
 
 
22
    if (strcmp(field, current_field) != 0) {
 
23
        sprintf(bptr, "%s:", field);
 
24
        bptr += flen + 1;
 
25
    }
 
26
 
 
27
    sprintf(bptr, "%s*", prefix);
 
28
    bptr += plen + 1;
 
29
    if (self->boost != 1.0) {
 
30
        *bptr = '^';
 
31
        dbl_to_s(++bptr, self->boost);
 
32
    }
 
33
 
 
34
    return buffer;
 
35
}
 
36
 
 
37
static Query *prq_rewrite(Query *self, IndexReader *ir)
 
38
{
 
39
    const char *field = PfxQ(self)->field;
 
40
    const int field_num = fis_get_field_num(ir->fis, field);
 
41
    Query *volatile q = multi_tq_new_conf(field, MTQMaxTerms(self), 0.0);
 
42
    q->boost = self->boost;        /* set the boost */
 
43
 
 
44
    if (field_num >= 0) {
 
45
        const char *prefix = PfxQ(self)->prefix;
 
46
        TermEnum *te = ir->terms_from(ir, field_num, prefix);
 
47
        const char *term = te->curr_term;
 
48
        size_t prefix_len = strlen(prefix);
 
49
 
 
50
        TRY
 
51
            do { 
 
52
                if (strncmp(term, prefix, prefix_len) != 0) {
 
53
                    break;
 
54
                }
 
55
                multi_tq_add_term(q, term);       /* found a match */
 
56
            } while (te->next(te));
 
57
        XFINALLY
 
58
            te->close(te);
 
59
        XENDTRY
 
60
    }
 
61
 
 
62
    return q;
 
63
}
 
64
 
 
65
static void prq_destroy(Query *self)
 
66
{
 
67
    free(PfxQ(self)->field);
 
68
    free(PfxQ(self)->prefix);
 
69
    q_destroy_i(self);
 
70
}
 
71
 
 
72
static unsigned long prq_hash(Query *self)
 
73
{
 
74
    return str_hash(PfxQ(self)->field) ^ str_hash(PfxQ(self)->prefix);
 
75
}
 
76
 
 
77
static int prq_eq(Query *self, Query *o)
 
78
{
 
79
    return (strcmp(PfxQ(self)->prefix, PfxQ(o)->prefix) == 0) 
 
80
        && (strcmp(PfxQ(self)->field,  PfxQ(o)->field) == 0);
 
81
}
 
82
 
 
83
Query *prefixq_new(const char *field, const char *prefix)
 
84
{
 
85
    Query *self = q_new(PrefixQuery);
 
86
 
 
87
    PfxQ(self)->field       = estrdup(field);
 
88
    PfxQ(self)->prefix      = estrdup(prefix);
 
89
    MTQMaxTerms(self)       = PREFIX_QUERY_MAX_TERMS;
 
90
 
 
91
    self->type              = PREFIX_QUERY;
 
92
    self->rewrite           = &prq_rewrite;
 
93
    self->to_s              = &prq_to_s;
 
94
    self->hash              = &prq_hash;
 
95
    self->eq                = &prq_eq;
 
96
    self->destroy_i         = &prq_destroy;
 
97
    self->create_weight_i   = &q_create_weight_unsup;
 
98
 
 
99
    return self;
 
100
}