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.

75 lines
2.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package hbase
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/tsuna/gohbase/hrpc"
  6. "time"
  7. )
  8. const(
  9. COLUMN_NAME = "a"
  10. )
  11. func (store *HbaseStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  12. return store.doPut(ctx, store.cfKv, key, value, 0)
  13. }
  14. func (store *HbaseStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  15. return store.doGet(ctx, store.cfKv, key)
  16. }
  17. func (store *HbaseStore) KvDelete(ctx context.Context, key []byte) (err error) {
  18. return store.doDelete(ctx, store.cfKv, key)
  19. }
  20. func (store *HbaseStore) doPut(ctx context.Context, cf string, key, value []byte, ttlSecond int32) (err error) {
  21. if ttlSecond > 0 {
  22. return store.doPutWithOptions(ctx, cf, key, value, hrpc.Durability(hrpc.AsyncWal), hrpc.TTL(time.Duration(ttlSecond)*time.Second))
  23. }
  24. return store.doPutWithOptions(ctx, cf, key, value, hrpc.Durability(hrpc.AsyncWal))
  25. }
  26. func (store *HbaseStore) doPutWithOptions(ctx context.Context, cf string, key, value []byte, options ...func(hrpc.Call) error) (err error) {
  27. values := map[string]map[string][]byte{cf: map[string][]byte{}}
  28. values[cf][COLUMN_NAME] = value
  29. putRequest, err := hrpc.NewPut(ctx, store.table, key, values, options...)
  30. if err != nil {
  31. return err
  32. }
  33. _, err = store.Client.Put(putRequest)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func (store *HbaseStore) doGet(ctx context.Context, cf string, key []byte) (value []byte, err error) {
  40. family := map[string][]string{cf: {COLUMN_NAME}}
  41. getRequest, err := hrpc.NewGet(context.Background(), store.table, key, hrpc.Families(family))
  42. if err != nil {
  43. return nil, err
  44. }
  45. getResp, err := store.Client.Get(getRequest)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if len(getResp.Cells) == 0 {
  50. return nil, filer.ErrKvNotFound
  51. }
  52. return getResp.Cells[0].Value, nil
  53. }
  54. func (store *HbaseStore) doDelete(ctx context.Context, cf string, key []byte) (err error) {
  55. values := map[string]map[string][]byte{cf: map[string][]byte{}}
  56. values[cf][COLUMN_NAME] = nil
  57. deleteRequest, err := hrpc.NewDel(ctx, store.table, key, values, hrpc.Durability(hrpc.AsyncWal))
  58. if err != nil {
  59. return err
  60. }
  61. _, err = store.Client.Delete(deleteRequest)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }