Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: DBM problem

by merlyn (Sage)
on Feb 15, 2001 at 19:24 UTC ( #58620=note: print w/replies, xml ) Need Help??

in reply to DBM problem

DBMs don't, in general, shrink. It's very difficult to give "this block" back to the O/S... only the blocks at the end. (Very similar in fact to the allocation of memory in traditional Unix.)

If you're concerned, you can simply create a new DBM. If you've got enough memory, pull the hash into memory, close the DBM, delete the files, and reopen it and re-store, like:

dbmopen %FOO, "my_db", 0666 or die; ... populate %FOO ... delete some stuff from %FOO { my %TEMP = %FOO; # cache it in memory dbmclose %FOO; unlink <my_db*>; # danger, but general enough {grin} dbmopen %FOO, "my_db", 0666 or die; %FOO = %TEMP; }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re (tilly) 2: DBM problem
by tilly (Archbishop) on Feb 15, 2001 at 23:13 UTC
Rea: DBM problem
by baku (Scribe) on Feb 15, 2001 at 21:11 UTC

    If you're concerned about disc space, though, I'm going to assume that the files are very large, ergo, unlikely to fit in memory... you'd likely want to do something like:

    dbmopen %ORIG, "original", 0666 or die; dbmopen %NOVA, "new", 0666 or die; # -- race condition start for my $key (keys %ORIG) { $NOVA{$key} = $ORIG{$key}; } dbmclose %ORIG; dbmclose %NOVA; unlink "original" link "new", "original" # race condition end unlink "new";

    So long as no-one else needs to get at your DBM while you're "shrinking" it, the above will work.

      But keys() will still build quite a large list. You may prefer while (my($k,$v) = each %hash) instead.

      japhy -- Perl and Regex Hacker

        Better yet :-)

      That won't work unless you know how to turn "new" into the actual list of files involved for that database. Perhaps "new.db", or even "new.dir" and "".

      -- Randal L. Schwartz, Perl hacker

Re: Re: DBM problem
by Pahrohfit (Sexton) on Feb 15, 2001 at 21:48 UTC
    I can't quote an exact answer or url to give you, but I experienced the same thing when I started using hash databaes for tasks. This is the PROPER behavior for a DBM, so if your worried about disk usage, revert back to a flat text file or get into something more robust like MySQL.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://58620]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2020-08-15 14:04 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (78 votes). Check out past polls.