htreepage_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package hash
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/eliasdb/storage"
  15. "devt.de/krotik/eliasdb/storage/file"
  16. )
  17. func TestHTreePageFetchExists(t *testing.T) {
  18. sm := storage.NewMemoryStorageManager("testsm")
  19. htree, _ := NewHTree(sm)
  20. page := htree.Root
  21. if loc := page.Location(); loc != 1 {
  22. t.Error("Unexpected root location:", loc)
  23. return
  24. }
  25. // Fill up the tree
  26. page.Put([]byte("testkey1"), "test1")
  27. page.Put([]byte("testkey2"), "test2")
  28. page.Put([]byte("testkey3"), "test3")
  29. page.Put([]byte("testkey4"), "test4")
  30. page.Put([]byte("testkey5"), "test5")
  31. page.Put([]byte("testkey6"), "test6")
  32. page.Put([]byte("testkey7"), "test7")
  33. page.Put([]byte("testkey8"), "test8")
  34. page.Put([]byte("testkey9"), "test9")
  35. page.Put([]byte("testkey10"), "test10")
  36. // Test negative cases
  37. if res, err := page.Exists([]byte("testkey40")); res != false || err != nil {
  38. t.Error("Unexpected exists result:", res, err)
  39. return
  40. }
  41. if res, _, err := page.Get([]byte("testkey40")); res != nil || err != nil {
  42. t.Error("Unexpected get result:", res, err)
  43. return
  44. }
  45. // Test positive cases
  46. if res, err := page.Exists([]byte("testkey4")); res != true || err != nil {
  47. t.Error("Unexpected exists result:", res, err)
  48. return
  49. }
  50. if res, _, err := page.Get([]byte("testkey4")); res != "test4" || err != nil {
  51. t.Error("Unexpected get result:", res, err)
  52. return
  53. }
  54. // Test error cases
  55. sm.AccessMap[8] = storage.AccessCacheAndFetchError
  56. if res, err := page.Exists([]byte("testkey4")); res != false || err != storage.ErrSlotNotFound {
  57. t.Error("Unexpected exists result:", res, err)
  58. return
  59. }
  60. if res, _, err := page.Get([]byte("testkey4")); res != nil || err != storage.ErrSlotNotFound {
  61. t.Error("Unexpected get result:", res, err)
  62. return
  63. }
  64. delete(sm.AccessMap, 8)
  65. }
  66. func TestHTreePageInsert(t *testing.T) {
  67. sm := storage.NewMemoryStorageManager("testsm")
  68. htree, _ := NewHTree(sm)
  69. page := htree.Root
  70. // Empty page operations
  71. if val, _, err := page.Get([]byte("test")); val != nil || err != nil {
  72. t.Error("Unexpected get result:", val, err)
  73. return
  74. }
  75. // Test error cases
  76. sm.AccessMap[2] = storage.AccessInsertError
  77. if _, err := page.Put([]byte("testkey1"), "test1"); err != file.ErrAlreadyInUse {
  78. t.Error("Unexpected put result:", err)
  79. return
  80. }
  81. delete(sm.AccessMap, 2)
  82. sm.AccessMap[1] = storage.AccessUpdateError
  83. if _, err := page.Put([]byte("testkey1"), "test1"); err != storage.ErrSlotNotFound {
  84. t.Error("Unexpected put result:", err)
  85. return
  86. }
  87. delete(sm.AccessMap, 1)
  88. // Fill the tree
  89. page.Put([]byte("testkey1"), "test1")
  90. // Test another error case
  91. sm.AccessMap[2] = storage.AccessCacheAndFetchError
  92. if _, err := page.Put([]byte("testkey2"), "test2"); err != storage.ErrSlotNotFound {
  93. t.Error("Unexpected put result:", err)
  94. return
  95. }
  96. delete(sm.AccessMap, 2)
  97. page.Put([]byte("testkey2"), "test2")
  98. page.Put([]byte("testkey3"), "test3")
  99. page.Put([]byte("testkey4"), "test4")
  100. page.Put([]byte("testkey5"), "test5")
  101. page.Put([]byte("testkey6"), "test6")
  102. page.Put([]byte("testkey7"), "test7")
  103. page.Put([]byte("testkey8"), "test8")
  104. // Check we have one full bucket
  105. if cc := countChildren(page); cc != 1 {
  106. t.Error("Unexpected number of children:", cc)
  107. return
  108. }
  109. // Now insert more data and see that the bucket is split
  110. // First test some error cases
  111. sm.AccessMap[3] = storage.AccessInsertError
  112. if _, err := page.Put([]byte("testkey9"), "test9"); err != file.ErrAlreadyInUse {
  113. t.Error("Unexpected put result:", err)
  114. return
  115. }
  116. delete(sm.AccessMap, 3)
  117. sm.AccessMap[1] = storage.AccessUpdateError
  118. if _, err := page.Put([]byte("testkey9"), "test9"); err != storage.ErrSlotNotFound {
  119. t.Error("Unexpected put result:", err)
  120. return
  121. }
  122. // Cleanup LocCount
  123. sm.LocCount--
  124. delete(sm.AccessMap, 1)
  125. // This puts the first bucket on the lowest level and extends it by 1
  126. page.Put([]byte("testkey9"), "test9")
  127. // Test sanity check for cache
  128. testMaxDepthExceededPanic(t, page, sm)
  129. // This creates a new bucket under the root
  130. page.Put([]byte("testkey10"), "test10")
  131. if cc := countChildren(page); cc != 2 {
  132. t.Error("Unexpected number of children:", cc)
  133. return
  134. }
  135. total, pages, buckets := countNodes(sm)
  136. if total != 6 || pages != 4 || buckets != 2 {
  137. t.Error("Unexpected number of nodes:", total, pages, buckets)
  138. return
  139. }
  140. // Test printing
  141. if out := htree.String(); out != "HTree: testsm (1)\n"+
  142. "HashPage 1 (depth: 0)\n"+
  143. " Hash 000000B9 (loc: 3)\n"+
  144. " HashPage 3 (depth: 1)\n"+
  145. " Hash 000000B0 (loc: 5)\n"+
  146. " HashPage 5 (depth: 2)\n"+
  147. " Hash 00000069 (loc: 7)\n"+
  148. " HashPage 7 (depth: 3)\n"+
  149. " Hash 000000AD (loc: 8)\n"+
  150. " HashBucket (9 elements, depth: 4)\n"+
  151. " [116 101 115 116 107 101 121 49] - test1\n"+
  152. " [116 101 115 116 107 101 121 50] - test2\n"+
  153. " [116 101 115 116 107 101 121 51] - test3\n"+
  154. " [116 101 115 116 107 101 121 52] - test4\n"+
  155. " [116 101 115 116 107 101 121 53] - test5\n"+
  156. " [116 101 115 116 107 101 121 54] - test6\n"+
  157. " [116 101 115 116 107 101 121 55] - test7\n"+
  158. " [116 101 115 116 107 101 121 56] - test8\n"+
  159. " [116 101 115 116 107 101 121 57] - test9\n"+
  160. " Hash 000000DE (loc: 9)\n"+
  161. " HashBucket (1 element, depth: 1)\n"+
  162. " [116 101 115 116 107 101 121 49 48] - test10\n"+
  163. " [] - <nil>\n"+
  164. " [] - <nil>\n"+
  165. " [] - <nil>\n"+
  166. " [] - <nil>\n"+
  167. " [] - <nil>\n"+
  168. " [] - <nil>\n"+
  169. " [] - <nil>\n" {
  170. t.Error("Unexpected htree output:", out)
  171. }
  172. if existing, err := page.Put([]byte("testkey10"), "test11"); existing != "test10" {
  173. t.Error("Unexpected put result", existing, err)
  174. return
  175. }
  176. }
  177. func TestHTreePageRemove(t *testing.T) {
  178. sm := storage.NewMemoryStorageManager("testsm")
  179. htree, _ := NewHTree(sm)
  180. page := htree.Root
  181. if val, err := page.Remove(nil); val != nil || err != nil {
  182. t.Error("Unexpected get result:", val, err)
  183. return
  184. }
  185. page.Put([]byte("testkey1"), "test1")
  186. page.Put([]byte("testkey2"), "test2")
  187. page.Put([]byte("testkey3"), "test3")
  188. page.Put([]byte("testkey4"), "test4")
  189. page.Put([]byte("testkey5"), "test5")
  190. page.Put([]byte("testkey6"), "test6")
  191. page.Put([]byte("testkey7"), "test7")
  192. page.Put([]byte("testkey8"), "test8")
  193. page.Put([]byte("testkey9"), "test9")
  194. page.Put([]byte("testkey10"), "test10")
  195. // Check the numbers
  196. if cc := countChildren(page); cc != 2 {
  197. t.Error("Unexpected number of children:", cc)
  198. return
  199. }
  200. total, pages, buckets := countNodes(sm)
  201. if total != 6 || pages != 4 || buckets != 2 {
  202. t.Error("Unexpected number of nodes:", total, pages, buckets)
  203. return
  204. }
  205. // Remove testkey10 which should remove a bucket
  206. res, _ := page.Put([]byte("testkey10"), nil)
  207. if res != "test10" {
  208. t.Error("Unexpected put result", res)
  209. return
  210. }
  211. if cc := countChildren(page); cc != 1 {
  212. t.Error("Unexpected number of children:", cc)
  213. return
  214. }
  215. total, pages, buckets = countNodes(sm)
  216. if total != 5 || pages != 4 || buckets != 1 {
  217. t.Error("Unexpected number of nodes:", total, pages, buckets)
  218. return
  219. }
  220. res, _ = page.Remove([]byte("testkey9"))
  221. if res != "test9" {
  222. t.Error("Unexpected remove result", res)
  223. return
  224. }
  225. _, err := page.Remove([]byte("testkey8"))
  226. if err != nil {
  227. t.Error(err)
  228. return
  229. }
  230. page.Remove([]byte("testkey7"))
  231. page.Remove([]byte("testkey6"))
  232. page.Remove([]byte("testkey5"))
  233. page.Remove([]byte("testkey4"))
  234. page.Remove([]byte("testkey3"))
  235. page.Remove([]byte("testkey2"))
  236. // Test error output when printing the tree
  237. sm.AccessMap[3] = storage.AccessCacheAndFetchError
  238. if out := htree.String(); out != "HTree: testsm (1)\n"+
  239. "HashPage 1 (depth: 0)\n"+
  240. " Hash 000000B9 (loc: 3)\n"+
  241. "Slot not found (testsm - Location:3)\n" {
  242. t.Error("Unexpected tree representation:", out)
  243. return
  244. }
  245. delete(sm.AccessMap, 3)
  246. // Check that the tree has the expected values
  247. if out := htree.String(); out != "HTree: testsm (1)\n"+
  248. "HashPage 1 (depth: 0)\n"+
  249. " Hash 000000B9 (loc: 3)\n"+
  250. " HashPage 3 (depth: 1)\n"+
  251. " Hash 000000B0 (loc: 5)\n"+
  252. " HashPage 5 (depth: 2)\n"+
  253. " Hash 00000069 (loc: 7)\n"+
  254. " HashPage 7 (depth: 3)\n"+
  255. " Hash 000000AD (loc: 8)\n"+
  256. " HashBucket (1 element, depth: 4)\n"+
  257. " [116 101 115 116 107 101 121 49] - test1\n"+
  258. " [] - <nil>\n"+
  259. " [] - <nil>\n"+
  260. " [] - <nil>\n"+
  261. " [] - <nil>\n"+
  262. " [] - <nil>\n"+
  263. " [] - <nil>\n"+
  264. " [] - <nil>\n"+
  265. " [] - <nil>\n" {
  266. t.Error("Unexpected tree representation:", out)
  267. return
  268. }
  269. res, err = page.Remove([]byte("testkey1"))
  270. if err != nil {
  271. t.Error(err)
  272. return
  273. }
  274. if out := htree.String(); out != "HTree: testsm (1)\n"+
  275. "HashPage 1 (depth: 0)\n" {
  276. t.Error("Unexpected tree representation:", out)
  277. return
  278. }
  279. // Error cases
  280. page.Put([]byte("testkey1"), "test1")
  281. page.Put([]byte("testkey2"), "test2")
  282. page.Put([]byte("testkey3"), "test3")
  283. page.Put([]byte("testkey4"), "test4")
  284. page.Put([]byte("testkey5"), "test5")
  285. page.Put([]byte("testkey6"), "test6")
  286. page.Put([]byte("testkey7"), "test7")
  287. page.Put([]byte("testkey8"), "test8")
  288. page.Put([]byte("testkey9"), "test9")
  289. page.Put([]byte("testkey10"), "test10")
  290. sm.AccessMap[16] = storage.AccessCacheAndFetchError
  291. if _, err := page.Remove([]byte("testkey1")); err != storage.ErrSlotNotFound {
  292. t.Error("Unexpected remove result", res)
  293. return
  294. }
  295. delete(sm.AccessMap, 16)
  296. sm.AccessMap[1] = storage.AccessUpdateError
  297. if _, err := page.Remove([]byte("testkey10")); err != storage.ErrSlotNotFound {
  298. t.Error("Unexpected remove result", res)
  299. return
  300. }
  301. delete(sm.AccessMap, 1)
  302. page.Remove([]byte("testkey1"))
  303. page.Remove([]byte("testkey8"))
  304. page.Remove([]byte("testkey7"))
  305. page.Remove([]byte("testkey6"))
  306. page.Remove([]byte("testkey5"))
  307. page.Remove([]byte("testkey4"))
  308. page.Remove([]byte("testkey3"))
  309. page.Remove([]byte("testkey2"))
  310. sm.AccessMap[1] = storage.AccessUpdateError
  311. if _, err := page.Remove([]byte("testkey9")); err != storage.ErrSlotNotFound {
  312. t.Error("Unexpected remove result", res)
  313. //return
  314. }
  315. delete(sm.AccessMap, 1)
  316. // We arrive to the same result even with all the errors
  317. // the tree is kept consistent - however there are now
  318. // orphaned data bits in the storage
  319. if out := htree.String(); out != "HTree: testsm (1)\n"+
  320. "HashPage 1 (depth: 0)\n" {
  321. t.Error("Unexpected tree representation:", out)
  322. return
  323. }
  324. }
  325. func testMaxDepthExceededPanic(t *testing.T, page *htreePage, sm *storage.MemoryStorageManager) {
  326. fn := &htreeNode{}
  327. fn.sm = sm
  328. node, _ := fn.fetchNode(8)
  329. defer func() {
  330. if r := recover(); r == nil {
  331. t.Error("Having a bucket on the lowest level which reports that it is too full should cause a panic.")
  332. }
  333. node.Depth = 4
  334. }()
  335. // The full bucket is on the lowest level - by setting the depth to 1 it
  336. // will report that it is too full even if it is located on the lowest level
  337. node.Depth = 1
  338. page.Put([]byte("testkey0"), "test9")
  339. }
  340. // Returns node numbers: total, pages, buckets
  341. //
  342. func countNodes(sm *storage.MemoryStorageManager) (int, int, int) {
  343. var total, pages, buckets int
  344. var i uint64
  345. fn := &htreeNode{}
  346. fn.sm = sm
  347. for i = 0; i < sm.LocCount; i++ {
  348. node, _ := fn.fetchNode(uint64(i))
  349. if node != nil {
  350. if node.Children != nil {
  351. total++
  352. pages++
  353. } else {
  354. total++
  355. buckets++
  356. }
  357. }
  358. }
  359. return total, pages, buckets
  360. }
  361. func countChildren(n *htreePage) int {
  362. if len(n.Children) == 0 {
  363. return 0
  364. }
  365. var count int
  366. for _, child := range n.Children {
  367. if child != 0 {
  368. count++
  369. }
  370. }
  371. return count
  372. }
  373. func TestHash(t *testing.T) {
  374. htp := &htreePage{&htreeNode{}}
  375. test := []byte{0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5}
  376. hash, _ := MurMurHashData(test, 0, len(test)-1, 42)
  377. hashstr := fmt.Sprintf("%08X", hash)
  378. if hashstr != "ACFFB715" {
  379. t.Error("Unexpected hash output:", hashstr)
  380. return
  381. }
  382. // Now check that on each level of the tree we get the right bit sequence
  383. htp.Depth = 3
  384. res := htp.hashKey(test)
  385. resstr := fmt.Sprintf("%08X", res)
  386. if resstr != "00000015" {
  387. t.Error("Unexpected hash output:", resstr)
  388. return
  389. }
  390. htp.Depth = 2
  391. res = htp.hashKey(test)
  392. resstr = fmt.Sprintf("%08X", res)
  393. if resstr != "000000B7" {
  394. t.Error("Unexpected hash output:", resstr)
  395. return
  396. }
  397. htp.Depth = 1
  398. res = htp.hashKey(test)
  399. resstr = fmt.Sprintf("%08X", res)
  400. if resstr != "000000FF" {
  401. t.Error("Unexpected hash output:", resstr)
  402. return
  403. }
  404. htp.Depth = 0
  405. res = htp.hashKey(test)
  406. resstr = fmt.Sprintf("%08X", res)
  407. if resstr != "000000AC" {
  408. t.Error("Unexpected hash output:", resstr)
  409. return
  410. }
  411. }