Ohm-Management - Projektarbeit B-ME
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.

index.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. module.exports = Pager
  2. function Pager (pageSize, opts) {
  3. if (!(this instanceof Pager)) return new Pager(pageSize, opts)
  4. this.length = 0
  5. this.updates = []
  6. this.path = new Uint16Array(4)
  7. this.pages = new Array(32768)
  8. this.maxPages = this.pages.length
  9. this.level = 0
  10. this.pageSize = pageSize || 1024
  11. this.deduplicate = opts ? opts.deduplicate : null
  12. this.zeros = this.deduplicate ? alloc(this.deduplicate.length) : null
  13. }
  14. Pager.prototype.updated = function (page) {
  15. while (this.deduplicate && page.buffer[page.deduplicate] === this.deduplicate[page.deduplicate]) {
  16. page.deduplicate++
  17. if (page.deduplicate === this.deduplicate.length) {
  18. page.deduplicate = 0
  19. if (page.buffer.equals && page.buffer.equals(this.deduplicate)) page.buffer = this.deduplicate
  20. break
  21. }
  22. }
  23. if (page.updated || !this.updates) return
  24. page.updated = true
  25. this.updates.push(page)
  26. }
  27. Pager.prototype.lastUpdate = function () {
  28. if (!this.updates || !this.updates.length) return null
  29. var page = this.updates.pop()
  30. page.updated = false
  31. return page
  32. }
  33. Pager.prototype._array = function (i, noAllocate) {
  34. if (i >= this.maxPages) {
  35. if (noAllocate) return
  36. grow(this, i)
  37. }
  38. factor(i, this.path)
  39. var arr = this.pages
  40. for (var j = this.level; j > 0; j--) {
  41. var p = this.path[j]
  42. var next = arr[p]
  43. if (!next) {
  44. if (noAllocate) return
  45. next = arr[p] = new Array(32768)
  46. }
  47. arr = next
  48. }
  49. return arr
  50. }
  51. Pager.prototype.get = function (i, noAllocate) {
  52. var arr = this._array(i, noAllocate)
  53. var first = this.path[0]
  54. var page = arr && arr[first]
  55. if (!page && !noAllocate) {
  56. page = arr[first] = new Page(i, alloc(this.pageSize))
  57. if (i >= this.length) this.length = i + 1
  58. }
  59. if (page && page.buffer === this.deduplicate && this.deduplicate && !noAllocate) {
  60. page.buffer = copy(page.buffer)
  61. page.deduplicate = 0
  62. }
  63. return page
  64. }
  65. Pager.prototype.set = function (i, buf) {
  66. var arr = this._array(i, false)
  67. var first = this.path[0]
  68. if (i >= this.length) this.length = i + 1
  69. if (!buf || (this.zeros && buf.equals && buf.equals(this.zeros))) {
  70. arr[first] = undefined
  71. return
  72. }
  73. if (this.deduplicate && buf.equals && buf.equals(this.deduplicate)) {
  74. buf = this.deduplicate
  75. }
  76. var page = arr[first]
  77. var b = truncate(buf, this.pageSize)
  78. if (page) page.buffer = b
  79. else arr[first] = new Page(i, b)
  80. }
  81. Pager.prototype.toBuffer = function () {
  82. var list = new Array(this.length)
  83. var empty = alloc(this.pageSize)
  84. var ptr = 0
  85. while (ptr < list.length) {
  86. var arr = this._array(ptr, true)
  87. for (var i = 0; i < 32768 && ptr < list.length; i++) {
  88. list[ptr++] = (arr && arr[i]) ? arr[i].buffer : empty
  89. }
  90. }
  91. return Buffer.concat(list)
  92. }
  93. function grow (pager, index) {
  94. while (pager.maxPages < index) {
  95. var old = pager.pages
  96. pager.pages = new Array(32768)
  97. pager.pages[0] = old
  98. pager.level++
  99. pager.maxPages *= 32768
  100. }
  101. }
  102. function truncate (buf, len) {
  103. if (buf.length === len) return buf
  104. if (buf.length > len) return buf.slice(0, len)
  105. var cpy = alloc(len)
  106. buf.copy(cpy)
  107. return cpy
  108. }
  109. function alloc (size) {
  110. if (Buffer.alloc) return Buffer.alloc(size)
  111. var buf = new Buffer(size)
  112. buf.fill(0)
  113. return buf
  114. }
  115. function copy (buf) {
  116. var cpy = Buffer.allocUnsafe ? Buffer.allocUnsafe(buf.length) : new Buffer(buf.length)
  117. buf.copy(cpy)
  118. return cpy
  119. }
  120. function Page (i, buf) {
  121. this.offset = i * buf.length
  122. this.buffer = buf
  123. this.updated = false
  124. this.deduplicate = 0
  125. }
  126. function factor (n, out) {
  127. n = (n - (out[0] = (n & 32767))) / 32768
  128. n = (n - (out[1] = (n & 32767))) / 32768
  129. out[3] = ((n - (out[2] = (n & 32767))) / 32768) & 32767
  130. }