83df0ee1b0
1. Added KeyValueStore.Iterate for implementing the different List, ListV2 etc. implementations. This allows for more efficient use of memory depending on the situation. 2. Implemented an inmemory teststore for running tests. This should allow to replace MockKeyValueStore in most places. 3. Rewrote tests 4. Pulled out logger from bolt implementation so it can be used for all other storage implementations. 5. Fixed multiple things in bolt and redis implementations.
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// GetBytes transforms an empty interface type into a byte slice
|
|
func GetBytes(key interface{}) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
enc := gob.NewEncoder(&buf)
|
|
err := enc.Encode(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// ParseURL extracts database parameters from a string as a URL
|
|
// bolt://storj.db
|
|
// bolt://C:\storj.db
|
|
// redis://hostname
|
|
func ParseURL(s string) (*url.URL, error) {
|
|
if strings.HasPrefix(s, "bolt://") {
|
|
return &url.URL{
|
|
Scheme: "bolt",
|
|
Path: strings.TrimPrefix(s, "bolt://"),
|
|
}, nil
|
|
}
|
|
|
|
return url.Parse(s)
|
|
}
|
|
|
|
// CombineErrors combines multiple errors to a single error
|
|
func CombineErrors(errs ...error) error { return combinedError(errs) }
|
|
|
|
type combinedError []error
|
|
|
|
func (errs combinedError) Cause() error {
|
|
if len(errs) > 0 {
|
|
return errs[0]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (errs combinedError) Error() string {
|
|
if len(errs) > 0 {
|
|
limit := 5
|
|
if len(errs) < limit {
|
|
limit = len(errs)
|
|
}
|
|
allErrors := errs[0].Error()
|
|
for _, err := range errs[1:limit] {
|
|
allErrors += "\n" + err.Error()
|
|
}
|
|
return allErrors
|
|
}
|
|
return ""
|
|
}
|