Revert #19498 since it broke threaded builds.
[p5sagit/p5-mst-13.2.git] / lib / Tie / File.pm
index 4f9b2f2..a478688 100644 (file)
@@ -1,15 +1,20 @@
 
 package Tie::File;
-use Carp;
-use POSIX 'SEEK_SET';
-use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX';
 require 5.005;
+use Carp ':DEFAULT', 'confess';
+use POSIX 'SEEK_SET';
+use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX', 'LOCK_SH', 'O_WRONLY', 'O_RDONLY';
+sub O_ACCMODE () { O_RDONLY | O_RDWR | O_WRONLY }
 
-$VERSION = "0.50";
+
+$VERSION = "0.95";
 my $DEFAULT_MEMORY_SIZE = 1<<21;    # 2 megabytes
+my $DEFAULT_AUTODEFER_THRESHHOLD = 3; # 3 records
+my $DEFAULT_AUTODEFER_FILELEN_THRESHHOLD = 65536; # 16 disk blocksful
 
-my %good_opt = map {$_ => 1, "-$_" => 1} 
-               qw(memory dw_size mode recsep discipline autochomp);
+my %good_opt = map {$_ => 1, "-$_" => 1}
+                 qw(memory dw_size mode recsep discipline 
+                    autodefer autochomp autodefer_threshhold);
 
 sub TIEARRAY {
   if (@_ % 2 != 0) {
@@ -32,7 +37,7 @@ sub TIEARRAY {
     # default is the larger of the default cache size and the 
     # deferred-write buffer size (if specified)
     $opts{memory} = $DEFAULT_MEMORY_SIZE;
-    $opts{memory} = $opts{dw_size} 
+    $opts{memory} = $opts{dw_size}
       if defined $opts{dw_size} && $opts{dw_size} > $DEFAULT_MEMORY_SIZE;
     # Dora Winifred Read
   }
@@ -44,12 +49,19 @@ sub TIEARRAY {
   $opts{defer} = 0 unless defined $opts{defer};
   $opts{deferred} = {};         # no records are presently deferred
   $opts{deferred_s} = 0;        # count of total bytes in ->{deferred}
+  $opts{deferred_max} = -1;     # empty
+
+  # What's a good way to arrange that this class can be overridden?
+  $opts{cache} = Tie::File::Cache->new($opts{memory});
 
-  # the cache is a hash instead of an array because it is likely to be
-  # sparsely populated
-  $opts{cache} = {}; 
-  $opts{cached} = 0;   # total size of cached data
-  $opts{lru} = [];     # replace with heap in later version
+  # autodeferment is enabled by default
+  $opts{autodefer} = 1 unless defined $opts{autodefer};
+  $opts{autodeferring} = 0;     # but is not initially active
+  $opts{ad_history} = [];
+  $opts{autodefer_threshhold} = $DEFAULT_AUTODEFER_THRESHHOLD
+    unless defined $opts{autodefer_threshhold};
+  $opts{autodefer_filelen_threshhold} = $DEFAULT_AUTODEFER_FILELEN_THRESHHOLD
+    unless defined $opts{autodefer_filelen_threshhold};
 
   $opts{offsets} = [0];
   $opts{filename} = $file;
@@ -63,7 +75,10 @@ sub TIEARRAY {
 
   $opts{autochomp} = 1 unless defined $opts{autochomp};
 
-  my $mode = defined($opts{mode}) ? $opts{mode} : O_CREAT|O_RDWR;
+  $opts{mode} = O_CREAT|O_RDWR unless defined $opts{mode};
+  $opts{rdonly} = (($opts{mode} & O_ACCMODE) == O_RDONLY);
+  $opts{sawlastrec} = undef;
+
   my $fh;
 
   if (UNIVERSAL::isa($file, 'GLOB')) {
@@ -80,7 +95,7 @@ sub TIEARRAY {
     croak "usage: tie \@array, $pack, filename, [option => value]...";
   } else {
     $fh = \do { local *FH };   # only works in 5.005 and later
-    sysopen $fh, $file, $mode, 0666 or return;
+    sysopen $fh, $file, $opts{mode}, 0666 or return;
     binmode $fh;
   }
   { my $ofh = select $fh; $| = 1; select $ofh } # autoflush on write
@@ -97,9 +112,16 @@ sub TIEARRAY {
 
 sub FETCH {
   my ($self, $n) = @_;
-  my $rec = exists $self->{deferred}{$n}
-                 ? $self->{deferred}{$n} : $self->_fetch($n);
-  $self->_chomp1($rec);
+  my $rec;
+
+  # check the defer buffer
+  $rec = $self->{deferred}{$n} if exists $self->{deferred}{$n};
+  $rec = $self->_fetch($n) unless defined $rec;
+
+  # inlined _chomp1
+  substr($rec, - $self->{recseplen}) = ""
+    if defined $rec && $self->{autochomp};
+  $rec;
 }
 
 # Chomp many records in-place; return nothing useful
@@ -127,11 +149,12 @@ sub _fetch {
   my ($self, $n) = @_;
 
   # check the record cache
-  { my $cached = $self->_check_cache($n);
+  { my $cached = $self->{cache}->lookup($n);
     return $cached if defined $cached;
   }
 
-  unless ($#{$self->{offsets}} >= $n) {
+  if ($#{$self->{offsets}} < $n) {
+    return if $self->{eof};  # request for record beyond end of file
     my $o = $self->_fill_offsets_to($n);
     # If it's still undefined, there is no such record, so return 'undef'
     return unless defined $o;
@@ -153,54 +176,57 @@ sub _fetch {
 #    }
 #  }
 
-  $self->_cache_insert($n, $rec) if defined $rec;
+  $self->{cache}->insert($n, $rec) if defined $rec && not $self->{flushing};
   $rec;
 }
 
 sub STORE {
   my ($self, $n, $rec) = @_;
+  die "STORE called from _check_integrity!" if $DIAGNOSTIC;
 
   $self->_fixrecs($rec);
 
-  return $self->_store_deferred($n, $rec) if $self->{defer};
+  if ($self->{autodefer}) {
+    $self->_annotate_ad_history($n);
+  }
+
+  return $self->_store_deferred($n, $rec) if $self->_is_deferring;
+
 
   # We need this to decide whether the new record will fit
   # It incidentally populates the offsets table 
   # Note we have to do this before we alter the cache
+  # 20020324 Wait, but this DOES alter the cache.  TODO BUG?
   my $oldrec = $self->_fetch($n);
 
-  if (my $cached = $self->_check_cache($n)) {
-    my $len_diff = length($rec) - length($cached);
-    $self->{cache}{$n} = $rec;
-    $self->{cached} += $len_diff;
-    $self->_cache_flush if $len_diff > 0 && $self->_cache_too_full;
-  }
-
   if (not defined $oldrec) {
     # We're storing a record beyond the end of the file
     $self->_extend_file_to($n+1);
     $oldrec = $self->{recsep};
   }
+#  return if $oldrec eq $rec;    # don't bother
   my $len_diff = length($rec) - length($oldrec);
 
   # length($oldrec) here is not consistent with text mode  TODO XXX BUG
-  $self->_twrite($rec, $self->{offsets}[$n], length($oldrec));
-
-  # now update the offsets
-  # array slice goes from element $n+1 (the first one to move)
-  # to the end
-  for (@{$self->{offsets}}[$n+1 .. $#{$self->{offsets}}]) {
-    $_ += $len_diff;
-  }
+  $self->_mtwrite($rec, $self->{offsets}[$n], length($oldrec));
+  $self->_oadjust([$n, 1, $rec]);
+  $self->{cache}->update($n, $rec);
 }
 
 sub _store_deferred {
   my ($self, $n, $rec) = @_;
-  $self->_uncache($n);
+  $self->{cache}->remove($n);
   my $old_deferred = $self->{deferred}{$n};
+
+  if (defined $self->{deferred_max} && $n > $self->{deferred_max}) {
+    $self->{deferred_max} = $n;
+  }
   $self->{deferred}{$n} = $rec;
-  $self->{deferred_s} += length($rec);
-  $self->{deferred_s} -= length($old_deferred) if defined $old_deferred;
+
+  my $len_diff = length($rec);
+  $len_diff -= length($old_deferred) if defined $old_deferred;
+  $self->{deferred_s} += $len_diff;
+  $self->{cache}->adj_limit(-$len_diff);
   if ($self->{deferred_s} > $self->{dw_size}) {
     $self->_flush;
   } elsif ($self->_cache_too_full) {
@@ -214,30 +240,38 @@ sub _delete_deferred {
   my ($self, $n) = @_;
   my $rec = delete $self->{deferred}{$n};
   return unless defined $rec;
+
+  if (defined $self->{deferred_max} 
+      && $n == $self->{deferred_max}) {
+    undef $self->{deferred_max};
+  }
+
   $self->{deferred_s} -= length $rec;
+  $self->{cache}->adj_limit(length $rec);
 }
 
 sub FETCHSIZE {
   my $self = shift;
-  my $n = $#{$self->{offsets}};
-  # 20020317 Change this to binary search
-  while (defined ($self->_fill_offsets_to($n+1))) {
-    ++$n;
-  }
-  for my $k (keys %{$self->{deferred}}) {
-    $n = $k+1 if $n < $k+1;
-  }
+  my $n = $self->{eof} ? $#{$self->{offsets}} : $self->_fill_offsets;
+
+  my $top_deferred = $self->_defer_max;
+  $n = $top_deferred+1 if defined $top_deferred && $n < $top_deferred+1;
   $n;
 }
 
 sub STORESIZE {
   my ($self, $len) = @_;
+
+  if ($self->{autodefer}) {
+    $self->_annotate_ad_history('STORESIZE');
+  }
+
   my $olen = $self->FETCHSIZE;
   return if $len == $olen;      # Woo-hoo!
 
   # file gets longer
   if ($len > $olen) {
-    if ($self->{defer}) {
+    if ($self->_is_deferring) {
       for ($olen .. $len-1) {
         $self->_store_deferred($_, $self->{recsep});
       }
@@ -248,24 +282,31 @@ sub STORESIZE {
   }
 
   # file gets shorter
-  if ($self->{defer}) {
+  if ($self->_is_deferring) {
+    # TODO maybe replace this with map-plus-assignment?
     for (grep $_ >= $len, keys %{$self->{deferred}}) {
       $self->_delete_deferred($_);
     }
+    $self->{deferred_max} = $len-1;
   }
 
   $self->_seek($len);
   $self->_chop_file;
   $#{$self->{offsets}} = $len;
 #  $self->{offsets}[0] = 0;      # in case we just chopped this
-  my @cached = grep $_ >= $len, keys %{$self->{cache}};
-  $self->_uncache(@cached);
+
+  $self->{cache}->remove(grep $_ >= $len, $self->{cache}->ckeys);
 }
 
+### OPTIMIZE ME
+### It should not be necessary to do FETCHSIZE
+### Just seek to the end of the file.
 sub PUSH {
   my $self = shift;
   $self->SPLICE($self->FETCHSIZE, scalar(@_), @_);
-#  $self->FETCHSIZE;  # av.c takes care of this for me
+
+  # No need to return:
+  #  $self->FETCHSIZE;  # because av.c takes care of this for me
 }
 
 sub POP {
@@ -288,29 +329,27 @@ sub UNSHIFT {
 }
 
 sub CLEAR {
-  # And enable auto-defer mode, since it's likely that they just
-  # did @a = (...); 
-  #
-  # 20020316
-  # Maybe that's too much dwimmery.  But stuffing a fake '-1' into the
-  # autodefer history might not be too much.  If you did that, you
-  # could also special-case [ -1, 0 ], which might not be too much.
   my $self = shift;
+
+  if ($self->{autodefer}) {
+    $self->_annotate_ad_history('CLEAR');
+  }
+
   $self->_seekb(0);
   $self->_chop_file;
-  %{$self->{cache}}   = ();
-    $self->{cached}   = 0;
-  @{$self->{lru}}     = ();
+    $self->{cache}->set_limit($self->{memory});
+    $self->{cache}->empty;
   @{$self->{offsets}} = (0);
   %{$self->{deferred}}= ();
     $self->{deferred_s} = 0;
+    $self->{deferred_max} = -1;
 }
 
 sub EXTEND {
   my ($self, $n) = @_;
 
   # No need to pre-extend anything in this case
-  return if $self->{defer};
+  return if $self->_is_deferring;
 
   $self->_fill_offsets_to($n);
   $self->_extend_file_to($n);
@@ -318,14 +357,19 @@ sub EXTEND {
 
 sub DELETE {
   my ($self, $n) = @_;
+
+  if ($self->{autodefer}) {
+    $self->_annotate_ad_history('DELETE');
+  }
+
   my $lastrec = $self->FETCHSIZE-1;
   my $rec = $self->FETCH($n);
-  $self->_delete_deferred($n) if $self->{defer};
+  $self->_delete_deferred($n) if $self->_is_deferring;
   if ($n == $lastrec) {
     $self->_seek($n);
     $self->_chop_file;
     $#{$self->{offsets}}--;
-    $self->_uncache($n);
+    $self->{cache}->remove($n);
     # perhaps in this case I should also remove trailing null records?
     # 20020316
     # Note that delete @a[-3..-1] deletes the records in the wrong order,
@@ -340,13 +384,17 @@ sub DELETE {
 sub EXISTS {
   my ($self, $n) = @_;
   return 1 if exists $self->{deferred}{$n};
-  $self->_fill_offsets_to($n);  # I think this is unnecessary
   $n < $self->FETCHSIZE;
 }
 
 sub SPLICE {
   my $self = shift;
-  $self->_flush if $self->{defer};
+
+  if ($self->{autodefer}) {
+    $self->_annotate_ad_history('SPLICE');
+  }
+
+  $self->_flush if $self->_is_deferring; # move this up?
   if (wantarray) {
     $self->_chomp(my @a = $self->_splice(@_));
     @a;
@@ -357,7 +405,8 @@ sub SPLICE {
 
 sub DESTROY {
   my $self = shift;
-  $self->flush if $self->{defer};
+  $self->flush if $self->_is_deferring;
+  $self->{cache}->delink if defined $self->{cache}; # break circular link
 }
 
 sub _splice {
@@ -384,6 +433,18 @@ sub _splice {
       return unless @data;
       $pos = $oldsize;          # This is what perl does for normal arrays
     }
+
+    # The manual is very unclear here
+    if ($nrecs < 0) {
+      $nrecs = $oldsize - $pos + $nrecs;
+      $nrecs = 0 if $nrecs < 0;
+    }
+
+    # nrecs is too big---it really means "until the end"
+    # 20030507
+    if ($nrecs + $pos > $oldsize) {
+      $nrecs = $oldsize - $pos;
+    }
   }
 
   $self->_fixrecs(@data);
@@ -392,98 +453,67 @@ sub _splice {
   my $oldlen = 0;
 
   # compute length of data being removed
-  # Incidentally fills offsets table
   for ($pos .. $pos+$nrecs-1) {
+    last unless defined $self->_fill_offsets_to($_);
     my $rec = $self->_fetch($_);
     last unless defined $rec;
     push @result, $rec;
-    $oldlen += length($rec);
-  }
-
-  # Modify the file
-  $self->_twrite($data, $self->{offsets}[$pos], $oldlen);
-
-  # update the offsets table part 1
-  # compute the offsets of the new records:
-  my @new_offsets;
-  if (@data) {
-    push @new_offsets, $self->{offsets}[$pos];
-    for (0 .. $#data-1) {
-      push @new_offsets, $new_offsets[-1] + length($data[$_]);
-    }
-  }
-  splice(@{$self->{offsets}}, $pos, $nrecs, @new_offsets);
 
-  # update the offsets table part 2
-  # adjust the offsets of the following old records
-  for ($pos+@data .. $#{$self->{offsets}}) {
-    $self->{offsets}[$_] += $datalen - $oldlen;
+    # Why don't we just use length($rec) here?
+    # Because that record might have come from the cache.  _splice
+    # might have been called to flush out the deferred-write records,
+    # and in this case length($rec) is the length of the record to be
+    # *written*, not the length of the actual record in the file.  But
+    # the offsets are still true. 20020322
+    $oldlen += $self->{offsets}[$_+1] - $self->{offsets}[$_]
+      if defined $self->{offsets}[$_+1];
   }
-  # If we scrubbed out all known offsets, regenerate the trivial table
-  # that knows that the file does indeed start at 0.
-  $self->{offsets}[0] = 0 unless @{$self->{offsets}};
-
-  # Perhaps the following cache foolery could be factored out
-  # into a bunch of mor opaque cache functions.  For example,
-  # it's odd to delete a record from the cache and then remove
-  # it from the LRU queue later on; there should be a function to
-  # do both at once.
+  $self->_fill_offsets_to($pos+$nrecs);
 
-  # update the read cache, part 1
-  # modified records
-  # Consider this carefully for correctness
-  for ($pos .. $pos+$nrecs-1) {
-    my $cached = $self->{cache}{$_};
-    next unless defined $cached;
-    my $new = $data[$_-$pos];
-    if (defined $new) {
-      $self->{cached} += length($new) - length($cached);
-      $self->{cache}{$_} = $new;
-    } else {
-      $self->_uncache($_);
-    }
-  }
-  # update the read cache, part 2
-  # moved records - records past the site of the change
-  # need to be renumbered
-  # Maybe merge this with the previous block?
-  {
-    my %adjusted;
-    for (keys %{$self->{cache}}) {
-      next unless $_ >= $pos + $nrecs;
-      $adjusted{$_-$nrecs+@data} = delete $self->{cache}{$_};
+  # Modify the file
+  $self->_mtwrite($data, $self->{offsets}[$pos], $oldlen);
+  # Adjust the offsets table
+  $self->_oadjust([$pos, $nrecs, @data]);
+
+  { # Take this read cache stuff out into a separate function
+    # You made a half-attempt to put it into _oadjust.  
+    # Finish something like that up eventually.
+    # STORE also needs to do something similarish
+
+    # update the read cache, part 1
+    # modified records
+    for ($pos .. $pos+$nrecs-1) {
+      my $new = $data[$_-$pos];
+      if (defined $new) {
+        $self->{cache}->update($_, $new);
+      } else {
+        $self->{cache}->remove($_);
+      }
     }
-    @{$self->{cache}}{keys %adjusted} = values %adjusted;
-#    for (keys %{$self->{cache}}) {
-#      next unless $_ >= $pos + $nrecs;
-#      $self->{cache}{$_-$nrecs+@data} = delete $self->{cache}{$_};
-#    }
-  }
     
-  # fix the LRU queue
-  my(@new, @changed);
-  for (@{$self->{lru}}) {
-    if ($_ >= $pos + $nrecs) {
-      push @new, $_ + @data - $nrecs;
-    } elsif ($_ >= $pos) {
-      push @changed, $_ if $_ < $pos + @data;
-    } else {
-      push @new, $_;
+    # update the read cache, part 2
+    # moved records - records past the site of the change
+    # need to be renumbered
+    # Maybe merge this with the previous block?
+    {
+      my @oldkeys = grep $_ >= $pos + $nrecs, $self->{cache}->ckeys;
+      my @newkeys = map $_-$nrecs+@data, @oldkeys;
+      $self->{cache}->rekey(\@oldkeys, \@newkeys);
     }
-  }
-  @{$self->{lru}} = (@new, @changed);
 
-  # Now there might be too much data in the cache, if we spliced out
-  # some short records and spliced in some long ones.  If so, flush
-  # the cache.
-  $self->_cache_flush;
+    # Now there might be too much data in the cache, if we spliced out
+    # some short records and spliced in some long ones.  If so, flush
+    # the cache.
+    $self->_cache_flush;
+  }
 
   # Yes, the return value of 'splice' *is* actually this complicated
   wantarray ? @result : @result ? $result[-1] : undef;
 }
 
+
 # write data into the file
-# $data is the data to be written. 
+# $data is the data to be written.
 # it should be written at position $pos, and should overwrite
 # exactly $len of the following bytes.  
 # Note that if length($data) > $len, the subsequent bytes will have to 
@@ -512,6 +542,7 @@ sub _twrite {
   my $bufsize = _bufsize($len_diff);
   my ($writepos, $readpos) = ($pos, $pos+$len);
   my $next_block;
+  my $more_data;
 
   # Seems like there ought to be a way to avoid the repeated code
   # and the special case here.  The read(1) is also a little weird.
@@ -519,7 +550,7 @@ sub _twrite {
   do {
     $self->_seekb($readpos);
     my $br = read $self->{fh}, $next_block, $bufsize;
-    my $more_data = read $self->{fh}, my($dummy), 1;
+    $more_data = read $self->{fh}, my($dummy), 1;
     $self->_seekb($writepos);
     $self->_write_record($data);
     $readpos += $br;
@@ -533,11 +564,235 @@ sub _twrite {
   $self->_chop_file if $len_diff < 0;
 }
 
+# _iwrite(D, S, E)
+# Insert text D at position S.
+# Let C = E-S-|D|.  If C < 0; die.  
+# Data in [S,S+C) is copied to [S+D,S+D+C) = [S+D,E).
+# Data in [S+C = E-D, E) is returned.  Data in [E, oo) is untouched.
+#
+# In a later version, don't read the entire intervening area into
+# memory at once; do the copying block by block.
+sub _iwrite {
+  my $self = shift;
+  my ($D, $s, $e) = @_;
+  my $d = length $D;
+  my $c = $e-$s-$d;
+  local *FH = $self->{fh};
+  confess "Not enough space to insert $d bytes between $s and $e"
+    if $c < 0;
+  confess "[$s,$e) is an invalid insertion range" if $e < $s;
+
+  $self->_seekb($s);
+  read FH, my $buf, $e-$s;
+
+  $D .= substr($buf, 0, $c, "");
+
+  $self->_seekb($s);
+  $self->_write_record($D);
+
+  return $buf;
+}
+
+# Like _twrite, but the data-pos-len triple may be repeated; you may
+# write several chunks.  All the writing will be done in
+# one pass.   Chunks SHALL be in ascending order and SHALL NOT overlap.
+sub _mtwrite {
+  my $self = shift;
+  my $unwritten = "";
+  my $delta = 0;
+
+  @_ % 3 == 0 
+    or die "Arguments to _mtwrite did not come in groups of three";
+
+  while (@_) {
+    my ($data, $pos, $len) = splice @_, 0, 3;
+    my $end = $pos + $len;  # The OLD end of the segment to be replaced
+    $data = $unwritten . $data;
+    $delta -= length($unwritten);
+    $unwritten  = "";
+    $pos += $delta;             # This is where the data goes now
+    my $dlen = length $data;
+    $self->_seekb($pos);
+    if ($len >= $dlen) {        # the data will fit
+      $self->_write_record($data);
+      $delta += ($dlen - $len); # everything following moves down by this much
+      $data = ""; # All the data in the buffer has been written
+    } else {                    # won't fit
+      my $writable = substr($data, 0, $len - $delta, "");
+      $self->_write_record($writable);
+      $delta += ($dlen - $len); # everything following moves down by this much
+    } 
+
+    # At this point we've written some but maybe not all of the data.
+    # There might be a gap to close up, or $data might still contain a
+    # bunch of unwritten data that didn't fit.
+    my $ndlen = length $data;
+    if ($delta == 0) {
+      $self->_write_record($data);
+    } elsif ($delta < 0) {
+      # upcopy (close up gap)
+      if (@_) {
+        $self->_upcopy($end, $end + $delta, $_[1] - $end);  
+      } else {
+        $self->_upcopy($end, $end + $delta);  
+      }
+    } else {
+      # downcopy (insert data that didn't fit; replace this data in memory
+      # with _later_ data that doesn't fit)
+      if (@_) {
+        $unwritten = $self->_downcopy($data, $end, $_[1] - $end);
+      } else {
+        # Make the file longer to accomodate the last segment that doesn'
+        $unwritten = $self->_downcopy($data, $end);
+      }
+    }
+  }
+}
+
+# Copy block of data of length $len from position $spos to position $dpos
+# $dpos must be <= $spos
+#
+# If $len is undefined, go all the way to the end of the file
+# and then truncate it ($spos - $dpos bytes will be removed)
+sub _upcopy {
+  my $blocksize = 8192;
+  my ($self, $spos, $dpos, $len) = @_;
+  if ($dpos > $spos) {
+    die "source ($spos) was upstream of destination ($dpos) in _upcopy";
+  } elsif ($dpos == $spos) {
+    return;
+  }
+  
+  while (! defined ($len) || $len > 0) {
+    my $readsize = ! defined($len) ? $blocksize
+               : $len > $blocksize ? $blocksize
+               : $len;
+      
+    my $fh = $self->{fh};
+    $self->_seekb($spos);
+    my $bytes_read = read $fh, my($data), $readsize;
+    $self->_seekb($dpos);
+    if ($data eq "") { 
+      $self->_chop_file;
+      last;
+    }
+    $self->_write_record($data);
+    $spos += $bytes_read;
+    $dpos += $bytes_read;
+    $len -= $bytes_read if defined $len;
+  }
+}
+
+# Write $data into a block of length $len at position $pos,
+# moving everything in the block forwards to make room.
+# Instead of writing the last length($data) bytes from the block
+# (because there isn't room for them any longer) return them.
+sub _downcopy {
+  my $blocksize = 8192;
+  my ($self, $data, $pos, $len) = @_;
+  my $fh = $self->{fh};
+
+  while (! defined $len || $len > 0) {
+    my $readsize = ! defined($len) ? $blocksize 
+      : $len > $blocksize? $blocksize : $len;
+    $self->_seekb($pos);
+    read $fh, my($old), $readsize;
+    $data .= $old;
+    $self->_seekb($pos);
+    my $writable = substr($data, 0, $readsize, "");
+    last if $writable eq "";
+    $self->_write_record($writable);
+    $len -= $readsize if defined $len;
+    $pos += $readsize;
+  }
+  return $data;
+}
+
+# Adjust the object data structures following an '_mtwrite'
+# Arguments are
+#  [$pos, $nrecs, @length]  items
+# indicating that $nrecs records were removed at $recpos (a record offset)
+# and replaced with records of length @length...
+# Arguments guarantee that $recpos is strictly increasing.
+# No return value
+sub _oadjust {
+  my $self = shift;
+  my $delta = 0;
+  my $delta_recs = 0;
+  my $prev_end = -1;
+  my %newkeys;
+
+  for (@_) {
+    my ($pos, $nrecs, @data) = @$_;
+    $pos += $delta_recs;
+
+    # Adjust the offsets of the records after the previous batch up
+    # to the first new one of this batch
+    for my $i ($prev_end+2 .. $pos - 1) {
+      $self->{offsets}[$i] += $delta;
+      $newkey{$i} = $i + $delta_recs;
+    }
+
+    $prev_end = $pos + @data - 1; # last record moved on this pass 
+
+    # Remove the offsets for the removed records;
+    # replace with the offsets for the inserted records
+    my @newoff = ($self->{offsets}[$pos] + $delta);
+    for my $i (0 .. $#data) {
+      my $newlen = length $data[$i];
+      push @newoff, $newoff[$i] + $newlen;
+      $delta += $newlen;
+    }
+
+    for my $i ($pos .. $pos+$nrecs-1) {
+      last if $i+1 > $#{$self->{offsets}};
+      my $oldlen = $self->{offsets}[$i+1] - $self->{offsets}[$i];
+      $delta -= $oldlen;
+    }
+
+#    # also this data has changed, so update it in the cache
+#    for (0 .. $#data) {
+#      $self->{cache}->update($pos + $_, $data[$_]);
+#    }
+#    if ($delta_recs) {
+#      my @oldkeys = grep $_ >= $pos + @data, $self->{cache}->ckeys;
+#      my @newkeys = map $_ + $delta_recs, @oldkeys;
+#      $self->{cache}->rekey(\@oldkeys, \@newkeys);
+#    }
+
+    # replace old offsets with new
+    splice @{$self->{offsets}}, $pos, $nrecs+1, @newoff;
+    # What if we just spliced out the end of the offsets table?
+    # shouldn't we clear $self->{eof}?   Test for this XXX BUG TODO
+
+    $delta_recs += @data - $nrecs; # net change in total number of records
+  }
+
+  # The trailing records at the very end of the file
+  if ($delta) {
+    for my $i ($prev_end+2 .. $#{$self->{offsets}}) {
+      $self->{offsets}[$i] += $delta;
+    }
+  }
+
+  # If we scrubbed out all known offsets, regenerate the trivial table
+  # that knows that the file does indeed start at 0.
+  $self->{offsets}[0] = 0 unless @{$self->{offsets}};
+  # If the file got longer, the offsets table is no longer complete
+  # $self->{eof} = 0 if $delta_recs > 0;
+
+  # Now there might be too much data in the cache, if we spliced out
+  # some short records and spliced in some long ones.  If so, flush
+  # the cache.
+  $self->_cache_flush;
+}
+
 # If a record does not already end with the appropriate terminator
 # string, append one.
 sub _fixrecs {
   my $self = shift;
   for (@_) {
+    $_ = "" unless defined $_;
     $_ .= $self->{recsep}
       unless substr($_, - $self->{recseplen}) eq $self->{recsep};
   }
@@ -562,9 +817,10 @@ sub _seek {
   defined($o)
     or confess("logic error: undefined offset for record $n");
   seek $self->{fh}, $o, SEEK_SET
-    or die "Couldn't seek filehandle: $!";  # "Should never happen."
+    or confess "Couldn't seek filehandle: $!";  # "Should never happen."
 }
 
+# seek to byte $b in the file
 sub _seekb {
   my ($self, $b) = @_;
   seek $self->{fh}, $b, SEEK_SET
@@ -575,33 +831,56 @@ sub _seekb {
 # return the offset of record $n
 sub _fill_offsets_to {
   my ($self, $n) = @_;
+
+  return $self->{offsets}[$n] if $self->{eof};
+
   my $fh = $self->{fh};
   local *OFF = $self->{offsets};
   my $rec;
 
   until ($#OFF >= $n) {
-    my $o = $OFF[-1];
     $self->_seek(-1);           # tricky -- see comment at _seek
     $rec = $self->_read_record;
     if (defined $rec) {
-      push @OFF, tell $fh;
+      push @OFF, int(tell $fh);  # Tels says that int() saves memory here
     } else {
+      $self->{eof} = 1;
       return;                   # It turns out there is no such record
     }
   }
 
   # we have now read all the records up to record n-1,
   # so we can return the offset of record n
-  return $OFF[$n];
+  $OFF[$n];
+}
+
+sub _fill_offsets {
+  my ($self) = @_;
+
+  my $fh = $self->{fh};
+  local *OFF = $self->{offsets};
+  
+  $self->_seek(-1);           # tricky -- see comment at _seek
+
+  # Tels says that inlining read_record() would make this loop
+  # five times faster. 20030508
+  while ( defined $self->_read_record()) {
+    # int() saves us memory here
+    push @OFF, int(tell $fh);
+  }
+
+  $self->{eof} = 1;
+  $#OFF;
 }
 
 # assumes that $rec is already suitably terminated
 sub _write_record {
   my ($self, $rec) = @_;
   my $fh = $self->{fh};
+  local $\ = "";
   print $fh $rec
     or die "Couldn't write record: $!";  # "Should never happen."
-
+#  $self->{_written} += length($rec);
 }
 
 sub _read_record {
@@ -611,65 +890,41 @@ sub _read_record {
     my $fh = $self->{fh};
     $rec = <$fh>;
   }
+  return unless defined $rec;
+  if (! $self->{sawlastrec} && 
+      substr($rec, -$self->{recseplen}) ne $self->{recsep}) {
+    # improperly terminated final record --- quietly fix it.
+#    my $ac = substr($rec, -$self->{recseplen});
+#    $ac =~ s/\n/\\n/g;
+    $self->{sawlastrec} = 1;
+    unless ($self->{rdonly}) {
+      local $\ = "";
+      my $fh = $self->{fh};
+      print $fh $self->{recsep};
+    }
+    $rec .= $self->{recsep};
+  }
+#  $self->{_read} += length($rec) if defined $rec;
   $rec;
 }
 
-################################################################
-#
-# Read cache management
-
-# Insert a record into the cache at position $n
-# Only appropriate when no data is cached for $n already
-sub _cache_insert {
-  my ($self, $n, $rec) = @_;
-
-  # Do not cache records that are too big to fit in the cache.
-  return unless length $rec <= $self->{memory};
-
-  $self->{cache}{$n} = $rec;
-  $self->{cached} += length $rec;
-  push @{$self->{lru}}, $n;     # most-recently-used is at the END
-
-  $self->_cache_flush if $self->_cache_too_full;
-}
-
-# Remove cached data for record $n, if there is any
-# (It is OK if $n is not in the cache at all)
-sub _uncache {
+sub _rw_stats {
   my $self = shift;
-  for my $n (@_) {
-    my $cached = delete $self->{cache}{$n};
-    next unless defined $cached;
-    @{$self->{lru}} = grep $_ != $n, @{$self->{lru}};
-    $self->{cached} -= length($cached);
-  }
+  @{$self}{'_read', '_written'};
 }
 
-# _check_cache promotes record $n to MRU.  Is this correct behavior?
-sub _check_cache {
-  my ($self, $n) = @_;
-  my $rec;
-  return unless defined($rec = $self->{cache}{$n});
+################################################################
+#
+# Read cache management
 
-  # cache hit; update LRU queue and return $rec
-  # replace this with a heap in a later version
-  # 20020317 This should be a separate method
-  @{$self->{lru}} = ((grep $_ ne $n, @{$self->{lru}}), $n);
-  $rec;
+sub _cache_flush {
+  my ($self) = @_;
+  $self->{cache}->reduce_size_to($self->{memory} - $self->{deferred_s});
 }
 
 sub _cache_too_full {
   my $self = shift;
-  $self->{cached} + $self->{deferred_s} > $self->{memory};
-}
-
-sub _cache_flush {
-  my ($self) = @_;
-  while ($self->_cache_too_full) {
-    my $lru = shift @{$self->{lru}};
-    my $rec = delete $self->{cache}{$lru};
-    $self->{cached} -= length $rec;
-  }
+  $self->{cache}->bytes + $self->{deferred_s} >= $self->{memory};
 }
 
 ################################################################
@@ -691,12 +946,12 @@ sub _extend_file_to {
   my $pos = $self->{offsets}[-1];
 
   # the offsets table has one entry more than the total number of records
-  $extras = $n - $#{$self->{offsets}};
+  my $extras = $n - $#{$self->{offsets}};
 
   # Todo : just use $self->{recsep} x $extras here?
   while ($extras-- > 0) {
     $self->_write_record($self->{recsep});
-    push @{$self->{offsets}}, tell $self->{fh};
+    push @{$self->{offsets}}, int(tell $self->{fh});
   }
 }
 
@@ -713,7 +968,7 @@ sub _chop_file {
 # The result should be at least $n.
 sub _bufsize {
   my $n = shift;
-  return 8192 if $n < 0;
+  return 8192 if $n <= 0;
   my $b = $n & ~8191;
   $b += 8192 if $n & 8191;
   $b;
@@ -733,7 +988,18 @@ sub flock {
   }
   my $fh = $self->{fh};
   $op = LOCK_EX unless defined $op;
-  flock $fh, $op;
+  my $locked = flock $fh, $op;
+  
+  if ($locked && ($op & (LOCK_EX | LOCK_SH))) {
+    # If you're locking the file, then presumably it's because
+    # there might have been a write access by another process.
+    # In that case, the read cache contents and the offsets table
+    # might be invalid, so discard them.  20030508
+    $self->{offsets} = [0];
+    $self->{cache}->empty;
+  }
+
+  $locked;
 }
 
 # Get/set autochomp option
@@ -748,6 +1014,25 @@ sub autochomp {
   }
 }
 
+# Get offset table entries; returns offset of nth record
+sub offset {
+  my ($self, $n) = @_;
+
+  if ($#{$self->{offsets}} < $n) {
+    return if $self->{eof};     # request for record beyond the end of file
+    my $o = $self->_fill_offsets_to($n);
+    # If it's still undefined, there is no such record, so return 'undef'
+    return unless defined $o;
+   }
+  $self->{offsets}[$n];
+}
+
+sub discard_offsets {
+  my $self = shift;
+  $self->{offsets} = [0];
+}
+
 ################################################################
 #
 # Matters related to deferred writing
@@ -756,6 +1041,8 @@ sub autochomp {
 # Defer writes
 sub defer {
   my $self = shift;
+  $self->_stop_autodeferring;
+  @{$self->{ad_history}} = ();
   $self->{defer} = 1;
 }
 
@@ -763,7 +1050,7 @@ sub defer {
 #
 # This could be better optimized to write the file in one pass, instead
 # of one pass per block of records.  But that will require modifications
-# to _twrite, so I should have a good _twite test suite first.
+# to _twrite, so I should have a good _twrite test suite first.
 sub flush {
   my $self = shift;
 
@@ -771,10 +1058,10 @@ sub flush {
   $self->{defer} = 0;
 }
 
-sub _flush {
+sub _old_flush {
   my $self = shift;
   my @writable = sort {$a<=>$b} (keys %{$self->{deferred}});
-  
+
   while (@writable) {
     # gather all consecutive records from the front of @writable
     my $first_rec = shift @writable;
@@ -790,6 +1077,40 @@ sub _flush {
   $self->_discard;               # clear out defered-write-cache
 }
 
+sub _flush {
+  my $self = shift;
+  my @writable = sort {$a<=>$b} (keys %{$self->{deferred}});
+  my @args;
+  my @adjust;
+
+  while (@writable) {
+    # gather all consecutive records from the front of @writable
+    my $first_rec = shift @writable;
+    my $last_rec = $first_rec+1;
+    ++$last_rec, shift @writable while @writable && $last_rec == $writable[0];
+    --$last_rec;
+    my $end = $self->_fill_offsets_to($last_rec+1);
+    if (not defined $end) {
+      $self->_extend_file_to($last_rec);
+      $end = $self->{offsets}[$last_rec];
+    }
+    my ($start) = $self->{offsets}[$first_rec];
+    push @args,
+         join("", @{$self->{deferred}}{$first_rec .. $last_rec}), # data
+         $start,                                                  # position
+         $end-$start;                                             # length
+    push @adjust, [$first_rec, # starting at this position...
+                   $last_rec-$first_rec+1,  # this many records...
+                   # are replaced with these...
+                   @{$self->{deferred}}{$first_rec .. $last_rec},
+                  ];
+  }
+
+  $self->_mtwrite(@args);  # write multiple record groups
+  $self->_discard;               # clear out defered-write-cache
+  $self->_oadjust(@adjust);
+}
+
 # Discard deferred writes and disable future deferred writes
 sub discard {
   my $self = shift;
@@ -800,12 +1121,116 @@ sub discard {
 # Discard deferred writes, but retain old deferred writing mode
 sub _discard {
   my $self = shift;
-  $self->{deferred} = {};
-  $self->{deferred_s} = 0;
+  %{$self->{deferred}} = ();
+  $self->{deferred_s}  = 0;
+  $self->{deferred_max}  = -1;
+  $self->{cache}->set_limit($self->{memory});
+}
+
+# Deferred writing is enabled, either explicitly ($self->{defer})
+# or automatically ($self->{autodeferring})
+sub _is_deferring {
+  my $self = shift;
+  $self->{defer} || $self->{autodeferring};
+}
+
+# The largest record number of any deferred record
+sub _defer_max {
+  my $self = shift;
+  return $self->{deferred_max} if defined $self->{deferred_max};
+  my $max = -1;
+  for my $key (keys %{$self->{deferred}}) {
+    $max = $key if $key > $max;
+  }
+  $self->{deferred_max} = $max;
+  $max;
+}
+
+################################################################
+#
+# Matters related to autodeferment
+#
+
+# Get/set autodefer option
+sub autodefer {
+  my $self = shift;
+  if (@_) {
+    my $old = $self->{autodefer};
+    $self->{autodefer} = shift;
+    if ($old) {
+      $self->_stop_autodeferring;
+      @{$self->{ad_history}} = ();
+    }
+    $old;
+  } else {
+    $self->{autodefer};
+  }
+}
+
+# The user is trying to store record #$n Record that in the history,
+# and then enable (or disable) autodeferment if that seems useful.
+# Note that it's OK for $n to be a non-number, as long as the function
+# is prepared to deal with that.  Nobody else looks at the ad_history.
+#
+# Now, what does the ad_history mean, and what is this function doing?
+# Essentially, the idea is to enable autodeferring when we see that the
+# user has made three consecutive STORE calls to three consecutive records.
+# ("Three" is actually ->{autodefer_threshhold}.)
+# A STORE call for record #$n inserts $n into the autodefer history,
+# and if the history contains three consecutive records, we enable 
+# autodeferment.  An ad_history of [X, Y] means that the most recent
+# STOREs were for records X, X+1, ..., Y, in that order.  
+#
+# Inserting a nonconsecutive number erases the history and starts over.
+#
+# Performing a special operation like SPLICE erases the history.
+#
+# There's one special case: CLEAR means that CLEAR was just called.
+# In this case, we prime the history with [-2, -1] so that if the next
+# write is for record 0, autodeferring goes on immediately.  This is for
+# the common special case of "@a = (...)".
+#
+sub _annotate_ad_history {
+  my ($self, $n) = @_;
+  return unless $self->{autodefer}; # feature is disabled
+  return if $self->{defer};     # already in explicit defer mode
+  return unless $self->{offsets}[-1] >= $self->{autodefer_filelen_threshhold};
+
+  local *H = $self->{ad_history};
+  if ($n eq 'CLEAR') {
+    @H = (-2, -1);              # prime the history with fake records
+    $self->_stop_autodeferring;
+  } elsif ($n =~ /^\d+$/) {
+    if (@H == 0) {
+      @H =  ($n, $n);
+    } else {                    # @H == 2
+      if ($H[1] == $n-1) {      # another consecutive record
+        $H[1]++;
+        if ($H[1] - $H[0] + 1 >= $self->{autodefer_threshhold}) {
+          $self->{autodeferring} = 1;
+        }
+      } else {                  # nonconsecutive- erase and start over
+        @H = ($n, $n);
+        $self->_stop_autodeferring;
+      }
+    }
+  } else {                      # SPLICE or STORESIZE or some such
+    @H = ();
+    $self->_stop_autodeferring;
+  }
+}
+
+# If autodeferring was enabled, cut it out and discard the history
+sub _stop_autodeferring {
+  my $self = shift;
+  if ($self->{autodeferring}) {
+    $self->_flush;
+  }
+  $self->{autodeferring} = 0;
 }
 
-# Not yet implemented
-sub autodefer { }
+################################################################
+
 
 # This is NOT a method.  It is here for two reasons:
 #  1. To factor a fairly complicated block out of the constructor
@@ -839,80 +1264,89 @@ sub _ci_warn {
 # with the existing test suite.
 sub _check_integrity {
   my ($self, $file, $warn) = @_;
+  my $rsl = $self->{recseplen};
+  my $rs  = $self->{recsep};
   my $good = 1; 
+  local *_;                     # local $_ does not work here
+  local $DIAGNOSTIC = 1;
+
+  if (not defined $rs) {
+    _ci_warn("recsep is undef!");
+    $good = 0;
+  } elsif ($rs eq "") {
+    _ci_warn("recsep is empty!");
+    $good = 0;
+  } elsif ($rsl != length $rs) {
+    my $ln = length $rs;
+    _ci_warn("recsep <$rs> has length $ln, should be $rsl");
+    $good = 0;
+  }
 
   if (not defined $self->{offsets}[0]) {
     _ci_warn("offset 0 is missing!");
     $good = 0;
+
   } elsif ($self->{offsets}[0] != 0) {
     _ci_warn("rec 0: offset <$self->{offsets}[0]> s/b 0!");
     $good = 0;
   }
 
-  local *_;
-  local *F = $self->{fh};
-  seek F, 0, SEEK_SET;
-  local $/ = $self->{recsep};
-  my $rsl = $self->{recseplen};
-  local $. = 0;
-
-  while (<F>) {
-    my $n = $. - 1;
-    my $cached = $self->{cache}{$n};
-    my $offset = $self->{offsets}[$.];
-    my $ao = tell F;
-    if (defined $offset && $offset != $ao) {
-      _ci_warn("rec $n: offset <$offset> actual <$ao>");
-      $good = 0;
-    }
-    if (defined $cached && $_ ne $cached) {
-      $good = 0;
-      chomp $cached;
-      chomp;
-      _ci_warn("rec $n: cached <$cached> actual <$_>");
+  my $cached = 0;
+  {
+    local *F = $self->{fh};
+    seek F, 0, SEEK_SET;
+    local $. = 0;
+    local $/ = $rs;
+
+    while (<F>) {
+      my $n = $. - 1;
+      my $cached = $self->{cache}->_produce($n);
+      my $offset = $self->{offsets}[$.];
+      my $ao = tell F;
+      if (defined $offset && $offset != $ao) {
+        _ci_warn("rec $n: offset <$offset> actual <$ao>");
+        $good = 0;
+      }
+      if (defined $cached && $_ ne $cached && ! $self->{deferred}{$n}) {
+        $good = 0;
+        _ci_warn("rec $n: cached <$cached> actual <$_>");
+      }
+      if (defined $cached && substr($cached, -$rsl) ne $rs) {
+        $good = 0;
+        _ci_warn("rec $n in the cache is missing the record separator");
+      }
+      if (! defined $offset && $self->{eof}) {
+        $good = 0;
+        _ci_warn("The offset table was marked complete, but it is missing element $.");
+      }
     }
-    if (defined $cached && substr($cached, -$rsl) ne $/) {
-      _ci_warn("rec $n in the cache is missing the record separator");
+    if (@{$self->{offsets}} > $.+1) {
+        $good = 0;
+        my $n = @{$self->{offsets}};
+        _ci_warn("The offset table has $n items, but the file has only $.");
     }
-  }
 
-  my $cached = 0;
-  while (my ($n, $r) = each %{$self->{cache}}) {
-    $cached += length($r);
-    next if $n+1 <= $.;         # checked this already
-    _ci_warn("spurious caching of record $n");
-    $good = 0;
-  }
-  if ($cached != $self->{cached}) {
-    _ci_warn("cache size is $self->{cached}, should be $cached");
-    $good = 0;
-  }
-
-  my (%seen, @duplicate);
-  for (@{$self->{lru}}) {
-    $seen{$_}++;
-    if (not exists $self->{cache}{$_}) {
-      _ci_warn("$_ is mentioned in the LRU queue, but not in the cache");
+    my $deferring = $self->_is_deferring;
+    for my $n ($self->{cache}->ckeys) {
+      my $r = $self->{cache}->_produce($n);
+      $cached += length($r);
+      next if $n+1 <= $.;         # checked this already
+      _ci_warn("spurious caching of record $n");
       $good = 0;
     }
-  }
-  @duplicate = grep $seen{$_}>1, keys %seen;
-  if (@duplicate) {
-    my $records = @duplicate == 1 ? 'Record' : 'Records';
-    my $appear  = @duplicate == 1 ? 'appears' : 'appear';
-    _ci_warn("$records @duplicate $appear multiple times in LRU queue: @{$self->{lru}}");
-    $good = 0;
-  }
-  for (keys %{$self->{cache}}) {
-    unless (exists $seen{$_}) {
-      _ci_warn("record $_ is in the cache but not the LRU queue");
+    my $b = $self->{cache}->bytes;
+    if ($cached != $b) {
+      _ci_warn("cache size is $b, should be $cached");
       $good = 0;
     }
   }
 
+  # That cache has its own set of tests
+  $good = 0 unless $self->{cache}->_check_integrity;
+
   # Now let's check the deferbuffer
   # Unless deferred writing is enabled, it should be empty
-  if (! $self->{defer} && %{$self->{deferred}}) {
+  if (! $self->_is_deferring && %{$self->{deferred}}) {
     _ci_warn("deferred writing disabled, but deferbuffer nonempty");
     $good = 0;
   }
@@ -921,11 +1355,11 @@ sub _check_integrity {
   my $deferred_s = 0;
   while (my ($n, $r) = each %{$self->{deferred}}) {
     $deferred_s += length($r);
-    if (exists $self->{cache}{$n}) {
+    if (defined $self->{cache}->_produce($n)) {
       _ci_warn("record $n is in the deferbuffer *and* the readcache");
       $good = 0;
     }
-    if (substr($r, -$rsl) ne $/) {
+    if (substr($r, -$rsl) ne $rs) {
       _ci_warn("rec $n in the deferbuffer is missing the record separator");
       $good = 0;
     }
@@ -950,18 +1384,608 @@ sub _check_integrity {
     $good = 0;
   }
 
+  # Stuff related to autodeferment
+  if (!$self->{autodefer} && @{$self->{ad_history}}) {
+    _ci_warn("autodefer is disabled, but ad_history is nonempty");
+    $good = 0;
+  }
+  if ($self->{autodeferring} && $self->{defer}) {
+    _ci_warn("both autodeferring and explicit deferring are active");
+    $good = 0;
+  }
+  if (@{$self->{ad_history}} == 0) {
+    # That's OK, no additional tests required
+  } elsif (@{$self->{ad_history}} == 2) {
+    my @non_number = grep !/^-?\d+$/, @{$self->{ad_history}};
+    if (@non_number) {
+      my $msg;
+      { local $" = ')(';
+        $msg = "ad_history contains non-numbers (@{$self->{ad_history}})";
+      }
+      _ci_warn($msg);
+      $good = 0;
+    } elsif ($self->{ad_history}[1] < $self->{ad_history}[0]) {
+      _ci_warn("ad_history has nonsensical values @{$self->{ad_history}}");
+      $good = 0;
+    }
+  } else {
+    _ci_warn("ad_history has bad length <@{$self->{ad_history}}>");
+    $good = 0;
+  }
+
   $good;
 }
 
+################################################################
+#
+# Tie::File::Cache
+#
+# Read cache
+
+package Tie::File::Cache;
+$Tie::File::Cache::VERSION = $Tie::File::VERSION;
+use Carp ':DEFAULT', 'confess';
+
+sub HEAP () { 0 }
+sub HASH () { 1 }
+sub MAX  () { 2 }
+sub BYTES() { 3 }
+#sub STAT () { 4 } # Array with request statistics for each record
+#sub MISS () { 5 } # Total number of cache misses
+#sub REQ  () { 6 } # Total number of cache requests 
+use strict 'vars';
+
+sub new {
+  my ($pack, $max) = @_;
+  local *_;
+  croak "missing argument to ->new" unless defined $max;
+  my $self = [];
+  bless $self => $pack;
+  @$self = (Tie::File::Heap->new($self), {}, $max, 0);
+  $self;
+}
+
+sub adj_limit {
+  my ($self, $n) = @_;
+  $self->[MAX] += $n;
+}
+
+sub set_limit {
+  my ($self, $n) = @_;
+  $self->[MAX] = $n;
+}
+
+# For internal use only
+# Will be called by the heap structure to notify us that a certain 
+# piece of data has moved from one heap element to another.
+# $k is the hash key of the item
+# $n is the new index into the heap at which it is stored
+# If $n is undefined, the item has been removed from the heap.
+sub _heap_move {
+  my ($self, $k, $n) = @_;
+  if (defined $n) {
+    $self->[HASH]{$k} = $n;
+  } else {
+    delete $self->[HASH]{$k};
+  }
+}
+
+sub insert {
+  my ($self, $key, $val) = @_;
+  local *_;
+  croak "missing argument to ->insert" unless defined $key;
+  unless (defined $self->[MAX]) {
+    confess "undefined max" ;
+  }
+  confess "undefined val" unless defined $val;
+  return if length($val) > $self->[MAX];
+
+#  if ($self->[STAT]) {
+#    $self->[STAT][$key] = 1;
+#    return;
+#  }
+
+  my $oldnode = $self->[HASH]{$key};
+  if (defined $oldnode) {
+    my $oldval = $self->[HEAP]->set_val($oldnode, $val);
+    $self->[BYTES] -= length($oldval);
+  } else {
+    $self->[HEAP]->insert($key, $val);
+  }
+  $self->[BYTES] += length($val);
+  $self->flush if $self->[BYTES] > $self->[MAX];
+}
+
+sub expire {
+  my $self = shift;
+  my $old_data = $self->[HEAP]->popheap;
+  return unless defined $old_data;
+  $self->[BYTES] -= length $old_data;
+  $old_data;
+}
+
+sub remove {
+  my ($self, @keys) = @_;
+  my @result;
+
+#  if ($self->[STAT]) {
+#    for my $key (@keys) {
+#      $self->[STAT][$key] = 0;
+#    }
+#    return;
+#  }
+
+  for my $key (@keys) {
+    next unless exists $self->[HASH]{$key};
+    my $old_data = $self->[HEAP]->remove($self->[HASH]{$key});
+    $self->[BYTES] -= length $old_data;
+    push @result, $old_data;
+  }
+  @result;
+}
+
+sub lookup {
+  my ($self, $key) = @_;
+  local *_;
+  croak "missing argument to ->lookup" unless defined $key;
+
+#  if ($self->[STAT]) {
+#    $self->[MISS]++  if $self->[STAT][$key]++ == 0;
+#    $self->[REQ]++;
+#    my $hit_rate = 1 - $self->[MISS] / $self->[REQ];
+#    # Do some testing to determine this threshhold
+#    $#$self = STAT - 1 if $hit_rate > 0.20; 
+#  }
+
+  if (exists $self->[HASH]{$key}) {
+    $self->[HEAP]->lookup($self->[HASH]{$key});
+  } else {
+    return;
+  }
+}
+
+# For internal use only
+sub _produce {
+  my ($self, $key) = @_;
+  my $loc = $self->[HASH]{$key};
+  return unless defined $loc;
+  $self->[HEAP][$loc][2];
+}
+
+# For internal use only
+sub _promote {
+  my ($self, $key) = @_;
+  $self->[HEAP]->promote($self->[HASH]{$key});
+}
+
+sub empty {
+  my ($self) = @_;
+  %{$self->[HASH]} = ();
+    $self->[BYTES] = 0;
+    $self->[HEAP]->empty;
+#  @{$self->[STAT]} = ();
+#    $self->[MISS] = 0;
+#    $self->[REQ] = 0;
+}
+
+sub is_empty {
+  my ($self) = @_;
+  keys %{$self->[HASH]} == 0;
+}
+
+sub update {
+  my ($self, $key, $val) = @_;
+  local *_;
+  croak "missing argument to ->update" unless defined $key;
+  if (length($val) > $self->[MAX]) {
+    my ($oldval) = $self->remove($key);
+    $self->[BYTES] -= length($oldval) if defined $oldval;
+  } elsif (exists $self->[HASH]{$key}) {
+    my $oldval = $self->[HEAP]->set_val($self->[HASH]{$key}, $val);
+    $self->[BYTES] += length($val);
+    $self->[BYTES] -= length($oldval) if defined $oldval;
+  } else {
+    $self->[HEAP]->insert($key, $val);
+    $self->[BYTES] += length($val);
+  }
+  $self->flush;
+}
+
+sub rekey {
+  my ($self, $okeys, $nkeys) = @_;
+  local *_;
+  my %map;
+  @map{@$okeys} = @$nkeys;
+  croak "missing argument to ->rekey" unless defined $nkeys;
+  croak "length mismatch in ->rekey arguments" unless @$nkeys == @$okeys;
+  my %adjusted;                 # map new keys to heap indices
+  # You should be able to cut this to one loop TODO XXX
+  for (0 .. $#$okeys) {
+    $adjusted{$nkeys->[$_]} = delete $self->[HASH]{$okeys->[$_]};
+  }
+  while (my ($nk, $ix) = each %adjusted) {
+    # @{$self->[HASH]}{keys %adjusted} = values %adjusted;
+    $self->[HEAP]->rekey($ix, $nk);
+    $self->[HASH]{$nk} = $ix;
+  }
+}
+
+sub ckeys {
+  my $self = shift;
+  my @a = keys %{$self->[HASH]};
+  @a;
+}
+
+# Return total amount of cached data
+sub bytes {
+  my $self = shift;
+  $self->[BYTES];
+}
+
+# Expire oldest item from cache until cache size is smaller than $max
+sub reduce_size_to {
+  my ($self, $max) = @_;
+  until ($self->[BYTES] <= $max) {
+    # Note that Tie::File::Cache::expire has been inlined here
+    my $old_data = $self->[HEAP]->popheap;
+    return unless defined $old_data;
+    $self->[BYTES] -= length $old_data;
+  }
+}
+
+# Why not just $self->reduce_size_to($self->[MAX])?
+# Try this when things stabilize   TODO XXX
+# If the cache is too full, expire the oldest records
+sub flush {
+  my $self = shift;
+  $self->reduce_size_to($self->[MAX]) if $self->[BYTES] > $self->[MAX];
+}
+
+# For internal use only
+sub _produce_lru {
+  my $self = shift;
+  $self->[HEAP]->expire_order;
+}
+
+BEGIN { *_ci_warn = \&Tie::File::_ci_warn }
+
+sub _check_integrity {          # For CACHE
+  my $self = shift;
+  my $good = 1;
+
+  # Test HEAP
+  $self->[HEAP]->_check_integrity or $good = 0;
+
+  # Test HASH
+  my $bytes = 0;
+  for my $k (keys %{$self->[HASH]}) {
+    if ($k ne '0' && $k !~ /^[1-9][0-9]*$/) {
+      $good = 0;
+      _ci_warn "Cache hash key <$k> is non-numeric";
+    }
+
+    my $h = $self->[HASH]{$k};
+    if (! defined $h) {
+      $good = 0;
+      _ci_warn "Heap index number for key $k is undefined";
+    } elsif ($h == 0) {
+      $good = 0;
+      _ci_warn "Heap index number for key $k is zero";
+    } else {
+      my $j = $self->[HEAP][$h];
+      if (! defined $j) {
+        $good = 0;
+        _ci_warn "Heap contents key $k (=> $h) are undefined";
+      } else {
+        $bytes += length($j->[2]);
+        if ($k ne $j->[1]) {
+          $good = 0;
+          _ci_warn "Heap contents key $k (=> $h) is $j->[1], should be $k";
+        }
+      }
+    }
+  }
+
+  # Test BYTES
+  if ($bytes != $self->[BYTES]) {
+    $good = 0;
+    _ci_warn "Total data in cache is $bytes, expected $self->[BYTES]";
+  }
+
+  # Test MAX
+  if ($bytes > $self->[MAX]) {
+    $good = 0;
+    _ci_warn "Total data in cache is $bytes, exceeds maximum $self->[MAX]";
+  }
+
+  return $good;
+}
+
+sub delink {
+  my $self = shift;
+  $self->[HEAP] = undef;        # Bye bye heap
+}
+
+################################################################
+#
+# Tie::File::Heap
+#
+# Heap data structure for use by cache LRU routines
+
+package Tie::File::Heap;
+use Carp ':DEFAULT', 'confess';
+$Tie::File::Heap::VERSION = $Tie::File::Cache::VERSION;
+sub SEQ () { 0 };
+sub KEY () { 1 };
+sub DAT () { 2 };
+
+sub new {
+  my ($pack, $cache) = @_;
+  die "$pack: Parent cache object $cache does not support _heap_move method"
+    unless eval { $cache->can('_heap_move') };
+  my $self = [[0,$cache,0]];
+  bless $self => $pack;
+}
+
+# Allocate a new sequence number, larger than all previously allocated numbers
+sub _nseq {
+  my $self = shift;
+  $self->[0][0]++;
+}
+
+sub _cache {
+  my $self = shift;
+  $self->[0][1];
+}
+
+sub _nelts {
+  my $self = shift;
+  $self->[0][2];
+}
+
+sub _nelts_inc {
+  my $self = shift;
+  ++$self->[0][2];
+}  
+
+sub _nelts_dec {
+  my $self = shift;
+  --$self->[0][2];
+}  
+
+sub is_empty {
+  my $self = shift;
+  $self->_nelts == 0;
+}
+
+sub empty {
+  my $self = shift;
+  $#$self = 0;
+  $self->[0][2] = 0;
+  $self->[0][0] = 0;            # might as well reset the sequence numbers
+}
+
+# notify the parent cache object that we moved something
+sub _heap_move {
+  my $self = shift;
+  $self->_cache->_heap_move(@_);
+}
+
+# Insert a piece of data into the heap with the indicated sequence number.
+# The item with the smallest sequence number is always at the top.
+# If no sequence number is specified, allocate a new one and insert the
+# item at the bottom.
+sub insert {
+  my ($self, $key, $data, $seq) = @_;
+  $seq = $self->_nseq unless defined $seq;
+  $self->_insert_new([$seq, $key, $data]);
+}
+
+# Insert a new, fresh item at the bottom of the heap
+sub _insert_new {
+  my ($self, $item) = @_;
+  my $i = @$self;
+  $i = int($i/2) until defined $self->[$i/2];
+  $self->[$i] = $item;
+  $self->[0][1]->_heap_move($self->[$i][KEY], $i);
+  $self->_nelts_inc;
+}
+
+# Insert [$data, $seq] pair at or below item $i in the heap.
+# If $i is omitted, default to 1 (the top element.)
+sub _insert {
+  my ($self, $item, $i) = @_;
+#  $self->_check_loc($i) if defined $i;
+  $i = 1 unless defined $i;
+  until (! defined $self->[$i]) {
+    if ($self->[$i][SEQ] > $item->[SEQ]) { # inserted item is older
+      ($self->[$i], $item) = ($item, $self->[$i]);
+      $self->[0][1]->_heap_move($self->[$i][KEY], $i);
+    }
+    # If either is undefined, go that way.  Otherwise, choose at random
+    my $dir;
+    $dir = 0 if !defined $self->[2*$i];
+    $dir = 1 if !defined $self->[2*$i+1];
+    $dir = int(rand(2)) unless defined $dir;
+    $i = 2*$i + $dir;
+  }
+  $self->[$i] = $item;
+  $self->[0][1]->_heap_move($self->[$i][KEY], $i);
+  $self->_nelts_inc;
+}
+
+# Remove the item at node $i from the heap, moving child items upwards.
+# The item with the smallest sequence number is always at the top.
+# Moving items upwards maintains this condition.
+# Return the removed item.  Return undef if there was no item at node $i.
+sub remove {
+  my ($self, $i) = @_;
+  $i = 1 unless defined $i;
+  my $top = $self->[$i];
+  return unless defined $top;
+  while (1) {
+    my $ii;
+    my ($L, $R) = (2*$i, 2*$i+1);
+
+    # If either is undefined, go the other way.
+    # Otherwise, go towards the smallest.
+    last unless defined $self->[$L] || defined $self->[$R];
+    $ii = $R if not defined $self->[$L];
+    $ii = $L if not defined $self->[$R];
+    unless (defined $ii) {
+      $ii = $self->[$L][SEQ] < $self->[$R][SEQ] ? $L : $R;
+    }
+
+    $self->[$i] = $self->[$ii]; # Promote child to fill vacated spot
+    $self->[0][1]->_heap_move($self->[$i][KEY], $i);
+    $i = $ii; # Fill new vacated spot
+  }
+  $self->[0][1]->_heap_move($top->[KEY], undef);
+  undef $self->[$i];
+  $self->_nelts_dec;
+  return $top->[DAT];
+}
+
+sub popheap {
+  my $self = shift;
+  $self->remove(1);
+}
+
+# set the sequence number of the indicated item to a higher number
+# than any other item in the heap, and bubble the item down to the
+# bottom.
+sub promote {
+  my ($self, $n) = @_;
+#  $self->_check_loc($n);
+  $self->[$n][SEQ] = $self->_nseq;
+  my $i = $n;
+  while (1) {
+    my ($L, $R) = (2*$i, 2*$i+1);
+    my $dir;
+    last unless defined $self->[$L] || defined $self->[$R];
+    $dir = $R unless defined $self->[$L];
+    $dir = $L unless defined $self->[$R];
+    unless (defined $dir) {
+      $dir = $self->[$L][SEQ] < $self->[$R][SEQ] ? $L : $R;
+    }
+    @{$self}[$i, $dir] = @{$self}[$dir, $i];
+    for ($i, $dir) {
+      $self->[0][1]->_heap_move($self->[$_][KEY], $_) if defined $self->[$_];
+    }
+    $i = $dir;
+  }
+}
+
+# Return item $n from the heap, promoting its LRU status
+sub lookup {
+  my ($self, $n) = @_;
+#  $self->_check_loc($n);
+  my $val = $self->[$n];
+  $self->promote($n);
+  $val->[DAT];
+}
+
+
+# Assign a new value for node $n, promoting it to the bottom of the heap
+sub set_val {
+  my ($self, $n, $val) = @_;
+#  $self->_check_loc($n);
+  my $oval = $self->[$n][DAT];
+  $self->[$n][DAT] = $val;
+  $self->promote($n);
+  return $oval;
+}
+
+# The hask key has changed for an item;
+# alter the heap's record of the hash key
+sub rekey {
+  my ($self, $n, $new_key) = @_;
+#  $self->_check_loc($n);
+  $self->[$n][KEY] = $new_key;
+}
+
+sub _check_loc {
+  my ($self, $n) = @_;
+  unless (1 || defined $self->[$n]) {
+    confess "_check_loc($n) failed";
+  }
+}
+
+BEGIN { *_ci_warn = \&Tie::File::_ci_warn }
+
+sub _check_integrity {
+  my $self = shift;
+  my $good = 1;
+  my %seq;
+
+  unless (eval {$self->[0][1]->isa("Tie::File::Cache")}) {
+    _ci_warn "Element 0 of heap corrupt";
+    $good = 0;
+  }
+  $good = 0 unless $self->_satisfies_heap_condition(1);
+  for my $i (2 .. $#{$self}) {
+    my $p = int($i/2);          # index of parent node
+    if (defined $self->[$i] && ! defined $self->[$p]) {
+      _ci_warn "Element $i of heap defined, but parent $p isn't";
+      $good = 0;
+    }
+
+    if (defined $self->[$i]) {
+      if ($seq{$self->[$i][SEQ]}) {
+        my $seq = $self->[$i][SEQ];
+        _ci_warn "Nodes $i and $seq{$seq} both have SEQ=$seq";
+        $good = 0;
+      } else {
+        $seq{$self->[$i][SEQ]} = $i;
+      }
+    }
+  }
+
+  return $good;
+}
+
+sub _satisfies_heap_condition {
+  my $self = shift;
+  my $n = shift || 1;
+  my $good = 1;
+  for (0, 1) {
+    my $c = $n*2 + $_;
+    next unless defined $self->[$c];
+    if ($self->[$n][SEQ] >= $self->[$c]) {
+      _ci_warn "Node $n of heap does not predate node $c";
+      $good = 0 ;
+    }
+    $good = 0 unless $self->_satisfies_heap_condition($c);
+  }
+  return $good;
+}
+
+# Return a list of all the values, sorted by expiration order
+sub expire_order {
+  my $self = shift;
+  my @nodes = sort {$a->[SEQ] <=> $b->[SEQ]} $self->_nodes;
+  map { $_->[KEY] } @nodes;
+}
+
+sub _nodes {
+  my $self = shift;
+  my $i = shift || 1;
+  return unless defined $self->[$i];
+  ($self->[$i], $self->_nodes($i*2), $self->_nodes($i*2+1));
+}
+
 "Cogito, ergo sum.";  # don't forget to return a true value from the file
 
+__END__
+
 =head1 NAME
 
 Tie::File - Access the lines of a disk file via a Perl array
 
 =head1 SYNOPSIS
 
-       # This file documents Tie::File version 0.50
+       # This file documents Tie::File version 0.95
+       use Tie::File;
 
        tie @array, 'Tie::File', filename or die ...;
 
@@ -982,7 +2006,7 @@ Tie::File - Access the lines of a disk file via a Perl array
        push @array, new recs...;
        my $r1 = pop @array;
        unshift @array, new recs...;
-       my $r1 = shift @array;
+       my $r2 = shift @array;
        @old_recs = splice @array, 3, 7, new recs...;
 
        untie @array;            # all finished
@@ -1006,7 +2030,7 @@ Lazy people and beginners may now stop reading the manual.
 
 What is a 'record'?  By default, the meaning is the same as for the
 C<E<lt>...E<gt>> operator: It's a string terminated by C<$/>, which is
-probably C<"\n">.  (Minor exception: on dos and Win32 systems, a
+probably C<"\n">.  (Minor exception: on DOS and Win32 systems, a
 'record' is a string terminated by C<"\r\n">.)  You may change the
 definition of "record" by supplying the C<recsep> option in the C<tie>
 call:
@@ -1018,7 +2042,7 @@ contained the following data:
 
        Curse these pesky flies!\n
 
-then the C<@array> would appear to have four elements: 
+then the C<@array> would appear to have four elements:
 
        "Curse th"
        "e p"
@@ -1030,7 +2054,7 @@ special "paragraph mode" semantics (E<agrave> la C<$/ = "">) are not
 emulated.
 
 Records read from the tied array do not have the record separator
-string on the end; this is to allow 
+string on the end; this is to allow
 
        $array[17] .= "extra";
 
@@ -1047,7 +2071,7 @@ the same thing:
 
 The result is that the contents of line 17 of the file will be
 replaced with "Cherry pie"; a newline character will separate line 17
-from line 18.  This means that in particular, this will do nothing:
+from line 18.  This means that this code will do nothing:
 
        chomp $array[17];
 
@@ -1055,9 +2079,10 @@ Because the C<chomp>ed value will have the separator reattached when
 it is written back to the file.  There is no way to create a file
 whose trailing record separator string is missing.
 
-Inserting records that I<contain> the record separator string will
-produce a reasonable result, but if you can't foresee what this result
-will be, you'd better avoid doing this.
+Inserting records that I<contain> the record separator string is not
+supported by this module.  It will probably produce a reasonable
+result, but what this result will be may change in a future version.
+Use 'splice' to insert records or to replace one record with several.
 
 =head2 C<autochomp>
 
@@ -1126,10 +2151,21 @@ desired cache size, in bytes.
 Setting the memory limit to 0 will inhibit caching; records will be
 fetched from disk every time you examine them.
 
+The C<memory> value is not an absolute or exact limit on the memory
+used.  C<Tie::File> objects contains some structures besides the read
+cache and the deferred write buffer, whose sizes are not charged
+against C<memory>. 
+
+The cache itself consumes about 310 bytes per cached record, so if
+your file has many short records, you may want to decrease the cache
+memory limit, or else the cache overhead may exceed the size of the
+cached data.
+
+
 =head2 C<dw_size>
 
 (This is an advanced feature.  Skip this section on first reading.)
+
 If you use deferred writing (See L<"Deferred Writing">, below) then
 data you write into the array will not be written directly to the
 file; instead, it will be saved in the I<deferred write buffer> to be
@@ -1156,7 +2192,7 @@ idea.
 
 =head1 Public Methods
 
-The C<tie> call returns an object, say C<$o>.  You may call 
+The C<tie> call returns an object, say C<$o>.  You may call
 
        $rec = $o->FETCH($n);
        $o->STORE($n, $rec);
@@ -1176,6 +2212,15 @@ the C<use Fcntl ':flock'> declaration.)
 
 C<MODE> is optional; the default is C<LOCK_EX>.
 
+C<Tie::File> maintains an internal table of the byte offset of each
+record it has seen in the file.  
+
+When you use C<flock> to lock the file, C<Tie::File> assumes that the
+read cache is no longer trustworthy, because another process might
+have modified the file since the last time it was read.  Therefore, a
+successful call to C<flock> discards the contents of the read cache
+and the internal record offset table.
+
 C<Tie::File> promises that the following sequence of operations will
 be safe:
 
@@ -1213,10 +2258,18 @@ the idiot does not also have a green light at the same time.
 
 See L<"autochomp">, above.
 
-=head2 C<defer>, C<flush>, and C<discard>
+=head2 C<defer>, C<flush>, C<discard>, and C<autodefer>
 
 See L<"Deferred Writing">, below.
 
+=head2 C<offset>
+
+       $off = $o->offset($n);
+
+This method returns the byte offset of the start of the C<$n>th record
+in the file.  If there is no such record, it returns an undefined
+value.
+
 =head1 Tying to an already-opened filehandle
 
 If C<$fh> is a filehandle, such as is returned by C<IO::File> or one
@@ -1232,9 +2285,9 @@ C<sysopen>, you may use:
 Handles that were opened write-only won't work.  Handles that were
 opened read-only will work as long as you don't try to modify the
 array.  Handles must be attached to seekable sources of data---that
-means no pipes or sockets.  If you supply a non-seekable handle, the
-C<tie> call will try to throw an exception.  (On Unix systems, it
-B<will> throw an exception.)
+means no pipes or sockets.  If C<Tie::File> can detect that you
+supplied a non-seekable handle, the C<tie> call will throw an
+exception.  (On Unix systems, it can detect this.)
 
 =head1 Deferred Writing
 
@@ -1275,11 +2328,18 @@ If C<Tie::File>'s memory limit is large enough, all the writing will
 done in memory.  Then, when you call C<-E<gt>flush>, the entire file
 will be rewritten in a single pass.
 
+(Actually, the preceding discussion is something of a fib.  You don't
+need to enable deferred writing to get good performance for this
+common case, because C<Tie::File> will do it for you automatically
+unless you specifically tell it not to.  See L<"autodeferring">,
+below.)
+
 Calling C<-E<gt>flush> returns the array to immediate-write mode.  If
 you wish to discard the deferred writes, you may call C<-E<gt>discard>
 instead of C<-E<gt>flush>.  Note that in some cases, some of the data
 will have been written already, and it will be too late for
-C<-E<gt>discard> to discard all the changes.
+C<-E<gt>discard> to discard all the changes.  Support for
+C<-E<gt>discard> may be withdrawn in a future version of C<Tie::File>.
 
 Deferred writes are cached in memory up to the limit specified by the
 C<dw_size> option (see above).  If the deferred-write buffer is full
@@ -1290,7 +2350,7 @@ deferred writes.
 
 If the deferred-write buffer isn't yet full, but the total size of the
 buffer and the read cache would exceed the C<memory> limit, the oldest
-records will be flushed out of the read cache until total usage is
+records will be expired from the read cache until the total size is
 under the limit.
 
 C<push>, C<pop>, C<shift>, C<unshift>, and C<splice> cannot be
@@ -1298,13 +2358,53 @@ deferred.  When you perform one of these operations, any deferred data
 is written to the file and the operation is performed immediately.
 This may change in a future version.
 
-A soon-to-be-released version of this module may enabled deferred
-write mode automagically if it guesses that you are about to write
-many consecutive records.  To disable this feature, use
+If you resize the array with deferred writing enabled, the file will
+be resized immediately, but deferred records will not be written.
+This has a surprising consequence: C<@a = (...)> erases the file
+immediately, but the writing of the actual data is deferred.  This
+might be a bug.  If it is a bug, it will be fixed in a future version.
+
+=head2 Autodeferring
+
+C<Tie::File> tries to guess when deferred writing might be helpful,
+and to turn it on and off automatically. 
+
+       for (@a) {
+         $_ = "> $_";
+       }
+
+In this example, only the first two assignments will be done
+immediately; after this, all the changes to the file will be deferred
+up to the user-specified memory limit.
+
+You should usually be able to ignore this and just use the module
+without thinking about deferring.  However, special applications may
+require fine control over which writes are deferred, or may require
+that all writes be immediate.  To disable the autodeferment feature,
+use
 
        (tied @o)->autodefer(0);
 
-(At present, this call does nothing.)
+or
+
+               tie @array, 'Tie::File', $file, autodefer => 0;
+
+
+Similarly, C<-E<gt>autodefer(1)> re-enables autodeferment, and 
+C<-E<gt>autodefer()> recovers the current value of the autodefer setting.
+
+
+=head1 CONCURRENT ACCESS TO FILES
+
+Caching and deferred writing are inappropriate if you want the same
+file to be accessed simultaneously from more than one process.  You
+will want to disable these features.  You should do that by including
+the C<memory =E<gt> 0> option in your C<tie> calls; this will inhibit
+caching and deferred writing.
+
+You will also want to lock the file while reading or writing it.  You
+can use the C<-E<gt>flock> method for this.  A future version of this
+module may provide an 'autolocking' mode.
 
 =head1 CAVEATS
 
@@ -1314,12 +2414,7 @@ many consecutive records.  To disable this feature, use
 
 =item *
 
-This is BETA RELEASE SOFTWARE.  It may have bugs.  See the discussion
-below about the (lack of any) warranty.
-
-=item * 
-
-Every effort was made to make this module efficient.  Nevertheless,
+Reasonable effort was made to make this module efficient.  Nevertheless,
 changing the size of a record in the middle of a large file will
 always be fairly slow, because everything after the new record must be
 moved.
@@ -1338,44 +2433,40 @@ get the empty string, so the supposedly-C<undef>'ed value will be
 defined.  Similarly, if you have C<autochomp> disabled, then
 
        # This DOES print "How unusual!" if 'autochomp' is disabled
-       undef $a[10];  
+       undef $a[10];
         print "How unusual!\n" if $a[10];
 
 Because when C<autochomp> is disabled, C<$a[10]> will read back as
 C<"\n"> (or whatever the record separator string is.)  
 
-There are other minor differences, but in general, the correspondence
-is extremely close.
+There are other minor differences, particularly regarding C<exists>
+and C<delete>, but in general, the correspondence is extremely close.
 
 =item *
 
-Not quite every effort was made to make this module as efficient as
-possible.  C<FETCHSIZE> should use binary search instead of linear
-search.  The cache's LRU queue should be a heap instead of a list.
+I have supposed that since this module is concerned with file I/O,
+almost all normal use of it will be heavily I/O bound.  This means
+that the time to maintain complicated data structures inside the
+module will be dominated by the time to actually perform the I/O.
+When there was an opportunity to spend CPU time to avoid doing I/O, I
+usually tried to take it.
 
-The performance of the C<flush> method could be improved.  At present,
-it still rewrites the tail of the file once for each block of
-contiguous lines to be changed.  In the typical case, this will result
-in only one rewrite, but in peculiar cases it might be bad.  It should
-be possible to perform I<all> deferred writing with a single rewrite.
+=item *
 
-These defects are probably minor; in any event, they will be fixed in
-a future version of the module.
+You might be tempted to think that deferred writing is like
+transactions, with C<flush> as C<commit> and C<discard> as
+C<rollback>, but it isn't, so don't.
 
 =item *
 
-The author has supposed that since this module is concerned with file
-I/O, almost all normal use of it will be heavily I/O bound, and that
-the time to maintain complicated data structures inside the module
-will be dominated by the time to actually perform the I/O.  This
-suggests, for example, that an LRU read-cache is a good tradeoff,
-even if it requires substantial adjustment following a C<splice>
-operation.
+There is a large memory overhead for each record offset and for each
+cache entry: about 310 bytes per cached data record, and about 21 bytes per offset table entry.
 
-=item *
-You might be tempted to think that deferred writing is like
-transactions, with C<flush> as C<commit> and C<discard> as
-C<rollback>, but it isn't, so don't.  
+The per-record overhead will limit the maximum number of records you
+can access per file. Note that I<accessing> the length of the array
+via C<$x = scalar @tied_file> accesses B<all> records and stores their
+offsets.  The same for C<foreach (@tied_file)>, even if you exit the
+loop early.
 
 =back
 
@@ -1387,87 +2478,11 @@ well-defined and stable subclassing API.
 
 =head1 WHAT ABOUT C<DB_File>?
 
-C<DB_File>'s C<DB_RECNO> feature does something similar to
-C<Tie::File>, but there are a number of reasons that you might prefer
-C<Tie::File>.  C<DB_File> is a great piece of software, but the
-C<DB_RECNO> part is less great than the rest of it.
-
-=over 4
-
-=item *
-
-C<DB_File> reads your entire file into memory, modifies it in memory,
-and the writes out the entire file again when you untie the file.
-This is completely impractical for large files.
-
-C<Tie::File> does not do any of those things.  It doesn't try to read
-the entire file into memory; instead it uses a lazy approach and
-caches recently-used records.  The cache size is strictly bounded by
-the C<memory> option.  DB_File's C<-E<gt>{cachesize}> doesn't prevent
-your process from blowing up when reading a big file.
-
-=item *
-
-C<DB_File> has an extremely poor writing strategy.  If you have a
-ten-megabyte file and tie it with C<DB_File>, and then use
-
-        $a[0] =~ s/PERL/Perl/;
-
-C<DB_file> will then read the entire ten-megabyte file into memory, do
-the change, and write the entire file back to disk, reading ten
-megabytes and writing ten megabytes.  C<Tie::File> will read and write
-only the first record.
-
-If you have a million-record file and tie it with C<DB_File>, and then
-use
-
-        $a[999998] =~ s/Larry/Larry Wall/;
-
-C<DB_File> will read the entire million-record file into memory, do
-the change, and write the entire file back to disk.  C<Tie::File> will
-only rewrite records 999998 and 999999.  During the writing process,
-it will never have more than a few kilobytes of data in memory at any
-time, even if the two records are very large.
-
-=item *
-
-Since changes to C<DB_File> files only appear when you do C<untie>, it
-can be inconvenient to arrange for concurrent access to the same file
-by two or more processes.  Each process needs to call C<$db-E<gt>sync>
-after every write.  When you change a C<Tie::File> array, the changes
-are reflected in the file immediately; no explicit C<-E<gt>sync> call
-is required.  (Or you can enable deferred writing mode to require that
-changes be explicitly sync'ed.)
-
-=item *
-
-C<DB_File> is only installed by default if you already have the C<db>
-library on your system; C<Tie::File> is pure Perl and is installed by
-default no matter what.  Starting with Perl 5.7.3 you can be
-absolutely sure it will be everywhere.  You will never have that
-surety with C<DB_File>.  If you don't have C<DB_File> yet, it requires
-a C compiler.  You can install C<Tie::File> from CPAN in five minutes
-with no compiler.
-
-=item *
-
-C<DB_File> is written in C, so if you aren't allowed to install
-modules on your system, it is useless.  C<Tie::File> is written in Perl,
-so even if you aren't allowed to install modules, you can look into
-the source code, see how it works, and copy the subroutines or the
-ideas from the subroutines directly into your own Perl program.
-
-=item *
-
-Except in very old, unsupported versions, C<DB_File>'s free license
-requires that you distribute the source code for your entire
-application.  If you are not able to distribute the source code for
-your application, you must negotiate an alternative license from
-Sleepycat, possibly for a fee.  Tie::File is under the Perl Artistic
-license and can be distributed free under the same terms as Perl
-itself.
+People sometimes point out that L<DB_File> will do something similar,
+and ask why C<Tie::File> module is necessary.
 
-=back
+There are a number of reasons that you might prefer C<Tie::File>.
+A list is available at C<http://perl.plover.com/TieFile/why-not-DB_File>.
 
 =head1 AUTHOR
 
@@ -1487,7 +2502,7 @@ any news of importance, will be available at
 
 =head1 LICENSE
 
-C<Tie::File> version 0.50 is copyright (C) 2002 Mark Jason Dominus.
+C<Tie::File> version 0.95 is copyright (C) 2002 Mark Jason Dominus.
 
 This library is free software; you may redistribute it and/or modify
 it under the same terms as Perl itself.
@@ -1515,7 +2530,7 @@ For licensing inquiries, contact the author at:
 
 =head1 WARRANTY
 
-C<Tie::File> version 0.50 comes with ABSOLUTELY NO WARRANTY.
+C<Tie::File> version 0.95 comes with ABSOLUTELY NO WARRANTY.
 For details, see the license.
 
 =head1 THANKS
@@ -1528,45 +2543,58 @@ Also big thanks to Abhijit Menon-Sen for all of the same things.
 Special thanks to Craig Berry and Peter Prymmer (for VMS portability
 help), Randy Kobes (for Win32 portability help), Clinton Pierce and
 Autrijus Tang (for heroic eleventh-hour Win32 testing above and beyond
-the call of duty), and the rest of the CPAN testers (for testing
-generally).
+the call of duty), Michael G Schwern (for testing advice), and the
+rest of the CPAN testers (for testing generally).
+
+Special thanks to Tels for suggesting several speed and memory
+optimizations.
 
 Additional thanks to:
 Edward Avis /
+Mattia Barbon /
 Gerrit Haase /
+Jarkko Hietaniemi (again) /
 Nikola Knezevic /
+John Kominetz /
 Nick Ing-Simmons /
 Tassilo von Parseval /
 H. Dieter Pearcey /
 Slaven Rezic /
+Eric Roode /
+Peter Scott /
 Peter Somu /
 Autrijus Tang (again) /
-Tels
+Tels (again) /
+Juerd Waalboer
 
 =head1 TODO
 
-Test DELETE machinery more carefully.
-
-More tests.  (C<mode> option.  _twrite should be tested separately,
-because there are a lot of weird special cases lurking in there.)
-
 More tests.  (Stuff I didn't think of yet.)
 
 Paragraph mode?
 
-More tests.
-
-Fixed-length mode.
+Fixed-length mode.  Leave-blanks mode.
 
 Maybe an autolocking mode?
 
-Autodeferment.
+For many common uses of the module, the read cache is a liability.
+For example, a program that inserts a single record, or that scans the
+file once, will have a cache hit rate of zero.  This suggests a major
+optimization: The cache should be initially disabled.  Here's a hybrid
+approach: Initially, the cache is disabled, but the cache code
+maintains statistics about how high the hit rate would be *if* it were
+enabled.  When it sees the hit rate get high enough, it enables
+itself.  The STAT comments in this code are the beginning of an
+implementation of this.
 
-Record locking with fcntl()?  Then you might support an undo log and
-get real transactions.  What a coup that would be.  All would bow
-before my might.
+Record locking with fcntl()?  Then the module might support an undo
+log and get real transactions.  What a tour de force that would be.
 
-Leave-blanks mode
+Keeping track of the highest cached record. This would allow reads-in-a-row
+to skip the cache lookup faster (if reading from 1..N with empty cache at
+start, the last cached value will be always N-1).
+
+More tests.
 
 =cut