dictionary - Delete a key-value pair in BerkeleyDB -
is there way delete key-value pair key start sub-string1 , ends sub-string2 in berkeleydb without iterating through keys in db?
for ex:
$sub1 = "b015";
$sub2 = "5646";
i want delete
$key = "b015hgujj75646"
note: guaranteed there 1 key combination of $sub1 , $sub2.
this can done taking iterator of db , checking every key condition, in-efficient large dbs. there way without iterating through complete db?
if you're using recno database, you're out of luck. but, if can use btree, have couple of options.
first, , easiest iterate on portion of database makes sense. assuming you're using default key comparison function, can use db_set_range position starting cursor (iterator) @ start of partial key string. in example, might "b0150000000000". scan forwards db_next, looking @ each key in turn. when either find key you're looking for, or if key find doesn't start "b015", you're done.
another technique applicable situation redefine key comparison function. if, state, there 1 combination of $sub1 , $sub2, perhaps need compare sections of keys guarantee uniqueness? here's example of full string comparison (i'm assuming you're using perl, syntax supplied above) https://www2.informatik.hu-berlin.de/themen/manuals/perl/db_file.html :
sub compare { ($key1, $key2) = @_ ; "\l$key1" cmp "\l$key2" ; } $db_btree->{compare} = 'compare' ; so, if can rig things such you're comparing starting , ending 4 characters, should able drop database iterator directly onto key you're interested in.
Comments
Post a Comment