6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-27 09:18:22 +00:00
prologic-saltyim/internal/store.go

56 lines
1013 B
Go
Raw Normal View History

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)
case "memory":
return newMemoryStore(), nil
default:
return nil, ErrInvalidStore
}
}