Added block consumer for LevelDB in interleaver.

This commit is contained in:
Sascha L. Teichmann 2014-08-20 15:26:31 +02:00
parent 74551198fa
commit 02ada38090
2 changed files with 55 additions and 1 deletions

View File

@ -26,7 +26,7 @@ type (
}
BlockConsumer interface {
Consume(Block)
Consume(Block) error
Close() error
}
)

54
interleaver/leveldb.go Normal file
View 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
}