1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #include "CLucene/_ApiHeader.h"
8 #include "DateFilter.h"
9 #include "CLucene/document/DateField.h"
10 #include "CLucene/index/Term.h"
11 #include "CLucene/index/IndexReader.h"
12 #include "CLucene/index/Terms.h"
13 #include "CLucene/util/BitSet.h"
14
15 CL_NS_USE(index)
CL_NS_USE(util)16 CL_NS_USE(util)
17 CL_NS_USE(document)
18 CL_NS_DEF(search)
19
20 DateFilter::~DateFilter(){
21 _CLDECDELETE( start );
22 _CLDECDELETE( end );
23 }
24
DateFilter(const DateFilter & copy)25 DateFilter::DateFilter(const DateFilter& copy):
26 start( _CL_POINTER(copy.start) ),
27 end ( _CL_POINTER(copy.end) )
28 {
29 }
30
31 /** Constructs a filter for field <code>f</code> matching times between
32 <code>from</code> and <code>to</code>. */
DateFilter(const TCHAR * f,int64_t from,int64_t to)33 DateFilter::DateFilter(const TCHAR* f, int64_t from, int64_t to)
34 {
35 TCHAR* tmp = DateField::timeToString(from);
36 start = _CLNEW Term(f, tmp);
37 _CLDELETE_CARRAY(tmp);
38
39 tmp = DateField::timeToString(to);
40 end = _CLNEW Term(start, tmp);
41 _CLDELETE_CARRAY(tmp);
42 }
43
44 /** Constructs a filter for field <code>f</code> matching times before
45 <code>time</code>. */
Before(const TCHAR * field,int64_t time)46 DateFilter* DateFilter::Before(const TCHAR* field, int64_t time) {
47 return _CLNEW DateFilter(field, 0,time);
48 }
49
50 /** Constructs a filter for field <code>f</code> matching times after
51 <code>time</code>. */
After(const TCHAR * field,int64_t time)52 DateFilter* DateFilter::After(const TCHAR* field, int64_t time) {
53 return _CLNEW DateFilter(field,time, DATEFIELD_DATE_MAX );
54 }
55
56 /** Returns a BitSet with true for documents which should be permitted in
57 search results, and false for those that should not. */
bits(IndexReader * reader)58 BitSet* DateFilter::bits(IndexReader* reader) {
59 BitSet* bts = _CLNEW BitSet(reader->maxDoc());
60
61 TermEnum* enumerator = reader->terms(start);
62 if (enumerator->term(false) == NULL){
63 _CLDELETE(enumerator);
64 return bts;
65 }
66 TermDocs* termDocs = reader->termDocs();
67
68 try {
69 while (enumerator->term(false)->compareTo(end) <= 0) {
70 termDocs->seek(enumerator->term(false));
71 while (termDocs->next()) {
72 bts->set(termDocs->doc());
73 }
74 if (!enumerator->next()) {
75 break;
76 }
77 }
78 } _CLFINALLY (
79 termDocs->close();
80 _CLDELETE(termDocs);
81 enumerator->close();
82 _CLDELETE(enumerator);
83 );
84 return bts;
85 }
86
clone() const87 Filter* DateFilter::clone() const{
88 return _CLNEW DateFilter(*this);
89 }
90
toString()91 TCHAR* DateFilter::toString(){
92 size_t len = _tcslen(start->field()) + start->textLength() + end->textLength() + 8;
93 TCHAR* ret = _CL_NEWARRAY(TCHAR,len);
94 ret[0]=0;
95 _sntprintf(ret,len,_T("%s: [%s-%s]"), start->field(),start->text(),end->text());
96 return ret;
97 }
98 CL_NS_END
99