anchor.diff:                          st
   
                   let addPairToLeaf (st:LeafState) =
  -                    let newPrefix = 
  +                    // TODO ignore prefixLen for overflowed keys?
  +                    let newPrefixLen = 
                           if List.isEmpty st.keys then
                               defaultPrefixLen k
                           else
  -                            bcmp.PrefixMatch ((List.head st.keys).pair.Key) k st.prefixLen
  +                            bcmp.PrefixMatch ((List.head st.keys).key) k st.prefixLen
                       let sofar = 
  -                        if newPrefix < st.prefixLen then
  +                        if newPrefixLen < st.prefixLen then
  +                            // the prefixLen will change with the addition of this key,
  +                            // so we need to recalc sofar
                               // TODO is it a problem that we're doing this without List.rev ?
  -                            List.sumBy (fun lp -> leafPairSize newPrefix lp) st.keys
  +                            List.sumBy (fun lp -> leafPairSize newPrefixLen lp) st.keys
                           else
                               st.sofarLeaf
  -                    let available = pageSize - (sofar + LEAF_PAGE_OVERHEAD + 1 + newPrefix)
  -                    let neededForKeyInline = neededForKeyBase + k.Length - newPrefix
  -                    let neededForBothInline = neededForKeyInline + neededForValueInline
  -                    let neededForKeyInlineValueOverflow = neededForKeyInline + neededForValueOverflow
  -                    let fitBothInline = (available >= neededForBothInline)
  -                    let fitKeyInlineValueOverflow = (available >= neededForKeyInlineValueOverflow)
  -                    let blk = st.blk
  -                    let newBlk,keyLoc,valueLoc = 
  -                        if fitBothInline then
  -                            blk,Inline,Inline
  -                        else
  -                            if null = v then failwith "never overflow a tombstone"
  -
  -                            if fitKeyInlineValueOverflow then
  -                                let valueLoc = Overflow (v.Length |> int,blk.firstPage)
  -                                writeOverflow blk v,Inline,valueLoc
  -                            else
  -                                let keyLoc = Overflow (k.Length, blk.firstPage)
  -                                let tmpBlk = writeOverflow blk (new MemoryStream(k)) 
  -                                let valueLoc = Overflow (v.Length |> int, tmpBlk.firstPage)
  -                                writeOverflow tmpBlk v,keyLoc,valueLoc
  -                    let lp = {
  -                                pair=pair
  -                                keyLoc=keyLoc
  -                                valueLoc=valueLoc
  -                                }
                       {st with 
  -                        sofarLeaf=sofar + leafPairSize newPrefix lp
  -                        blk=newBlk
  +                        sofarLeaf=sofar + leafPairSize newPrefixLen lp
                           keys=lp::st.keys
  -                        prefixLen=newPrefix
  +                        prefixLen=newPrefixLen
                           }
                           
  -                // this is the body of the foldLeaf function
  -                maybeWriteLeaf st |> addPairToLeaf
  +                maybeWriteLeaf {st with blk=blkAfterValue} |> addPairToLeaf
   
               // this is the body of writeLeaves
               //let source = seq { csr.First(); while csr.IsValid() do yield (csr.Key(), csr.Value()); csr.Next(); done }
  
positive.code:             blk:PageBlock
          }
  
      type private Location =
          | Inline
          | Overflow of len:int*page:int
  
      type private LeafPair =
          {
              pair:kvp
              keyLoc:Location
              valueLoc:Location
          }
  
      type private LeafState =
  
negative.code:                 // TODO prefixLen is one byte.  should it be two?
                  pb.PutByte(byte st.prefixLen)
                  if st.prefixLen > 0 then
                      let k = (List.head st.keys).key
                      pb.PutArray(k, 0, st.prefixLen)
                  pb.PutInt16 (st.keys.Length)
                  List.iter (fun lp ->
                      let k = lp.key
                      match lp.kLoc with
                      | Inline ->
                          pb.PutByte(0uy) // flags
                          pb.PutVarint(int64 k.Length)
                          pb.PutArray(k, st.prefixLen, k.Length - st.prefixLen)
                      | Overflow kpage ->
                          pb.PutByte(FLAG_OVERFLOW)
                          pb.PutVarint(int64 k.Length)
                          pb.PutInt32(kpage)
                      match lp.vLoc with
                      | Tombstone ->
                          pb.PutByte(FLAG_TOMBSTONE)
                          pb.PutVarint(0L) // TODO remove this
                      | Buffer (vbuf) ->
                          pb.PutByte(0uy)
                          pb.PutVarint(int64 vbuf.Length)
                          pb.PutArray(vbuf)
                      | Overflowed (vlen,vpage) ->
                          pb.PutByte(FLAG_OVERFLOW)
                          pb.PutVarint(int64 vlen)
                          pb.PutInt32(vpage)
  