changeset 303:6ff0bc50ac46

Prevent multiple insertion of the same file in a collection. Use a hash table to speed up existence tests.
author zas_
date Fri, 11 Apr 2008 09:02:20 +0000
parents c1f75ee307ec
children 0fc0a130e8cf
files src/collect.c src/typedefs.h
diffstat 2 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/collect.c	Fri Apr 11 08:10:40 2008 +0000
+++ b/src/collect.c	Fri Apr 11 09:02:20 2008 +0000
@@ -333,6 +333,8 @@
 
 	cd->changed = FALSE;
 
+	cd->existence = g_hash_table_new(NULL, NULL);
+
 	if (path)
 		{
 		cd->path = g_strdup(path);
@@ -369,6 +371,8 @@
 
 	collection_list = g_list_remove(collection_list, cd);
 
+	g_hash_table_destroy(cd->existence);
+	
 	g_free(cd->path);
 	g_free(cd->name);
 
@@ -586,6 +590,17 @@
 	cd->info_updated_data = data;
 }
 
+static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
+{
+	CollectInfo *ci;
+
+	if (g_hash_table_lookup(cd->existence, fd->path)) return NULL;
+
+	ci = collection_info_new(fd, st, NULL);
+	if (ci) g_hash_table_insert(cd->existence, fd->path, "");
+	return ci;
+}
+
 gint collection_add_check(CollectionData *cd, FileData *fd, gint sorted, gint must_exist)
 {
 	struct stat st;
@@ -605,7 +620,11 @@
 	if (valid)
 		{
 		CollectInfo *ci;
-		ci = collection_info_new(fd, &st, NULL);
+
+		ci = collection_info_new_if_not_exists(cd, &st, fd);
+		if (!ci) return FALSE;
+		if (debug > 2) printf("add to collection: %s\n", fd->path);
+
 		cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
 		cd->changed = TRUE;
 
@@ -636,7 +655,12 @@
 	if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
 		{
 		CollectInfo *ci;
-		ci = collection_info_new(fd, &st, NULL);
+
+		ci = collection_info_new_if_not_exists(cd, &st, fd);
+		if (!ci) return FALSE;
+
+		if (debug > 2) printf("insert in collection: %s\n", fd->path);
+
 		cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
 		cd->changed = TRUE;
 
@@ -656,6 +680,8 @@
 
 	if (!ci) return FALSE;
 
+	g_hash_table_remove(cd->existence, fd->path);
+
 	cd->list = g_list_remove(cd->list, ci);
 	cd->changed = TRUE;
 
--- a/src/typedefs.h	Fri Apr 11 08:10:40 2008 +0000
+++ b/src/typedefs.h	Fri Apr 11 09:02:20 2008 +0000
@@ -234,6 +234,8 @@
 
 	/* contents changed since save flag */
 	gint changed;
+
+	GHashTable *existence;
 };
 
 struct _CollectTable