Skip to content

Commit

Permalink
remove the cache, we do not use anymore (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored Sep 7, 2024
1 parent cade0cd commit 16d2d26
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 179 deletions.
59 changes: 17 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const hypercoreCrypto = require('hypercore-crypto')
const CoreStorage = require('hypercore-on-the-rocks')
const c = require('compact-encoding')
const b4a = require('b4a')
const Xache = require('xache')
const NoiseSecretStream = require('@hyperswarm/secret-stream')
const Protomux = require('protomux')
const z32 = require('z32')
Expand Down Expand Up @@ -64,7 +63,6 @@ module.exports = class Hypercore extends EventEmitter {
this.replicator = null
this.encryption = null
this.extensions = new Map()
this.cache = createCache(opts.cache)

this.valueEncoding = null
this.encodeBatch = null
Expand Down Expand Up @@ -229,11 +227,6 @@ module.exports = class Hypercore extends EventEmitter {

s._passCapabilities(this)

// Configure the cache unless explicitly disabled.
if (opts.cache !== false) {
s.cache = opts.cache === true || !opts.cache ? this.cache : opts.cache
}

if (this.opened) ensureEncryption(s, opts)
this._addSession(s)

Expand Down Expand Up @@ -302,7 +295,6 @@ module.exports = class Hypercore extends EventEmitter {
await opts._opening
}
if (opts.preload) opts = { ...opts, ...(await this._retryPreload(opts.preload)) }
if (this.cache === null && opts.cache) this.cache = createCache(opts.cache)

if (isFirst) {
await this._openCapabilities(key, storage, opts)
Expand Down Expand Up @@ -671,8 +663,6 @@ module.exports = class Hypercore extends EventEmitter {
const s = this.sessions[i]

if (truncated) {
if (s.cache) s.cache.clear()

// If snapshotted, make sure to update our compat so we can fail gets
if (s._snapshot && bitfield.start < s._snapshot.compatLength) s._snapshot.compatLength = bitfield.start
}
Expand Down Expand Up @@ -842,8 +832,7 @@ module.exports = class Hypercore extends EventEmitter {

const encoding = (opts && opts.valueEncoding && c.from(opts.valueEncoding)) || this.valueEncoding

let req = this.cache && this.cache.get(index)
if (!req) req = this._get(index, opts)
const req = this._get(index, opts)

let block = await req
if (!block) return null
Expand Down Expand Up @@ -889,18 +878,17 @@ module.exports = class Hypercore extends EventEmitter {
async _get (index, opts) {
if (this.core.isFlushing) await this.core.flushed()

let block = await readBlock(this.state.createReadBatch(), index)
const block = await readBlock(this.state.createReadBatch(), index)

if (block !== null) return block

// snapshot should check if core has block
if (block === null && this._snapshot !== null) {
checkSnapshot(this._snapshot, index)
block = await readBlock(this.core.state.createReadBatch(), index)
if (this._snapshot !== null) {
checkSnapshot(this._snapshot, index)
}
const coreBlock = await readBlock(this.core.state.createReadBatch(), index)

if (block !== null) {
if (this.cache) this.cache.set(index, Promise.resolve(block))
return block
checkSnapshot(this._snapshot, index)
if (coreBlock !== null) return coreBlock
}

if (!this._shouldWait(opts, this.wait)) return null
Expand All @@ -916,31 +904,17 @@ module.exports = class Hypercore extends EventEmitter {
const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
if (timeout) req.context.setTimeout(req, timeout)

return this._cacheOnResolve(index, req.promise, this.state.tree.fork)
const replicatedBlock = await req.promise
if (this._snapshot !== null) checkSnapshot(this._snapshot, index)

return maybeUnslab(replicatedBlock)
}

async restoreBatch (length, blocks) {
if (this.opened === false) await this.opening
return this.state.tree.restoreBatch(length)
}

async _cacheOnResolve (index, req, fork) {
const resolved = await req

// Unslab only when it takes up less then half the slab
const block = resolved !== null && 2 * resolved.byteLength < resolved.buffer.byteLength
? unslab(resolved)
: resolved

if (this._snapshot !== null) checkSnapshot(this._snapshot, index)

if (this.cache && fork === this.core.tree.fork) {
this.cache.set(index, Promise.resolve(block))
}

return block
}

_shouldWait (opts, defaultValue) {
if (opts) {
if (opts.wait === false) return false
Expand Down Expand Up @@ -1148,14 +1122,15 @@ function ensureEncryption (core, opts) {
core.encryption = new BlockEncryption(opts.encryptionKey, core.key, { compat: core.core ? core.core.compat : true, isBlockKey: opts.isBlockKey })
}

function createCache (cache) {
return cache === true ? new Xache({ maxSize: 65536, maxAge: 0 }) : (cache || null)
}

function isValidIndex (index) {
return index === 0 || index > 0
}

function maybeUnslab (block) {
// Unslab only when it takes up less then half the slab
return block !== null && 2 * block.byteLength < block.buffer.byteLength ? unslab(block) : block
}

function checkSnapshot (snapshot, index) {
if (index >= snapshot.compatLength) throw SNAPSHOT_NOT_AVAILABLE()
}
Expand Down
4 changes: 0 additions & 4 deletions lib/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ module.exports = async function auditCore (core, update) {
if (rightNode.size) clearNode(rightNode)
}

if (corrections.tree) {
core.tree.cache.clear()
}

let i = 0
let nextOffset = -1
while (i < length) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"sodium-universal": "^4.0.0",
"streamx": "^2.12.4",
"unslab": "^1.3.0",
"xache": "^1.1.0",
"z32": "^1.0.0"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ async function runTests () {
await import('./basic.js') // todo: implement storageInfo API
await import('./batch.js') // todo: implement batch api
await import('./bitfield.js')
await import('./cache.js')
await import('./clear.js') // todo: replace Info.bytesUsed API
// await import('./compat.js') // todo: how to test compat?
await import('./conflicts.js')
Expand Down
131 changes: 0 additions & 131 deletions test/cache.js

This file was deleted.

0 comments on commit 16d2d26

Please sign in to comment.