You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

  1. package leveldb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/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. }