namespaced.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "encoding/binary"
  9. "time"
  10. "github.com/syndtr/goleveldb/leveldb"
  11. )
  12. // NamespacedKV is a simple key-value store using a specific namespace within
  13. // a leveldb.
  14. type NamespacedKV struct {
  15. db *leveldb.DB
  16. prefix []byte
  17. }
  18. // NewNamespacedKV returns a new NamespacedKV that lives in the namespace
  19. // specified by the prefix.
  20. func NewNamespacedKV(db *leveldb.DB, prefix string) *NamespacedKV {
  21. return &NamespacedKV{
  22. db: db,
  23. prefix: []byte(prefix),
  24. }
  25. }
  26. // PutInt64 stores a new int64. Any existing value (even if of another type)
  27. // is overwritten.
  28. func (n *NamespacedKV) PutInt64(key string, val int64) {
  29. keyBs := append(n.prefix, []byte(key)...)
  30. var valBs [8]byte
  31. binary.BigEndian.PutUint64(valBs[:], uint64(val))
  32. n.db.Put(keyBs, valBs[:], nil)
  33. }
  34. // Int64 returns the stored value interpreted as an int64 and a boolean that
  35. // is false if no value was stored at the key.
  36. func (n *NamespacedKV) Int64(key string) (int64, bool) {
  37. keyBs := append(n.prefix, []byte(key)...)
  38. valBs, err := n.db.Get(keyBs, nil)
  39. if err != nil {
  40. return 0, false
  41. }
  42. val := binary.BigEndian.Uint64(valBs)
  43. return int64(val), true
  44. }
  45. // PutTime stores a new time.Time. Any existing value (even if of another
  46. // type) is overwritten.
  47. func (n *NamespacedKV) PutTime(key string, val time.Time) {
  48. keyBs := append(n.prefix, []byte(key)...)
  49. valBs, _ := val.MarshalBinary() // never returns an error
  50. n.db.Put(keyBs, valBs, nil)
  51. }
  52. // Time returns the stored value interpreted as a time.Time and a boolean
  53. // that is false if no value was stored at the key.
  54. func (n NamespacedKV) Time(key string) (time.Time, bool) {
  55. var t time.Time
  56. keyBs := append(n.prefix, []byte(key)...)
  57. valBs, err := n.db.Get(keyBs, nil)
  58. if err != nil {
  59. return t, false
  60. }
  61. err = t.UnmarshalBinary(valBs)
  62. return t, err == nil
  63. }
  64. // PutString stores a new string. Any existing value (even if of another type)
  65. // is overwritten.
  66. func (n *NamespacedKV) PutString(key, val string) {
  67. keyBs := append(n.prefix, []byte(key)...)
  68. n.db.Put(keyBs, []byte(val), nil)
  69. }
  70. // String returns the stored value interpreted as a string and a boolean that
  71. // is false if no value was stored at the key.
  72. func (n NamespacedKV) String(key string) (string, bool) {
  73. keyBs := append(n.prefix, []byte(key)...)
  74. valBs, err := n.db.Get(keyBs, nil)
  75. if err != nil {
  76. return "", false
  77. }
  78. return string(valBs), true
  79. }
  80. // Delete deletes the specified key. It is allowed to delete a nonexistent
  81. // key.
  82. func (n NamespacedKV) Delete(key string) {
  83. keyBs := append(n.prefix, []byte(key)...)
  84. n.db.Delete(keyBs, nil)
  85. }