mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-30 18:51:03 +00:00
![James Mills](/assets/img/avatar_default.png)
Co-authored-by: James Mills <prologic@shortcircuit.net.au> Co-authored-by: Jon Lundy <jon@xuu.cc> Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/36
54 lines
966 B
Go
54 lines
966 B
Go
package internal
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidStore = errors.New("error: invalid store")
|
|
ErrPollNotFound = errors.New("error: poll not found")
|
|
)
|
|
|
|
type StoreURI struct {
|
|
Type string
|
|
Path string
|
|
}
|
|
|
|
func (u StoreURI) IsZero() bool {
|
|
return u.Type == "" && u.Path == ""
|
|
}
|
|
|
|
func (u StoreURI) String() string {
|
|
return fmt.Sprintf("%s://%s", u.Type, u.Path)
|
|
}
|
|
|
|
func ParseStoreURI(uri string) (*StoreURI, error) {
|
|
parts := strings.Split(uri, "://")
|
|
if len(parts) == 2 {
|
|
return &StoreURI{Type: strings.ToLower(parts[0]), Path: parts[1]}, nil
|
|
}
|
|
return nil, fmt.Errorf("invalid uri: %s", uri)
|
|
}
|
|
|
|
type Store interface {
|
|
Merge() error
|
|
Close() error
|
|
Sync() error
|
|
}
|
|
|
|
func NewStore(store string) (Store, error) {
|
|
u, err := ParseStoreURI(store)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing store uri: %s", err)
|
|
}
|
|
|
|
switch u.Type {
|
|
case "bitcask":
|
|
return newBitcaskStore(u.Path)
|
|
default:
|
|
return nil, ErrInvalidStore
|
|
}
|
|
}
|