From 35d05ba9c81ed1016ab88bc3c734b7147c9e7645 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 4 Aug 2014 17:10:19 +0200 Subject: [PATCH] LevelDB needs re-coding of positions with zero components. --- coords.go | 22 ++++++++++++++++++++++ leveldb.go | 7 +++++++ sqlite.go | 5 ----- 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 coords.go diff --git a/coords.go b/coords.go new file mode 100644 index 0000000..aa33ed8 --- /dev/null +++ b/coords.go @@ -0,0 +1,22 @@ +// 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 "strconv" + +// Returns database pos as a byte slice. +func pos2bytes(pos int64) []byte { + buf := make([]byte, 8, 8) + for i := 7; i >= 0; i-- { + buf[i] = byte(pos & 0xff) + pos >>= 8 + } + return buf +} + +// Constructs a database key out of byte slice. +func bytes2pos(key []byte) (pos int64, err error) { + return strconv.ParseInt(string(key), 10, 64) +} diff --git a/leveldb.go b/leveldb.go index 85496d8..85060af 100644 --- a/leveldb.go +++ b/leveldb.go @@ -64,6 +64,13 @@ func (ldb *LevelDBBackend) keyExists(key []byte) (exists bool, err error) { func (ldb *LevelDBBackend) Store(hash, key, value []byte) (exists bool, err error) { + var pos int64 + if pos, err = bytes2pos(key); err != nil { + return + } + // Re-code it to make LevelDB happy. + key = pos2bytes(pos) + if exists, err = ldb.keyExists(key); err != nil { return } diff --git a/sqlite.go b/sqlite.go index f1e015f..90a826e 100644 --- a/sqlite.go +++ b/sqlite.go @@ -7,7 +7,6 @@ package main import ( "database/sql" "log" - "strconv" "sync" _ "github.com/mattn/go-sqlite3" @@ -113,10 +112,6 @@ func (sqlb *SqliteBackend) txStmt(stmt *sql.Stmt) *sql.Stmt { return stmt } -func bytes2pos(key []byte) (pos int64, err error) { - return strconv.ParseInt(string(key), 10, 64) -} - func (sqlb *SqliteBackend) Fetch(hash, key []byte) (data []byte, err error) { var pos int64 if pos, err = bytes2pos(key); err != nil {