comparison src/collect.c @ 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 6a7298988a7a
children b16b9b8979e5
comparison
equal deleted inserted replaced
302:c1f75ee307ec 303:6ff0bc50ac46
331 cd->window_w = COLLECT_DEF_WIDTH; 331 cd->window_w = COLLECT_DEF_WIDTH;
332 cd->window_h = COLLECT_DEF_HEIGHT; 332 cd->window_h = COLLECT_DEF_HEIGHT;
333 333
334 cd->changed = FALSE; 334 cd->changed = FALSE;
335 335
336 cd->existence = g_hash_table_new(NULL, NULL);
337
336 if (path) 338 if (path)
337 { 339 {
338 cd->path = g_strdup(path); 340 cd->path = g_strdup(path);
339 cd->name = g_strdup(filename_from_path(cd->path)); 341 cd->name = g_strdup(filename_from_path(cd->path));
340 /* load it */ 342 /* load it */
367 collection_load_stop(cd); 369 collection_load_stop(cd);
368 collection_list_free(cd->list); 370 collection_list_free(cd->list);
369 371
370 collection_list = g_list_remove(collection_list, cd); 372 collection_list = g_list_remove(collection_list, cd);
371 373
374 g_hash_table_destroy(cd->existence);
375
372 g_free(cd->path); 376 g_free(cd->path);
373 g_free(cd->name); 377 g_free(cd->name);
374 378
375 g_free(cd); 379 g_free(cd);
376 } 380 }
584 { 588 {
585 cd->info_updated_func = func; 589 cd->info_updated_func = func;
586 cd->info_updated_data = data; 590 cd->info_updated_data = data;
587 } 591 }
588 592
593 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
594 {
595 CollectInfo *ci;
596
597 if (g_hash_table_lookup(cd->existence, fd->path)) return NULL;
598
599 ci = collection_info_new(fd, st, NULL);
600 if (ci) g_hash_table_insert(cd->existence, fd->path, "");
601 return ci;
602 }
603
589 gint collection_add_check(CollectionData *cd, FileData *fd, gint sorted, gint must_exist) 604 gint collection_add_check(CollectionData *cd, FileData *fd, gint sorted, gint must_exist)
590 { 605 {
591 struct stat st; 606 struct stat st;
592 gint valid; 607 gint valid;
593 608
603 } 618 }
604 619
605 if (valid) 620 if (valid)
606 { 621 {
607 CollectInfo *ci; 622 CollectInfo *ci;
608 ci = collection_info_new(fd, &st, NULL); 623
624 ci = collection_info_new_if_not_exists(cd, &st, fd);
625 if (!ci) return FALSE;
626 if (debug > 2) printf("add to collection: %s\n", fd->path);
627
609 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE); 628 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
610 cd->changed = TRUE; 629 cd->changed = TRUE;
611 630
612 if (!sorted || cd->sort_method == SORT_NONE) 631 if (!sorted || cd->sort_method == SORT_NONE)
613 { 632 {
634 if (!insert_ci) return collection_add(cd, fd, sorted); 653 if (!insert_ci) return collection_add(cd, fd, sorted);
635 654
636 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode)) 655 if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
637 { 656 {
638 CollectInfo *ci; 657 CollectInfo *ci;
639 ci = collection_info_new(fd, &st, NULL); 658
659 ci = collection_info_new_if_not_exists(cd, &st, fd);
660 if (!ci) return FALSE;
661
662 if (debug > 2) printf("insert in collection: %s\n", fd->path);
663
640 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE); 664 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
641 cd->changed = TRUE; 665 cd->changed = TRUE;
642 666
643 collection_window_insert(collection_window_find(cd), ci); 667 collection_window_insert(collection_window_find(cd), ci);
644 668
653 CollectInfo *ci; 677 CollectInfo *ci;
654 678
655 ci = collection_list_find(cd->list, fd->path); 679 ci = collection_list_find(cd->list, fd->path);
656 680
657 if (!ci) return FALSE; 681 if (!ci) return FALSE;
682
683 g_hash_table_remove(cd->existence, fd->path);
658 684
659 cd->list = g_list_remove(cd->list, ci); 685 cd->list = g_list_remove(cd->list, ci);
660 cd->changed = TRUE; 686 cd->changed = TRUE;
661 687
662 collection_window_remove(collection_window_find(cd), ci); 688 collection_window_remove(collection_window_find(cd), ci);