Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions badger/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package badger

import "github.com/dgraph-io/badger/badger"

// Connection - Connection
type Connection struct {
DB *badger.KV
}

// Open - open connection and set up
func (c *Connection) Open(path string) {
opt := badger.DefaultOptions
opt.Dir = path
opt.ValueDir = opt.Dir
db, err := badger.NewKV(&opt)
if err != nil {
panic(err)
}
c.DB = db
}

// Close - close connection and clean up
func (c *Connection) Close() {
c.DB.Close()
}
63 changes: 63 additions & 0 deletions badger/coord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package badger

import (
"encoding/binary"
"log"
"math"

"github.com/dgraph-io/badger/badger"
"github.com/missinglink/gosmparse"
)

// WriteCoord - encode and write lat/lon pair to db
func (c *Connection) WriteCoord(item gosmparse.Node) error {

// encode id
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(item.ID))

// encode lat
lat := make([]byte, 8)
binary.BigEndian.PutUint64(lat, math.Float64bits(item.Lat))

// encode lon
lon := make([]byte, 8)
binary.BigEndian.PutUint64(lon, math.Float64bits(item.Lon))

// value
value := append(lat, lon...)

// write to db
err := c.DB.Set(key, value)
log.Println("wrote", item.ID)
if err != nil {
log.Println("write error", err)
return err
}

return nil
}

// ReadCoord - read lat/lon pair from db and decode
func (c *Connection) ReadCoord(id int64) (*gosmparse.Node, error) {

// encode id
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(id))

// read from db
var item badger.KVItem
err := c.DB.Get(key, &item)
if err != nil {
return nil, err
}

data := item.Value()

// decode item
return &gosmparse.Node{
ID: id,
Lat: math.Float64frombits(binary.BigEndian.Uint64(data[:8])),
Lon: math.Float64frombits(binary.BigEndian.Uint64(data[8:])),
}, nil
}
4 changes: 2 additions & 2 deletions command/json_flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"log"
"os"

"github.com/missinglink/pbf/badger"
"github.com/missinglink/pbf/handler"
"github.com/missinglink/pbf/leveldb"
"github.com/missinglink/pbf/lib"
"github.com/missinglink/pbf/parser"
"github.com/missinglink/pbf/proxy"
Expand Down Expand Up @@ -42,7 +42,7 @@ func JSONFlat(c *cli.Context) error {
lib.EnsureDirectoryExists(leveldbPath, "leveldb")

// open database connection
conn := &leveldb.Connection{}
conn := &badger.Connection{}
conn.Open(leveldbPath)
defer conn.Close()

Expand Down
4 changes: 2 additions & 2 deletions command/store_noderefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"log"
"os"

"github.com/missinglink/pbf/badger"
"github.com/missinglink/pbf/handler"
"github.com/missinglink/pbf/leveldb"
"github.com/missinglink/pbf/lib"
"github.com/missinglink/pbf/parser"
"github.com/missinglink/pbf/proxy"
Expand Down Expand Up @@ -36,7 +36,7 @@ func StoreNodeRefs(c *cli.Context) error {
lib.EnsureDirectoryExists(leveldbPath, "leveldb")

// open database connection
conn := &leveldb.Connection{}
conn := &badger.Connection{}
conn.Open(leveldbPath)
defer conn.Close()

Expand Down
4 changes: 2 additions & 2 deletions handler/denormalized_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"log"

"github.com/missinglink/gosmparse"
"github.com/missinglink/pbf/badger"
"github.com/missinglink/pbf/json"
"github.com/missinglink/pbf/leveldb"
"github.com/missinglink/pbf/lib"
)

// DenormalizedJSON - JSON
type DenormalizedJSON struct {
Writer *lib.BufferedWriter
Conn *leveldb.Connection
Conn *badger.Connection
ComputeCentroid bool
ExportLatLons bool
}
Expand Down
4 changes: 2 additions & 2 deletions proxy/store_noderefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"log"

"github.com/missinglink/gosmparse"
"github.com/missinglink/pbf/leveldb"
"github.com/missinglink/pbf/badger"
"github.com/missinglink/pbf/lib"
)

// StoreRefs - filter only elements that appear in masks
type StoreRefs struct {
Handler gosmparse.OSMReader
Conn *leveldb.Connection
Conn *badger.Connection
Masks *lib.BitmaskMap
}

Expand Down