File size: 15,797 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
import { Path } from 'slate'
import { Key } from 'slate-dom'
import {
Chunk,
ChunkTree,
ChunkLeaf,
ChunkDescendant,
ChunkAncestor,
} from './types'
type SavedPointer =
| 'start'
| {
chunk: ChunkAncestor
node: ChunkDescendant
}
export interface ChunkTreeHelperOptions {
chunkSize: number
debug?: boolean
}
/**
* Traverse and modify a chunk tree
*/
export class ChunkTreeHelper {
/**
* The root of the chunk tree
*/
private root: ChunkTree
/**
* The ideal size of a chunk
*/
private chunkSize: number
/**
* Whether debug mode is enabled
*
* If enabled, the pointer state will be checked for internal consistency
* after each mutating operation.
*/
private debug: boolean
/**
* Whether the traversal has reached the end of the chunk tree
*
* When this is true, the pointerChunk and pointerIndex point to the last
* top-level node in the chunk tree, although pointerNode returns null.
*/
private reachedEnd: boolean
/**
* The chunk containing the current node
*/
private pointerChunk: ChunkAncestor
/**
* The index of the current node within pointerChunk
*
* Can be -1 to indicate that the pointer is before the start of the tree.
*/
private pointerIndex: number
/**
* Similar to a Slate path; tracks the path of pointerChunk relative to the
* root.
*
* Used to move the pointer from the current chunk to the parent chunk more
* efficiently.
*/
private pointerIndexStack: number[]
/**
* Indexing the current chunk's children has a slight time cost, which adds up
* when traversing very large trees, so the current node is cached.
*
* A value of undefined means that the current node is not cached. This
* property must be set to undefined whenever the pointer is moved, unless
* the pointer is guaranteed to point to the same node that it did previously.
*/
private cachedPointerNode: ChunkDescendant | null | undefined
constructor(
chunkTree: ChunkTree,
{ chunkSize, debug }: ChunkTreeHelperOptions
) {
this.root = chunkTree
this.chunkSize = chunkSize
// istanbul ignore next
this.debug = debug ?? false
this.pointerChunk = chunkTree
this.pointerIndex = -1
this.pointerIndexStack = []
this.reachedEnd = false
this.validateState()
}
/**
* Move the pointer to the next leaf in the chunk tree
*/
public readLeaf(): ChunkLeaf | null {
// istanbul ignore next
if (this.reachedEnd) return null
// Get the next sibling or aunt node
while (true) {
if (this.pointerIndex + 1 < this.pointerSiblings.length) {
this.pointerIndex++
this.cachedPointerNode = undefined
break
} else if (this.pointerChunk.type === 'root') {
this.reachedEnd = true
return null
} else {
this.exitChunk()
}
}
this.validateState()
// If the next sibling or aunt is a chunk, descend into it
this.enterChunkUntilLeaf(false)
return this.pointerNode as ChunkLeaf
}
/**
* Move the pointer to the previous leaf in the chunk tree
*/
public returnToPreviousLeaf() {
// If we were at the end of the tree, descend into the end of the last
// chunk in the tree
if (this.reachedEnd) {
this.reachedEnd = false
this.enterChunkUntilLeaf(true)
return
}
// Get the previous sibling or aunt node
while (true) {
if (this.pointerIndex >= 1) {
this.pointerIndex--
this.cachedPointerNode = undefined
break
} else if (this.pointerChunk.type === 'root') {
this.pointerIndex = -1
return
} else {
this.exitChunk()
}
}
this.validateState()
// If the previous sibling or aunt is a chunk, descend into it
this.enterChunkUntilLeaf(true)
}
/**
* Insert leaves before the current leaf, leaving the pointer unchanged
*/
public insertBefore(leaves: ChunkLeaf[]) {
this.returnToPreviousLeaf()
this.insertAfter(leaves)
this.readLeaf()
}
/**
* Insert leaves after the current leaf, leaving the pointer on the last
* inserted leaf
*
* The insertion algorithm first checks for any chunk we're currently at the
* end of that can receive additional leaves. Next, it tries to insert leaves
* at the starts of any subsequent chunks.
*
* Any remaining leaves are passed to rawInsertAfter to be chunked and
* inserted at the highest possible level.
*/
public insertAfter(leaves: ChunkLeaf[]) {
// istanbul ignore next
if (leaves.length === 0) return
let beforeDepth = 0
let afterDepth = 0
// While at the end of a chunk, insert any leaves that will fit, and then
// exit the chunk
while (
this.pointerChunk.type === 'chunk' &&
this.pointerIndex === this.pointerSiblings.length - 1
) {
const remainingCapacity = this.chunkSize - this.pointerSiblings.length
const toInsertCount = Math.min(remainingCapacity, leaves.length)
if (toInsertCount > 0) {
const leavesToInsert = leaves.splice(0, toInsertCount)
this.rawInsertAfter(leavesToInsert, beforeDepth)
}
this.exitChunk()
beforeDepth++
}
if (leaves.length === 0) return
// Save the pointer so that we can come back here after inserting leaves
// into the starts of subsequent blocks
const rawInsertPointer = this.savePointer()
// If leaves are inserted into the start of a subsequent block, then we
// eventually need to restore the pointer to the last such inserted leaf
let finalPointer: SavedPointer | null = null
// Move the pointer into the chunk containing the next leaf, if it exists
if (this.readLeaf()) {
// While at the start of a chunk, insert any leaves that will fit, and
// then exit the chunk
while (this.pointerChunk.type === 'chunk' && this.pointerIndex === 0) {
const remainingCapacity = this.chunkSize - this.pointerSiblings.length
const toInsertCount = Math.min(remainingCapacity, leaves.length)
if (toInsertCount > 0) {
const leavesToInsert = leaves.splice(-toInsertCount, toInsertCount)
// Insert the leaves at the start of the chunk
this.pointerIndex = -1
this.cachedPointerNode = undefined
this.rawInsertAfter(leavesToInsert, afterDepth)
// If this is the first batch of insertions at the start of a
// subsequent chunk, set the final pointer to the last inserted leaf
if (!finalPointer) {
finalPointer = this.savePointer()
}
}
this.exitChunk()
afterDepth++
}
}
this.restorePointer(rawInsertPointer)
// If there are leaves left to insert, insert them between the end of the
// previous chunk and the start of the first subsequent chunk, or wherever
// the pointer ended up after the first batch of insertions
const minDepth = Math.max(beforeDepth, afterDepth)
this.rawInsertAfter(leaves, minDepth)
if (finalPointer) {
this.restorePointer(finalPointer)
}
this.validateState()
}
/**
* Remove the current node and decrement the pointer, deleting any ancestor
* chunk that becomes empty as a result
*/
public remove() {
this.pointerSiblings.splice(this.pointerIndex--, 1)
this.cachedPointerNode = undefined
if (
this.pointerSiblings.length === 0 &&
this.pointerChunk.type === 'chunk'
) {
this.exitChunk()
this.remove()
} else {
this.invalidateChunk()
}
this.validateState()
}
/**
* Add the current chunk and all ancestor chunks to the list of modified
* chunks
*/
public invalidateChunk() {
for (let c = this.pointerChunk; c.type === 'chunk'; c = c.parent) {
this.root.modifiedChunks.add(c)
}
}
/**
* Whether the pointer is at the start of the tree
*/
private get atStart() {
return this.pointerChunk.type === 'root' && this.pointerIndex === -1
}
/**
* The siblings of the current node
*/
private get pointerSiblings(): ChunkDescendant[] {
return this.pointerChunk.children
}
/**
* Get the current node (uncached)
*
* If the pointer is at the start or end of the document, returns null.
*
* Usually, the current node is a chunk leaf, although it can be a chunk
* while insertions are in progress.
*/
private getPointerNode(): ChunkDescendant | null {
if (this.reachedEnd || this.pointerIndex === -1) {
return null
}
return this.pointerSiblings[this.pointerIndex]
}
/**
* Cached getter for the current node
*/
private get pointerNode(): ChunkDescendant | null {
if (this.cachedPointerNode !== undefined) return this.cachedPointerNode
const pointerNode = this.getPointerNode()
this.cachedPointerNode = pointerNode
return pointerNode
}
/**
* Get the path of a chunk relative to the root, returning null if the chunk
* is not connected to the root
*/
private getChunkPath(chunk: ChunkAncestor): number[] | null {
const path: number[] = []
for (let c = chunk; c.type === 'chunk'; c = c.parent) {
const index = c.parent.children.indexOf(c)
// istanbul ignore next
if (index === -1) {
return null
}
path.unshift(index)
}
return path
}
/**
* Save the current pointer to be restored later
*/
private savePointer(): SavedPointer {
if (this.atStart) return 'start'
// istanbul ignore next
if (!this.pointerNode) {
throw new Error('Cannot save pointer when pointerNode is null')
}
return {
chunk: this.pointerChunk,
node: this.pointerNode,
}
}
/**
* Restore the pointer to a previous state
*/
private restorePointer(savedPointer: SavedPointer) {
if (savedPointer === 'start') {
this.pointerChunk = this.root
this.pointerIndex = -1
this.pointerIndexStack = []
this.reachedEnd = false
this.cachedPointerNode = undefined
return
}
// Since nodes may have been inserted or removed prior to the saved
// pointer since it was saved, the index and index stack must be
// recomputed. This is slow, but this is fine since restoring a pointer is
// not a frequent operation.
const { chunk, node } = savedPointer
const index = chunk.children.indexOf(node)
// istanbul ignore next
if (index === -1) {
throw new Error(
'Cannot restore point because saved node is no longer in saved chunk'
)
}
const indexStack = this.getChunkPath(chunk)
// istanbul ignore next
if (!indexStack) {
throw new Error(
'Cannot restore point because saved chunk is no longer connected to root'
)
}
this.pointerChunk = chunk
this.pointerIndex = index
this.pointerIndexStack = indexStack
this.reachedEnd = false
this.cachedPointerNode = node
this.validateState()
}
/**
* Assuming the current node is a chunk, move the pointer into that chunk
*
* @param end If true, place the pointer on the last node of the chunk.
* Otherwise, place the pointer on the first node.
*/
private enterChunk(end: boolean) {
// istanbul ignore next
if (this.pointerNode?.type !== 'chunk') {
throw new Error('Cannot enter non-chunk')
}
this.pointerIndexStack.push(this.pointerIndex)
this.pointerChunk = this.pointerNode
this.pointerIndex = end ? this.pointerSiblings.length - 1 : 0
this.cachedPointerNode = undefined
this.validateState()
// istanbul ignore next
if (this.pointerChunk.children.length === 0) {
throw new Error('Cannot enter empty chunk')
}
}
/**
* Assuming the current node is a chunk, move the pointer into that chunk
* repeatedly until the current node is a leaf
*
* @param end If true, place the pointer on the last node of the chunk.
* Otherwise, place the pointer on the first node.
*/
private enterChunkUntilLeaf(end: boolean) {
while (this.pointerNode?.type === 'chunk') {
this.enterChunk(end)
}
}
/**
* Move the pointer to the parent chunk
*/
private exitChunk() {
// istanbul ignore next
if (this.pointerChunk.type === 'root') {
throw new Error('Cannot exit root')
}
const previousPointerChunk = this.pointerChunk
this.pointerChunk = previousPointerChunk.parent
this.pointerIndex = this.pointerIndexStack.pop()!
this.cachedPointerNode = undefined
this.validateState()
}
/**
* Insert leaves immediately after the current node, leaving the pointer on
* the last inserted leaf
*
* Leaves are chunked according to the number of nodes already in the parent
* plus the number of nodes being inserted, or the minimum depth if larger
*/
private rawInsertAfter(leaves: ChunkLeaf[], minDepth: number) {
if (leaves.length === 0) return
const groupIntoChunks = (
leaves: ChunkLeaf[],
parent: ChunkAncestor,
perChunk: number
): ChunkDescendant[] => {
if (perChunk === 1) return leaves
const chunks: Chunk[] = []
for (let i = 0; i < this.chunkSize; i++) {
const chunkNodes = leaves.slice(i * perChunk, (i + 1) * perChunk)
if (chunkNodes.length === 0) break
const chunk: Chunk = {
type: 'chunk',
key: new Key(),
parent,
children: [],
}
chunk.children = groupIntoChunks(
chunkNodes,
chunk,
perChunk / this.chunkSize
)
chunks.push(chunk)
}
return chunks
}
// Determine the chunking depth based on the number of existing nodes in
// the chunk and the number of nodes being inserted
const newTotal = this.pointerSiblings.length + leaves.length
let depthForTotal = 0
for (let i = this.chunkSize; i < newTotal; i *= this.chunkSize) {
depthForTotal++
}
// A depth of 0 means no chunking
const depth = Math.max(depthForTotal, minDepth)
const perTopLevelChunk = Math.pow(this.chunkSize, depth)
const chunks = groupIntoChunks(leaves, this.pointerChunk, perTopLevelChunk)
this.pointerSiblings.splice(this.pointerIndex + 1, 0, ...chunks)
this.pointerIndex += chunks.length
this.cachedPointerNode = undefined
this.invalidateChunk()
this.validateState()
}
/**
* If debug mode is enabled, ensure that the state is internally consistent
*/
// istanbul ignore next
private validateState() {
if (!this.debug) return
const validateDescendant = (node: ChunkDescendant) => {
if (node.type === 'chunk') {
const { parent, children } = node
if (!parent.children.includes(node)) {
throw new Error(
`Debug: Chunk ${node.key.id} has an incorrect parent property`
)
}
children.forEach(validateDescendant)
}
}
this.root.children.forEach(validateDescendant)
if (
this.cachedPointerNode !== undefined &&
this.cachedPointerNode !== this.getPointerNode()
) {
throw new Error(
'Debug: The cached pointer is incorrect and has not been invalidated'
)
}
const actualIndexStack = this.getChunkPath(this.pointerChunk)
if (!actualIndexStack) {
throw new Error('Debug: The pointer chunk is not connected to the root')
}
if (!Path.equals(this.pointerIndexStack, actualIndexStack)) {
throw new Error(
`Debug: The cached index stack [${this.pointerIndexStack.join(
', '
)}] does not match the path of the pointer chunk [${actualIndexStack.join(
', '
)}]`
)
}
}
}
|