00001 00004 /* Copyright (C) 2008 Olly Betts 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License as 00008 * published by the Free Software Foundation; either version 2 of the 00009 * License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include <config.h> 00022 00023 #include "slowvaluelist.h" 00024 00025 #include "document.h" 00026 #include "str.h" 00027 00028 #include "xapian/error.h" 00029 00030 #include "autoptr.h" 00031 00032 using namespace std; 00033 00034 Xapian::docid 00035 SlowValueList::get_docid() const 00036 { 00037 return current_did; 00038 } 00039 00040 string 00041 SlowValueList::get_value() const 00042 { 00043 return current_value; 00044 } 00045 00046 Xapian::valueno 00047 SlowValueList::get_valueno() const 00048 { 00049 return slot; 00050 } 00051 00052 bool 00053 SlowValueList::at_end() const 00054 { 00055 return last_docid == 0; 00056 } 00057 00058 void 00059 SlowValueList::next() 00060 { 00061 // Compare before incrementing, since last_docid could be the largest 00062 // representable value in which case current_did will wrap to zero before 00063 // exceeding it. 00064 while (current_did++ < last_docid) { 00065 try { 00066 // Open document lazily so that we don't waste time checking for 00067 // its existence. 00068 AutoPtr<Xapian::Document::Internal> 00069 doc(db.get_document_lazily(current_did)); 00070 if (!doc.get()) continue; 00071 string value = doc->get_value(slot); 00072 if (!value.empty()) { 00073 swap(current_value, value); 00074 return; 00075 } 00076 } catch (const Xapian::DocNotFoundError &) { 00077 } 00078 } 00079 00080 // Indicate that we're at_end(). 00081 last_docid = 0; 00082 } 00083 00084 void 00085 SlowValueList::skip_to(Xapian::docid did) 00086 { 00087 if (did <= current_did) return; 00088 00089 if (did > last_docid) { 00090 // Indicate that we're at_end(). 00091 last_docid = 0; 00092 return; 00093 } 00094 00095 current_did = did - 1; 00096 next(); 00097 } 00098 00099 bool 00100 SlowValueList::check(Xapian::docid did) 00101 { 00102 if (did <= current_did) return true; 00103 00104 if (did > last_docid) { 00105 // Indicate that we're at_end(). 00106 last_docid = 0; 00107 return true; 00108 } 00109 00110 current_did = did; 00111 try { 00112 AutoPtr<Xapian::Document::Internal> 00113 doc(db.get_document_lazily(current_did)); 00114 if (doc.get()) { 00115 current_value = doc->get_value(slot); 00116 if (!current_value.empty()) return true; 00117 } 00118 } catch (const Xapian::DocNotFoundError &) { 00119 } 00120 return false; 00121 } 00122 00123 string 00124 SlowValueList::get_description() const 00125 { 00126 string desc = "SlowValueList(slot="; 00127 desc += str(slot); 00128 if (last_docid != 0) { 00129 desc += ", docid="; 00130 desc += str(current_did); 00131 desc += ", value=\""; 00132 desc += current_value; 00133 desc += "\")"; 00134 } else { 00135 desc += ", atend)"; 00136 } 00137 return desc; 00138 }