-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial support for memory overlays (#563)
* initial support for memory overlays * name the option dryRun * drafts are working * add destroy
- Loading branch information
Showing
6 changed files
with
351 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
const b4a = require('b4a') | ||
const { ASSERTION } = require('hypercore-errors') | ||
|
||
class MemoryOverlay { | ||
constructor (storage) { | ||
this.storage = storage | ||
this.head = null | ||
this.auth = null | ||
this.localKeyPair = null | ||
this.encryptionKey = null | ||
this.dataInfo = null | ||
this.userData = null | ||
this.blocks = null | ||
this.treeNodes = null | ||
this.bitfields = null | ||
} | ||
|
||
createReadBatch (read = this.storage.createReadBatch()) { | ||
return new MemoryOverlayReadBatch(this, read) | ||
} | ||
|
||
createWriteBatch () { | ||
return new MemoryOverlayWriteBatch(this) | ||
} | ||
|
||
merge (overlay) { | ||
if (overlay.head !== null) this.head = overlay.head | ||
if (overlay.auth !== null) this.auth = overlay.auth | ||
if (overlay.localKeyPair !== null) this.localKeyPair = overlay.localKeyPair | ||
if (overlay.encryptionKey !== null) this.encryptionKey = overlay.encryptionKey | ||
if (overlay.dataInfo !== null) this.dataInfo = overlay.dataInfo | ||
if (overlay.userData !== null) this.userData = mergeMap(this.userData, overlay.userData) | ||
if (overlay.blocks !== null) this.blocks = mergeTip(this.blocks, overlay.blocks) | ||
if (overlay.treeNodes !== null) this.treeNodes = mergeMap(this.treeNodes, overlay.treeNodes) | ||
if (overlay.bitfields !== null) this.bitfields = mergeTip(this.bitfields, overlay.bitfields) | ||
} | ||
} | ||
|
||
class TipList { | ||
constructor () { | ||
this.offset = 0 | ||
this.data = [] | ||
} | ||
|
||
end () { | ||
return this.offset + this.data.length | ||
} | ||
|
||
put (index, value) { | ||
if (this.data.length === 0) { | ||
this.offset = index | ||
this.data.push(value) | ||
return | ||
} | ||
|
||
if (this.data.length === index) { | ||
this.push.push(value) | ||
return | ||
} | ||
|
||
throw ASSERTION('Invalid put on tip list') | ||
} | ||
|
||
get (index) { | ||
index -= this.offset | ||
if (index >= this.data.length) return null | ||
return this.data[index] | ||
} | ||
|
||
* [Symbol.iterator] () { | ||
for (let i = 0; i < this.data.length; i++) { | ||
yield [i + this.offset, this.data[i]] | ||
} | ||
} | ||
} | ||
|
||
module.exports = MemoryOverlay | ||
|
||
class MemoryOverlayReadBatch { | ||
constructor (overlay, read) { | ||
this.read = read | ||
this.overlay = overlay | ||
} | ||
|
||
async getCoreHead () { | ||
return this.overlay.head !== null ? this.overlay.head : this.read.getCoreHead() | ||
} | ||
|
||
async getCoreAuth () { | ||
return this.overlay.auth !== null ? this.overlay.auth : this.read.getCoreAuth() | ||
} | ||
|
||
async getLocalKeyPair () { | ||
return this.overlay.localKeyPair !== null ? this.overlay.localKeyPair : this.read.getLocalKeyPair() | ||
} | ||
|
||
async getEncryptionKey () { | ||
return this.overlay.encryptionKey !== null ? this.overlay.encryptionKey : this.read.getEncryptionKey() | ||
} | ||
|
||
async getDataInfo () { | ||
return this.overlay.dataInfo !== null ? this.overlay.dataInfo : this.read.getDataInfo() | ||
} | ||
|
||
async getUserData (key) { | ||
const hex = this.overlay.userData === null ? null : b4a.toString('hex', key) | ||
return hex !== null && this.userData.has(hex) ? this.overlay.dataInfo.get(hex) : this.read.getUserData(key) | ||
} | ||
|
||
async hasBlock (index) { | ||
if (this.overlay.blocks !== null && index >= this.overlay.blocks.offset) { | ||
const blk = this.overlay.blocks.get(index) | ||
if (blk !== null) return true | ||
} | ||
return this.read.hasBlock(index) | ||
} | ||
|
||
async getBlock (index, error) { | ||
if (this.overlay.blocks !== null && index >= this.overlay.blocks.offset) { | ||
const blk = this.overlay.blocks.get(index) | ||
if (blk !== null) return blk | ||
} | ||
return this.read.getBlock(index, error) | ||
} | ||
|
||
async hasTreeNode (index) { | ||
return (this.overlay.treeNodes !== null && this.overlay.treeNodes.has(index)) || this.read.hasTreeNode(index) | ||
} | ||
|
||
async getTreeNode (index, error) { | ||
if (this.overlay.treeNodes !== null && this.overlay.treeNodes.has(index)) { | ||
return this.overlay.treeNodes.get(index) | ||
} | ||
return this.read.getTreeNode(index, error) | ||
} | ||
|
||
async getBitfieldPage (index) { | ||
if (this.overlay.bitfields !== null && index >= this.overlay.bitfields.offset) { | ||
const page = this.overlay.bitfields.get(index) | ||
if (page !== null) return page | ||
} | ||
return this.read.getBitfieldPage(index) | ||
} | ||
|
||
destroy () { | ||
this.read.destroy() | ||
} | ||
|
||
flush () { | ||
return this.read.flush() | ||
} | ||
|
||
tryFlush () { | ||
this.read.tryFlush() | ||
} | ||
} | ||
|
||
class MemoryOverlayWriteBatch { | ||
constructor (storage) { | ||
this.storage = storage | ||
this.overlay = new MemoryOverlay() | ||
} | ||
|
||
setCoreHead (head) { | ||
this.overlay.head = head | ||
} | ||
|
||
setCoreAuth (auth) { | ||
this.overlay.auth = auth | ||
} | ||
|
||
setBatchPointer (name, pointer) { | ||
throw ASSERTION('Not supported') | ||
} | ||
|
||
setDataDependency (dataInfo) { | ||
throw ASSERTION('Not supported') | ||
} | ||
|
||
setLocalKeyPair (keyPair) { | ||
this.overlay.localKeyPair = keyPair | ||
} | ||
|
||
setEncryptionKey (encryptionKey) { | ||
this.overlay.encryptionKey = encryptionKey | ||
} | ||
|
||
setDataInfo (info) { | ||
this.overlay.dataInfo = info | ||
} | ||
|
||
setUserData (key, value) { | ||
if (this.overlay.userData === null) this.overlay.userData = new Map() | ||
this.overlay.userData.set(b4a.toString(key, 'hex'), value) | ||
} | ||
|
||
putBlock (index, data) { | ||
if (this.overlay.blocks === null) this.overlay.blocks = new TipList() | ||
this.overlay.blocks.put(index, data) | ||
} | ||
|
||
deleteBlock (index) { | ||
throw ASSERTION('Not supported yet, but will be') | ||
} | ||
|
||
deleteBlockRange (start, end) { | ||
throw ASSERTION('Not supported yet, but will be') | ||
} | ||
|
||
putTreeNode (node) { | ||
if (this.overlay.treeNodes === null) this.overlay.treeNodes = new Map() | ||
this.overlay.treeNodes.set(node.index, node) | ||
} | ||
|
||
deleteTreeNode (index) { | ||
throw ASSERTION('Not supported yet, but will be') | ||
} | ||
|
||
deleteTreeNodeRange (start, end) { | ||
throw ASSERTION('Not supported yet, but will be') | ||
} | ||
|
||
putBitfieldPage (index, page) { | ||
if (this.overlay.bitfields === null) this.overlay.bitfields = new TipList() | ||
this.overlay.bitfields.put(index, page) | ||
} | ||
|
||
deleteBitfieldPage (index) { | ||
throw new Error('Not supported yet, but will be') | ||
} | ||
|
||
deleteBitfieldPageRange (start, end) { | ||
throw new Error('Not supported yet, but will be') | ||
} | ||
|
||
destroy () {} | ||
|
||
flush () { | ||
this.storage.merge(this.overlay) | ||
return Promise.resolve() | ||
} | ||
} | ||
|
||
function mergeMap (a, b) { | ||
if (a === null) return b | ||
for (const [key, value] of b) a.set(key, value) | ||
return a | ||
} | ||
|
||
function mergeTip (a, b) { | ||
if (a === null) return b | ||
while (a.end() !== b.offset && b.offset >= a.offset && b.end() >= a.end()) a.data.pop() | ||
if (a.end() !== b.offset) throw ASSERTION('Cannot merge tip list') | ||
for (const data of b.data) a.data.push(data) | ||
return a | ||
} |
Oops, something went wrong.