Skip to content

Commit

Permalink
keep async but have tmp cache
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Oct 18, 2021
1 parent b7a4093 commit a5a855b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
51 changes: 38 additions & 13 deletions packages/gatsby/src/datastore/lmdb/lmdb-datastore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RootDatabase, open } from "lmdb-store"
import { RootDatabase, open, ArrayLikeIterable } from "lmdb-store"
// import { performance } from "perf_hooks"
import { ActionsUnion, IGatsbyNode } from "../../redux/types"
import { ActionsUnion, IDeleteNodeAction, IGatsbyNode } from "../../redux/types"
import { updateNodes } from "./updates/nodes"
import { updateNodesByType } from "./updates/nodes-by-type"
import { IDataStore, ILmdbDatabases, IQueryResult } from "../types"
Expand All @@ -27,6 +27,8 @@ const lmdbDatastore = {
getNodesByType,
}

const preSyncDeletedNodeIdsCache = new Set()

function getDefaultDbPath(): string {
const dbFileName =
process.env.NODE_ENV === `test`
Expand Down Expand Up @@ -122,10 +124,8 @@ function iterateNodes(): GatsbyIterable<IGatsbyNode> {
return new GatsbyIterable(
nodesDb
.getKeys({ snapshot: false })
.map(
nodeId => (typeof nodeId === `string` ? getNode(nodeId) : undefined)!
)
.filter(Boolean)
.map(nodeId => (typeof nodeId === `string` ? getNode(nodeId) : undefined))
.filter(Boolean) as ArrayLikeIterable<IGatsbyNode>
)
}

Expand All @@ -134,13 +134,22 @@ function iterateNodesByType(type: string): GatsbyIterable<IGatsbyNode> {
return new GatsbyIterable(
nodesByType
.getValues(type)
.map(nodeId => getNode(nodeId)!)
.filter(Boolean)
.map(nodeId => {
if (preSyncDeletedNodeIdsCache.has(nodeId)) {
return undefined
}

return getNode(nodeId)
})
.filter(Boolean) as ArrayLikeIterable<IGatsbyNode>
)
}

function getNode(id: string): IGatsbyNode | undefined {
if (!id) return undefined
if (!id || preSyncDeletedNodeIdsCache.has(id)) {
return undefined
}

const { nodes } = getDatabases()
return nodes.get(id)
}
Expand All @@ -151,9 +160,11 @@ function getTypes(): Array<string> {

function countNodes(typeName?: string): number {
if (!typeName) {
const stats = getDatabases().nodes.getStats()
// @ts-ignore
return Number(stats.entryCount || 0) // FIXME: add -1 when restoring shared structures key
const stats = getDatabases().nodes.getStats() as { entryCount: number }
return Math.max(
Number(stats.entryCount) - preSyncDeletedNodeIdsCache.size,
0
) // FIXME: add -1 when restoring shared structures key
}

const { nodesByType } = getDatabases()
Expand Down Expand Up @@ -192,15 +203,29 @@ function updateDataStore(action: ActionsUnion): void {
break
}
case `CREATE_NODE`:
case `DELETE_NODE`:
case `ADD_FIELD_TO_NODE`:
case `ADD_CHILD_NODE_TO_PARENT_NODE`:
case `DELETE_NODE`:
case `MATERIALIZE_PAGE_MODE`: {
const dbs = getDatabases()
lastOperationPromise = Promise.all([
updateNodes(dbs.nodes, action),
updateNodesByType(dbs.nodesByType, action),
])

// if create is used in the same transaction as delete we should remove it from cache
if (action.type === `CREATE_NODE`) {
preSyncDeletedNodeIdsCache.delete(action.payload.id)
}

if (action.type === `DELETE_NODE`) {
preSyncDeletedNodeIdsCache.add(
((action as IDeleteNodeAction).payload as IGatsbyNode).id
)
lastOperationPromise.then(() => {
preSyncDeletedNodeIdsCache.clear()
})
}
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby/src/datastore/lmdb/updates/nodes-by-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export function updateNodesByType(
}
case `DELETE_NODE`: {
return action.payload
? nodesByTypeDb.removeSync(
action.payload.internal.type,
action.payload.id
)
? nodesByTypeDb.remove(action.payload.internal.type, action.payload.id)
: false
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/datastore/lmdb/updates/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export function updateNodes(
}
case `DELETE_NODE`: {
if (action.payload) {
return nodesDb.removeSync(action.payload.id)
return nodesDb.remove(action.payload.id)
}

return false
}
case `MATERIALIZE_PAGE_MODE`: {
Expand Down

0 comments on commit a5a855b

Please sign in to comment.