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.

563 lines
13 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package skiplist
  2. // adapted from https://github.com/MauriceGit/skiplist/blob/master/skiplist.go
  3. import (
  4. "bytes"
  5. "fmt"
  6. "math/bits"
  7. "math/rand"
  8. "time"
  9. )
  10. const (
  11. // maxLevel denotes the maximum height of the skiplist. This height will keep the skiplist
  12. // efficient for up to 34m entries. If there is a need for much more, please adjust this constant accordingly.
  13. maxLevel = 25
  14. )
  15. type SkipList struct {
  16. startLevels [maxLevel]*SkipListElementReference
  17. endLevels [maxLevel]*SkipListElementReference
  18. maxNewLevel int
  19. maxLevel int
  20. listStore ListStore
  21. hasChanges bool
  22. // elementCount int
  23. }
  24. // NewSeedEps returns a new empty, initialized Skiplist.
  25. // Given a seed, a deterministic height/list behaviour can be achieved.
  26. // Eps is used to compare keys given by the ExtractKey() function on equality.
  27. func NewSeed(seed int64, listStore ListStore) *SkipList {
  28. // Initialize random number generator.
  29. rand.Seed(seed)
  30. //fmt.Printf("SkipList seed: %v\n", seed)
  31. list := &SkipList{
  32. maxNewLevel: maxLevel,
  33. maxLevel: 0,
  34. listStore: listStore,
  35. // elementCount: 0,
  36. }
  37. return list
  38. }
  39. // New returns a new empty, initialized Skiplist.
  40. func New(listStore ListStore) *SkipList {
  41. return NewSeed(time.Now().UTC().UnixNano(), listStore)
  42. }
  43. // IsEmpty checks, if the skiplist is empty.
  44. func (t *SkipList) IsEmpty() bool {
  45. return t.startLevels[0] == nil
  46. }
  47. func (t *SkipList) generateLevel(maxLevel int) int {
  48. level := maxLevel - 1
  49. // First we apply some mask which makes sure that we don't get a level
  50. // above our desired level. Then we find the first set bit.
  51. var x = rand.Uint64() & ((1 << uint(maxLevel-1)) - 1)
  52. zeroes := bits.TrailingZeros64(x)
  53. if zeroes <= maxLevel {
  54. level = zeroes
  55. }
  56. return level
  57. }
  58. func (t *SkipList) findEntryIndex(key []byte, minLevel int) int {
  59. // Find good entry point so we don't accidentally skip half the list.
  60. for i := t.maxLevel; i >= 0; i-- {
  61. if t.startLevels[i] != nil && bytes.Compare(t.startLevels[i].Key, key) < 0 || i <= minLevel {
  62. return i
  63. }
  64. }
  65. return 0
  66. }
  67. func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (prevElementIfVisited *SkipListElement, foundElem *SkipListElement, ok bool, err error) {
  68. foundElem = nil
  69. ok = false
  70. if t.IsEmpty() {
  71. return
  72. }
  73. index := t.findEntryIndex(key, 0)
  74. var currentNode *SkipListElement
  75. currentNode, err = t.loadElement(t.startLevels[index])
  76. if err != nil {
  77. return
  78. }
  79. if currentNode == nil {
  80. return
  81. }
  82. // In case, that our first element is already greater-or-equal!
  83. if findGreaterOrEqual && compareElement(currentNode, key) > 0 {
  84. foundElem = currentNode
  85. ok = true
  86. return
  87. }
  88. for {
  89. if compareElement(currentNode, key) == 0 {
  90. foundElem = currentNode
  91. ok = true
  92. return
  93. }
  94. // Which direction are we continuing next time?
  95. if currentNode.Next[index] != nil && bytes.Compare(currentNode.Next[index].Key, key) <= 0 {
  96. // Go right
  97. currentNode, err = t.loadElement(currentNode.Next[index])
  98. if err != nil {
  99. return
  100. }
  101. if currentNode == nil {
  102. return
  103. }
  104. } else {
  105. if index > 0 {
  106. // Early exit
  107. if currentNode.Next[0] != nil && bytes.Compare(currentNode.Next[0].Key, key) == 0 {
  108. prevElementIfVisited = currentNode
  109. var currentNodeNext *SkipListElement
  110. currentNodeNext, err = t.loadElement(currentNode.Next[0])
  111. if err != nil {
  112. return
  113. }
  114. if currentNodeNext == nil {
  115. return
  116. }
  117. foundElem = currentNodeNext
  118. ok = true
  119. return
  120. }
  121. // Go down
  122. index--
  123. } else {
  124. // Element is not found and we reached the bottom.
  125. if findGreaterOrEqual {
  126. foundElem, err = t.loadElement(currentNode.Next[index])
  127. if err != nil {
  128. return
  129. }
  130. ok = foundElem != nil
  131. }
  132. return
  133. }
  134. }
  135. }
  136. }
  137. // Find tries to find an element in the skiplist based on the key from the given ListElement.
  138. // elem can be used, if ok is true.
  139. // Find runs in approx. O(log(n))
  140. func (t *SkipList) Find(key []byte) (prevIfVisited *SkipListElement, elem *SkipListElement, ok bool, err error) {
  141. if t == nil || key == nil {
  142. return
  143. }
  144. prevIfVisited, elem, ok, err = t.findExtended(key, false)
  145. return
  146. }
  147. // FindGreaterOrEqual finds the first element, that is greater or equal to the given ListElement e.
  148. // The comparison is done on the keys (So on ExtractKey()).
  149. // FindGreaterOrEqual runs in approx. O(log(n))
  150. func (t *SkipList) FindGreaterOrEqual(key []byte) (prevIfVisited *SkipListElement, elem *SkipListElement, ok bool, err error) {
  151. if t == nil || key == nil {
  152. return
  153. }
  154. prevIfVisited, elem, ok, err = t.findExtended(key, true)
  155. return
  156. }
  157. // Delete removes an element equal to e from the skiplist, if there is one.
  158. // If there are multiple entries with the same value, Delete will remove one of them
  159. // (Which one will change based on the actual skiplist layout)
  160. // Delete runs in approx. O(log(n))
  161. func (t *SkipList) Delete(key []byte) (err error) {
  162. if t == nil || t.IsEmpty() || key == nil {
  163. return
  164. }
  165. index := t.findEntryIndex(key, t.maxLevel)
  166. var currentNode *SkipListElement
  167. var nextNode *SkipListElement
  168. for {
  169. if currentNode == nil {
  170. nextNode, err = t.loadElement(t.startLevels[index])
  171. } else {
  172. nextNode, err = t.loadElement(currentNode.Next[index])
  173. }
  174. if err != nil {
  175. return err
  176. }
  177. // Found and remove!
  178. if nextNode != nil && compareElement(nextNode, key) == 0 {
  179. if currentNode != nil {
  180. currentNode.Next[index] = nextNode.Next[index]
  181. if err = t.saveElement(currentNode); err != nil {
  182. return err
  183. }
  184. }
  185. if index == 0 {
  186. if nextNode.Next[index] != nil {
  187. nextNextNode, err := t.loadElement(nextNode.Next[index])
  188. if err != nil {
  189. return err
  190. }
  191. if nextNextNode != nil {
  192. nextNextNode.Prev = currentNode.Reference()
  193. if err = t.saveElement(nextNextNode); err != nil {
  194. return err
  195. }
  196. }
  197. }
  198. // t.elementCount--
  199. if err = t.deleteElement(nextNode); err != nil {
  200. return err
  201. }
  202. }
  203. // Link from start needs readjustments.
  204. startNextKey := t.startLevels[index].Key
  205. if compareElement(nextNode, startNextKey) == 0 {
  206. t.hasChanges = true
  207. t.startLevels[index] = nextNode.Next[index]
  208. // This was our currently highest node!
  209. if t.startLevels[index] == nil {
  210. t.maxLevel = index - 1
  211. }
  212. }
  213. // Link from end needs readjustments.
  214. if nextNode.Next[index] == nil {
  215. t.endLevels[index] = currentNode.Reference()
  216. t.hasChanges = true
  217. }
  218. nextNode.Next[index] = nil
  219. }
  220. if nextNode != nil && compareElement(nextNode, key) < 0 {
  221. // Go right
  222. currentNode = nextNode
  223. } else {
  224. // Go down
  225. index--
  226. if index < 0 {
  227. break
  228. }
  229. }
  230. }
  231. return
  232. }
  233. // Insert inserts the given ListElement into the skiplist.
  234. // Insert runs in approx. O(log(n))
  235. func (t *SkipList) Insert(key, value []byte) (err error) {
  236. if t == nil || key == nil {
  237. return
  238. }
  239. level := t.generateLevel(t.maxNewLevel)
  240. // Only grow the height of the skiplist by one at a time!
  241. if level > t.maxLevel {
  242. level = t.maxLevel + 1
  243. t.maxLevel = level
  244. t.hasChanges = true
  245. }
  246. elem := &SkipListElement{
  247. Id: rand.Int63(),
  248. Next: make([]*SkipListElementReference, t.maxNewLevel, t.maxNewLevel),
  249. Level: int32(level),
  250. Key: key,
  251. Value: value,
  252. }
  253. // t.elementCount++
  254. newFirst := true
  255. newLast := true
  256. if !t.IsEmpty() {
  257. newFirst = compareElement(elem, t.startLevels[0].Key) < 0
  258. newLast = compareElement(elem, t.endLevels[0].Key) > 0
  259. }
  260. normallyInserted := false
  261. if !newFirst && !newLast {
  262. normallyInserted = true
  263. index := t.findEntryIndex(key, level)
  264. var currentNode *SkipListElement
  265. var nextNodeRef *SkipListElementReference
  266. for {
  267. if currentNode == nil {
  268. nextNodeRef = t.startLevels[index]
  269. } else {
  270. nextNodeRef = currentNode.Next[index]
  271. }
  272. var nextNode *SkipListElement
  273. // Connect node to next
  274. if index <= level && (nextNodeRef == nil || bytes.Compare(nextNodeRef.Key, key) > 0) {
  275. elem.Next[index] = nextNodeRef
  276. if currentNode != nil {
  277. currentNode.Next[index] = elem.Reference()
  278. if err = t.saveElement(currentNode); err != nil {
  279. return
  280. }
  281. }
  282. if index == 0 {
  283. elem.Prev = currentNode.Reference()
  284. if nextNodeRef != nil {
  285. if nextNode, err = t.loadElement(nextNodeRef); err != nil {
  286. return
  287. }
  288. if nextNode != nil {
  289. nextNode.Prev = elem.Reference()
  290. if err = t.saveElement(nextNode); err != nil {
  291. return
  292. }
  293. }
  294. }
  295. }
  296. }
  297. if nextNodeRef != nil && bytes.Compare(nextNodeRef.Key, key) <= 0 {
  298. // Go right
  299. if nextNode == nil {
  300. // reuse nextNode when index == 0
  301. if nextNode, err = t.loadElement(nextNodeRef); err != nil {
  302. return
  303. }
  304. }
  305. currentNode = nextNode
  306. if currentNode == nil {
  307. return
  308. }
  309. } else {
  310. // Go down
  311. index--
  312. if index < 0 {
  313. break
  314. }
  315. }
  316. }
  317. }
  318. // Where we have a left-most position that needs to be referenced!
  319. for i := level; i >= 0; i-- {
  320. didSomething := false
  321. if newFirst || normallyInserted {
  322. if t.startLevels[i] == nil || bytes.Compare(t.startLevels[i].Key, key) > 0 {
  323. if i == 0 && t.startLevels[i] != nil {
  324. startLevelElement, err := t.loadElement(t.startLevels[i])
  325. if err != nil {
  326. return err
  327. }
  328. if startLevelElement != nil {
  329. startLevelElement.Prev = elem.Reference()
  330. if err = t.saveElement(startLevelElement); err != nil {
  331. return err
  332. }
  333. }
  334. }
  335. elem.Next[i] = t.startLevels[i]
  336. t.startLevels[i] = elem.Reference()
  337. t.hasChanges = true
  338. }
  339. // link the endLevels to this element!
  340. if elem.Next[i] == nil {
  341. t.endLevels[i] = elem.Reference()
  342. t.hasChanges = true
  343. }
  344. didSomething = true
  345. }
  346. if newLast {
  347. // Places the element after the very last element on this level!
  348. // This is very important, so we are not linking the very first element (newFirst AND newLast) to itself!
  349. if !newFirst {
  350. if t.endLevels[i] != nil {
  351. endLevelElement, err := t.loadElement(t.endLevels[i])
  352. if err != nil {
  353. return err
  354. }
  355. if endLevelElement != nil {
  356. endLevelElement.Next[i] = elem.Reference()
  357. if err = t.saveElement(endLevelElement); err != nil {
  358. return err
  359. }
  360. }
  361. }
  362. if i == 0 {
  363. elem.Prev = t.endLevels[i]
  364. }
  365. t.endLevels[i] = elem.Reference()
  366. t.hasChanges = true
  367. }
  368. // Link the startLevels to this element!
  369. if t.startLevels[i] == nil || bytes.Compare(t.startLevels[i].Key, key) > 0 {
  370. t.startLevels[i] = elem.Reference()
  371. t.hasChanges = true
  372. }
  373. didSomething = true
  374. }
  375. if !didSomething {
  376. break
  377. }
  378. }
  379. if err = t.saveElement(elem); err != nil {
  380. return err
  381. }
  382. return nil
  383. }
  384. // GetSmallestNode returns the very first/smallest node in the skiplist.
  385. // GetSmallestNode runs in O(1)
  386. func (t *SkipList) GetSmallestNode() (*SkipListElement, error) {
  387. return t.loadElement(t.startLevels[0])
  388. }
  389. // GetLargestNode returns the very last/largest node in the skiplist.
  390. // GetLargestNode runs in O(1)
  391. func (t *SkipList) GetLargestNode() (*SkipListElement, error) {
  392. return t.loadElement(t.endLevels[0])
  393. }
  394. // Next returns the next element based on the given node.
  395. // Next will loop around to the first node, if you call it on the last!
  396. func (t *SkipList) Next(e *SkipListElement) (*SkipListElement, error) {
  397. if e.Next[0] == nil {
  398. return t.loadElement(t.startLevels[0])
  399. }
  400. return t.loadElement(e.Next[0])
  401. }
  402. // Prev returns the previous element based on the given node.
  403. // Prev will loop around to the last node, if you call it on the first!
  404. func (t *SkipList) Prev(e *SkipListElement) (*SkipListElement, error) {
  405. if e.Prev == nil {
  406. return t.loadElement(t.endLevels[0])
  407. }
  408. return t.loadElement(e.Prev)
  409. }
  410. // ChangeValue can be used to change the actual value of a node in the skiplist
  411. // without the need of Deleting and reinserting the node again.
  412. // Be advised, that ChangeValue only works, if the actual key from ExtractKey() will stay the same!
  413. // ok is an indicator, wether the value is actually changed.
  414. func (t *SkipList) ChangeValue(e *SkipListElement, newValue []byte) (err error) {
  415. // The key needs to stay correct, so this is very important!
  416. e.Value = newValue
  417. return t.saveElement(e)
  418. }
  419. // String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging.
  420. func (t *SkipList) println() {
  421. print("start --> ")
  422. for i, l := range t.startLevels {
  423. if l == nil {
  424. break
  425. }
  426. if i > 0 {
  427. print(" -> ")
  428. }
  429. next := "---"
  430. if l != nil {
  431. next = string(l.Key)
  432. }
  433. print(fmt.Sprintf("[%v]", next))
  434. }
  435. println()
  436. nodeRef := t.startLevels[0]
  437. for nodeRef != nil {
  438. print(fmt.Sprintf("%v: ", string(nodeRef.Key)))
  439. node, _ := t.loadElement(nodeRef)
  440. if node == nil {
  441. break
  442. }
  443. for i := 0; i <= int(node.Level); i++ {
  444. l := node.Next[i]
  445. next := "---"
  446. if l != nil {
  447. next = string(l.Key)
  448. }
  449. if i == 0 {
  450. prev := "---"
  451. if node.Prev != nil {
  452. prev = string(node.Prev.Key)
  453. }
  454. print(fmt.Sprintf("[%v|%v]", prev, next))
  455. } else {
  456. print(fmt.Sprintf("[%v]", next))
  457. }
  458. if i < int(node.Level) {
  459. print(" -> ")
  460. }
  461. }
  462. nodeRef = node.Next[0]
  463. println()
  464. }
  465. print("end --> ")
  466. for i, l := range t.endLevels {
  467. if l == nil {
  468. break
  469. }
  470. if i > 0 {
  471. print(" -> ")
  472. }
  473. next := "---"
  474. if l != nil {
  475. next = string(l.Key)
  476. }
  477. print(fmt.Sprintf("[%v]", next))
  478. }
  479. println()
  480. }