2011-06-04 19:00:53 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package rollsum implements rolling checksums similar to apenwarr's bup, which
|
|
|
|
// is similar to librsync.
|
2011-06-07 02:09:58 +00:00
|
|
|
//
|
|
|
|
// The bup project is at https://github.com/apenwarr/bup and its splitting in
|
|
|
|
// particular is at https://github.com/apenwarr/bup/blob/master/lib/bup/bupsplit.c
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package rollsum // import "perkeep.org/pkg/rollsum"
|
2011-06-04 19:00:53 +00:00
|
|
|
|
2014-08-27 02:16:13 +00:00
|
|
|
const windowSize = 64 // Roll assumes windowSize is a power of 2
|
2011-06-04 19:00:53 +00:00
|
|
|
const charOffset = 31
|
|
|
|
|
2012-10-28 12:40:29 +00:00
|
|
|
const blobBits = 13
|
|
|
|
const blobSize = 1 << blobBits // 8k
|
2011-06-04 23:14:33 +00:00
|
|
|
|
2011-06-04 19:00:53 +00:00
|
|
|
type RollSum struct {
|
|
|
|
s1, s2 uint32
|
|
|
|
window [windowSize]uint8
|
|
|
|
wofs int
|
|
|
|
}
|
|
|
|
|
2011-06-04 23:14:33 +00:00
|
|
|
func New() *RollSum {
|
2011-06-04 19:00:53 +00:00
|
|
|
return &RollSum{
|
|
|
|
s1: windowSize * charOffset,
|
|
|
|
s2: windowSize * (windowSize - 1) * charOffset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 02:16:13 +00:00
|
|
|
func (rs *RollSum) add(drop, add uint32) {
|
|
|
|
s1 := rs.s1 + add - drop
|
|
|
|
rs.s1 = s1
|
|
|
|
rs.s2 += s1 - uint32(windowSize)*(drop+charOffset)
|
2011-06-04 19:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RollSum) Roll(ch byte) {
|
2014-08-27 02:16:13 +00:00
|
|
|
wp := &rs.window[rs.wofs]
|
|
|
|
rs.add(uint32(*wp), uint32(ch))
|
|
|
|
*wp = ch
|
|
|
|
rs.wofs = (rs.wofs + 1) & (windowSize - 1)
|
2011-06-04 19:00:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 17:48:30 +00:00
|
|
|
// OnSplit returns whether at least 13 consecutive trailing bits of
|
|
|
|
// the current checksum are set the same way.
|
2011-06-04 23:14:33 +00:00
|
|
|
func (rs *RollSum) OnSplit() bool {
|
2011-07-02 16:09:50 +00:00
|
|
|
return (rs.s2 & (blobSize - 1)) == ((^0) & (blobSize - 1))
|
2011-06-04 23:14:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 17:48:30 +00:00
|
|
|
// OnSplit returns whether at least n consecutive trailing bits
|
|
|
|
// of the current checksum are set the same way.
|
|
|
|
func (rs *RollSum) OnSplitWithBits(n uint32) bool {
|
|
|
|
mask := (uint32(1) << n) - 1
|
2014-03-20 19:29:45 +00:00
|
|
|
return rs.s2&mask == (^uint32(0))&mask
|
2013-06-09 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 23:14:33 +00:00
|
|
|
func (rs *RollSum) Bits() int {
|
|
|
|
bits := blobBits
|
|
|
|
rsum := rs.Digest()
|
2011-07-02 16:09:50 +00:00
|
|
|
rsum >>= blobBits
|
|
|
|
for ; (rsum>>1)&1 != 0; bits++ {
|
2011-06-04 23:14:33 +00:00
|
|
|
rsum >>= 1
|
|
|
|
}
|
|
|
|
return bits
|
|
|
|
}
|
|
|
|
|
2011-06-04 19:00:53 +00:00
|
|
|
func (rs *RollSum) Digest() uint32 {
|
|
|
|
return (rs.s1 << 16) | (rs.s2 & 0xffff)
|
|
|
|
}
|