storage.go 779 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. const DEFAULT_STORAGE_ENGINE = "bolt"
  8. var supportedStorage = map[string]func(path string) (Storage, error){
  9. "kv": openKVStorage,
  10. "bolt": openBoltStorage,
  11. }
  12. func RegisterStorageEngine(name string, fn func(path string) (Storage, error)) {
  13. supportedStorage[name] = fn
  14. }
  15. type Storage interface {
  16. Set(k, v []byte) error
  17. Get(k []byte) ([]byte, error)
  18. Delete(k []byte) error
  19. ForEach(fn func(k, v []byte) error) error
  20. Close() error
  21. WALName() string
  22. }
  23. func OpenStorage(path string) (Storage, error) {
  24. wse := os.Getenv("WUKONG_STORAGE_ENGINE")
  25. if wse == "" {
  26. wse = DEFAULT_STORAGE_ENGINE
  27. }
  28. if has, fn := supportedStorage[wse]; has {
  29. return fn(path)
  30. }
  31. return nil, fmt.Errorf("unsupported storage engine %v", wse)
  32. }