e3: kv/temporal prototype 2 (#792)

This commit is contained in:
Alex Sharov 2022-12-20 09:28:09 +07:00 committed by GitHub
parent 0d5c9f0fed
commit f5db7c9bef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 0 deletions

View File

@ -24,6 +24,8 @@ const (
Addr = 20
// BlockNumberLen length of uint64 big endian
BlockNum = 8
// Ts TimeStamp (BlockNum, TxNum or any other uint64 equivalent of Time)
Ts = 8
// Incarnation length of uint64 for contract incarnations
Incarnation = 8
)

View File

@ -26,6 +26,7 @@ import (
//Naming:
// ts - TimeStamp
// tx - Database Transaction
// txn - Ethereum Transaction (and TxNum - is also number of Etherum Transaction)
// RoTx - Read-Only Database Transaction
// RwTx - Read-Write Database Transaction
// k - key

View File

@ -0,0 +1,78 @@
/*
Copyright 2022 Erigon contributors
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 historyv2
import (
"bytes"
"errors"
"fmt"
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
)
func FindByHistory(indexC kv.Cursor, changesC kv.CursorDupSort, storage bool, key []byte, timestamp uint64) ([]byte, bool, error) {
var csBucket string
if storage {
csBucket = kv.StorageChangeSet
} else {
csBucket = kv.AccountChangeSet
}
k, v, seekErr := indexC.Seek(Mapper[csBucket].IndexChunkKey(key, timestamp))
if seekErr != nil {
return nil, false, seekErr
}
if k == nil {
return nil, false, nil
}
if storage {
if !bytes.Equal(k[:length.Addr], key[:length.Addr]) ||
!bytes.Equal(k[length.Addr:length.Addr+length.Hash], key[length.Addr+length.Incarnation:]) {
return nil, false, nil
}
} else {
if !bytes.HasPrefix(k, key) {
return nil, false, nil
}
}
index := roaring64.New()
if _, err := index.ReadFrom(bytes.NewReader(v)); err != nil {
return nil, false, err
}
found, ok := bitmapdb.SeekInBitmap64(index, timestamp)
changeSetBlock := found
var data []byte
var err error
if ok {
data, err = Mapper[csBucket].Find(changesC, changeSetBlock, key)
if err != nil {
if !errors.Is(err, ErrNotFound) {
return nil, false, fmt.Errorf("finding %x in the changeset %d: %w", key, changeSetBlock, err)
}
return nil, false, nil
}
} else {
return nil, false, nil
}
return data, true, nil
}

View File

@ -569,6 +569,13 @@ func (it *InvertedIterator) Next() uint64 {
it.advance()
return n
}
func (it *InvertedIterator) ToBitamp() *roaring64.Bitmap {
bm := bitmapdb.NewBitmap64()
for it.HasNext() {
bm.Add(it.Next())
}
return bm
}
type InvertedIndexContext struct {
ii *InvertedIndex