leveldb2_store_kv.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package leveldb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/syndtr/goleveldb/leveldb"
  7. )
  8. func (store *LevelDB2Store) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  9. partitionId := bucketKvKey(key, store.dbCount)
  10. err = store.dbs[partitionId].Put(key, value, nil)
  11. if err != nil {
  12. return fmt.Errorf("kv bucket %d put: %v", partitionId, err)
  13. }
  14. return nil
  15. }
  16. func (store *LevelDB2Store) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  17. partitionId := bucketKvKey(key, store.dbCount)
  18. value, err = store.dbs[partitionId].Get(key, nil)
  19. if err == leveldb.ErrNotFound {
  20. return nil, filer.ErrKvNotFound
  21. }
  22. if err != nil {
  23. return nil, fmt.Errorf("kv bucket %d get: %v", partitionId, err)
  24. }
  25. return
  26. }
  27. func (store *LevelDB2Store) KvDelete(ctx context.Context, key []byte) (err error) {
  28. partitionId := bucketKvKey(key, store.dbCount)
  29. err = store.dbs[partitionId].Delete(key, nil)
  30. if err != nil {
  31. return fmt.Errorf("kv bucket %d delete: %v", partitionId, err)
  32. }
  33. return nil
  34. }
  35. func bucketKvKey(key []byte, dbCount int) (partitionId int) {
  36. return int(key[len(key)-1]) % dbCount
  37. }