Speed up clear()
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Engine.pm
index 8f6a8f8..dc6b14c 100644 (file)
@@ -86,6 +86,8 @@ is the following:
 
 =item * get_next_key
 
+=item * clear
+
 =item * setup_fh
 
 =item * begin_work
@@ -556,7 +558,7 @@ sub get_next_key {
     my ($obj, $prev_key) = @_;
 
     # XXX Need to add logic about resetting the iterator if any key in the reference has changed
-    unless ( $prev_key ) {
+    unless ( defined $prev_key ) {
         $obj->{iterator} = DBM::Deep::Iterator->new({
             base_offset => $obj->_base_offset,
             engine      => $self,
@@ -566,6 +568,28 @@ sub get_next_key {
     return $obj->{iterator}->get_next_key( $obj );
 }
 
+=head2 clear( $obj )
+
+This takes an object that provides _base_offset() and deletes all its 
+elements, returning nothing.
+
+=cut
+
+sub clear {
+    my $self = shift;
+    my $obj = shift;
+
+    my $sector = $self->_load_sector( $obj->_base_offset )
+        or return;
+
+    if ( $sector->staleness != $obj->_staleness ) {
+        return;
+    }
+
+    $sector->clear;
+    return;
+}
+
 =head2 setup_fh( $obj )
 
 This takes an object that provides _base_offset(). It will do everything needed