6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-28 09:41:02 +00:00
prologic-saltyim/internal/store.go
James Mills be4b4a5e9d Add a basic structure for a go-app PWA + integrated Broker (#36)
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
2022-03-21 22:27:35 +00:00

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
}
}