storage.go 833 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. const DEFAULT_STORAGE_ENGIND = "bolt"
  8. var _supported_storage = 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. _supported_storage[name] = fn
  14. }
  15. type Options struct {
  16. Timeout time.Duration
  17. }
  18. type Storage interface {
  19. Set(k, v []byte) error
  20. Get(k []byte) ([]byte, error)
  21. Delete(k []byte) error
  22. ForEach(fn func(k, v []byte) error) error
  23. Close() error
  24. WAlName() string
  25. }
  26. func OpenStorage(path string) (Storage, error) {
  27. wse := os.Getenv("WUKONG_STORAGE_ENGINE")
  28. if wse == "" {
  29. wse = DEFAULT_STORAGE_ENGIND
  30. }
  31. if has, fn := _supported_storage[wse]; has {
  32. return fn(path)
  33. }
  34. return nil, fmt.Errorf("unsupported storage engine %v", wse)
  35. }