storage.go 771 B

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