Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: DBM problem

by merlyn (Sage)
on Feb 15, 2001 at 14: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'.
Rea: DBM problem
by baku (Scribe) on Feb 15, 2001 at 16: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 "new.page".

      -- Randal L. Schwartz, Perl hacker

Re (tilly) 2: DBM problem
by tilly (Archbishop) on Feb 15, 2001 at 18:13 UTC
Re: Re: DBM problem
by Pahrohfit (Sexton) on Feb 15, 2001 at 16: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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2019-08-25 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?