backends/quartz/quartz_record.cc

Go to the documentation of this file.
00001 /* quartz_record.cc: Records in quartz databases
00002  *
00003  * ----START-LICENCE----
00004  * Copyright 1999,2000,2001 BrightStation PLC
00005  * Copyright 2002 Ananova Ltd
00006  * Copyright 2002,2003,2004 Olly Betts
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as
00010  * published by the Free Software Foundation; either version 2 of the
00011  * License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021  * USA
00022  * -----END-LICENCE-----
00023  */
00024 
00025 #include <config.h>
00026 #include "quartz_record.h"
00027 #include "quartz_utils.h"
00028 #include "utils.h"
00029 #include <xapian/error.h>
00030 #include "omassert.h"
00031 #include "omdebug.h"
00032 
00033 using std::string;
00034 
00035 // Magic key (which corresponds to an invalid docid) is used to store the
00036 // next free docid and total length of all documents
00037 static const string METAINFO_KEY("", 1);
00038 
00039 string
00040 QuartzRecordTable::get_record(Xapian::docid did) const
00041 {
00042     DEBUGCALL(DB, string, "QuartzRecordTable::get_record", did);
00043     string key(quartz_docid_to_key(did));
00044     string tag;
00045 
00046     if (!get_exact_entry(key, tag)) {
00047         throw Xapian::DocNotFoundError("Document " + om_tostring(did) + " not found.");
00048     }
00049 
00050     RETURN(tag);
00051 }
00052 
00053 
00054 Xapian::doccount
00055 QuartzRecordTable::get_doccount() const
00056 {   
00057     DEBUGCALL(DB, Xapian::doccount, "QuartzRecordTable::get_doccount", "");
00058     // Check that we can't overflow (the unsigned test is actually too
00059     // strict as we can typically assign an unsigned short to a signed long,
00060     // but this shouldn't actually matter here).
00061     CASSERT(sizeof(Xapian::doccount) >= sizeof(quartz_tablesize_t));
00062     CASSERT_TYPE_UNSIGNED(Xapian::doccount);
00063     CASSERT_TYPE_UNSIGNED(quartz_tablesize_t);
00064     Xapian::doccount entries = get_entry_count();
00065     RETURN(entries ? entries - 1 : 0);
00066 }
00067 
00068 Xapian::docid
00069 QuartzRecordTable::get_lastdocid() const
00070 {
00071     DEBUGCALL(DB, Xapian::docid, "QuartzRecordTable::get_lastdocid", "");
00072 
00073     string tag;
00074     if (!get_exact_entry(METAINFO_KEY, tag)) RETURN(0u);
00075 
00076     Xapian::docid did;
00077     const char * data = tag.data();
00078     const char * end = data + tag.size();
00079     if (!unpack_uint(&data, end, &did)) {
00080         throw Xapian::DatabaseCorruptError("Record containing meta information is corrupt.");
00081     }
00082     RETURN(did);
00083 }
00084 
00085 void
00086 QuartzRecordTable::replace_record(const string & data, Xapian::docid did)
00087 {
00088     DEBUGCALL(DB, void, "QuartzRecordTable::replace_record", data << ", " << did);
00089     string key(quartz_docid_to_key(did));
00090     add(key, data);
00091 }
00092 
00093 void
00094 QuartzRecordTable::set_total_length_and_lastdocid(quartz_totlen_t totlen,
00095                                                   Xapian::docid did)
00096 {
00097     DEBUGCALL(DB, void, "QuartzRecordTable::set_total_length_and_lastdocid",
00098                         totlen << ", " << did);
00099     string tag = pack_uint(did);
00100     tag += pack_uint_last(totlen);
00101     add(METAINFO_KEY, tag);
00102 }
00103 
00104 quartz_totlen_t
00105 QuartzRecordTable::get_total_length() const
00106 {
00107     DEBUGCALL(DB, quartz_totlen_t, "QuartzRecordTable::get_total_length", "");
00108 
00109     string tag;
00110     if (!get_exact_entry(METAINFO_KEY, tag)) RETURN(0);
00111 
00112     Xapian::docid did;
00113     quartz_totlen_t totlen;
00114     const char * data = tag.data();
00115     const char * end = data + tag.size();
00116     if (!unpack_uint(&data, end, &did)) {
00117         throw Xapian::DatabaseCorruptError("Record containing meta information is corrupt.");
00118     }
00119     if (!unpack_uint_last(&data, end, &totlen)) {
00120         throw Xapian::DatabaseCorruptError("Record containing meta information is corrupt.");
00121     }
00122     RETURN(totlen);
00123 }
00124 
00125 void
00126 QuartzRecordTable::delete_record(Xapian::docid did)
00127 {
00128     DEBUGCALL(DB, void, "QuartzRecordTable::delete_record", did);
00129     if (!del(quartz_docid_to_key(did)))
00130         throw Xapian::DocNotFoundError("Can't delete non-existent document #" + om_tostring(did));
00131 }

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