Commit 4a61a6dd authored by Ivar Derksen's avatar Ivar Derksen Committed by Sietse Ringers
Browse files

Use bbolt instead of bolthold

parent a8eeddcf
...@@ -53,6 +53,7 @@ func TestLogging(t *testing.T) { ...@@ -53,6 +53,7 @@ func TestLogging(t *testing.T) {
logs, err = client.LoadLogsBefore(entry.ID, 100) logs, err = client.LoadLogsBefore(entry.ID, 100)
require.NoError(t, err) require.NoError(t, err)
require.True(t, len(logs) == oldLogLength+1) require.True(t, len(logs) == oldLogLength+1)
require.True(t, logs[0].ID < entry.ID)
// Test max parameter // Test max parameter
logs, err = client.LoadNewestLogs(1) logs, err = client.LoadNewestLogs(1)
...@@ -60,8 +61,6 @@ func TestLogging(t *testing.T) { ...@@ -60,8 +61,6 @@ func TestLogging(t *testing.T) {
require.True(t, len(logs) == oldLogLength+1) require.True(t, len(logs) == oldLogLength+1)
// Do signature session // Do signature session
// This test might fail due to bolthold issue https://github.com/timshannon/bolthold/issues/68
// This issue is fixed, so just run `dep ensure -update github.com/timshannon/bolthold`
request = getSigningRequest(attrid) request = getSigningRequest(attrid)
sessionHelper(t, request, "signature", client) sessionHelper(t, request, "signature", client)
logs, err = client.LoadNewestLogs(100) logs, err = client.LoadNewestLogs(100)
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// LogEntry is a log entry of a past event. // LogEntry is a log entry of a past event.
type LogEntry struct { type LogEntry struct {
// General info // General info
ID uint64 `boltholdKey:"ID"` ID uint64
Type irma.Action Type irma.Action
Time irma.Timestamp // Time at which the session was completed Time irma.Timestamp // Time at which the session was completed
......
package irmaclient package irmaclient
import ( import (
"encoding/binary"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"time" "time"
"github.com/privacybydesign/gabi" "github.com/privacybydesign/gabi"
"github.com/privacybydesign/irmago" "github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs" "github.com/privacybydesign/irmago/internal/fs"
"github.com/timshannon/bolthold"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
...@@ -21,7 +20,7 @@ import ( ...@@ -21,7 +20,7 @@ import (
// Storage provider for a Client // Storage provider for a Client
type storage struct { type storage struct {
storagePath string storagePath string
db *bolthold.Store db *bbolt.DB
Configuration *irma.Configuration Configuration *irma.Configuration
} }
...@@ -38,6 +37,11 @@ const ( ...@@ -38,6 +37,11 @@ const (
databaseFile = "db" databaseFile = "db"
) )
// Bucketnames bbolt
const (
logsBucket = "logs"
)
func (s *storage) path(p string) string { func (s *storage) path(p string) string {
return filepath.Join(s.storagePath, p) return filepath.Join(s.storagePath, p)
} }
...@@ -55,11 +59,7 @@ func (s *storage) EnsureStorageExists() error { ...@@ -55,11 +59,7 @@ func (s *storage) EnsureStorageExists() error {
if err = fs.EnsureDirectoryExists(s.path(signaturesDir)); err != nil { if err = fs.EnsureDirectoryExists(s.path(signaturesDir)); err != nil {
return err return err
} }
s.db, err = bolthold.Open(s.path(databaseFile), 0600, &bolthold.Options{ s.db, err = bbolt.Open(s.path(databaseFile), 0600, &bbolt.Options{Timeout: 1 * time.Second})
Options: &bbolt.Options{Timeout: 1 * time.Second},
Encoder: json.Marshal,
Decoder: json.Unmarshal,
})
return err return err
} }
...@@ -124,8 +124,31 @@ func (s *storage) StoreLogs(logs []*LogEntry) error { ...@@ -124,8 +124,31 @@ func (s *storage) StoreLogs(logs []*LogEntry) error {
} }
func (s *storage) AddLogEntry(entry *LogEntry) error { func (s *storage) AddLogEntry(entry *LogEntry) error {
// TODO: Key is stored in string format which breaks sorting order return s.db.Update(func(tx *bbolt.Tx) error {
return s.db.Insert(bolthold.NextSequence(), entry) return s.TxAddLogEntry(tx, entry)
})
}
func (s *storage) TxAddLogEntry(tx *bbolt.Tx, entry *LogEntry) error {
b, err := tx.CreateBucketIfNotExists([]byte(logsBucket))
if err != nil {
return err
}
entry.ID, err = b.NextSequence()
if err != nil {
return err
}
k := s.logEntryKeyToBytes(entry.ID)
v, err := json.Marshal(entry)
return b.Put(k, v)
}
func (s *storage) logEntryKeyToBytes(id uint64) []byte {
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, id)
return k
} }
func (s *storage) StorePreferences(prefs Preferences) error { func (s *storage) StorePreferences(prefs Preferences) error {
...@@ -205,7 +228,7 @@ func (s *storage) LoadKeyshareServers() (ksses map[irma.SchemeManagerIdentifier] ...@@ -205,7 +228,7 @@ func (s *storage) LoadKeyshareServers() (ksses map[irma.SchemeManagerIdentifier]
// a maximum result length of 'max'. // a maximum result length of 'max'.
func (s *storage) LoadLogsBefore(startBeforeIndex uint64, max int) ([]*LogEntry, error) { func (s *storage) LoadLogsBefore(startBeforeIndex uint64, max int) ([]*LogEntry, error) {
return s.loadLogsFromBbolt(max, func(c *bbolt.Cursor) (key, value []byte) { return s.loadLogsFromBbolt(max, func(c *bbolt.Cursor) (key, value []byte) {
c.Seek([]byte(strconv.FormatUint(startBeforeIndex, 10))) c.Seek(s.logEntryKeyToBytes(startBeforeIndex))
return c.Prev() return c.Prev()
}) })
} }
...@@ -222,8 +245,8 @@ func (s *storage) LoadNewestLogs(max int) ([]*LogEntry, error) { ...@@ -222,8 +245,8 @@ func (s *storage) LoadNewestLogs(max int) ([]*LogEntry, error) {
// the key and the value of the first element from the bbolt database that should be loaded. // the key and the value of the first element from the bbolt database that should be loaded.
func (s *storage) loadLogsFromBbolt(max int, startAt func(*bbolt.Cursor) (key, value []byte)) ([]*LogEntry, error) { func (s *storage) loadLogsFromBbolt(max int, startAt func(*bbolt.Cursor) (key, value []byte)) ([]*LogEntry, error) {
var logs []*LogEntry var logs []*LogEntry
return logs, s.db.Bolt().View(func(tx *bbolt.Tx) error { return logs, s.db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte("LogEntry")) bucket := tx.Bucket([]byte(logsBucket))
if bucket == nil { if bucket == nil {
return nil return nil
} }
...@@ -235,13 +258,6 @@ func (s *storage) loadLogsFromBbolt(max int, startAt func(*bbolt.Cursor) (key, v ...@@ -235,13 +258,6 @@ func (s *storage) loadLogsFromBbolt(max int, startAt func(*bbolt.Cursor) (key, v
return err return err
} }
// Manually set ID in struct, because somehow bolthold does not store this (also not when uint64 is used)
id, err := strconv.ParseUint(string(k), 10, 64)
if err != nil {
return err
}
log.ID = id
logs = append(logs, &log) logs = append(logs, &log)
} }
return nil return nil
......
...@@ -3,7 +3,6 @@ package irmaclient ...@@ -3,7 +3,6 @@ package irmaclient
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/timshannon/bolthold"
"time" "time"
"github.com/privacybydesign/irmago" "github.com/privacybydesign/irmago"
...@@ -44,7 +43,7 @@ var clientUpdates = []func(client *Client) error{ ...@@ -44,7 +43,7 @@ var clientUpdates = []func(client *Client) error{
// 6: Remove earlier log items of wrong format // 6: Remove earlier log items of wrong format
nil, // No longer necessary nil, // No longer necessary
// 7: Concert log entries to bolthold database // 7: Concert log entries to bbolt database
func(client *Client) error { func(client *Client) error {
var logs []*LogEntry var logs []*LogEntry
var err error var err error
...@@ -59,7 +58,7 @@ var clientUpdates = []func(client *Client) error{ ...@@ -59,7 +58,7 @@ var clientUpdates = []func(client *Client) error{
fmt.Println(loaded.Sub(start), "loaded") fmt.Println(loaded.Sub(start), "loaded")
// Open one bolt transaction to process all our log entries in // Open one bolt transaction to process all our log entries in
err = client.storage.db.Bolt().Update(func(tx *bbolt.Tx) error { err = client.storage.db.Update(func(tx *bbolt.Tx) error {
for _, log := range logs { for _, log := range logs {
// As log.Request is a json.RawMessage it would not get updated to the new session request // As log.Request is a json.RawMessage it would not get updated to the new session request
// format by re-marshaling the containing struct, as normal struct members would, // format by re-marshaling the containing struct, as normal struct members would,
...@@ -72,7 +71,7 @@ var clientUpdates = []func(client *Client) error{ ...@@ -72,7 +71,7 @@ var clientUpdates = []func(client *Client) error{
if err != nil { if err != nil {
return err return err
} }
if err = client.storage.db.TxUpsert(tx, bolthold.NextSequence(), log); err != nil { if err = client.storage.TxAddLogEntry(tx, log); err != nil {
return err return err
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment