api/sorter.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2007 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 <xapian/sorter.h>
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 using namespace std;
00029 
00030 namespace Xapian {
00031 
00032 Sorter::~Sorter() { }
00033 
00034 string
00035 MultiValueSorter::operator()(const Xapian::Document & doc) const
00036 {
00037     string result;
00038 
00039     vector<pair<Xapian::valueno, bool> >::const_iterator i = valnos.begin();
00040     // Don't crash if valnos is empty.
00041     if (rare(i == valnos.end())) return result;
00042 
00043     while (true) {
00044         // All values (except for the last if it's sorted forwards) need to
00045         // be adjusted.
00046         //
00047         // FIXME: allow Xapian::BAD_VALNO to mean "relevance?"
00048         string v = doc.get_value(i->first);
00049         bool reverse_sort = !i->second;
00050 
00051         if (++i == valnos.end() && !reverse_sort) {
00052             // No need to adjust the last value if it's sorted forwards.
00053             result += v;
00054             break;
00055         }
00056 
00057         if (reverse_sort) {
00058             // For a reverse ordered value, we subtract each byte from '\xff',
00059             // except for '\0' which we convert to "\xff\0".  We insert
00060             // "\xff\xff" after the encoded value.
00061             for (string::const_iterator j = v.begin(); j != v.end(); ++j) {
00062                 unsigned char ch(*j);
00063                 result += char(255 - ch);
00064                 if (ch == 0) result += '\0';
00065             }
00066             result.append("\xff\xff", 2);
00067             if (i == valnos.end()) break;
00068         } else {
00069             // For a forward ordered value (unless it's the last value), we
00070             // convert any '\0' to "\0\xff".  We insert "\0\0" after the
00071             // encoded value.
00072             string::size_type j = 0, nul;
00073             while ((nul = v.find('\0', j)) != string::npos) {
00074                 ++nul;
00075                 result.append(v, j, nul - j);
00076                 result += '\xff';
00077                 j = nul;
00078             }
00079             result.append(v, j, string::npos);
00080             result.append("\0", 2);
00081         }
00082     }
00083     return result;
00084 }
00085 
00086 MultiValueSorter::~MultiValueSorter() { }
00087 
00088 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.