Skip to content

Commit 2c760ca

Browse files
committed
keep async but have tmp cache
1 parent 2c173f1 commit 2c760ca

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

packages/gatsby/src/datastore/lmdb/lmdb-datastore.ts

+38-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { RootDatabase, open } from "lmdb-store"
1+
import { RootDatabase, open, ArrayLikeIterable } from "lmdb-store"
22
// import { performance } from "perf_hooks"
3-
import { ActionsUnion, IGatsbyNode } from "../../redux/types"
3+
import { ActionsUnion, IDeleteNodeAction, IGatsbyNode } from "../../redux/types"
44
import { updateNodes } from "./updates/nodes"
55
import { updateNodesByType } from "./updates/nodes-by-type"
66
import { IDataStore, ILmdbDatabases, IQueryResult } from "../types"
@@ -27,6 +27,8 @@ const lmdbDatastore = {
2727
getNodesByType,
2828
}
2929

30+
const preSyncDeletedNodeIdsCache = new Set()
31+
3032
function getDefaultDbPath(): string {
3133
const dbFileName =
3234
process.env.NODE_ENV === `test`
@@ -122,10 +124,8 @@ function iterateNodes(): GatsbyIterable<IGatsbyNode> {
122124
return new GatsbyIterable(
123125
nodesDb
124126
.getKeys({ snapshot: false })
125-
.map(
126-
nodeId => (typeof nodeId === `string` ? getNode(nodeId) : undefined)!
127-
)
128-
.filter(Boolean)
127+
.map(nodeId => (typeof nodeId === `string` ? getNode(nodeId) : undefined))
128+
.filter(Boolean) as ArrayLikeIterable<IGatsbyNode>
129129
)
130130
}
131131

@@ -134,13 +134,22 @@ function iterateNodesByType(type: string): GatsbyIterable<IGatsbyNode> {
134134
return new GatsbyIterable(
135135
nodesByType
136136
.getValues(type)
137-
.map(nodeId => getNode(nodeId)!)
138-
.filter(Boolean)
137+
.map(nodeId => {
138+
if (preSyncDeletedNodeIdsCache.has(nodeId)) {
139+
return undefined
140+
}
141+
142+
return getNode(nodeId)
143+
})
144+
.filter(Boolean) as ArrayLikeIterable<IGatsbyNode>
139145
)
140146
}
141147

142148
function getNode(id: string): IGatsbyNode | undefined {
143-
if (!id) return undefined
149+
if (!id || preSyncDeletedNodeIdsCache.has(id)) {
150+
return undefined
151+
}
152+
144153
const { nodes } = getDatabases()
145154
return nodes.get(id)
146155
}
@@ -151,9 +160,11 @@ function getTypes(): Array<string> {
151160

152161
function countNodes(typeName?: string): number {
153162
if (!typeName) {
154-
const stats = getDatabases().nodes.getStats()
155-
// @ts-ignore
156-
return Number(stats.entryCount || 0) // FIXME: add -1 when restoring shared structures key
163+
const stats = getDatabases().nodes.getStats() as { entryCount: number }
164+
return Math.max(
165+
Number(stats.entryCount) - preSyncDeletedNodeIdsCache.size,
166+
0
167+
) // FIXME: add -1 when restoring shared structures key
157168
}
158169

159170
const { nodesByType } = getDatabases()
@@ -192,15 +203,29 @@ function updateDataStore(action: ActionsUnion): void {
192203
break
193204
}
194205
case `CREATE_NODE`:
206+
case `DELETE_NODE`:
195207
case `ADD_FIELD_TO_NODE`:
196208
case `ADD_CHILD_NODE_TO_PARENT_NODE`:
197-
case `DELETE_NODE`:
198209
case `MATERIALIZE_PAGE_MODE`: {
199210
const dbs = getDatabases()
200211
lastOperationPromise = Promise.all([
201212
updateNodes(dbs.nodes, action),
202213
updateNodesByType(dbs.nodesByType, action),
203214
])
215+
216+
// if create is used in the same transaction as delete we should remove it from cache
217+
if (action.type === `CREATE_NODE`) {
218+
preSyncDeletedNodeIdsCache.delete(action.payload.id)
219+
}
220+
221+
if (action.type === `DELETE_NODE`) {
222+
preSyncDeletedNodeIdsCache.add(
223+
((action as IDeleteNodeAction).payload as IGatsbyNode).id
224+
)
225+
lastOperationPromise.then(() => {
226+
preSyncDeletedNodeIdsCache.clear()
227+
})
228+
}
204229
}
205230
}
206231
}

packages/gatsby/src/datastore/lmdb/updates/nodes-by-type.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ export function updateNodesByType(
1414
}
1515
case `DELETE_NODE`: {
1616
return action.payload
17-
? nodesByTypeDb.removeSync(
18-
action.payload.internal.type,
19-
action.payload.id
20-
)
17+
? nodesByTypeDb.remove(action.payload.internal.type, action.payload.id)
2118
: false
2219
}
2320
}

packages/gatsby/src/datastore/lmdb/updates/nodes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ export function updateNodes(
1515
}
1616
case `DELETE_NODE`: {
1717
if (action.payload) {
18-
return nodesDb.removeSync(action.payload.id)
18+
return nodesDb.remove(action.payload.id)
1919
}
20+
2021
return false
2122
}
2223
case `MATERIALIZE_PAGE_MODE`: {

0 commit comments

Comments
 (0)