00001 00004 /* Copyright (C) 2007,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 "chert_valuelist.h" 00024 00025 #include "chert_cursor.h" 00026 #include "chert_database.h" 00027 #include "omassert.h" 00028 #include "str.h" 00029 00030 using namespace std; 00031 00032 bool 00033 ChertValueList::update_reader() 00034 { 00035 Xapian::docid first_did = docid_from_key(slot, cursor->current_key); 00036 if (!first_did) return false; 00037 00038 cursor->read_tag(); 00039 const string & tag = cursor->current_tag; 00040 reader.assign(tag.data(), tag.size(), first_did); 00041 return true; 00042 } 00043 00044 ChertValueList::~ChertValueList() 00045 { 00046 delete cursor; 00047 } 00048 00049 Xapian::docid 00050 ChertValueList::get_docid() const 00051 { 00052 Assert(!at_end()); 00053 return reader.get_docid(); 00054 } 00055 00056 Xapian::valueno 00057 ChertValueList::get_valueno() const 00058 { 00059 return slot; 00060 } 00061 00062 std::string 00063 ChertValueList::get_value() const 00064 { 00065 Assert(!at_end()); 00066 return reader.get_value(); 00067 } 00068 00069 bool 00070 ChertValueList::at_end() const 00071 { 00072 return cursor == NULL; 00073 } 00074 00075 void 00076 ChertValueList::next() 00077 { 00078 if (!cursor) { 00079 cursor = db->get_postlist_cursor(); 00080 if (!cursor) return; 00081 cursor->find_entry_ge(make_valuechunk_key(slot, 1)); 00082 } else if (!reader.at_end()) { 00083 reader.next(); 00084 if (!reader.at_end()) return; 00085 cursor->next(); 00086 } 00087 00088 if (!cursor->after_end()) { 00089 if (update_reader()) { 00090 if (!reader.at_end()) return; 00091 } 00092 } 00093 00094 // We've reached the end. 00095 delete cursor; 00096 cursor = NULL; 00097 } 00098 00099 void 00100 ChertValueList::skip_to(Xapian::docid did) 00101 { 00102 if (!cursor) { 00103 cursor = db->get_postlist_cursor(); 00104 if (!cursor) return; 00105 } else if (!reader.at_end()) { 00106 reader.skip_to(did); 00107 if (!reader.at_end()) return; 00108 } 00109 00110 if (!cursor->find_entry(make_valuechunk_key(slot, did))) { 00111 if (update_reader()) { 00112 reader.skip_to(did); 00113 if (!reader.at_end()) return; 00114 } 00115 // The requested docid is between two chunks. 00116 cursor->next(); 00117 } 00118 00119 // Either an exact match, or in a gap before the start of a chunk. 00120 if (!cursor->after_end()) { 00121 if (update_reader()) { 00122 if (!reader.at_end()) return; 00123 } 00124 } 00125 00126 // We've reached the end. 00127 delete cursor; 00128 cursor = NULL; 00129 } 00130 00131 bool 00132 ChertValueList::check(Xapian::docid did) 00133 { 00134 if (!cursor) { 00135 cursor = db->get_postlist_cursor(); 00136 if (!cursor) return true; 00137 } else if (!reader.at_end()) { 00138 // Check for the requested docid in the current block. 00139 reader.skip_to(did); 00140 if (!reader.at_end()) return true; 00141 } 00142 00143 // Try moving to the appropriate chunk. 00144 if (!cursor->find_entry(make_valuechunk_key(slot, did))) { 00145 // We're in a chunk which might contain the docid. 00146 if (update_reader()) { 00147 reader.skip_to(did); 00148 if (!reader.at_end()) return true; 00149 } 00150 return false; 00151 } 00152 00153 // We had an exact match for a chunk starting with specified docid. 00154 Assert(!cursor->after_end()); 00155 if (!update_reader()) { 00156 // We found the exact key we built, so it must match the slot. 00157 // Therefore update_reader() "can't possibly fail". 00158 Assert(false); 00159 } 00160 00161 return true; 00162 } 00163 00164 string 00165 ChertValueList::get_description() const 00166 { 00167 string desc("ChertValueList(slot="); 00168 desc += str(slot); 00169 desc += ')'; 00170 return desc; 00171 }