ferret 0.11.6 → 0.11.8.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +10 -22
- data/RELEASE_CHANGES +137 -0
- data/RELEASE_NOTES +60 -0
- data/Rakefile +379 -274
- data/TODO +100 -8
- data/bin/ferret-browser +0 -0
- data/ext/BZLIB_blocksort.c +1094 -0
- data/ext/BZLIB_bzlib.c +1578 -0
- data/ext/BZLIB_compress.c +672 -0
- data/ext/BZLIB_crctable.c +104 -0
- data/ext/BZLIB_decompress.c +626 -0
- data/ext/BZLIB_huffman.c +205 -0
- data/ext/BZLIB_randtable.c +84 -0
- data/ext/{api.c → STEMMER_api.c} +7 -10
- data/ext/{libstemmer.c → STEMMER_libstemmer.c} +3 -2
- data/ext/{stem_ISO_8859_1_danish.c → STEMMER_stem_ISO_8859_1_danish.c} +123 -124
- data/ext/{stem_ISO_8859_1_dutch.c → STEMMER_stem_ISO_8859_1_dutch.c} +177 -188
- data/ext/STEMMER_stem_ISO_8859_1_english.c +1117 -0
- data/ext/{stem_ISO_8859_1_finnish.c → STEMMER_stem_ISO_8859_1_finnish.c} +276 -306
- data/ext/STEMMER_stem_ISO_8859_1_french.c +1246 -0
- data/ext/{stem_ISO_8859_1_german.c → STEMMER_stem_ISO_8859_1_german.c} +161 -170
- data/ext/STEMMER_stem_ISO_8859_1_hungarian.c +1230 -0
- data/ext/STEMMER_stem_ISO_8859_1_italian.c +1065 -0
- data/ext/STEMMER_stem_ISO_8859_1_norwegian.c +297 -0
- data/ext/{stem_ISO_8859_1_porter.c → STEMMER_stem_ISO_8859_1_porter.c} +263 -290
- data/ext/{stem_ISO_8859_1_portuguese.c → STEMMER_stem_ISO_8859_1_portuguese.c} +362 -380
- data/ext/STEMMER_stem_ISO_8859_1_spanish.c +1093 -0
- data/ext/STEMMER_stem_ISO_8859_1_swedish.c +307 -0
- data/ext/STEMMER_stem_ISO_8859_2_romanian.c +998 -0
- data/ext/{stem_KOI8_R_russian.c → STEMMER_stem_KOI8_R_russian.c} +244 -245
- data/ext/STEMMER_stem_UTF_8_danish.c +339 -0
- data/ext/{stem_UTF_8_dutch.c → STEMMER_stem_UTF_8_dutch.c} +192 -211
- data/ext/STEMMER_stem_UTF_8_english.c +1125 -0
- data/ext/{stem_UTF_8_finnish.c → STEMMER_stem_UTF_8_finnish.c} +284 -324
- data/ext/STEMMER_stem_UTF_8_french.c +1256 -0
- data/ext/{stem_UTF_8_german.c → STEMMER_stem_UTF_8_german.c} +170 -187
- data/ext/STEMMER_stem_UTF_8_hungarian.c +1234 -0
- data/ext/STEMMER_stem_UTF_8_italian.c +1073 -0
- data/ext/STEMMER_stem_UTF_8_norwegian.c +299 -0
- data/ext/{stem_UTF_8_porter.c → STEMMER_stem_UTF_8_porter.c} +271 -310
- data/ext/STEMMER_stem_UTF_8_portuguese.c +1023 -0
- data/ext/STEMMER_stem_UTF_8_romanian.c +1004 -0
- data/ext/STEMMER_stem_UTF_8_russian.c +694 -0
- data/ext/STEMMER_stem_UTF_8_spanish.c +1097 -0
- data/ext/STEMMER_stem_UTF_8_swedish.c +309 -0
- data/ext/STEMMER_stem_UTF_8_turkish.c +2205 -0
- data/ext/{utilities.c → STEMMER_utilities.c} +100 -68
- data/ext/analysis.c +276 -121
- data/ext/analysis.h +190 -143
- data/ext/api.h +3 -4
- data/ext/array.c +5 -3
- data/ext/array.h +52 -43
- data/ext/bitvector.c +38 -482
- data/ext/bitvector.h +446 -124
- data/ext/bzlib.h +282 -0
- data/ext/bzlib_private.h +503 -0
- data/ext/compound_io.c +23 -22
- data/ext/config.h +21 -11
- data/ext/document.c +43 -40
- data/ext/document.h +31 -21
- data/ext/except.c +20 -38
- data/ext/except.h +89 -76
- data/ext/extconf.rb +3 -2
- data/ext/ferret.c +49 -35
- data/ext/ferret.h +14 -11
- data/ext/field_index.c +262 -0
- data/ext/field_index.h +52 -0
- data/ext/filter.c +11 -10
- data/ext/fs_store.c +65 -47
- data/ext/global.c +245 -165
- data/ext/global.h +252 -54
- data/ext/hash.c +200 -243
- data/ext/hash.h +205 -163
- data/ext/hashset.c +118 -96
- data/ext/hashset.h +110 -82
- data/ext/header.h +19 -19
- data/ext/helper.c +11 -10
- data/ext/helper.h +14 -6
- data/ext/index.c +745 -366
- data/ext/index.h +503 -529
- data/ext/internal.h +1020 -0
- data/ext/lang.c +10 -0
- data/ext/lang.h +35 -15
- data/ext/mempool.c +5 -4
- data/ext/mempool.h +30 -22
- data/ext/modules.h +35 -7
- data/ext/multimapper.c +43 -2
- data/ext/multimapper.h +32 -23
- data/ext/posh.c +0 -0
- data/ext/posh.h +4 -38
- data/ext/priorityqueue.c +10 -12
- data/ext/priorityqueue.h +33 -21
- data/ext/q_boolean.c +22 -9
- data/ext/q_const_score.c +3 -2
- data/ext/q_filtered_query.c +15 -12
- data/ext/q_fuzzy.c +147 -135
- data/ext/q_match_all.c +3 -2
- data/ext/q_multi_term.c +28 -32
- data/ext/q_parser.c +451 -173
- data/ext/q_phrase.c +158 -79
- data/ext/q_prefix.c +16 -18
- data/ext/q_range.c +363 -31
- data/ext/q_span.c +130 -141
- data/ext/q_term.c +21 -21
- data/ext/q_wildcard.c +19 -23
- data/ext/r_analysis.c +369 -242
- data/ext/r_index.c +421 -434
- data/ext/r_qparser.c +142 -92
- data/ext/r_search.c +790 -407
- data/ext/r_store.c +44 -44
- data/ext/r_utils.c +264 -96
- data/ext/ram_store.c +29 -23
- data/ext/scanner.c +895 -0
- data/ext/scanner.h +36 -0
- data/ext/scanner_mb.c +6701 -0
- data/ext/scanner_utf8.c +4415 -0
- data/ext/search.c +210 -87
- data/ext/search.h +556 -488
- data/ext/similarity.c +17 -16
- data/ext/similarity.h +51 -44
- data/ext/sort.c +157 -354
- data/ext/stem_ISO_8859_1_hungarian.h +16 -0
- data/ext/stem_ISO_8859_2_romanian.h +16 -0
- data/ext/stem_UTF_8_hungarian.h +16 -0
- data/ext/stem_UTF_8_romanian.h +16 -0
- data/ext/stem_UTF_8_turkish.h +16 -0
- data/ext/stopwords.c +287 -278
- data/ext/store.c +57 -51
- data/ext/store.h +308 -286
- data/ext/symbol.c +10 -0
- data/ext/symbol.h +23 -0
- data/ext/term_vectors.c +14 -293
- data/ext/threading.h +22 -22
- data/ext/win32.h +12 -4
- data/lib/ferret.rb +2 -1
- data/lib/ferret/browser.rb +1 -1
- data/lib/ferret/field_symbol.rb +94 -0
- data/lib/ferret/index.rb +221 -34
- data/lib/ferret/number_tools.rb +6 -6
- data/lib/ferret/version.rb +3 -0
- data/test/{unit → long_running}/largefile/tc_largefile.rb +1 -1
- data/test/test_helper.rb +7 -2
- data/test/test_installed.rb +1 -0
- data/test/threading/thread_safety_index_test.rb +10 -1
- data/test/threading/thread_safety_read_write_test.rb +4 -7
- data/test/threading/thread_safety_test.rb +0 -0
- data/test/unit/analysis/tc_analyzer.rb +29 -27
- data/test/unit/analysis/tc_token_stream.rb +23 -16
- data/test/unit/index/tc_index.rb +116 -11
- data/test/unit/index/tc_index_reader.rb +27 -27
- data/test/unit/index/tc_index_writer.rb +10 -0
- data/test/unit/index/th_doc.rb +38 -21
- data/test/unit/search/tc_filter.rb +31 -10
- data/test/unit/search/tc_index_searcher.rb +6 -0
- data/test/unit/search/tm_searcher.rb +53 -1
- data/test/unit/store/tc_fs_store.rb +40 -2
- data/test/unit/store/tc_ram_store.rb +0 -0
- data/test/unit/store/tm_store.rb +0 -0
- data/test/unit/store/tm_store_lock.rb +7 -6
- data/test/unit/tc_field_symbol.rb +26 -0
- data/test/unit/ts_analysis.rb +0 -0
- data/test/unit/ts_index.rb +0 -0
- data/test/unit/ts_store.rb +0 -0
- data/test/unit/ts_utils.rb +0 -0
- data/test/unit/utils/tc_number_tools.rb +0 -0
- data/test/utils/content_generator.rb +226 -0
- metadata +262 -221
- data/ext/inc/lang.h +0 -48
- data/ext/inc/threading.h +0 -31
- data/ext/stem_ISO_8859_1_english.c +0 -1156
- data/ext/stem_ISO_8859_1_french.c +0 -1276
- data/ext/stem_ISO_8859_1_italian.c +0 -1091
- data/ext/stem_ISO_8859_1_norwegian.c +0 -296
- data/ext/stem_ISO_8859_1_spanish.c +0 -1119
- data/ext/stem_ISO_8859_1_swedish.c +0 -307
- data/ext/stem_UTF_8_danish.c +0 -344
- data/ext/stem_UTF_8_english.c +0 -1176
- data/ext/stem_UTF_8_french.c +0 -1296
- data/ext/stem_UTF_8_italian.c +0 -1113
- data/ext/stem_UTF_8_norwegian.c +0 -302
- data/ext/stem_UTF_8_portuguese.c +0 -1055
- data/ext/stem_UTF_8_russian.c +0 -709
- data/ext/stem_UTF_8_spanish.c +0 -1137
- data/ext/stem_UTF_8_swedish.c +0 -313
- data/lib/ferret_version.rb +0 -3
data/ext/similarity.c
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
#include "search.h"
|
3
3
|
#include "array.h"
|
4
4
|
#include "helper.h"
|
5
|
+
#include "symbol.h"
|
5
6
|
#include <math.h>
|
6
7
|
#include <stdlib.h>
|
7
8
|
#include <string.h>
|
9
|
+
#include "internal.h"
|
8
10
|
|
9
11
|
/****************************************************************************
|
10
12
|
*
|
@@ -12,10 +14,10 @@
|
|
12
14
|
*
|
13
15
|
****************************************************************************/
|
14
16
|
|
15
|
-
Term *term_new(
|
17
|
+
Term *term_new(Symbol field, const char *text)
|
16
18
|
{
|
17
19
|
Term *t = ALLOC(Term);
|
18
|
-
t->field =
|
20
|
+
t->field = field;
|
19
21
|
t->text = estrdup(text);
|
20
22
|
return t;
|
21
23
|
}
|
@@ -23,19 +25,18 @@ Term *term_new(const char *field, const char *text)
|
|
23
25
|
void term_destroy(Term *self)
|
24
26
|
{
|
25
27
|
free(self->text);
|
26
|
-
free(self->field);
|
27
28
|
free(self);
|
28
29
|
}
|
29
30
|
|
30
31
|
int term_eq(const void *t1, const void *t2)
|
31
32
|
{
|
32
33
|
return (strcmp(((Term *)t1)->text, ((Term *)t2)->text)) == 0 &&
|
33
|
-
(
|
34
|
+
(((Term *)t1)->field == ((Term *)t2)->field);
|
34
35
|
}
|
35
36
|
|
36
37
|
unsigned long term_hash(const void *t)
|
37
38
|
{
|
38
|
-
return str_hash(((Term *)t)->text) *
|
39
|
+
return str_hash(((Term *)t)->text) * sym_hash(((Term *)t)->field);
|
39
40
|
}
|
40
41
|
|
41
42
|
/****************************************************************************
|
@@ -44,39 +45,39 @@ unsigned long term_hash(const void *t)
|
|
44
45
|
*
|
45
46
|
****************************************************************************/
|
46
47
|
|
47
|
-
float simdef_length_norm(Similarity *s,
|
48
|
+
static float simdef_length_norm(Similarity *s, Symbol field, int num_terms)
|
48
49
|
{
|
49
50
|
(void)s;
|
50
51
|
(void)field;
|
51
52
|
return (float)(1.0 / sqrt(num_terms));
|
52
53
|
}
|
53
54
|
|
54
|
-
float simdef_query_norm(struct Similarity *s, float sum_of_squared_weights)
|
55
|
+
static float simdef_query_norm(struct Similarity *s, float sum_of_squared_weights)
|
55
56
|
{
|
56
57
|
(void)s;
|
57
58
|
return (float)(1.0 / sqrt(sum_of_squared_weights));
|
58
59
|
}
|
59
60
|
|
60
|
-
float simdef_tf(struct Similarity *s, float freq)
|
61
|
+
static float simdef_tf(struct Similarity *s, float freq)
|
61
62
|
{
|
62
63
|
(void)s;
|
63
64
|
return (float)sqrt(freq);
|
64
65
|
}
|
65
66
|
|
66
|
-
float simdef_sloppy_freq(struct Similarity *s, int distance)
|
67
|
+
static float simdef_sloppy_freq(struct Similarity *s, int distance)
|
67
68
|
{
|
68
69
|
(void)s;
|
69
70
|
return (float)(1.0 / (double)(distance + 1));
|
70
71
|
}
|
71
72
|
|
72
|
-
float simdef_idf_term(struct Similarity *s,
|
73
|
+
static float simdef_idf_term(struct Similarity *s, Symbol field, char *term,
|
73
74
|
Searcher *searcher)
|
74
75
|
{
|
75
76
|
return s->idf(s, searcher->doc_freq(searcher, field, term),
|
76
77
|
searcher->max_doc(searcher));
|
77
78
|
}
|
78
79
|
|
79
|
-
float simdef_idf_phrase(struct Similarity *s,
|
80
|
+
static float simdef_idf_phrase(struct Similarity *s, Symbol field,
|
80
81
|
PhrasePosition *positions,
|
81
82
|
int pp_cnt, Searcher *searcher)
|
82
83
|
{
|
@@ -91,30 +92,30 @@ float simdef_idf_phrase(struct Similarity *s, const char *field,
|
|
91
92
|
return idf;
|
92
93
|
}
|
93
94
|
|
94
|
-
float simdef_idf(struct Similarity *s, int doc_freq, int num_docs)
|
95
|
+
static float simdef_idf(struct Similarity *s, int doc_freq, int num_docs)
|
95
96
|
{
|
96
97
|
(void)s;
|
97
98
|
return (float)(log((float)num_docs/(float)(doc_freq+1)) + 1.0);
|
98
99
|
}
|
99
100
|
|
100
|
-
float simdef_coord(struct Similarity *s, int overlap, int max_overlap)
|
101
|
+
static float simdef_coord(struct Similarity *s, int overlap, int max_overlap)
|
101
102
|
{
|
102
103
|
(void)s;
|
103
104
|
return (float)((double)overlap / (double)max_overlap);
|
104
105
|
}
|
105
106
|
|
106
|
-
float simdef_decode_norm(struct Similarity *s, uchar b)
|
107
|
+
static float simdef_decode_norm(struct Similarity *s, uchar b)
|
107
108
|
{
|
108
109
|
return s->norm_table[b];
|
109
110
|
}
|
110
111
|
|
111
|
-
uchar simdef_encode_norm(struct Similarity *s, float f)
|
112
|
+
static uchar simdef_encode_norm(struct Similarity *s, float f)
|
112
113
|
{
|
113
114
|
(void)s;
|
114
115
|
return float2byte(f);
|
115
116
|
}
|
116
117
|
|
117
|
-
void simdef_destroy(Similarity *s)
|
118
|
+
static void simdef_destroy(Similarity *s)
|
118
119
|
{
|
119
120
|
(void)s;
|
120
121
|
/* nothing to do here */
|
data/ext/similarity.h
CHANGED
@@ -1,82 +1,89 @@
|
|
1
1
|
#ifndef FRT_SIMILARITY_H
|
2
2
|
#define FRT_SIMILARITY_H
|
3
3
|
|
4
|
-
|
4
|
+
#ifdef __cplusplus
|
5
|
+
extern "C" {
|
6
|
+
#endif
|
7
|
+
|
8
|
+
#include "symbol.h"
|
9
|
+
|
10
|
+
typedef struct FrtSearcher FrtSearcher;
|
5
11
|
|
6
12
|
/****************************************************************************
|
7
13
|
*
|
8
|
-
*
|
14
|
+
* FrtTerm
|
9
15
|
*
|
10
16
|
****************************************************************************/
|
11
17
|
|
12
|
-
|
13
|
-
hs_new((hash_ft)&term_hash, (eq_ft)&term_eq, (free_ft)&term_destroy)
|
14
|
-
|
15
|
-
typedef struct Term
|
18
|
+
typedef struct FrtTerm
|
16
19
|
{
|
17
|
-
|
20
|
+
FrtSymbol field;
|
18
21
|
char *text;
|
19
|
-
}
|
22
|
+
} FrtTerm;
|
20
23
|
|
21
|
-
extern
|
22
|
-
extern void
|
23
|
-
extern int
|
24
|
-
extern unsigned long
|
24
|
+
extern FrtTerm *frt_term_new(FrtSymbol field, const char *text);
|
25
|
+
extern void frt_term_destroy(FrtTerm *self);
|
26
|
+
extern int frt_term_eq(const void *t1, const void *t2);
|
27
|
+
extern unsigned long frt_term_hash(const void *t);
|
25
28
|
|
26
29
|
/***************************************************************************
|
27
30
|
*
|
28
|
-
*
|
31
|
+
* FrtPhrasePosition
|
29
32
|
*
|
30
33
|
***************************************************************************/
|
31
34
|
|
32
|
-
typedef struct
|
35
|
+
typedef struct FrtPhrasePosition
|
33
36
|
{
|
34
37
|
int pos;
|
35
38
|
char **terms;
|
36
|
-
}
|
39
|
+
} FrtPhrasePosition;
|
37
40
|
|
38
41
|
/***************************************************************************
|
39
42
|
*
|
40
|
-
*
|
43
|
+
* FrtSimilarity
|
41
44
|
*
|
42
45
|
***************************************************************************/
|
43
46
|
|
44
|
-
typedef struct
|
47
|
+
typedef struct FrtSimilarity FrtSimilarity;
|
45
48
|
|
46
|
-
struct
|
49
|
+
struct FrtSimilarity
|
47
50
|
{
|
48
51
|
void *data;
|
49
52
|
float norm_table[256];
|
50
|
-
float (*length_norm)(
|
51
|
-
float (*query_norm)(
|
52
|
-
float (*tf)(
|
53
|
-
float (*sloppy_freq)(
|
54
|
-
float (*idf_term)(
|
55
|
-
|
56
|
-
float (*idf_phrase)(
|
57
|
-
|
58
|
-
int pp_cnt,
|
59
|
-
float (*idf)(
|
60
|
-
float (*coord)(
|
61
|
-
float (*decode_norm)(
|
62
|
-
unsigned char (*encode_norm)(
|
63
|
-
void (*destroy)(
|
53
|
+
float (*length_norm)(FrtSimilarity *self, FrtSymbol field, int num_terms);
|
54
|
+
float (*query_norm)(FrtSimilarity *self, float sum_of_squared_weights);
|
55
|
+
float (*tf)(FrtSimilarity *self, float freq);
|
56
|
+
float (*sloppy_freq)(FrtSimilarity *self, int distance);
|
57
|
+
float (*idf_term)(FrtSimilarity *self, FrtSymbol field, char *term,
|
58
|
+
FrtSearcher *searcher);
|
59
|
+
float (*idf_phrase)(FrtSimilarity *self, FrtSymbol field,
|
60
|
+
FrtPhrasePosition *positions,
|
61
|
+
int pp_cnt, FrtSearcher *searcher);
|
62
|
+
float (*idf)(FrtSimilarity *self, int doc_freq, int num_docs);
|
63
|
+
float (*coord)(FrtSimilarity *self, int overlap, int max_overlap);
|
64
|
+
float (*decode_norm)(FrtSimilarity *self, unsigned char b);
|
65
|
+
unsigned char (*encode_norm)(FrtSimilarity *self, float f);
|
66
|
+
void (*destroy)(FrtSimilarity *self);
|
64
67
|
};
|
65
68
|
|
66
|
-
#define
|
67
|
-
#define
|
68
|
-
#define
|
69
|
-
#define
|
70
|
-
#define
|
69
|
+
#define frt_sim_length_norm(msim, field, num_terms) msim->length_norm(msim, field, num_terms)
|
70
|
+
#define frt_sim_query_norm(msim, sosw) msim->query_norm(msim, sosw)
|
71
|
+
#define frt_sim_tf(msim, freq) msim->tf(msim, freq)
|
72
|
+
#define frt_sim_sloppy_freq(msim, distance) msim->sloppy_freq(msim, distance)
|
73
|
+
#define frt_sim_idf_term(msim, field, term, searcher)\
|
71
74
|
msim->idf_term(msim, field, term, searcher)
|
72
|
-
#define
|
75
|
+
#define frt_sim_idf_phrase(msim, field, positions, pos_cnt, searcher)\
|
73
76
|
msim->idf_phrase(msim, field, positions, pos_cnt, searcher)
|
74
|
-
#define
|
75
|
-
#define
|
76
|
-
#define
|
77
|
-
#define
|
78
|
-
#define
|
77
|
+
#define frt_sim_idf(msim, doc_freq, num_docs) msim->idf(msim, doc_freq, num_docs)
|
78
|
+
#define frt_sim_coord(msim, overlap, max_overlap) msim->coord(msim, overlap, max_overlap)
|
79
|
+
#define frt_sim_decode_norm(msim, b) msim->decode_norm(msim, b)
|
80
|
+
#define frt_sim_encode_norm(msim, f) msim->encode_norm(msim, f)
|
81
|
+
#define frt_sim_destroy(msim) msim->destroy(msim)
|
82
|
+
|
83
|
+
FrtSimilarity *frt_sim_create_default();
|
79
84
|
|
80
|
-
|
85
|
+
#ifdef __cplusplus
|
86
|
+
} // extern "C"
|
87
|
+
#endif
|
81
88
|
|
82
89
|
#endif
|
data/ext/sort.c
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
#include <string.h>
|
2
2
|
#include "search.h"
|
3
3
|
#include "index.h"
|
4
|
+
#include "field_index.h"
|
5
|
+
#include "symbol.h"
|
6
|
+
#include "internal.h"
|
4
7
|
|
5
8
|
/***************************************************************************
|
6
9
|
*
|
@@ -8,53 +11,24 @@
|
|
8
11
|
*
|
9
12
|
***************************************************************************/
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
static int sort_field_cache_eq(const void *p1, const void *p2)
|
26
|
-
{
|
27
|
-
SortField *key1 = (SortField *)p1;
|
28
|
-
SortField *key2 = (SortField *)p2;
|
29
|
-
int equal = (strcmp(key1->field, key2->field) == 0)
|
30
|
-
&& key1->type == key2->type;
|
31
|
-
|
32
|
-
return equal;
|
33
|
-
}
|
34
|
-
|
35
|
-
static SortField *sort_field_clone(SortField *self)
|
36
|
-
{
|
37
|
-
SortField *clone = ALLOC(SortField);
|
38
|
-
memcpy(clone, self, sizeof(SortField));
|
39
|
-
mutex_init(&clone->mutex, NULL);
|
40
|
-
clone->field = estrdup(self->field);
|
41
|
-
return clone;
|
42
|
-
}
|
43
|
-
|
44
|
-
static SortField *sort_field_alloc(char *field, int type, bool reverse)
|
45
|
-
{
|
46
|
-
SortField *self = ALLOC(SortField);
|
47
|
-
mutex_init(&self->mutex, NULL);
|
48
|
-
self->field = field ? estrdup(field) : NULL;
|
49
|
-
self->type = type;
|
50
|
-
self->reverse = reverse;
|
51
|
-
self->index = NULL;
|
52
|
-
self->destroy_index = &free;
|
53
|
-
self->compare = NULL;
|
14
|
+
static INLINE SortField *sort_field_alloc(Symbol field,
|
15
|
+
SortType type,
|
16
|
+
bool reverse,
|
17
|
+
int (*compare)(void *index_ptr, Hit *hit1, Hit *hit2),
|
18
|
+
void (*get_val)(void *index_ptr, Hit *hit1, Comparable *comparable),
|
19
|
+
const FieldIndexClass *field_index_class)
|
20
|
+
{
|
21
|
+
SortField *self = ALLOC(SortField);
|
22
|
+
self->field = field;
|
23
|
+
self->type = type;
|
24
|
+
self->reverse = reverse;
|
25
|
+
self->field_index_class = field_index_class;
|
26
|
+
self->compare = compare;
|
27
|
+
self->get_val = get_val;
|
54
28
|
return self;
|
55
29
|
}
|
56
30
|
|
57
|
-
SortField *sort_field_new(
|
31
|
+
SortField *sort_field_new(Symbol field, SortType type, bool reverse)
|
58
32
|
{
|
59
33
|
SortField *sf = NULL;
|
60
34
|
switch (type) {
|
@@ -85,12 +59,6 @@ SortField *sort_field_new(char *field, enum SORT_TYPE type, bool reverse)
|
|
85
59
|
|
86
60
|
void sort_field_destroy(void *p)
|
87
61
|
{
|
88
|
-
SortField *self = (SortField *)p;
|
89
|
-
if (self->index) {
|
90
|
-
self->destroy_index(self->index);
|
91
|
-
}
|
92
|
-
free(self->field);
|
93
|
-
mutex_destroy(&self->mutex);
|
94
62
|
free(p);
|
95
63
|
}
|
96
64
|
|
@@ -125,10 +93,11 @@ char *sort_field_to_s(SortField *self)
|
|
125
93
|
break;
|
126
94
|
}
|
127
95
|
if (self->field) {
|
128
|
-
str = ALLOC_N(char,
|
129
|
-
sprintf(str, "%s:%s%s", self->field, type, (self->reverse ? "!" : ""));
|
130
|
-
}
|
131
|
-
|
96
|
+
str = ALLOC_N(char, 3 + sym_len(self->field) + strlen(type));
|
97
|
+
sprintf(str, "%s:%s%s", S(self->field), type, (self->reverse ? "!" : ""));
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
str = ALLOC_N(char, 2 + strlen(type));
|
132
101
|
sprintf(str, "%s%s", type, (self->reverse ? "!" : ""));
|
133
102
|
}
|
134
103
|
return str;
|
@@ -138,13 +107,13 @@ char *sort_field_to_s(SortField *self)
|
|
138
107
|
* ScoreSortField
|
139
108
|
***************************************************************************/
|
140
109
|
|
141
|
-
void sf_score_get_val(void *index, Hit *hit, Comparable *comparable)
|
110
|
+
static void sf_score_get_val(void *index, Hit *hit, Comparable *comparable)
|
142
111
|
{
|
143
112
|
(void)index;
|
144
113
|
comparable->val.f = hit->score;
|
145
114
|
}
|
146
115
|
|
147
|
-
int sf_score_compare(void *index_ptr, Hit *hit2, Hit *hit1)
|
116
|
+
static int sf_score_compare(void *index_ptr, Hit *hit2, Hit *hit1)
|
148
117
|
{
|
149
118
|
float val1 = hit1->score;
|
150
119
|
float val2 = hit2->score;
|
@@ -157,49 +126,39 @@ int sf_score_compare(void *index_ptr, Hit *hit2, Hit *hit1)
|
|
157
126
|
|
158
127
|
SortField *sort_field_score_new(bool reverse)
|
159
128
|
{
|
160
|
-
|
161
|
-
|
162
|
-
self->get_val = &sf_score_get_val;
|
163
|
-
return self;
|
129
|
+
return sort_field_alloc(NULL, SORT_TYPE_SCORE, reverse,
|
130
|
+
&sf_score_compare, &sf_score_get_val, NULL);
|
164
131
|
}
|
165
132
|
|
166
133
|
const SortField SORT_FIELD_SCORE = {
|
167
|
-
|
134
|
+
NULL, /* field_index_class */
|
168
135
|
NULL, /* field */
|
169
136
|
SORT_TYPE_SCORE, /* type */
|
170
137
|
false, /* reverse */
|
171
|
-
NULL, /* index */
|
172
138
|
&sf_score_compare, /* compare */
|
173
139
|
&sf_score_get_val, /* get_val */
|
174
|
-
NULL, /* create_index */
|
175
|
-
NULL, /* destroy_index */
|
176
|
-
NULL, /* handle_term */
|
177
140
|
};
|
178
141
|
|
179
142
|
const SortField SORT_FIELD_SCORE_REV = {
|
180
|
-
|
143
|
+
NULL, /* field_index_class */
|
181
144
|
NULL, /* field */
|
182
145
|
SORT_TYPE_SCORE, /* type */
|
183
146
|
true, /* reverse */
|
184
|
-
NULL, /* index */
|
185
147
|
&sf_score_compare, /* compare */
|
186
148
|
&sf_score_get_val, /* get_val */
|
187
|
-
NULL, /* create_index */
|
188
|
-
NULL, /* destroy_index */
|
189
|
-
NULL, /* handle_term */
|
190
149
|
};
|
191
150
|
|
192
151
|
/**************************************************************************
|
193
152
|
* DocSortField
|
194
153
|
***************************************************************************/
|
195
154
|
|
196
|
-
void sf_doc_get_val(void *index, Hit *hit, Comparable *comparable)
|
155
|
+
static void sf_doc_get_val(void *index, Hit *hit, Comparable *comparable)
|
197
156
|
{
|
198
157
|
(void)index;
|
199
|
-
comparable->val.
|
158
|
+
comparable->val.l = hit->doc;
|
200
159
|
}
|
201
160
|
|
202
|
-
int sf_doc_compare(void *index_ptr, Hit *hit1, Hit *hit2)
|
161
|
+
static int sf_doc_compare(void *index_ptr, Hit *hit1, Hit *hit2)
|
203
162
|
{
|
204
163
|
int val1 = hit1->doc;
|
205
164
|
int val2 = hit2->doc;
|
@@ -212,36 +171,26 @@ int sf_doc_compare(void *index_ptr, Hit *hit1, Hit *hit2)
|
|
212
171
|
|
213
172
|
SortField *sort_field_doc_new(bool reverse)
|
214
173
|
{
|
215
|
-
|
216
|
-
|
217
|
-
self->get_val = &sf_doc_get_val;
|
218
|
-
return self;
|
174
|
+
return sort_field_alloc(NULL, SORT_TYPE_DOC, reverse,
|
175
|
+
&sf_doc_compare, &sf_doc_get_val, NULL);
|
219
176
|
}
|
220
177
|
|
221
178
|
const SortField SORT_FIELD_DOC = {
|
222
|
-
|
179
|
+
NULL, /* field_index_class */
|
223
180
|
NULL, /* field */
|
224
181
|
SORT_TYPE_DOC, /* type */
|
225
182
|
false, /* reverse */
|
226
|
-
NULL, /* index */
|
227
183
|
&sf_doc_compare, /* compare */
|
228
184
|
&sf_doc_get_val, /* get_val */
|
229
|
-
NULL, /* create_index */
|
230
|
-
NULL, /* destroy_index */
|
231
|
-
NULL, /* handle_term */
|
232
185
|
};
|
233
186
|
|
234
187
|
const SortField SORT_FIELD_DOC_REV = {
|
235
|
-
|
188
|
+
NULL, /* field_index_class */
|
236
189
|
NULL, /* field */
|
237
190
|
SORT_TYPE_DOC, /* type */
|
238
191
|
true, /* reverse */
|
239
|
-
NULL, /* index */
|
240
192
|
&sf_doc_compare, /* compare */
|
241
193
|
&sf_doc_get_val, /* get_val */
|
242
|
-
NULL, /* create_index */
|
243
|
-
NULL, /* destroy_index */
|
244
|
-
NULL, /* handle_term */
|
245
194
|
};
|
246
195
|
|
247
196
|
/***************************************************************************
|
@@ -250,117 +199,60 @@ const SortField SORT_FIELD_DOC_REV = {
|
|
250
199
|
|
251
200
|
static void sf_byte_get_val(void *index, Hit *hit, Comparable *comparable)
|
252
201
|
{
|
253
|
-
comparable->val.
|
202
|
+
comparable->val.l = ((long *)index)[hit->doc];
|
254
203
|
}
|
255
204
|
|
256
205
|
static int sf_byte_compare(void *index, Hit *hit1, Hit *hit2)
|
257
206
|
{
|
258
|
-
|
259
|
-
|
207
|
+
long val1 = ((long *)index)[hit1->doc];
|
208
|
+
long val2 = ((long *)index)[hit2->doc];
|
260
209
|
if (val1 > val2) return 1;
|
261
210
|
else if (val1 < val2) return -1;
|
262
211
|
else return 0;
|
263
212
|
}
|
264
213
|
|
265
|
-
|
266
|
-
{
|
267
|
-
int *index = ALLOC_AND_ZERO_N(int, size + 1);
|
268
|
-
index[0]++;
|
269
|
-
return &index[1];
|
270
|
-
}
|
271
|
-
|
272
|
-
static void sf_byte_destroy_index(void *p)
|
273
|
-
{
|
274
|
-
int *index = (int *)p;
|
275
|
-
free(&index[-1]);
|
276
|
-
}
|
277
|
-
|
278
|
-
static void sf_byte_handle_term(void *index_ptr, TermDocEnum *tde, char *text)
|
214
|
+
SortField *sort_field_byte_new(Symbol field, bool reverse)
|
279
215
|
{
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
while (tde->next(tde)) {
|
284
|
-
index[tde->doc_num(tde)] = val;
|
285
|
-
}
|
286
|
-
}
|
287
|
-
|
288
|
-
static void sort_field_byte_methods(SortField *self)
|
289
|
-
{
|
290
|
-
self->type = SORT_TYPE_BYTE;
|
291
|
-
self->compare = &sf_byte_compare;
|
292
|
-
self->get_val = &sf_byte_get_val;
|
293
|
-
self->create_index = &sf_byte_create_index;
|
294
|
-
self->destroy_index = &sf_byte_destroy_index;
|
295
|
-
self->handle_term = &sf_byte_handle_term;
|
296
|
-
}
|
297
|
-
|
298
|
-
SortField *sort_field_byte_new(char *field, bool reverse)
|
299
|
-
{
|
300
|
-
SortField *self = sort_field_alloc(field, SORT_TYPE_BYTE, reverse);
|
301
|
-
sort_field_byte_methods(self);
|
302
|
-
return self;
|
216
|
+
return sort_field_alloc(field, SORT_TYPE_BYTE, reverse,
|
217
|
+
&sf_byte_compare, &sf_byte_get_val,
|
218
|
+
&BYTE_FIELD_INDEX_CLASS);
|
303
219
|
}
|
304
220
|
|
305
221
|
/***************************************************************************
|
306
222
|
* IntegerSortField
|
307
223
|
***************************************************************************/
|
308
224
|
|
309
|
-
void sf_int_get_val(void *index, Hit *hit, Comparable *comparable)
|
225
|
+
static void sf_int_get_val(void *index, Hit *hit, Comparable *comparable)
|
310
226
|
{
|
311
|
-
comparable->val.
|
227
|
+
comparable->val.l = ((long *)index)[hit->doc];
|
312
228
|
}
|
313
229
|
|
314
|
-
int sf_int_compare(void *index, Hit *hit1, Hit *hit2)
|
230
|
+
static int sf_int_compare(void *index, Hit *hit1, Hit *hit2)
|
315
231
|
{
|
316
|
-
|
317
|
-
|
232
|
+
long val1 = ((long *)index)[hit1->doc];
|
233
|
+
long val2 = ((long *)index)[hit2->doc];
|
318
234
|
if (val1 > val2) return 1;
|
319
235
|
else if (val1 < val2) return -1;
|
320
236
|
else return 0;
|
321
237
|
}
|
322
238
|
|
323
|
-
|
239
|
+
SortField *sort_field_int_new(Symbol field, bool reverse)
|
324
240
|
{
|
325
|
-
return
|
326
|
-
|
327
|
-
|
328
|
-
void sf_int_handle_term(void *index_ptr, TermDocEnum *tde, char *text)
|
329
|
-
{
|
330
|
-
int *index = (int *)index_ptr;
|
331
|
-
int val;
|
332
|
-
sscanf(text, "%d", &val);
|
333
|
-
while (tde->next(tde)) {
|
334
|
-
index[tde->doc_num(tde)] = val;
|
335
|
-
}
|
336
|
-
}
|
337
|
-
|
338
|
-
void sort_field_int_methods(SortField *self)
|
339
|
-
{
|
340
|
-
self->type = SORT_TYPE_INTEGER;
|
341
|
-
self->compare = &sf_int_compare;
|
342
|
-
self->get_val = &sf_int_get_val;
|
343
|
-
self->create_index = &sf_int_create_index;
|
344
|
-
self->handle_term = &sf_int_handle_term;
|
345
|
-
}
|
346
|
-
|
347
|
-
SortField *sort_field_int_new(char *field, bool reverse)
|
348
|
-
{
|
349
|
-
SortField *self = sort_field_alloc(field, SORT_TYPE_INTEGER, reverse);
|
350
|
-
sort_field_int_methods(self);
|
351
|
-
return self;
|
241
|
+
return sort_field_alloc(field, SORT_TYPE_INTEGER, reverse,
|
242
|
+
&sf_int_compare, &sf_int_get_val,
|
243
|
+
&INTEGER_FIELD_INDEX_CLASS);
|
352
244
|
}
|
353
245
|
|
354
246
|
/***************************************************************************
|
355
247
|
* FloatSortField
|
356
248
|
***************************************************************************/
|
357
249
|
|
358
|
-
void sf_float_get_val(void *index, Hit *hit, Comparable *comparable)
|
250
|
+
static void sf_float_get_val(void *index, Hit *hit, Comparable *comparable)
|
359
251
|
{
|
360
252
|
comparable->val.f = ((float *)index)[hit->doc];
|
361
253
|
}
|
362
254
|
|
363
|
-
int sf_float_compare(void *index, Hit *hit1, Hit *hit2)
|
255
|
+
static int sf_float_compare(void *index, Hit *hit1, Hit *hit2)
|
364
256
|
{
|
365
257
|
float val1 = ((float *)index)[hit1->doc];
|
366
258
|
float val2 = ((float *)index)[hit2->doc];
|
@@ -369,58 +261,25 @@ int sf_float_compare(void *index, Hit *hit1, Hit *hit2)
|
|
369
261
|
else return 0;
|
370
262
|
}
|
371
263
|
|
372
|
-
|
264
|
+
SortField *sort_field_float_new(Symbol field, bool reverse)
|
373
265
|
{
|
374
|
-
return
|
375
|
-
|
376
|
-
|
377
|
-
void sf_float_handle_term(void *index_ptr, TermDocEnum *tde, char *text)
|
378
|
-
{
|
379
|
-
float *index = (float *)index_ptr;
|
380
|
-
float val;
|
381
|
-
sscanf(text, "%g", &val);
|
382
|
-
while (tde->next(tde)) {
|
383
|
-
index[tde->doc_num(tde)] = val;
|
384
|
-
}
|
385
|
-
}
|
386
|
-
|
387
|
-
void sort_field_float_methods(SortField *self)
|
388
|
-
{
|
389
|
-
self->type = SORT_TYPE_FLOAT;
|
390
|
-
self->compare = &sf_float_compare;
|
391
|
-
self->get_val = &sf_float_get_val;
|
392
|
-
self->create_index = &sf_float_create_index;
|
393
|
-
self->handle_term = &sf_float_handle_term;
|
394
|
-
}
|
395
|
-
|
396
|
-
SortField *sort_field_float_new(char *field, bool reverse)
|
397
|
-
{
|
398
|
-
SortField *self = sort_field_alloc(field, SORT_TYPE_FLOAT, reverse);
|
399
|
-
sort_field_float_methods(self);
|
400
|
-
return self;
|
266
|
+
return sort_field_alloc(field, SORT_TYPE_FLOAT, reverse,
|
267
|
+
&sf_float_compare, &sf_float_get_val,
|
268
|
+
&FLOAT_FIELD_INDEX_CLASS);
|
401
269
|
}
|
402
270
|
|
403
271
|
/***************************************************************************
|
404
272
|
* StringSortField
|
405
273
|
***************************************************************************/
|
406
274
|
|
407
|
-
|
408
|
-
typedef struct StringIndex {
|
409
|
-
int size;
|
410
|
-
int *index;
|
411
|
-
char **values;
|
412
|
-
int v_size;
|
413
|
-
int v_capa;
|
414
|
-
} StringIndex;
|
415
|
-
|
416
|
-
void sf_string_get_val(void *index, Hit *hit, Comparable *comparable)
|
275
|
+
static void sf_string_get_val(void *index, Hit *hit, Comparable *comparable)
|
417
276
|
{
|
418
277
|
comparable->val.s
|
419
278
|
= ((StringIndex *)index)->values[
|
420
279
|
((StringIndex *)index)->index[hit->doc]];
|
421
280
|
}
|
422
281
|
|
423
|
-
int sf_string_compare(void *index, Hit *hit1, Hit *hit2)
|
282
|
+
static int sf_string_compare(void *index, Hit *hit1, Hit *hit2)
|
424
283
|
{
|
425
284
|
char *s1 = ((StringIndex *)index)->values[
|
426
285
|
((StringIndex *)index)->index[hit1->doc]];
|
@@ -447,151 +306,20 @@ int sf_string_compare(void *index, Hit *hit1, Hit *hit2)
|
|
447
306
|
*/
|
448
307
|
}
|
449
308
|
|
450
|
-
|
309
|
+
SortField *sort_field_string_new(Symbol field, bool reverse)
|
451
310
|
{
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
self->v_capa = VALUES_ARRAY_START_SIZE;
|
456
|
-
self->v_size = 1; /* leave the first value as NULL */
|
457
|
-
self->values = ALLOC_AND_ZERO_N(char *, VALUES_ARRAY_START_SIZE);
|
458
|
-
return self;
|
459
|
-
}
|
460
|
-
|
461
|
-
void sf_string_destroy_index(void *p)
|
462
|
-
{
|
463
|
-
StringIndex *self = (StringIndex *)p;
|
464
|
-
int i;
|
465
|
-
free(self->index);
|
466
|
-
for (i = 0; i < self->v_size; i++) {
|
467
|
-
free(self->values[i]);
|
468
|
-
}
|
469
|
-
free(self->values);
|
470
|
-
free(self);
|
471
|
-
}
|
472
|
-
|
473
|
-
void sf_string_handle_term(void *index_ptr, TermDocEnum *tde, char *text)
|
474
|
-
{
|
475
|
-
StringIndex *index = (StringIndex *)index_ptr;
|
476
|
-
if (index->v_size >= index->v_capa) {
|
477
|
-
index->v_capa *= 2;
|
478
|
-
index->values = REALLOC_N(index->values, char *, index->v_capa);
|
479
|
-
}
|
480
|
-
index->values[index->v_size] = estrdup(text);
|
481
|
-
while (tde->next(tde)) {
|
482
|
-
index->index[tde->doc_num(tde)] = index->v_size;
|
483
|
-
}
|
484
|
-
index->v_size++;
|
485
|
-
}
|
486
|
-
|
487
|
-
void sort_field_string_methods(SortField *self)
|
488
|
-
{
|
489
|
-
self->type = SORT_TYPE_STRING;
|
490
|
-
self->compare = &sf_string_compare;
|
491
|
-
self->get_val = &sf_string_get_val;
|
492
|
-
self->create_index = &sf_string_create_index;
|
493
|
-
self->destroy_index = &sf_string_destroy_index;
|
494
|
-
self->handle_term = &sf_string_handle_term;
|
495
|
-
}
|
496
|
-
|
497
|
-
SortField *sort_field_string_new(char *field, bool reverse)
|
498
|
-
{
|
499
|
-
SortField *self = sort_field_alloc(field, SORT_TYPE_STRING, reverse);
|
500
|
-
sort_field_string_methods(self);
|
501
|
-
return self;
|
311
|
+
return sort_field_alloc(field, SORT_TYPE_STRING, reverse,
|
312
|
+
&sf_string_compare, &sf_string_get_val,
|
313
|
+
&STRING_FIELD_INDEX_CLASS);
|
502
314
|
}
|
503
315
|
|
504
316
|
/***************************************************************************
|
505
317
|
* AutoSortField
|
506
318
|
***************************************************************************/
|
507
319
|
|
508
|
-
|
320
|
+
SortField *sort_field_auto_new(Symbol field, bool reverse)
|
509
321
|
{
|
510
|
-
|
511
|
-
float float_val;
|
512
|
-
int text_len = 0, scan_len = 0;
|
513
|
-
|
514
|
-
text_len = (int)strlen(text);
|
515
|
-
sscanf(text, "%d%n", &int_val, &scan_len);
|
516
|
-
if (scan_len == text_len) {
|
517
|
-
sort_field_int_methods(sf);
|
518
|
-
} else {
|
519
|
-
sscanf(text, "%f%n", &float_val, &scan_len);
|
520
|
-
if (scan_len == text_len) {
|
521
|
-
sort_field_float_methods(sf);
|
522
|
-
} else {
|
523
|
-
sort_field_string_methods(sf);
|
524
|
-
}
|
525
|
-
}
|
526
|
-
}
|
527
|
-
|
528
|
-
SortField *sort_field_auto_new(char *field, bool reverse)
|
529
|
-
{
|
530
|
-
return sort_field_alloc(field, SORT_TYPE_AUTO, reverse);
|
531
|
-
}
|
532
|
-
|
533
|
-
/***************************************************************************
|
534
|
-
*
|
535
|
-
* FieldCache
|
536
|
-
*
|
537
|
-
***************************************************************************/
|
538
|
-
|
539
|
-
void *field_cache_get_index(IndexReader *ir, SortField *sf)
|
540
|
-
{
|
541
|
-
void *volatile index = NULL;
|
542
|
-
int length = 0;
|
543
|
-
TermEnum *volatile te = NULL;
|
544
|
-
TermDocEnum *volatile tde = NULL;
|
545
|
-
SortField *sf_clone;
|
546
|
-
const int field_num = fis_get_field_num(ir->fis, sf->field);
|
547
|
-
|
548
|
-
if (field_num < 0) {
|
549
|
-
RAISE(ARG_ERROR,
|
550
|
-
"Cannot sort by field \"%s\". It doesn't exist in the index.",
|
551
|
-
sf->field);
|
552
|
-
}
|
553
|
-
|
554
|
-
mutex_lock(&sf->mutex);
|
555
|
-
if (!ir->sort_cache) {
|
556
|
-
ir->sort_cache = h_new(&sort_field_hash, &sort_field_cache_eq,
|
557
|
-
&sort_field_destroy, NULL);
|
558
|
-
}
|
559
|
-
|
560
|
-
if (sf->type == SORT_TYPE_AUTO) {
|
561
|
-
te = ir->terms(ir, field_num);
|
562
|
-
if (!te->next(te) && (ir->num_docs(ir) > 0)) {
|
563
|
-
RAISE(ARG_ERROR,
|
564
|
-
"Cannot sort by field \"%s\" as there are no terms "
|
565
|
-
"in that field in the index.", sf->field);
|
566
|
-
}
|
567
|
-
sort_field_auto_evaluate(sf, te->curr_term);
|
568
|
-
te->close(te);
|
569
|
-
}
|
570
|
-
|
571
|
-
index = h_get(ir->sort_cache, sf);
|
572
|
-
|
573
|
-
if (index == NULL) {
|
574
|
-
length = ir->max_doc(ir);
|
575
|
-
if (length > 0) {
|
576
|
-
TRY
|
577
|
-
tde = ir->term_docs(ir);
|
578
|
-
te = ir->terms(ir, field_num);
|
579
|
-
index = sf->create_index(length);
|
580
|
-
while (te->next(te)) {
|
581
|
-
tde->seek_te(tde, te);
|
582
|
-
sf->handle_term(index, tde, te->curr_term);
|
583
|
-
}
|
584
|
-
XFINALLY
|
585
|
-
tde->close(tde);
|
586
|
-
te->close(te);
|
587
|
-
XENDTRY
|
588
|
-
}
|
589
|
-
sf_clone = sort_field_clone(sf);
|
590
|
-
sf_clone->index = index;
|
591
|
-
h_set(ir->sort_cache, sf_clone, index);
|
592
|
-
}
|
593
|
-
mutex_unlock(&sf->mutex);
|
594
|
-
return index;
|
322
|
+
return sort_field_alloc(field, SORT_TYPE_AUTO, reverse, NULL, NULL, NULL);
|
595
323
|
}
|
596
324
|
|
597
325
|
/***************************************************************************
|
@@ -610,7 +338,7 @@ typedef struct Comparator {
|
|
610
338
|
int (*compare)(void *index_ptr, Hit *hit1, Hit *hit2);
|
611
339
|
} Comparator;
|
612
340
|
|
613
|
-
Comparator *comparator_new(void *index, bool reverse,
|
341
|
+
static Comparator *comparator_new(void *index, bool reverse,
|
614
342
|
int (*compare)(void *index_ptr, Hit *hit1, Hit *hit2))
|
615
343
|
{
|
616
344
|
Comparator *self = ALLOC(Comparator);
|
@@ -630,17 +358,93 @@ typedef struct Sorter {
|
|
630
358
|
Sort *sort;
|
631
359
|
} Sorter;
|
632
360
|
|
633
|
-
|
361
|
+
#define SET_AUTO(upper_type, lower_type) \
|
362
|
+
sf->type = SORT_TYPE_ ## upper_type;\
|
363
|
+
sf->field_index_class = &upper_type ## _FIELD_INDEX_CLASS;\
|
364
|
+
sf->compare = sf_ ## lower_type ## _compare;\
|
365
|
+
sf->get_val = sf_ ## lower_type ## _get_val
|
366
|
+
|
367
|
+
static void sort_field_auto_evaluate(SortField *sf, char *text)
|
368
|
+
{
|
369
|
+
int int_val;
|
370
|
+
float float_val;
|
371
|
+
int text_len = 0, scan_len = 0;
|
372
|
+
|
373
|
+
text_len = (int)strlen(text);
|
374
|
+
sscanf(text, "%d%n", &int_val, &scan_len);
|
375
|
+
if (scan_len == text_len) {
|
376
|
+
SET_AUTO(INTEGER, int);
|
377
|
+
} else {
|
378
|
+
sscanf(text, "%f%n", &float_val, &scan_len);
|
379
|
+
if (scan_len == text_len) {
|
380
|
+
SET_AUTO(FLOAT, float);
|
381
|
+
} else {
|
382
|
+
SET_AUTO(STRING, string);
|
383
|
+
}
|
384
|
+
}
|
385
|
+
}
|
386
|
+
/*
|
387
|
+
static INLINE void set_auto(SortField *sf,
|
388
|
+
SortType type,
|
389
|
+
const FieldIndexClass *field_index_class,
|
390
|
+
int (*compare)(void *index_ptr, Hit *hit1, Hit *hit2),
|
391
|
+
void (*get_val)(void *index_ptr, Hit *hit1, Comparable *comparable))
|
392
|
+
{
|
393
|
+
sf->type = type;
|
394
|
+
sf->field_index_class = field_index_class;
|
395
|
+
sf->compare = compare;
|
396
|
+
sf->get_val = get_val;
|
397
|
+
}
|
398
|
+
|
399
|
+
static void sort_field_auto_evaluate(SortField *sf, char *text)
|
400
|
+
{
|
401
|
+
int int_val;
|
402
|
+
float float_val;
|
403
|
+
int text_len = 0, scan_len = 0;
|
404
|
+
|
405
|
+
text_len = (int)strlen(text);
|
406
|
+
sscanf(text, "%d%n", &int_val, &scan_len);
|
407
|
+
if (scan_len == text_len) {
|
408
|
+
set_auto(sf, SORT_TYPE_INTEGER, &INTEGER_FIELD_INDEX_CLASS,
|
409
|
+
sf_int_compare, sf_int_get_val);
|
410
|
+
} else {
|
411
|
+
sscanf(text, "%f%n", &float_val, &scan_len);
|
412
|
+
if (scan_len == text_len) {
|
413
|
+
set_auto(sf, SORT_TYPE_FLOAT, &FLOAT_FIELD_INDEX_CLASS,
|
414
|
+
sf_float_compare, sf_float_get_val);
|
415
|
+
} else {
|
416
|
+
set_auto(sf, SORT_TYPE_STRING, &STRING_FIELD_INDEX_CLASS,
|
417
|
+
sf_string_compare, sf_string_get_val);
|
418
|
+
}
|
419
|
+
}
|
420
|
+
}
|
421
|
+
*/
|
422
|
+
|
423
|
+
static Comparator *sorter_get_comparator(SortField *sf, IndexReader *ir)
|
634
424
|
{
|
635
425
|
void *index = NULL;
|
636
426
|
|
637
427
|
if (sf->type > SORT_TYPE_DOC) {
|
638
|
-
|
428
|
+
FieldIndex *field_index = NULL;
|
429
|
+
if (sf->type == SORT_TYPE_AUTO) {
|
430
|
+
TermEnum *te = ir_terms(ir, sf->field);
|
431
|
+
if (!te->next(te) && (ir->num_docs(ir) > 0)) {
|
432
|
+
RAISE(ARG_ERROR,
|
433
|
+
"Cannot sort by field \"%s\" as there are no terms "
|
434
|
+
"in that field in the index.", S(sf->field));
|
435
|
+
}
|
436
|
+
sort_field_auto_evaluate(sf, te->curr_term);
|
437
|
+
te->close(te);
|
438
|
+
}
|
439
|
+
mutex_lock(&ir->field_index_mutex);
|
440
|
+
field_index = field_index_get(ir, sf->field, sf->field_index_class);
|
441
|
+
mutex_unlock(&ir->field_index_mutex);
|
442
|
+
index = field_index->index;
|
639
443
|
}
|
640
444
|
return comparator_new(index, sf->reverse, sf->compare);
|
641
445
|
}
|
642
446
|
|
643
|
-
void sorter_destroy(Sorter *self)
|
447
|
+
static void sorter_destroy(Sorter *self)
|
644
448
|
{
|
645
449
|
int i;
|
646
450
|
|
@@ -651,7 +455,7 @@ void sorter_destroy(Sorter *self)
|
|
651
455
|
free(self);
|
652
456
|
}
|
653
457
|
|
654
|
-
Sorter *sorter_new(Sort *sort)
|
458
|
+
static Sorter *sorter_new(Sort *sort)
|
655
459
|
{
|
656
460
|
Sorter *self = ALLOC(Sorter);
|
657
461
|
self->c_cnt = sort->size;
|
@@ -664,7 +468,7 @@ Sorter *sorter_new(Sort *sort)
|
|
664
468
|
* FieldSortedHitQueue
|
665
469
|
***************************************************************************/
|
666
470
|
|
667
|
-
bool fshq_less_than(const void *hit1, const void *hit2)
|
471
|
+
static bool fshq_less_than(const void *hit1, const void *hit2)
|
668
472
|
{
|
669
473
|
int cmp = 0;
|
670
474
|
printf("Whoops, shouldn't call this.\n");
|
@@ -675,7 +479,7 @@ bool fshq_less_than(const void *hit1, const void *hit2)
|
|
675
479
|
}
|
676
480
|
}
|
677
481
|
|
678
|
-
INLINE bool fshq_lt(Sorter *sorter, Hit *hit1, Hit *hit2)
|
482
|
+
static INLINE bool fshq_lt(Sorter *sorter, Hit *hit1, Hit *hit2)
|
679
483
|
{
|
680
484
|
Comparator *comp;
|
681
485
|
int diff = 0, i;
|
@@ -734,7 +538,7 @@ Hit *fshq_pq_pop(PriorityQueue *pq)
|
|
734
538
|
}
|
735
539
|
}
|
736
540
|
|
737
|
-
INLINE void fshq_pq_up(PriorityQueue *pq)
|
541
|
+
static INLINE void fshq_pq_up(PriorityQueue *pq)
|
738
542
|
{
|
739
543
|
Hit **heap = (Hit **)pq->heap;
|
740
544
|
Hit *node;
|
@@ -772,7 +576,7 @@ void fshq_pq_insert(PriorityQueue *pq, Hit *hit)
|
|
772
576
|
|
773
577
|
void fshq_pq_destroy(PriorityQueue *self)
|
774
578
|
{
|
775
|
-
sorter_destroy(self->heap[0]);
|
579
|
+
sorter_destroy((Sorter *)self->heap[0]);
|
776
580
|
pq_destroy(self);
|
777
581
|
}
|
778
582
|
|
@@ -812,7 +616,7 @@ Hit *fshq_pq_pop_fd(PriorityQueue *pq)
|
|
812
616
|
fshq_pq_down(pq); /* adjust heap */
|
813
617
|
|
814
618
|
field_doc = (FieldDoc *)emalloc(sizeof(FieldDoc)
|
815
|
-
+ sizeof(Comparable)*cmp_cnt);
|
619
|
+
+ sizeof(Comparable) * cmp_cnt);
|
816
620
|
comparables = field_doc->comparables;
|
817
621
|
memcpy(field_doc, hit, sizeof(Hit));
|
818
622
|
field_doc->size = cmp_cnt;
|
@@ -864,12 +668,12 @@ bool fdshq_lt(FieldDoc *fd1, FieldDoc *fd2)
|
|
864
668
|
if (fd1->hit.doc < fd2->hit.doc) c = -1;
|
865
669
|
break;
|
866
670
|
case SORT_TYPE_INTEGER:
|
867
|
-
if (cmps1[i].val.
|
868
|
-
if (cmps1[i].val.
|
671
|
+
if (cmps1[i].val.l > cmps2[i].val.l) c = 1;
|
672
|
+
if (cmps1[i].val.l < cmps2[i].val.l) c = -1;
|
869
673
|
break;
|
870
674
|
case SORT_TYPE_BYTE:
|
871
|
-
if (cmps1[i].val.
|
872
|
-
if (cmps1[i].val.
|
675
|
+
if (cmps1[i].val.l > cmps2[i].val.l) c = 1;
|
676
|
+
if (cmps1[i].val.l < cmps2[i].val.l) c = -1;
|
873
677
|
break;
|
874
678
|
case SORT_TYPE_STRING:
|
875
679
|
do {
|
@@ -969,8 +773,7 @@ char *sort_to_s(Sort *self)
|
|
969
773
|
|
970
774
|
s = str + len;
|
971
775
|
for (i = 0; i < self->size; i++) {
|
972
|
-
sprintf(s, "%s, ", sf_strs[i]);
|
973
|
-
s += (int)strlen(s);
|
776
|
+
s += sprintf(s, "%s, ", sf_strs[i]);
|
974
777
|
free(sf_strs[i]);
|
975
778
|
}
|
976
779
|
free(sf_strs);
|