LevelDB needs re-coding of positions with zero components.

This commit is contained in:
Sascha L. Teichmann 2014-08-04 17:10:19 +02:00
parent de9f738306
commit 35d05ba9c8
3 changed files with 29 additions and 5 deletions

22
coords.go Normal file
View File

@ -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)
}

View File

@ -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
}

View File

@ -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 {