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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
*
* Distributable under the terms of either the Apache License (Version 2.0) or
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "DateFilter.h"
CL_NS_USE(index)
CL_NS_USE(util)
CL_NS_USE(document)
CL_NS_DEF(search)
DateFilter::~DateFilter(){
_CLDECDELETE( start );
_CLDECDELETE( end );
}
DateFilter::DateFilter(const DateFilter& copy):
start( _CL_POINTER(copy.start) ),
end ( _CL_POINTER(copy.end) )
{
}
/** Constructs a filter for field <code>f</code> matching times between
<code>from</code> and <code>to</code>. */
DateFilter::DateFilter(const TCHAR* f, int64_t from, int64_t to)
{
TCHAR* tmp = DateField::timeToString(from);
start = _CLNEW Term(f, tmp);
_CLDELETE_CARRAY(tmp);
tmp = DateField::timeToString(to);
end = _CLNEW Term(start, tmp);
_CLDELETE_CARRAY(tmp);
}
/** Constructs a filter for field <code>f</code> matching times before
<code>time</code>. */
DateFilter* DateFilter::Before(const TCHAR* field, int64_t time) {
return _CLNEW DateFilter(field, 0,time);
}
/** Constructs a filter for field <code>f</code> matching times after
<code>time</code>. */
DateFilter* DateFilter::After(const TCHAR* field, int64_t time) {
return _CLNEW DateFilter(field,time, DATEFIELD_DATE_MAX );
}
/** Returns a BitSet with true for documents which should be permitted in
search results, and false for those that should not. */
BitSet* DateFilter::bits(IndexReader* reader) {
BitSet* bts = _CLNEW BitSet(reader->maxDoc());
TermEnum* enumerator = reader->terms(start);
if (enumerator->term(false) == NULL){
_CLDELETE(enumerator);
return bts;
}
TermDocs* termDocs = reader->termDocs();
try {
while (enumerator->term(false)->compareTo(end) <= 0) {
termDocs->seek(enumerator->term(false));
while (termDocs->next()) {
bts->set(termDocs->doc());
}
if (!enumerator->next()) {
break;
}
}
} _CLFINALLY (
termDocs->close();
_CLDELETE(termDocs);
enumerator->close();
_CLDELETE(enumerator);
);
return bts;
}
Filter* DateFilter::clone() const{
return _CLNEW DateFilter(*this);
}
TCHAR* DateFilter::toString(){
size_t len = _tcslen(start->field()) + start->textLength() + end->textLength() + 8;
TCHAR* ret = _CL_NEWARRAY(TCHAR,len);
ret[0]=0;
_sntprintf(ret,len,_T("%s: [%s-%s]"), start->field(),start->text(),end->text());
return ret;
}
CL_NS_END
|