From 4303aa143b408acd3475e01f6ed243d61c559b2b Mon Sep 17 00:00:00 2001 From: Matthias Ruester Date: Fri, 9 Dec 2011 09:04:20 +0100 Subject: [PATCH 01/11] bee-dep: assign TBLSIZE to a long-int constant --- src/hash.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hash.h b/src/hash.h index c4faf35..ab9a797 100644 --- a/src/hash.h +++ b/src/hash.h @@ -29,7 +29,7 @@ #include "beedep_tree.h" -#define TBLSIZE 2000003 /* prime number */ +#define TBLSIZE 2000003L /* prime number */ struct hash { struct tree *tbl[TBLSIZE]; From a8d55f5bca91595aa84c31c599f0b2977be57c3d Mon Sep 17 00:00:00 2001 From: Matthias Ruester Date: Fri, 9 Dec 2011 09:09:13 +0100 Subject: [PATCH 02/11] bee-dep: add hash_safe_insert and remove unneeded struct member variable --- src/graph.c | 6 ------ src/hash.c | 14 +++++++++++++- src/hash.h | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/graph.c b/src/graph.c index 0643d26..9b31abf 100644 --- a/src/graph.c +++ b/src/graph.c @@ -498,7 +498,6 @@ int print_removable(struct hash *hash, char *remove) int save_cache(struct hash *hash, char *path) { int i; - unsigned long index; struct tree_node *s, *t; FILE *file; @@ -507,11 +506,6 @@ int save_cache(struct hash *hash, char *path) return EXIT_FAILURE; } - index = 0; - - if (hash->cnt == 0) - return EXIT_SUCCESS; - for (i = 0; i < TBLSIZE; i++) { if (hash->tbl[i]->root) { t = tree_first(hash->tbl[i]->root); diff --git a/src/hash.c b/src/hash.c index f3b137e..70d72a9 100644 --- a/src/hash.c +++ b/src/hash.c @@ -62,8 +62,20 @@ void hash_insert(struct hash *hash, struct node *n) unsigned long index = hash_index(n->name); tree_insert(hash->tbl[index], n); +} + +struct node *hash_safe_insert(struct hash *hash, struct node *n) +{ + unsigned long index = hash_index(n->name); + struct node *r; - hash->cnt++; + r = tree_search_node(hash->tbl[index], n->name); + + if (r) + return r; + + tree_insert(hash->tbl[index], n); + return n; } struct node *hash_search(struct hash *hash, char *key) diff --git a/src/hash.h b/src/hash.h index ab9a797..f0d0835 100644 --- a/src/hash.h +++ b/src/hash.h @@ -33,12 +33,12 @@ struct hash { struct tree *tbl[TBLSIZE]; - unsigned long cnt; }; extern struct hash *hash_new(void); extern unsigned long hash_index(char *key); extern void hash_insert(struct hash *hash, struct node *n); +extern struct node *hash_safe_insert(struct hash *hash, struct node *n); extern struct node *hash_search(struct hash *hash, char *key); extern void hash_free(struct hash *hash); From 9b6e6ee558b71f4bed78389b94de880ea609cc53 Mon Sep 17 00:00:00 2001 From: Matthias Ruester Date: Fri, 9 Dec 2011 09:22:36 +0100 Subject: [PATCH 03/11] bee-dep: use hash_safe_insert to gain a litte speedup --- src/graph.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/graph.c b/src/graph.c index 9b31abf..a3db32a 100644 --- a/src/graph.c +++ b/src/graph.c @@ -93,7 +93,7 @@ int graph_insert_nodes(struct hash *hash, char *filename) pkgname[NODENAME_MAX] = {0}, nodename[NODENAME_MAX] = {0}; int l, line_cnt; - struct node *n, *h, *v; + struct node *n, *h, *v, *c; if ((file = fopen(filename, "r")) == NULL) { perror("bee-dep: graph_insert_nodes: fopen"); @@ -160,10 +160,10 @@ int graph_insert_nodes(struct hash *hash, char *filename) sprintf(nodename, "%s", s); } - if ((n = hash_search(hash, nodename)) == NULL) { - n = node_new(nodename, UNKNOWN); - hash_insert(hash, n); - } + c = node_new(nodename, UNKNOWN); + n = hash_safe_insert(hash, c); + if (c != n) + node_free(c); type_flag = 0; continue; @@ -215,18 +215,20 @@ int graph_insert_nodes(struct hash *hash, char *filename) type_flag = 1; } else if (strcasecmp(prop, PROVIDES) == 0) { - if ((h = hash_search(hash, value)) == NULL) { - h = node_new(value, UNKNOWN); - hash_insert(hash, h); - } + c = node_new(value, UNKNOWN); + h = hash_safe_insert(hash, c); + + if (c != h) + node_free(c); if (IS_FILE(value)) { sprintf(nodename, "%s%s", pkgname, value); - if ((v = hash_search(hash, nodename)) == NULL) { - v = node_new(nodename, UNKNOWN); - hash_insert(hash, v); - } + c = node_new(nodename, UNKNOWN); + v = hash_safe_insert(hash, c); + + if (v != c) + node_free(c); add_provide(n, v); add_provide(v, h); @@ -234,10 +236,11 @@ int graph_insert_nodes(struct hash *hash, char *filename) add_provide(n, h); } } else if (strcasecmp(prop, NEEDS) == 0) { - if ((h = hash_search(hash, value)) == NULL) { - h = node_new(value, UNKNOWN); - hash_insert(hash, h); - } + c = node_new(value, UNKNOWN); + h = hash_safe_insert(hash, c); + + if (c != h) + node_free(c); add_need(n, h); } From cdf2a7697fc8edbfd7fd9586d5d78167c05fe54c Mon Sep 17 00:00:00 2001 From: Matthias Ruester Date: Fri, 9 Dec 2011 09:20:58 +0100 Subject: [PATCH 04/11] bee-dep: assign often used variables as register variables --- src/graph.c | 2 +- src/hash.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graph.c b/src/graph.c index a3db32a..b0f3938 100644 --- a/src/graph.c +++ b/src/graph.c @@ -85,7 +85,7 @@ char is_virtual_file(char *s) int graph_insert_nodes(struct hash *hash, char *filename) { FILE *file; - char *s, *p, *a; + register char *s, *p, *a; char type_flag, u; char line[LINE_MAX], prop[LINE_MAX], diff --git a/src/hash.c b/src/hash.c index 70d72a9..85bed0b 100644 --- a/src/hash.c +++ b/src/hash.c @@ -47,7 +47,7 @@ struct hash *hash_new(void) unsigned long hash_index(char *key) { - unsigned long index = 0; + register unsigned long index = 0; char c; while ((c = *key++)) From e8ab566ceff76eb37869515834073a0127dc6bca Mon Sep 17 00:00:00 2001 From: Matthias Ruester Date: Fri, 9 Dec 2011 09:29:04 +0100 Subject: [PATCH 05/11] bee-dep: add features and therefore rewrite the option logic --- src/bee-dep.c | 863 +++++++++++++++++++++++++++++++++++++++----------- src/graph.c | 595 ++++++++++++++++++++++++++++++---- src/graph.h | 13 +- 3 files changed, 1217 insertions(+), 254 deletions(-) diff --git a/src/bee-dep.c b/src/bee-dep.c index c94d0fd..27b981f 100644 --- a/src/bee-dep.c +++ b/src/bee-dep.c @@ -30,54 +30,205 @@ #include #include #include -#include #include #include #include +#include #include "graph.h" #define CACHENAME "index.db" +#define TMPNAME "index.tmp" +#define LOCKNAME "index.lock" + +#define REBUILD 1 +#define UPDATE 2 +#define REMOVE 3 +#define LIST 4 +#define CONFLICTS 5 + +static char *bee_version(void) +{ + static char *bee_v = NULL; + + if (!bee_v) + bee_v = getenv("BEE_VERSION"); + + return bee_v; +} + +static char *bee_metadir(void) +{ + static char *bee_md = NULL; + + if (!bee_md) + bee_md = getenv("BEE_METADIR"); + + return bee_md; +} + +static char *bee_cachedir(void) +{ + static char *bee_cd = NULL; + + if (!bee_cd) + bee_cd = getenv("BEE_CACHEDIR"); + + return bee_cd; +} + +static void get_bee_variables(void) +{ + if (!bee_version()) { + fprintf(stderr, "BEE-ERROR: please call bee-dep from bee\n"); + exit(1); + } + + if (!bee_metadir()) { + fprintf(stderr, "BEE-ERROR: BEE_METADIR not set\n"); + exit(1); + } + + if (!bee_cachedir()) { + fprintf(stderr, "BEE_ERROR: BEE_CACHEDIR not set\n"); + exit(1); + } +} + +static char *cache_filename(void) +{ + static char cache[PATH_MAX + 1] = {0}; + + if (!cache[0]) + sprintf(cache, "%s/%s", bee_cachedir(), CACHENAME); + + return cache; +} + +static char *lock_filename(void) +{ + static char lock[PATH_MAX + 1] = {0}; + + if (!lock[0]) + sprintf(lock, "%s/%s", bee_cachedir(), LOCKNAME); + + return lock; +} + +static void usage_header(void) +{ + printf("bee-dep v%s 2011\n" + " by Matthias Ruester and Lucas Schwass\n" + " Max Planck Institute for Molecular Genetics Berlin Dahlem\n\n", + getenv("BEE_VERSION")); +} static void usage(void) { - printf("bee-dep v%s 2011\n" - " by Matthias Ruester and Lucas Schwass\n" - " Max Planck Institute for Molecular Genetics Berlin Dahlem\n\n" - - "Usage: bee dep