From 70e9b035ff33d649adb32028258f9676fc12d126 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Wed, 5 May 2010 12:49:16 +0200 Subject: [PATCH] Convert tsmem->removedPackages to a hash table and remove rpmdbPruneIterator from the API --- lib/depends.c | 33 ++++++++++++++------------------- lib/rpmdb.c | 25 ++++++++++++++++++++----- lib/rpmdb.h | 12 ------------ lib/rpmdb_internal.h | 19 +++++++++++++++++++ lib/rpmts.c | 22 +++++++++++++++------- lib/rpmts_internal.h | 4 +--- lib/transaction.c | 9 +-------- 7 files changed, 70 insertions(+), 54 deletions(-) diff --git a/lib/depends.c b/lib/depends.c index b069268..1cfc610 100644 --- a/lib/depends.c +++ b/lib/depends.c @@ -36,6 +36,16 @@ static rpmds rpmlibP = NULL; #define HTDATATYPE int #include "lib/rpmhash.H" #include "lib/rpmhash.C" +#undef HASHTYPE +#undef HTKEYTYPE +#undef HTDATATYPE + +#define HASHTYPE intHash +#define HTKEYTYPE unsigned int +#include "rpmhash.C" +#undef HASHTYPE +#undef HASHKEYTYPE + /** * Compare removed package instances (qsort/bsearch). @@ -67,26 +77,11 @@ static int removePackage(tsMembers tsmem, Header h, rpmte depends) if (dboffset == 0) return 1; /* Filter out duplicate erasures. */ - if (tsmem->numRemovedPackages > 0 && tsmem->removedPackages != NULL) { - if (bsearch(&dboffset, - tsmem->removedPackages, tsmem->numRemovedPackages, - sizeof(*tsmem->removedPackages), intcmp) != NULL) - return 0; - } - - if (tsmem->numRemovedPackages == tsmem->allocedRemovedPackages) { - tsmem->allocedRemovedPackages += tsmem->delta; - tsmem->removedPackages = xrealloc(tsmem->removedPackages, - sizeof(tsmem->removedPackages) * tsmem->allocedRemovedPackages); + if (intHashHasEntry(tsmem->removedPackages, dboffset)) { + return 0; } - if (tsmem->removedPackages != NULL) { /* XXX can't happen. */ - tsmem->removedPackages[tsmem->numRemovedPackages] = dboffset; - tsmem->numRemovedPackages++; - if (tsmem->numRemovedPackages > 1) - qsort(tsmem->removedPackages, tsmem->numRemovedPackages, - sizeof(*tsmem->removedPackages), intcmp); - } + intHashAddEntry(tsmem->removedPackages, dboffset); if (tsmem->orderCount >= tsmem->orderAlloced) { tsmem->orderAlloced += (tsmem->orderCount - tsmem->orderAlloced) + tsmem->delta; @@ -107,7 +102,7 @@ static rpmdbMatchIterator rpmtsPrunedIterator(rpmts ts, rpmTag tag, const char * { rpmdbMatchIterator mi = rpmtsInitIterator(ts, tag, key, 0); tsMembers tsmem = rpmtsMembers(ts); - rpmdbPruneIterator(mi, tsmem->removedPackages, tsmem->numRemovedPackages,1); + rpmdbPruneIterator(mi, tsmem->removedPackages); return mi; } diff --git a/lib/rpmdb.c b/lib/rpmdb.c index 43f86e9..194e47a 100644 --- a/lib/rpmdb.c +++ b/lib/rpmdb.c @@ -1916,14 +1916,29 @@ static int rpmdbGrowIterator(rpmdbMatchIterator mi) return rc; } -int rpmdbPruneIterator(rpmdbMatchIterator mi, int * hdrNums, - int nHdrNums, int sorted) +int rpmdbPruneIterator(rpmdbMatchIterator mi, intHash hdrNums) { - if (mi == NULL || hdrNums == NULL || nHdrNums <= 0) + if (mi == NULL || hdrNums == NULL || intHashNumKeys(hdrNums) <= 0) return 1; - if (mi->mi_set) - (void) dbiPruneSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), sorted); + if (!mi->mi_set) + return 0; + + unsigned int from; + unsigned int to = 0; + unsigned int num = mi->mi_set->count; + + assert(mi->mi_set->count > 0); + + for (from = 0; from < num; from++) { + if (intHashHasEntry(hdrNums, mi->mi_set->recs[from].hdrNum)) { + mi->mi_set->count--; + continue; + } + if (from != to) + mi->mi_set->recs[to] = mi->mi_set->recs[from]; /* structure assignment */ + to++; + } return 0; } diff --git a/lib/rpmdb.h b/lib/rpmdb.h index dc21184..a4b61bc 100644 --- a/lib/rpmdb.h +++ b/lib/rpmdb.h @@ -144,18 +144,6 @@ int rpmdbAppendIterator(rpmdbMatchIterator mi, const int * hdrNums, int nHdrNums); /** \ingroup rpmdb - * Remove items from set of package instances to iterate. - * @note Sorted hdrNums are always passed in rpmlib. - * @param mi rpm database iterator - * @param hdrNums array of package instances - * @param nHdrNums number of elements in array - * @param sorted is the array sorted? (array will be sorted on return) - * @return 0 on success, 1 on failure (bad args) - */ -int rpmdbPruneIterator(rpmdbMatchIterator mi, - int * hdrNums, int nHdrNums, int sorted); - -/** \ingroup rpmdb * Add pattern to iterator selector. * @param mi rpm database iterator * @param tag rpm tag diff --git a/lib/rpmdb_internal.h b/lib/rpmdb_internal.h index 1f7be34..c738d68 100644 --- a/lib/rpmdb_internal.h +++ b/lib/rpmdb_internal.h @@ -56,6 +56,25 @@ int rpmdbExtendIterator(rpmdbMatchIterator mi, */ void rpmdbSortIterator(rpmdbMatchIterator mi); +/* avoid importing rpmts_internal.h */ +#undef HASHTYPE +#undef HTKEYTYPE +#undef HTDATATYPE +#define HASHTYPE intHash +#define HTKEYTYPE unsigned int +#include "rpmhash.H" +#undef HASHTYPE +#undef HTKEYTYPE + +/** \ingroup rpmdb + * Remove items from set of package instances to iterate. + * @note Sorted hdrNums are always passed in rpmlib. + * @param mi rpm database iterator + * @param hdrNums hash of package instances + * @return 0 on success, 1 on failure (bad args) + */ +int rpmdbPruneIterator(rpmdbMatchIterator mi, intHash hdrNums); + #ifndef __APPLE__ /** * * Mergesort, same arguments as qsort(2). diff --git a/lib/rpmts.c b/lib/rpmts.c index 433160d..1688889 100644 --- a/lib/rpmts.c +++ b/lib/rpmts.c @@ -515,6 +515,18 @@ void rpmtsClean(rpmts ts) rpmtsCleanProblems(ts); } +/* hash comparison function */ +static int uintCmp(unsigned int a, unsigned int b) +{ + return (a != b); +} + +/* "hash"function*/ +static unsigned int uintId(unsigned int a) +{ + return a; +} + void rpmtsEmpty(rpmts ts) { tsMembers tsmem = rpmtsMembers(ts); @@ -528,7 +540,7 @@ void rpmtsEmpty(rpmts ts) } tsmem->orderCount = 0; - tsmem->numRemovedPackages = 0; + intHashEmpty(tsmem->removedPackages); return; } @@ -577,7 +589,7 @@ rpmts rpmtsFree(rpmts ts) (void) rpmtsCloseDB(ts); - tsmem->removedPackages = _free(tsmem->removedPackages); + tsmem->removedPackages = intHashFree(tsmem->removedPackages); tsmem->order = _free(tsmem->order); ts->members = _free(ts->members); @@ -906,13 +918,9 @@ rpmts rpmtsCreate(void) } tsmem = xcalloc(1, sizeof(*ts->members)); - tsmem->delta = 5; tsmem->numAddedPackages = 0; tsmem->addedPackages = NULL; - tsmem->numRemovedPackages = 0; - tsmem->allocedRemovedPackages = tsmem->delta; - tsmem->removedPackages = xcalloc(tsmem->allocedRemovedPackages, - sizeof(*tsmem->removedPackages)); + tsmem->removedPackages = intHashCreate(128, uintId, uintCmp, NULL); tsmem->orderAlloced = 0; tsmem->orderCount = 0; tsmem->order = NULL; diff --git a/lib/rpmts_internal.h b/lib/rpmts_internal.h index 551cdd7..f70401a 100644 --- a/lib/rpmts_internal.h +++ b/lib/rpmts_internal.h @@ -12,9 +12,7 @@ typedef struct diskspaceInfo_s * rpmDiskSpaceInfo; /* Transaction set elements information */ typedef struct tsMembers_s { - int * removedPackages; /*!< Set of packages being removed. */ - int numRemovedPackages; /*!< No. removed package instances. */ - int allocedRemovedPackages; /*!< Size of removed packages array. */ + intHash removedPackages; /*!< Set of packages being removed. */ rpmal addedPackages; /*!< Set of packages being installed. */ int numAddedPackages; /*!< No. added package instances. */ diff --git a/lib/transaction.c b/lib/transaction.c index 93903fb..e9e1f0c 100644 --- a/lib/transaction.c +++ b/lib/transaction.c @@ -954,14 +954,7 @@ void checkInstalledFiles(rpmts ts, uint64_t fileCount, rpmFpHash ht, fingerPrint /* Is this package being removed? */ installedPkg = rpmdbGetIteratorOffset(mi); - beingRemoved = 0; - if (tsmem->removedPackages != NULL) - for (j = 0; j < tsmem->numRemovedPackages; j++) { - if (tsmem->removedPackages[j] != installedPkg) - continue; - beingRemoved = 1; - break; - } + beingRemoved = intHashHasEntry(tsmem->removedPackages, installedPkg); h = headerLink(h); headerGet(h, RPMTAG_BASENAMES, &bnames, hgflags); -- 2.7.4