--- evolution-data-server/camel/camel-vee-folder.c.orig	2008-12-09 01:36:53.000000000 -0500
+++ evolution-data-server/camel/camel-vee-folder.c	2008-12-09 02:38:31.000000000 -0500
@@ -1021,10 +1021,16 @@
 	if (vf->expression == NULL) {
 		match = g_ptr_array_new();
 	} else {
-		/* Load the folder results from the DB. */
+		/* Load the cached matches from the DB. */
 		match = camel_vee_summary_get_ids ((CamelVeeSummary *)folder->summary, u.hash);
-		d(printf("len = %d %d for %s %s\n", match ? match->len:0, rebuilded, source->full_name, shash));
-		if (!match) {
+		if (match) {
+			/* Note: When the cached match set is valid,
+			 * we insert a dummy row containing just shash (no uid)
+			 * to force this test to succeed even if the set is empty. */
+			dd(printf("  got %d matches from DB (may include dummy)\n", match->len));
+		} else {
+			/* No cached matches; re-execute the query. */
+			rebuilded = TRUE;
 			match = camel_folder_search_by_expression(f, vf->expression, ex);
 			if (match == NULL)
 				/* We still need to rebuild: we may need to clear out
@@ -1032,10 +1038,7 @@
 				 * longer has any matches from this source folder. - Matt */
 				match = g_ptr_array_new();
 			dd(printf("  no matches in DB, re-ran query, got %d matches\n", match->len));
-			rebuilded = TRUE;
-		} else
-			dd(printf("  got %d matches from DB\n", match->len));
-		
+		}
 	}
 
 	u.source = source;
@@ -1049,8 +1052,17 @@
 	/* we build 2 hash tables, one for all uid's not matched, the other for all matched uid's,
 	   we just ref the real memory */
 	matchhash = g_hash_table_new(g_str_hash, g_str_equal);
-	for (i=0;i<match->len;i++)
+	for (i=0;i<match->len;i++) {
+		/* Filter out the dummy, if present. */
+		if (strcmp(match->pdata[i], "") == 0) {
+			camel_pstring_free(match->pdata[i]);
+			g_ptr_array_remove_index_fast(match, i);
+			dd(printf("  found and removed dummy from match array\n"));
+			i--;
+			continue;
+		}
 		g_hash_table_insert(matchhash, match->pdata[i], GINT_TO_POINTER (1));
+	}
 
 	allhash = g_hash_table_new(g_str_hash, g_str_equal);
 	fullhash = g_hash_table_new(g_str_hash, g_str_equal);
@@ -1108,8 +1120,11 @@
 	/* now matchhash contains any new uid's, add them, etc */
 	g_hash_table_foreach(matchhash, (GHFunc)rebuild_added_uid, &u);
 
-	if (rebuilded)
+	if (rebuilded) {
+		/* Cached matches are valid; insert the dummy. */
+		camel_db_add_to_vfolder_transaction(folder->parent_store->cdb_w, folder->full_name, shash, NULL);
 		camel_db_end_transaction (folder->parent_store->cdb_w, NULL);
+	}
 	
 	if (folder_unmatched != NULL) {
 		/* scan unmatched, remove any that have vanished, etc */
