Unify shared code (and constants) with cindex package.

This commit is contained in:
Piotr Tabor
2021-04-29 09:34:54 +02:00
parent f53b70facb
commit e90504fe62
4 changed files with 25 additions and 31 deletions

View File

@ -80,11 +80,7 @@ func (ci *consistentIndex) UnsafeSave(tx backend.BatchTx) {
// Never save 0 as it means that we didn't loaded the real index yet.
return
}
bs := make([]byte, 8) // this is kept on stack (not heap) so its quick.
binary.BigEndian.PutUint64(bs, index)
// put the index into the underlying backend
// tx has been locked in TxnBegin, so there is no need to lock it again
tx.UnsafePut(MetaBucketName, ConsistentIndexKeyName, bs)
unsafeUpdateConsistentIndex(tx, index)
}
func (ci *consistentIndex) SetBatchTx(tx backend.BatchTx) {
@ -130,3 +126,22 @@ func ReadConsistentIndex(tx backend.ReadTx) uint64 {
defer tx.Unlock()
return unsafeReadConsistentIndex(tx)
}
func unsafeUpdateConsistentIndex(tx backend.BatchTx, index uint64) {
bs := make([]byte, 8) // this is kept on stack (not heap) so its quick.
binary.BigEndian.PutUint64(bs, index)
// put the index into the underlying backend
// tx has been locked in TxnBegin, so there is no need to lock it again
tx.UnsafePut(MetaBucketName, ConsistentIndexKeyName, bs)
}
func UpdateConsistentIndex(tx backend.BatchTx, index uint64) {
tx.Lock()
defer tx.Unlock()
oldi := unsafeReadConsistentIndex(tx)
if index <= oldi {
return
}
unsafeUpdateConsistentIndex(tx, index)
}