1 /*
2  * Copyright (C) 2006 Tommi Maekitalo
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  */
19 
20 #ifndef ZIM_SEARCH_INTERNAL_H
21 #define ZIM_SEARCH_INTERNAL_H
22 
23 #include "config.h"
24 
25 #if defined(ENABLE_XAPIAN)
26 #include <xapian.h>
27 #endif
28 
29 namespace zim {
30 
31 struct Search::InternalData {
32 #if defined(ENABLE_XAPIAN)
33     std::vector<Xapian::Database> xapian_databases;
34     Xapian::Database database;
35     Xapian::MSet results;
36 #endif
37 };
38 
39 struct search_iterator::InternalData {
40 #if defined(ENABLE_XAPIAN)
41     const Search* search;
42     Xapian::MSetIterator iterator;
43     Xapian::Document _document;
44     bool document_fetched;
45 #endif
46     Article _article;
47     bool article_fetched;
48 
49 
50 #if defined(ENABLE_XAPIAN)
InternalDataInternalData51     InternalData(const Search* search, Xapian::MSetIterator iterator) :
52         search(search),
53         iterator(iterator),
54         document_fetched(false),
55         article_fetched(false)
56     {};
57 
get_documentInternalData58     Xapian::Document get_document() {
59         if ( !document_fetched ) {
60             if (iterator != search->internal->results.end()) {
61                 _document = iterator.get_document();
62             }
63             document_fetched = true;
64         }
65         return _document;
66     }
67 #endif
68 
get_databasenumberInternalData69     int get_databasenumber() {
70 #if defined(ENABLE_XAPIAN)
71         Xapian::docid docid = *iterator;
72         return (docid - 1) % search->zimfiles.size();
73 #endif
74         return 0;
75     }
76 
get_articleInternalData77     Article& get_article() {
78 #if defined(ENABLE_XAPIAN)
79         if ( !article_fetched ) {
80             int databasenumber = get_databasenumber();
81             const File* file = search->zimfiles[databasenumber];
82             if ( ! file )
83                 _article = Article();
84             else
85                 _article = file->getArticleByUrl(get_document().get_data());
86             article_fetched = true;
87         }
88 #endif
89         return _article;
90     }
91 };
92 
93 
94 
95 }; //namespace zim
96 
97 #endif //ZIM_SEARCH_INTERNAL_H
98