Skip to content

Commit

Permalink
ndb: implement index regeneration if the index is out of sync
Browse files Browse the repository at this point in the history
We compare the user generation stored in the index database with
the generation count from the package database. In case there
is a mismatch, we delete all the index databases and rely on the
already existing missing index creation code to rebuild the
database.
  • Loading branch information
mlschroe authored and pmatilai committed Jan 13, 2020
1 parent 49d43a3 commit 40269d4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/backend/ndb/glue.c
Expand Up @@ -80,6 +80,31 @@ static int ndb_Close(dbiIndex dbi, unsigned int flags)
return 0;
}

static void ndb_CheckIndexSync(rpmpkgdb pkgdb, rpmxdb xdb)
{
unsigned int generation, xdb_generation;
if (!pkgdb || !xdb)
return;
if (rpmpkgLock(pkgdb, 0))
return;
if (rpmpkgGeneration(pkgdb, &generation)) {
rpmpkgUnlock(pkgdb, 0);
return;
}
if (!rpmxdbGetUserGeneration(xdb, &xdb_generation) && generation == xdb_generation) {
rpmpkgUnlock(pkgdb, 0);
return;
}
rpmpkgUnlock(pkgdb, 0);
/* index corrupt or with different generation */
if (rpmxdbIsRdonly(xdb)) {
rpmlog(RPMLOG_WARNING, _("Detected outdated index databases\n"));
} else {
rpmlog(RPMLOG_WARNING, _("Rebuilding outdated index databases\n"));
rpmxdbDelAllBlobs(xdb);
}
}

static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
{
const char *dbhome = rpmdbHome(rdb);
Expand Down Expand Up @@ -130,6 +155,7 @@ static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
}
if (!ndbenv->xdb) {
char *path = rstrscat(NULL, dbhome, "/Index.db", NULL);
int created = 0;
rpmlog(RPMLOG_DEBUG, "opening db index %s mode=0x%x\n", path, rdb->db_mode);

/* Open indexes readwrite if possible */
Expand All @@ -144,6 +170,7 @@ static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
} else if (rc && errno == ENOENT) {
ioflags = O_CREAT|O_RDWR;
rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
created = 1;
}
if (rc) {
perror("rpmxdbOpen");
Expand All @@ -153,6 +180,8 @@ static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
}
free(path);
rpmxdbSetFsync(ndbenv->xdb, ndbenv->dofsync);
if (!created)
ndb_CheckIndexSync(ndbenv->pkgdb, ndbenv->xdb);
}
if (rpmxdbLookupBlob(ndbenv->xdb, &id, rpmtag, 0, 0) == RPMRC_NOTFOUND) {
dbi->dbi_flags |= DBI_CREATED;
Expand Down

0 comments on commit 40269d4

Please sign in to comment.