namespaced.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 https://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "encoding/binary"
  9. "time"
  10. "github.com/syndtr/goleveldb/leveldb/util"
  11. )
  12. // NamespacedKV is a simple key-value store using a specific namespace within
  13. // a leveldb.
  14. type NamespacedKV struct {
  15. db *Lowlevel
  16. prefix []byte
  17. }
  18. // NewNamespacedKV returns a new NamespacedKV that lives in the namespace
  19. // specified by the prefix.
  20. func NewNamespacedKV(db *Lowlevel, prefix string) *NamespacedKV {
  21. prefixBs := []byte(prefix)
  22. // After the conversion from string the cap will be larger than the len (in Go 1.11.5,
  23. // 32 bytes cap for small strings). We need to cut it down to ensure append() calls
  24. // on the prefix make a new allocation.
  25. prefixBs = prefixBs[:len(prefixBs):len(prefixBs)]
  26. return &NamespacedKV{
  27. db: db,
  28. prefix: prefixBs,
  29. }
  30. }
  31. // Reset removes all entries in this namespace.
  32. func (n *NamespacedKV) Reset() {
  33. it := n.db.NewIterator(util.BytesPrefix(n.prefix), nil)
  34. defer it.Release()
  35. batch := n.db.newBatch()
  36. for it.Next() {
  37. batch.Delete(it.Key())
  38. batch.checkFlush()
  39. }
  40. batch.flush()
  41. }
  42. // PutInt64 stores a new int64. Any existing value (even if of another type)
  43. // is overwritten.
  44. func (n *NamespacedKV) PutInt64(key string, val int64) {
  45. var valBs [8]byte
  46. binary.BigEndian.PutUint64(valBs[:], uint64(val))
  47. n.db.Put(n.prefixedKey(key), valBs[:], nil)
  48. }
  49. // Int64 returns the stored value interpreted as an int64 and a boolean that
  50. // is false if no value was stored at the key.
  51. func (n *NamespacedKV) Int64(key string) (int64, bool) {
  52. valBs, err := n.db.Get(n.prefixedKey(key), nil)
  53. if err != nil {
  54. return 0, false
  55. }
  56. val := binary.BigEndian.Uint64(valBs)
  57. return int64(val), true
  58. }
  59. // PutTime stores a new time.Time. Any existing value (even if of another
  60. // type) is overwritten.
  61. func (n *NamespacedKV) PutTime(key string, val time.Time) {
  62. valBs, _ := val.MarshalBinary() // never returns an error
  63. n.db.Put(n.prefixedKey(key), valBs, nil)
  64. }
  65. // Time returns the stored value interpreted as a time.Time and a boolean
  66. // that is false if no value was stored at the key.
  67. func (n NamespacedKV) Time(key string) (time.Time, bool) {
  68. var t time.Time
  69. valBs, err := n.db.Get(n.prefixedKey(key), nil)
  70. if err != nil {
  71. return t, false
  72. }
  73. err = t.UnmarshalBinary(valBs)
  74. return t, err == nil
  75. }
  76. // PutString stores a new string. Any existing value (even if of another type)
  77. // is overwritten.
  78. func (n *NamespacedKV) PutString(key, val string) {
  79. n.db.Put(n.prefixedKey(key), []byte(val), nil)
  80. }
  81. // String returns the stored value interpreted as a string and a boolean that
  82. // is false if no value was stored at the key.
  83. func (n NamespacedKV) String(key string) (string, bool) {
  84. valBs, err := n.db.Get(n.prefixedKey(key), nil)
  85. if err != nil {
  86. return "", false
  87. }
  88. return string(valBs), true
  89. }
  90. // PutBytes stores a new byte slice. Any existing value (even if of another type)
  91. // is overwritten.
  92. func (n *NamespacedKV) PutBytes(key string, val []byte) {
  93. n.db.Put(n.prefixedKey(key), val, nil)
  94. }
  95. // Bytes returns the stored value as a raw byte slice and a boolean that
  96. // is false if no value was stored at the key.
  97. func (n NamespacedKV) Bytes(key string) ([]byte, bool) {
  98. valBs, err := n.db.Get(n.prefixedKey(key), nil)
  99. if err != nil {
  100. return nil, false
  101. }
  102. return valBs, true
  103. }
  104. // PutBool stores a new boolean. Any existing value (even if of another type)
  105. // is overwritten.
  106. func (n *NamespacedKV) PutBool(key string, val bool) {
  107. if val {
  108. n.db.Put(n.prefixedKey(key), []byte{0x0}, nil)
  109. } else {
  110. n.db.Put(n.prefixedKey(key), []byte{0x1}, nil)
  111. }
  112. }
  113. // Bool returns the stored value as a boolean and a boolean that
  114. // is false if no value was stored at the key.
  115. func (n NamespacedKV) Bool(key string) (bool, bool) {
  116. valBs, err := n.db.Get(n.prefixedKey(key), nil)
  117. if err != nil {
  118. return false, false
  119. }
  120. return valBs[0] == 0x0, true
  121. }
  122. // Delete deletes the specified key. It is allowed to delete a nonexistent
  123. // key.
  124. func (n NamespacedKV) Delete(key string) {
  125. n.db.Delete(n.prefixedKey(key), nil)
  126. }
  127. func (n NamespacedKV) prefixedKey(key string) []byte {
  128. return append(n.prefix, []byte(key)...)
  129. }
  130. // Well known namespaces that can be instantiated without knowing the key
  131. // details.
  132. // NewDeviceStatisticsNamespace creates a KV namespace for device statistics
  133. // for the given device.
  134. func NewDeviceStatisticsNamespace(db *Lowlevel, device string) *NamespacedKV {
  135. return NewNamespacedKV(db, string(KeyTypeDeviceStatistic)+device)
  136. }
  137. // NewFolderStatisticsNamespace creates a KV namespace for folder statistics
  138. // for the given folder.
  139. func NewFolderStatisticsNamespace(db *Lowlevel, folder string) *NamespacedKV {
  140. return NewNamespacedKV(db, string(KeyTypeFolderStatistic)+folder)
  141. }
  142. // NewMiscDateNamespace creates a KV namespace for miscellaneous metadata.
  143. func NewMiscDataNamespace(db *Lowlevel) *NamespacedKV {
  144. return NewNamespacedKV(db, string(KeyTypeMiscData))
  145. }