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) {
logs, err = client.LoadLogsBefore(entry.ID, 100)
require.NoError(t, err)
require.True(t, len(logs) == oldLogLength+1)
require.True(t, logs[0].ID < entry.ID)
// Test max parameter
logs, err = client.LoadNewestLogs(1)
......@@ -60,8 +61,6 @@ func TestLogging(t *testing.T) {
require.True(t, len(logs) == oldLogLength+1)
// 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)
sessionHelper(t, request, "signature", client)
logs, err = client.LoadNewestLogs(100)
......
......@@ -12,7 +12,7 @@ import (
// LogEntry is a log entry of a past event.
type LogEntry struct {
// General info
ID uint64 `boltholdKey:"ID"`
ID uint64
Type irma.Action
Time irma.Timestamp // Time at which the session was completed
......
package irmaclient
import (
"encoding/binary"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/timshannon/bolthold"
"go.etcd.io/bbolt"
)
......@@ -21,7 +20,7 @@ import (
// Storage provider for a Client
type storage struct {
storagePath string
db *bolthold.Store
db *bbolt.DB
Configuration *irma.Configuration
}
......@@ -38,6 +37,11 @@ const (
databaseFile = "db"
)
// Bucketnames bbolt
const (
logsBucket = "logs"
)
func (s *storage) path(p string) string {
return filepath.Join(s.storagePath, p)
}
......@@ -55,11 +59,7 @@ func (s *storage) EnsureStorageExists() error {
if err = fs.EnsureDirectoryExists(s.path(signaturesDir)); err != nil {
return err
}
s.db, err = bolthold.Open(s.path(databaseFile), 0600, &bolthold.Options{
Options: &bbolt.Options{Timeout: 1 * time.Second},
Encoder: json.Marshal,
Decoder: json.Unmarshal,
})
s.db, err = bbolt.Open(s.path(databaseFile), 0600, &bbolt.Options{Timeout: 1 * time.Second})
return err
}
......@@ -124,8 +124,31 @@ func (s *storage) StoreLogs(logs []*LogEntry) error {
}
func (s *storage) AddLogEntry(entry *LogEntry) error {
// TODO: Key is stored in string format which breaks sorting order
return s.db.Insert(bolthold.NextSequence(), entry)
return s.db.Update(func(tx *bbolt.Tx) error {
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 {
......@@ -205,7 +228,7 @@ func (s *storage) LoadKeyshareServers() (ksses map[irma.SchemeManagerIdentifier]
// a maximum result length of 'max'.
func (s *storage) LoadLogsBefore(startBeforeIndex uint64, max int) ([]*LogEntry, error) {
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()
})
}
......@@ -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.
func (s *storage) loadLogsFromBbolt(max int, startAt func(*bbolt.Cursor) (key, value []byte)) ([]*LogEntry, error) {
var logs []*LogEntry
return logs, s.db.Bolt().View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte("LogEntry"))
return logs, s.db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(logsBucket))
if bucket == nil {
return nil
}
......@@ -235,13 +258,6 @@ func (s *storage) loadLogsFromBbolt(max int, startAt func(*bbolt.Cursor) (key, v
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)
}
return nil
......
......@@ -3,7 +3,6 @@ package irmaclient
import (
"encoding/json"
"fmt"
"github.com/timshannon/bolthold"
"time"
"github.com/privacybydesign/irmago"
......@@ -44,7 +43,7 @@ var clientUpdates = []func(client *Client) error{
// 6: Remove earlier log items of wrong format
nil, // No longer necessary
// 7: Concert log entries to bolthold database
// 7: Concert log entries to bbolt database
func(client *Client) error {
var logs []*LogEntry
var err error
......@@ -59,7 +58,7 @@ var clientUpdates = []func(client *Client) error{
fmt.Println(loaded.Sub(start), "loaded")
// 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 {
// 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,
......@@ -72,7 +71,7 @@ var clientUpdates = []func(client *Client) error{
if err != nil {
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
}
}
......
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