本站源代码
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.

388 lines
6.4KB

  1. package nodb
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. type KVPair struct {
  7. Key []byte
  8. Value []byte
  9. }
  10. var errKVKey = errors.New("invalid encode kv key")
  11. func checkKeySize(key []byte) error {
  12. if len(key) > MaxKeySize || len(key) == 0 {
  13. return errKeySize
  14. }
  15. return nil
  16. }
  17. func checkValueSize(value []byte) error {
  18. if len(value) > MaxValueSize {
  19. return errValueSize
  20. }
  21. return nil
  22. }
  23. func (db *DB) encodeKVKey(key []byte) []byte {
  24. ek := make([]byte, len(key)+2)
  25. ek[0] = db.index
  26. ek[1] = KVType
  27. copy(ek[2:], key)
  28. return ek
  29. }
  30. func (db *DB) decodeKVKey(ek []byte) ([]byte, error) {
  31. if len(ek) < 2 || ek[0] != db.index || ek[1] != KVType {
  32. return nil, errKVKey
  33. }
  34. return ek[2:], nil
  35. }
  36. func (db *DB) encodeKVMinKey() []byte {
  37. ek := db.encodeKVKey(nil)
  38. return ek
  39. }
  40. func (db *DB) encodeKVMaxKey() []byte {
  41. ek := db.encodeKVKey(nil)
  42. ek[len(ek)-1] = KVType + 1
  43. return ek
  44. }
  45. func (db *DB) incr(key []byte, delta int64) (int64, error) {
  46. if err := checkKeySize(key); err != nil {
  47. return 0, err
  48. }
  49. var err error
  50. key = db.encodeKVKey(key)
  51. t := db.kvBatch
  52. t.Lock()
  53. defer t.Unlock()
  54. var n int64
  55. n, err = StrInt64(db.bucket.Get(key))
  56. if err != nil {
  57. return 0, err
  58. }
  59. n += delta
  60. t.Put(key, StrPutInt64(n))
  61. //todo binlog
  62. err = t.Commit()
  63. return n, err
  64. }
  65. // ps : here just focus on deleting the key-value data,
  66. // any other likes expire is ignore.
  67. func (db *DB) delete(t *batch, key []byte) int64 {
  68. key = db.encodeKVKey(key)
  69. t.Delete(key)
  70. return 1
  71. }
  72. func (db *DB) setExpireAt(key []byte, when int64) (int64, error) {
  73. t := db.kvBatch
  74. t.Lock()
  75. defer t.Unlock()
  76. if exist, err := db.Exists(key); err != nil || exist == 0 {
  77. return 0, err
  78. } else {
  79. db.expireAt(t, KVType, key, when)
  80. if err := t.Commit(); err != nil {
  81. return 0, err
  82. }
  83. }
  84. return 1, nil
  85. }
  86. func (db *DB) Decr(key []byte) (int64, error) {
  87. return db.incr(key, -1)
  88. }
  89. func (db *DB) DecrBy(key []byte, decrement int64) (int64, error) {
  90. return db.incr(key, -decrement)
  91. }
  92. func (db *DB) Del(keys ...[]byte) (int64, error) {
  93. if len(keys) == 0 {
  94. return 0, nil
  95. }
  96. codedKeys := make([][]byte, len(keys))
  97. for i, k := range keys {
  98. codedKeys[i] = db.encodeKVKey(k)
  99. }
  100. t := db.kvBatch
  101. t.Lock()
  102. defer t.Unlock()
  103. for i, k := range keys {
  104. t.Delete(codedKeys[i])
  105. db.rmExpire(t, KVType, k)
  106. }
  107. err := t.Commit()
  108. return int64(len(keys)), err
  109. }
  110. func (db *DB) Exists(key []byte) (int64, error) {
  111. if err := checkKeySize(key); err != nil {
  112. return 0, err
  113. }
  114. var err error
  115. key = db.encodeKVKey(key)
  116. var v []byte
  117. v, err = db.bucket.Get(key)
  118. if v != nil && err == nil {
  119. return 1, nil
  120. }
  121. return 0, err
  122. }
  123. func (db *DB) Get(key []byte) ([]byte, error) {
  124. if err := checkKeySize(key); err != nil {
  125. return nil, err
  126. }
  127. key = db.encodeKVKey(key)
  128. return db.bucket.Get(key)
  129. }
  130. func (db *DB) GetSet(key []byte, value []byte) ([]byte, error) {
  131. if err := checkKeySize(key); err != nil {
  132. return nil, err
  133. } else if err := checkValueSize(value); err != nil {
  134. return nil, err
  135. }
  136. key = db.encodeKVKey(key)
  137. t := db.kvBatch
  138. t.Lock()
  139. defer t.Unlock()
  140. oldValue, err := db.bucket.Get(key)
  141. if err != nil {
  142. return nil, err
  143. }
  144. t.Put(key, value)
  145. //todo, binlog
  146. err = t.Commit()
  147. return oldValue, err
  148. }
  149. func (db *DB) Incr(key []byte) (int64, error) {
  150. return db.incr(key, 1)
  151. }
  152. func (db *DB) IncrBy(key []byte, increment int64) (int64, error) {
  153. return db.incr(key, increment)
  154. }
  155. func (db *DB) MGet(keys ...[]byte) ([][]byte, error) {
  156. values := make([][]byte, len(keys))
  157. it := db.bucket.NewIterator()
  158. defer it.Close()
  159. for i := range keys {
  160. if err := checkKeySize(keys[i]); err != nil {
  161. return nil, err
  162. }
  163. values[i] = it.Find(db.encodeKVKey(keys[i]))
  164. }
  165. return values, nil
  166. }
  167. func (db *DB) MSet(args ...KVPair) error {
  168. if len(args) == 0 {
  169. return nil
  170. }
  171. t := db.kvBatch
  172. var err error
  173. var key []byte
  174. var value []byte
  175. t.Lock()
  176. defer t.Unlock()
  177. for i := 0; i < len(args); i++ {
  178. if err := checkKeySize(args[i].Key); err != nil {
  179. return err
  180. } else if err := checkValueSize(args[i].Value); err != nil {
  181. return err
  182. }
  183. key = db.encodeKVKey(args[i].Key)
  184. value = args[i].Value
  185. t.Put(key, value)
  186. //todo binlog
  187. }
  188. err = t.Commit()
  189. return err
  190. }
  191. func (db *DB) Set(key []byte, value []byte) error {
  192. if err := checkKeySize(key); err != nil {
  193. return err
  194. } else if err := checkValueSize(value); err != nil {
  195. return err
  196. }
  197. var err error
  198. key = db.encodeKVKey(key)
  199. t := db.kvBatch
  200. t.Lock()
  201. defer t.Unlock()
  202. t.Put(key, value)
  203. err = t.Commit()
  204. return err
  205. }
  206. func (db *DB) SetNX(key []byte, value []byte) (int64, error) {
  207. if err := checkKeySize(key); err != nil {
  208. return 0, err
  209. } else if err := checkValueSize(value); err != nil {
  210. return 0, err
  211. }
  212. var err error
  213. key = db.encodeKVKey(key)
  214. var n int64 = 1
  215. t := db.kvBatch
  216. t.Lock()
  217. defer t.Unlock()
  218. if v, err := db.bucket.Get(key); err != nil {
  219. return 0, err
  220. } else if v != nil {
  221. n = 0
  222. } else {
  223. t.Put(key, value)
  224. //todo binlog
  225. err = t.Commit()
  226. }
  227. return n, err
  228. }
  229. func (db *DB) flush() (drop int64, err error) {
  230. t := db.kvBatch
  231. t.Lock()
  232. defer t.Unlock()
  233. return db.flushType(t, KVType)
  234. }
  235. //if inclusive is true, scan range [key, inf) else (key, inf)
  236. func (db *DB) Scan(key []byte, count int, inclusive bool, match string) ([][]byte, error) {
  237. return db.scan(KVType, key, count, inclusive, match)
  238. }
  239. func (db *DB) Expire(key []byte, duration int64) (int64, error) {
  240. if duration <= 0 {
  241. return 0, errExpireValue
  242. }
  243. return db.setExpireAt(key, time.Now().Unix()+duration)
  244. }
  245. func (db *DB) ExpireAt(key []byte, when int64) (int64, error) {
  246. if when <= time.Now().Unix() {
  247. return 0, errExpireValue
  248. }
  249. return db.setExpireAt(key, when)
  250. }
  251. func (db *DB) TTL(key []byte) (int64, error) {
  252. if err := checkKeySize(key); err != nil {
  253. return -1, err
  254. }
  255. return db.ttl(KVType, key)
  256. }
  257. func (db *DB) Persist(key []byte) (int64, error) {
  258. if err := checkKeySize(key); err != nil {
  259. return 0, err
  260. }
  261. t := db.kvBatch
  262. t.Lock()
  263. defer t.Unlock()
  264. n, err := db.rmExpire(t, KVType, key)
  265. if err != nil {
  266. return 0, err
  267. }
  268. err = t.Commit()
  269. return n, err
  270. }
  271. func (db *DB) Lock() {
  272. t := db.kvBatch
  273. t.Lock()
  274. }
  275. func (db *DB) Remove(key []byte) bool {
  276. if len(key) == 0 {
  277. return false
  278. }
  279. t := db.kvBatch
  280. t.Delete(db.encodeKVKey(key))
  281. _, err := db.rmExpire(t, KVType, key)
  282. if err != nil {
  283. return false
  284. }
  285. return true
  286. }
  287. func (db *DB) Commit() error {
  288. t := db.kvBatch
  289. return t.Commit()
  290. }
  291. func (db *DB) Unlock() {
  292. t := db.kvBatch
  293. t.Unlock()
  294. }
上海开阖软件有限公司 沪ICP备12045867号-1