2020-05-31 07:32:33 +00:00
|
|
|
package etl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-05-31 12:23:34 +00:00
|
|
|
"runtime"
|
2020-05-31 07:32:33 +00:00
|
|
|
|
2020-05-31 12:23:34 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
2020-05-31 07:32:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type dataProvider interface {
|
2020-06-10 20:07:14 +00:00
|
|
|
Next(decoder Decoder) ([]byte, []byte, error)
|
2020-05-31 07:32:33 +00:00
|
|
|
Dispose() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileDataProvider struct {
|
|
|
|
file *os.File
|
|
|
|
reader io.Reader
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
type Encoder interface {
|
|
|
|
Encode(toWrite interface{}) error
|
|
|
|
Reset(writer io.Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FlushToDisk(encoder Encoder, currentKey []byte, b Buffer, datadir string) (dataProvider, error) {
|
|
|
|
if b.Len() == 0 {
|
2020-05-31 07:32:33 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
bufferFile, err := ioutil.TempFile(datadir, "tg-sync-sortable-buf")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-25 07:34:40 +00:00
|
|
|
defer bufferFile.Sync() //nolint:errcheck
|
2020-05-31 07:32:33 +00:00
|
|
|
|
2020-06-25 07:34:40 +00:00
|
|
|
w := bufio.NewWriterSize(bufferFile, BufIOSize)
|
2020-05-31 07:32:33 +00:00
|
|
|
defer w.Flush() //nolint:errcheck
|
2020-06-25 07:34:40 +00:00
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
encoder.Reset(w)
|
|
|
|
for _, entry := range b.GetEntries() {
|
|
|
|
err = writeToDisk(encoder, entry.key, entry.value)
|
2020-05-31 07:32:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error writing entries to disk: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 12:23:34 +00:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
log.Info(
|
|
|
|
"Flushed buffer file",
|
2020-06-21 14:13:17 +00:00
|
|
|
"current key", makeCurrentKeyStr(currentKey),
|
2020-05-31 12:23:34 +00:00
|
|
|
"name", bufferFile.Name(),
|
|
|
|
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC))
|
2020-06-10 20:07:14 +00:00
|
|
|
b.Reset()
|
2020-05-31 07:32:33 +00:00
|
|
|
|
|
|
|
return &fileDataProvider{bufferFile, nil}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
func (p *fileDataProvider) Next(decoder Decoder) ([]byte, []byte, error) {
|
2020-05-31 07:32:33 +00:00
|
|
|
if p.reader == nil {
|
|
|
|
_, err := p.file.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-06-25 07:34:40 +00:00
|
|
|
p.reader = bufio.NewReaderSize(p.file, BufIOSize)
|
2020-05-31 07:32:33 +00:00
|
|
|
}
|
|
|
|
decoder.Reset(p.reader)
|
|
|
|
return readElementFromDisk(decoder)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *fileDataProvider) Dispose() error {
|
|
|
|
errClose := p.file.Close()
|
|
|
|
errRemove := os.Remove(p.file.Name())
|
|
|
|
if errClose != nil {
|
|
|
|
return errClose
|
|
|
|
}
|
|
|
|
if errRemove != nil {
|
|
|
|
return errRemove
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *fileDataProvider) String() string {
|
|
|
|
return fmt.Sprintf("%T(file: %s)", p, p.file.Name())
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
func writeToDisk(encoder Encoder, key []byte, value []byte) error {
|
2020-05-31 07:32:33 +00:00
|
|
|
toWrite := [][]byte{key, value}
|
|
|
|
return encoder.Encode(toWrite)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readElementFromDisk(decoder Decoder) ([]byte, []byte, error) {
|
|
|
|
result := make([][]byte, 2)
|
|
|
|
err := decoder.Decode(&result)
|
|
|
|
return result[0], result[1], err
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryDataProvider struct {
|
2020-06-10 20:07:14 +00:00
|
|
|
buffer Buffer
|
2020-05-31 07:32:33 +00:00
|
|
|
currentIndex int
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
func KeepInRAM(buffer Buffer) dataProvider {
|
2020-05-31 07:32:33 +00:00
|
|
|
return &memoryDataProvider{buffer, 0}
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
func (p *memoryDataProvider) Next(decoder Decoder) ([]byte, []byte, error) {
|
2020-05-31 07:32:33 +00:00
|
|
|
if p.currentIndex >= p.buffer.Len() {
|
|
|
|
return nil, nil, io.EOF
|
|
|
|
}
|
|
|
|
entry := p.buffer.Get(p.currentIndex)
|
|
|
|
p.currentIndex++
|
|
|
|
return entry.key, entry.value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *memoryDataProvider) Dispose() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *memoryDataProvider) String() string {
|
|
|
|
return fmt.Sprintf("%T(buffer.Len: %d)", p, p.buffer.Len())
|
|
|
|
}
|