[Xapian-discuss] Latest document of a database?

Robert Pollak robert.pollak@fabasoft.com
Thu, 03 Jun 2004 15:02:00 +0200


This is a multi-part message in MIME format.
--------------020600010209060701000705
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

here is a patch that defines Database::get_lastdocid() for the quartz 
backend.
I also had to call Database::remove/add instead of replace_document, to 
always get the most recently added doc. (replace reuses the docid.)

-- 
Robert Pollak
GPG Key ID: 748646AD

--------------020600010209060701000705
Content-Type: text/plain;
 name="last-docid2.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="last-docid2.patch"

Index: ChangeLog
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/ChangeLog,v
retrieving revision 1.2
diff -u -r1.2 ChangeLog
--- ChangeLog	15 Apr 2004 10:00:09 -0000	1.2
+++ ChangeLog	3 Jun 2004 10:09:40 -0000
@@ -1,3 +1,11 @@
+2004-06-02 Robert Pollak <robert.pollak@fabasoft.com>
+
+	* api/omdatabase.cc, include/xapian/database.h,
+	  backends/database.cc, common/database.h,
+	  backends/quartz/quartz_database.cc, backends/quartz/quartz_database.h,
+	  backends/quartz/quartz_record.cc, backends/quartz/quartz_record.h:
+	  New method Database::get_lastdocid for re-synchronizing an old quartz index.
+
 2004-04-15 Robert Pollak <robert.pollak@fabasoft.com>
 
 	* docs/Makefile.in: "make install" now works with CVS dir in apidoc/html.
Index: api/omdatabase.cc
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/api/omdatabase.cc,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 omdatabase.cc
--- api/omdatabase.cc	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ api/omdatabase.cc	3 Jun 2004 09:24:34 -0000
@@ -226,6 +226,18 @@
     RETURN(docs);
 }
 
+Xapian::docid
+Database::get_lastdocid() const
+{
+    DEBUGAPICALL(Xapian::docid, "Database::get_lastdocid", "");
+    Xapian::docid did = 0;
+    vector<Xapian::Internal::RefCntPtr<Database::Internal> >::const_iterator i;
+    for (i = internal.begin(); i != internal.end(); ++i) {
+	did = std::max(did, (*i)->get_lastdocid());
+    }
+    RETURN(did);
+}
+
 Xapian::doclength
 Database::get_avlength() const
 {
Index: backends/database.cc
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/backends/database.cc,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 database.cc
--- backends/database.cc	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ backends/database.cc	3 Jun 2004 10:01:37 -0000
@@ -366,4 +366,11 @@
     do_replace_document(did, document);
 }
 
+Xapian::docid
+Database::Internal::get_lastdocid() const
+{
+    DEBUGCALL(DB, void, "Database::Internal::get_lastdocid", "");
+    throw Xapian::UnimplementedError("Database::Internal::get_lastdocid() not yet implemented");
+}
+
 }
Index: backends/quartz/quartz_database.cc
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/backends/quartz/quartz_database.cc,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 quartz_database.cc
--- backends/quartz/quartz_database.cc	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ backends/quartz/quartz_database.cc	3 Jun 2004 09:42:31 -0000
@@ -126,6 +126,13 @@
     RETURN(QuartzRecordManager::get_doccount(*(tables->get_record_table())));
 }
 
