mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-20 00:50:20 +01:00
Added block consumer for LevelDB in interleaver.
This commit is contained in:
parent
74551198fa
commit
02ada38090
@ -26,7 +26,7 @@ type (
|
||||
}
|
||||
|
||||
BlockConsumer interface {
|
||||
Consume(Block)
|
||||
Consume(Block) error
|
||||
Close() error
|
||||
}
|
||||
)
|
||||
|
54
interleaver/leveldb.go
Normal file
54
interleaver/leveldb.go
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright 2014 by Sascha L. Teichmann
|
||||
// Use of this source code is governed by the MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
||||
|
||||
leveldb "github.com/jmhodges/levigo"
|
||||
)
|
||||
|
||||
type LevelDBBlockConsumer struct {
|
||||
db *leveldb.DB
|
||||
wo *leveldb.WriteOptions
|
||||
joiner common.KeyJoiner
|
||||
encoder common.KeyEncoder
|
||||
}
|
||||
|
||||
func NewLevelDBBlockConsumer(
|
||||
path string,
|
||||
joiner common.KeyJoiner,
|
||||
encoder common.KeyEncoder) (ldbc *LevelDBBlockConsumer, err error) {
|
||||
|
||||
opts := leveldb.NewOptions()
|
||||
opts.SetCreateIfMissing(true)
|
||||
|
||||
var db *leveldb.DB
|
||||
if db, err = leveldb.Open(path, opts); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ldbc = &LevelDBBlockConsumer{
|
||||
db: db,
|
||||
wo: leveldb.NewWriteOptions(),
|
||||
joiner: joiner,
|
||||
encoder: encoder}
|
||||
return
|
||||
}
|
||||
|
||||
func (ldbc *LevelDBBlockConsumer) Close() error {
|
||||
ldbc.wo.Close()
|
||||
ldbc.db.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ldbc *LevelDBBlockConsumer) Consume(block Block) (err error) {
|
||||
var encodedKey []byte
|
||||
if encodedKey, err = ldbc.encoder(ldbc.joiner(block.Coord)); err != nil {
|
||||
return
|
||||
}
|
||||
err = ldbc.db.Put(ldbc.wo, encodedKey, block.Data)
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user