gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4b59c03d4b59c03dHM50 mission: doc pathmre4b59c03d/lib/shortid-owners.hl

1.8 KB

  1. // Packed short-ID owners. Twelve bytes per hash slot: key+1, payload offset, byte length.
  2. // The payload holds the existing prefixed owner strings as UTF-8, without a String per row.
  3. // Empty slots have key zero; no owner is removed, including failed-write reservations.
  4. static alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
  5. static read32 = (&bytes, Number pos) => {
  6. return bytes[pos] + bytes[pos + 1] * 256 + bytes[pos + 2] * 65536 + bytes[pos + 3] * 16777216
  7. }
  8. static write32 = (&bytes, Number pos, Number value) => {
  9. let n = value
  10. let i = 0
  11. while (i < 4) {
  12. bytes[pos + i] = n % 256
  13. n = (n - n % 256) / 256
  14. i = i + 1
  15. }
  16. return true
  17. }
  18. static keyOf = (String sid) => {
  19. let key = 0
  20. let i = 0
  21. while (i < 5) {
  22. key = key * 36 + alphabet.indexOf(sid[i])
  23. i = i + 1
  24. }
  25. return key + 1
  26. }
  27. slots = toBytes(12288)
  28. payload = toBytes(0)
  29. capacity = 1024
  30. count = 0
  31. slot(Number key) {
  32. let pos = key % capacity
  33. while (true) {
  34. stored = read32(&slots, pos * 12)
  35. if (stored == 0 || stored == key) { return pos * 12 }
  36. pos = (pos + 1) % capacity
  37. }
  38. }
  39. grow() {
  40. old = slots
  41. oldCapacity = capacity
  42. capacity = capacity * 2
  43. slots = toBytes(capacity * 12)
  44. let i = 0
  45. while (i < oldCapacity) {
  46. key = read32(&old, i * 12)
  47. if (key != 0) {
  48. pos = slot(key)
  49. let j = 0
  50. while (j < 12) { slots[pos + j] = old[i * 12 + j] j = j + 1 }
  51. }
  52. i = i + 1
  53. }
  54. return true
  55. }
  56. fetch(String sid) {
  57. pos = slot(keyOf(sid))
  58. if (read32(&slots, pos) == 0) { return null }
  59. start = read32(&slots, pos + 4)
  60. return payload.text(start, start + read32(&slots, pos + 8))
  61. }
  62. put(String sid, String owner) {
  63. if ((count + 1) * 2 > capacity) { grow() }
  64. key = keyOf(sid)
  65. pos = slot(key)
  66. if (read32(&slots, pos) == 0) { count = count + 1 }
  67. bytes = toBytes(owner)
  68. write32(&slots, pos, key)
  69. write32(&slots, pos + 4, payload.length)
  70. write32(&slots, pos + 8, bytes.length)
  71. payload.append(&bytes)
  72. return true
  73. }

Branches

Latest commits

  • 4b59c03dHM50 mission: doc pathmre
  • ed526d67HM50 missionmre
  • 2f986ca3Log: RT50 passmre
  • 1c4715a4RT50 mission: day50 repro retestmre
  • 57d1c0dcLog: day50 retest requestmre
  • 60b1f419Log: Anton #336 result, wake checkmre
  • 919a18aalog: scratchmre
  • 6b504440status tidymre
  • 9332bd10log: DP70 livemre
  • be11daa0DP70 brief fixmre
  • 886a7028DP70 brief, HM67 mergedmre
  • b41cdd6dMerge hm67-searchids: packed search IDsmre
  • 385904eaReport HM67 matched heap measurements and gate proofmre
  • e5c9b659Pack retained search IDs into UTF-8 bytesmre
  • 62a5f6c7HM67 brief, HM66 mergedmre
  • b32b8440Merge hm66-catalogindex: packed catalog indexmre
  • 2db0f7adReport HM66 heap measurements and validationmre
  • 6ce15623Pack catalog lists and membership maps in UTF-8 bytesmre
  • 71134e21HM66 briefmre
  • ed4cbab2status timemre