Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add entry-index module #254

Merged
merged 1 commit into from Jul 1, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -0,0 +1,29 @@
'use strict'

class EntryIndex {
constructor (entries = {}) {
this._cache = entries
}

set (k, v) {
this._cache[k] = v
}

get (k) {
return this._cache[k]
}

delete (k) {
return delete this._cache[k]
}

add (newItems) {
this._cache = Object.assign(this._cache, newItems)
}

get length () {
return Object.values(this._cache).length
}
}

module.exports = EntryIndex
@@ -9,7 +9,7 @@ const Clock = require('./lamport-clock')
const { LastWriteWins, NoZeroes } = require('./log-sorting')
const AccessController = require('./default-access-controller')
const { isDefined, findUniques } = require('./utils')

const EntryIndex = require('./entry-index')
const randomId = () => new Date().getTime().toString()
const getHash = e => e.hash
const flatMap = (res, acc) => res.concat(acc)
@@ -83,7 +83,7 @@ class Log extends GSet {

// Add entries to the internal cache
entries = entries || []
this._entryIndex = entries.reduce(uniqueEntriesReducer, {})
this._entryIndex = new EntryIndex(entries.reduce(uniqueEntriesReducer, {}))

// Set heads if not passed as an argument
heads = heads || Log.findHeads(entries)
@@ -169,7 +169,7 @@ class Log extends GSet {
* @returns {Entry|undefined}
*/
get (hash) {
return this._entryIndex[hash]
return this._entryIndex.get(hash)
}

/**
@@ -178,7 +178,7 @@ class Log extends GSet {
* @returns {boolean}
*/
has (entry) {
return this._entryIndex[entry.hash || entry] !== undefined
return this._entryIndex.get(entry.hash || entry) !== undefined
}

traverse (rootEntries, amount = -1, endHash) {
@@ -264,7 +264,7 @@ class Log extends GSet {
throw new Error(`Could not append entry, key "${this._identity.id}" is not allowed to write to the log`)
}

this._entryIndex[entry.hash] = entry
this._entryIndex.set(entry.hash, entry)
nexts.forEach(e => (this._nextsIndex[e] = entry.hash))
this._headsIndex = {}
this._headsIndex[entry.hash] = entry
@@ -383,7 +383,7 @@ class Log extends GSet {
Object.values(newItems).forEach(addToNextsIndex)

// Update the internal entry index
this._entryIndex = Object.assign(this._entryIndex, newItems)
this._entryIndex.add(newItems)

// Merge the heads
const notReferencedByNewItems = e => !nextsFromNewItems.find(a => a === e.hash)
@@ -400,9 +400,10 @@ class Log extends GSet {
if (size > -1) {
let tmp = this.values
tmp = tmp.slice(-size)
this._entryIndex = tmp.reduce(uniqueEntriesReducer, {})
this._entryIndex = null
this._entryIndex = new EntryIndex(tmp.reduce(uniqueEntriesReducer, {}))
this._headsIndex = Log.findHeads(tmp).reduce(uniqueEntriesReducer, {})
this._length = Object.values(this._entryIndex).length
this._length = this._entryIndex.length
}

// Find the latest clock from the heads
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.