+Xapian::docid
+QuartzDatabase::get_lastdocid() const
+{
+    DEBUGCALL(DB, Xapian::docid, "QuartzDatabase::get_lastdocid", "");
+    RETURN(QuartzRecordManager::get_lastdocid(*(tables->get_record_table())));
+}
+
 Xapian::doclength
 QuartzDatabase::get_avlength() const
 {
@@ -755,6 +762,13 @@
 {
     DEBUGCALL(DB, Xapian::doccount, "QuartzWritableDatabase::get_doccount", "");
     RETURN(database_ro.get_doccount());
+}
+
+Xapian::docid
+QuartzWritableDatabase::get_lastdocid() const
+{
+    DEBUGCALL(DB, Xapian::docid, "QuartzWritableDatabase::get_lastdocid", "");
+    RETURN(database_ro.get_lastdocid());
 }
 
 Xapian::doclength
Index: backends/quartz/quartz_database.h
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/backends/quartz/quartz_database.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 quartz_database.h
--- backends/quartz/quartz_database.h	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ backends/quartz/quartz_database.h	3 Jun 2004 09:44:01 -0000
@@ -97,6 +97,7 @@
 	 */
 	//@{
 	Xapian::doccount  get_doccount() const;
+	Xapian::docid get_lastdocid() const;
 	Xapian::doclength get_avlength() const;
 	Xapian::doclength get_doclength(Xapian::docid did) const;
 	Xapian::doccount get_termfreq(const string & tname) const;
@@ -182,6 +183,7 @@
 	 */
 	//@{
 	Xapian::doccount  get_doccount() const;
+	Xapian::docid get_lastdocid() const;
 	Xapian::doclength get_avlength() const;
 	Xapian::doclength get_doclength(Xapian::docid did) const;
 	Xapian::doccount get_termfreq(const string & tname) const;
Index: backends/quartz/quartz_record.cc
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/backends/quartz/quartz_record.cc,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 quartz_record.cc
--- backends/quartz/quartz_record.cc	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ backends/quartz/quartz_record.cc	2 Jun 2004 15:07:05 -0000
@@ -96,6 +96,23 @@
 }
 
 Xapian::docid
+QuartzRecordManager::get_lastdocid(QuartzTable & table)
+{
+    DEBUGCALL_STATIC(DB, Xapian::docid, "QuartzRecordManager::get_lastdocid", "[table]");
+
+    string tag;
+    if (!table.get_exact_entry(METAINFO_KEY, tag)) RETURN(0u);
+
+    Xapian::docid did;
+    const char * data = tag.data();
+    const char * end = data + tag.size();
+    if (!unpack_uint(&data, end, &did)) {
+	throw Xapian::DatabaseCorruptError("Record containing meta information is corrupt.");
+    }
+    RETURN(did);
+}
+
+Xapian::docid
 QuartzRecordManager::add_record(QuartzBufferedTable & table,
 				const string & data)
 {
Index: backends/quartz/quartz_record.h
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/backends/quartz/quartz_record.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 quartz_record.h
--- backends/quartz/quartz_record.h	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ backends/quartz/quartz_record.h	2 Jun 2004 15:07:13 -0000
@@ -57,6 +57,10 @@
 	 */
 	static Xapian::docid get_newdocid(QuartzBufferedTable & table);
 
+	/** Get the last document ID used.
+	 */
+	static Xapian::docid get_lastdocid(QuartzTable & table);
+
 	/** Add a new record to the table.
 	 *
 	 */
Index: common/database.h
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/common/database.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 database.h
--- common/database.h	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ common/database.h	3 Jun 2004 09:56:28 -0000
@@ -154,6 +154,10 @@
 	 */
 	virtual Xapian::doccount get_doccount() const = 0;
 
+	/** Return the last used document id of this (sub) database.
+	 */
+	virtual Xapian::docid get_lastdocid() const;
+
 	/** Return the average length of a document in this (sub) database.
 	 *
 	 *  See Database::Internal::get_doclength() for the meaning of document
Index: include/xapian/database.h
===================================================================
RCS file: /home/rp/indexing/xapian-rpc/repository/xapian-core/include/xapian/database.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 database.h
--- include/xapian/database.h	15 Apr 2004 08:54:05 -0000	1.1.1.1
+++ include/xapian/database.h	2 Jun 2004 15:28:30 -0000
@@ -141,7 +141,10 @@
 
 	/// Get the number of documents in the database.
 	Xapian::doccount get_doccount() const;
-	
+
+	/// Get the last used document id of the database.
+	Xapian::docid get_lastdocid() const;
+
 	/// Get the average length of the documents in the database.
 	Xapian::doclength get_avlength() const;
 

--------------020600010209060701000705--