Browse Source

Import 470750fa2e3cf987f10de48451b1ee13aab03907 from ctdb:
tdb: detect tdb store of identical records and skip

This can help with ldb where we rewrite the index records
(cherry picked from samba commit d4c0e8fdf063f88032c32de7ece60d502b322089)

Signed-off-by: Stefan Metzmacher <metze@samba.org>

Rusty Russell 16 years ago
parent
commit
e55e729c18
1 changed files with 20 additions and 0 deletions
  1. 20 0
      ccan/tdb/tdb.c

+ 20 - 0
ccan/tdb/tdb.c

@@ -122,6 +122,7 @@ tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t has
 	return rec_ptr;
 	return rec_ptr;
 }
 }
 
 
+static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
 
 
 /* update an entry in place - this only works if the new data size
 /* update an entry in place - this only works if the new data size
    is <= the old data size and the key exists.
    is <= the old data size and the key exists.
@@ -136,6 +137,25 @@ static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
 	if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
 	if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
 		return -1;
 		return -1;
 
 
+	/* it could be an exact duplicate of what is there - this is
+	 * surprisingly common (eg. with a ldb re-index). */
+	if (rec.key_len == key.dsize && 
+	    rec.data_len == dbuf.dsize &&
+	    rec.full_hash == hash) {
+		TDB_DATA data = _tdb_fetch(tdb, key);
+		if (data.dsize == dbuf.dsize &&
+		    memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
+			if (data.dptr) {
+				free(data.dptr);
+			}
+			return 0;
+		}
+		if (data.dptr) {
+			free(data.dptr);
+		}
+	}
+	 
+
 	/* must be long enough key, data and tailer */
 	/* must be long enough key, data and tailer */
 	if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
 	if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
 		tdb->ecode = TDB_SUCCESS; /* Not really an error */
 		tdb->ecode = TDB_SUCCESS; /* Not really an error */