Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import Foundation

public extension EtcdKeyRange {
init(key: Data, rangeEnd: Data?) {
self.key = key
self.rangeEnd = rangeEnd
}
}

public struct EtcdRangeBatch<Item> {
public let items: [Item]
public let more: Bool
public let revision: Int64?

public init(items: [Item], more: Bool, revision: Int64?) {
self.items = items
self.more = more
self.revision = revision
}
}

public struct EtcdPageStart: Equatable, Sendable {
public let range: EtcdKeyRange
public let revision: Int64?

public init(range: EtcdKeyRange, revision: Int64?) {
self.range = range
self.revision = revision
}
}

// Reads a key range a bounded batch per request instead of in one response. A batch sends no sort order, so etcd
// applies the limit as it reads, in ascending key order; a descending sort makes it read the whole range first. Every
// request after the first reads at the first one's revision, so the batches are one snapshot, as a single request is.
public enum EtcdRangeScan {
public static func keyAfter(_ key: Data) -> Data {
key + Data([0])
}

// The rest of `range` after `key`, in ascending order.
public static func range(after key: Data, in range: EtcdKeyRange) -> EtcdKeyRange {
EtcdKeyRange(key: keyAfter(key), rangeEnd: range.rangeEnd)
}

// The part of `range` below `key`, which is where a descending read goes on.
public static func range(before key: Data, in range: EtcdKeyRange) -> EtcdKeyRange {
EtcdKeyRange(key: range.key, rangeEnd: key)
}

// Hands each batch to `body` until it returns false, the range ends or `maxItems` have been read.
public static func scan<Item>(
_ range: EtcdKeyRange,
batchSize: Int,
maxItems: Int,
key: (Item) -> Data,
fetch: (_ range: EtcdKeyRange, _ limit: Int64, _ revision: Int64?) async throws -> EtcdRangeBatch<Item>,
body: ([Item]) throws -> Bool
) async throws {
var current = range
var revision: Int64?
var read = 0
while read < maxItems {
try Task.checkCancellation()
let batch = try await fetch(current, Int64(min(max(1, batchSize), maxItems - read)), revision)
revision = revision ?? batch.revision
read += batch.items.count
guard try body(batch.items), batch.more, let last = batch.items.last else { return }
current = Self.range(after: key(last), in: range)
}
}

// Where the page at `offset` begins, found by reading keys alone and never past the offset. Nil when the range
// has no key at that offset.
public static func pageStart(
of range: EtcdKeyRange,
skipping offset: Int,
batchSize: Int,
fetchKeys: (_ range: EtcdKeyRange, _ limit: Int64, _ revision: Int64?) async throws -> EtcdRangeBatch<Data>
) async throws -> EtcdPageStart? {
guard offset > 0 else { return EtcdPageStart(range: range, revision: nil) }
var current = range
var revision: Int64?
var remaining = offset
while remaining > 0 {
try Task.checkCancellation()
let batch = try await fetchKeys(current, Int64(min(remaining, max(1, batchSize))), revision)
revision = revision ?? batch.revision
guard batch.more, let last = batch.items.last else { return nil }
remaining -= batch.items.count
current = Self.range(after: last, in: range)
}
return EtcdPageStart(range: current, revision: revision)
}
}

public enum EtcdCompaction {
// etcd's ErrCompacted, sent as gRPC OutOfRange.
public static func isCompacted(grpcCode: Int?, message: String) -> Bool {
grpcCode == 11 && message.contains("required revision has been compacted")
}

// A read pinned to its first batch's revision fails once the store compacts past it. It starts over once at the
// current revision, which is what a single request would have read.
public static func restartingOnce<Value>(
when isCompacted: (Error) -> Bool,
_ read: () async throws -> Value
) async throws -> Value {
do {
return try await read()
} catch let error where isCompacted(error) {
return try await read()
}
}
}

// The matching rows of one page, gathered from rows read in ascending key order. A descending page is the tail of the
// ascending matches, so it keeps only the last `offset + limit` of them rather than every row in the range.
public struct EtcdPageCollector<Item> {
private let offset: Int
private let limit: Int
private let ascending: Bool
private var skipped = 0
private var kept: [Item] = []

public init(offset: Int, limit: Int, ascending: Bool) {
self.offset = max(0, offset)
self.limit = max(0, limit)
self.ascending = ascending
}

// False once an ascending page is complete, so the read can stop.
public mutating func add(_ item: Item) -> Bool {
if ascending {
guard kept.count < limit else { return false }
if skipped < offset {
skipped += 1
} else {
kept.append(item)
}
return kept.count < limit
}
let capacity = offset + limit
guard capacity > 0 else { return true }
kept.append(item)
if kept.count >= 2 * capacity {
kept.removeFirst(kept.count - capacity)
}
return true
}

public var page: [Item] {
guard !ascending else { return kept }
return Array(kept.suffix(offset + limit).dropLast(offset).reversed())
}

var heldCount: Int { kept.count }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Foundation

// Answers `SQLStatementScanner.locatedStatementAtCursor` for a document that changes a little at a time: the scan
// resumes from the last recorded statement boundary before the caret instead of from the top.
public struct SQLStatementCursorIndex: Sendable {
public static let defaultCheckpointSpacing = 4_096

public let grammar: SQLLexicalGrammar
public private(set) var unitsScanned = 0

private let checkpointSpacing: Int
// In offset order, and so in `decidedThrough` order too.
private var checkpoints: [SQLStatementScanner.Checkpoint] = []

public init(grammar: SQLLexicalGrammar, checkpointSpacing: Int = SQLStatementCursorIndex.defaultCheckpointSpacing) {
self.grammar = grammar
self.checkpointSpacing = max(1, checkpointSpacing)
}

public var checkpointCount: Int {
checkpoints.count
}

// Call before the next lookup whenever the text changed at or after `location`.
public mutating func textDidChange(at location: Int) {
while let last = checkpoints.last, last.decidedThrough >= location {
checkpoints.removeLast()
}
}

public mutating func statementAtCursor(_ cursorPosition: Int, in text: String) -> SQLStatementScanner.LocatedStatement {
let length = (text as NSString).length
let cursor = min(max(0, cursorPosition), length)
let resume = lastCheckpoint(before: cursor)
let start = resume?.offset ?? 0
let spacing = checkpointSpacing

var lastRecorded = checkpoints.last?.offset ?? 0
var recorded: [SQLStatementScanner.Checkpoint] = []
var result = SQLStatementScanner.LocatedStatement(sql: "", offset: 0, hasContent: false)
SQLStatementScanner.scan(
sql: text,
cursorPosition: cursor,
grammar: grammar,
resumingAt: resume,
onCheckpoint: { checkpoint in
guard checkpoint.offset - lastRecorded >= spacing else { return }
recorded.append(checkpoint)
lastRecorded = checkpoint.offset
},
onStatement: { statement in
result = statement
return false
}
)
checkpoints.append(contentsOf: recorded)
unitsScanned += max(cursor, NSMaxRange(result.range)) - start
return result
}

private func lastCheckpoint(before offset: Int) -> SQLStatementScanner.Checkpoint? {
var low = 0
var high = checkpoints.count
while low < high {
let middle = (low + high) / 2
if checkpoints[middle].offset < offset {
low = middle + 1
} else {
high = middle
}
}
return low > 0 ? checkpoints[low - 1] : nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,45 @@ public enum SQLStatementScanner {
return result
}

// MARK: - Private
// MARK: - Resuming

// The span of a statement that had content, kept without its text so a checkpoint stays small.
struct EndedStatement: Sendable, Equatable {
let range: NSRange
let terminator: SQLStatementTerminator
let acceptsBindParameters: Bool

func located(in text: NSString) -> LocatedStatement {
LocatedStatement(
sql: text.substring(with: range),
offset: range.location,
hasContent: true,
terminator: terminator,
acceptsBindParameters: acceptsBindParameters
)
}
}

/// Walks the document once and hands each segment to `onStatement`, which returns false to stop.
///
/// Segments tile the document: a statement runs from the end of the previous one to its terminator, so it carries
/// the whitespace before it. With a `cursorPosition`, only the segment holding it is reported, or the last one
/// when the cursor sits past every terminator. Where a statement ends is the tracker's decision; this loop only
/// lexes, so a `;` inside a string, a comment or a quoted body never reaches it. A `GO` line is the exception: it
/// ends the batch, so it ends whatever statement is open whatever the tracker thinks, and is a segment of its own
/// that holds nothing to run.
private static func scan(
// A point where the scan holds no state but the last statement with content, so it can start again there.
struct Checkpoint: Sendable, Equatable {
let offset: Int
// The last offset whose text decided this boundary: an edit there or earlier moves it.
let decidedThrough: Int
let endedStatement: EndedStatement?
}

// MARK: - Scanning

// Segments tile the document: each runs from the end of the previous one to its terminator, whitespace included.
// With a cursor only the segment holding it is reported, or the last one when it sits past every terminator.
// Where a statement ends is the tracker's decision, except at a `GO` line, which ends the batch whatever the
// tracker thinks and is a segment of its own that runs nothing.
static func scan(
sql: String,
cursorPosition: Int?,
grammar: SQLLexicalGrammar,
resumingAt resume: Checkpoint? = nil,
onCheckpoint: (Checkpoint) -> Void = { _ in },
onBatchSeparator: (SQLBatchSeparator) -> Void = { _ in },
onFinish: (_ holdsOpenBlock: Bool) -> Void = { _ in },
onStatement: (LocatedStatement) -> Bool
Expand All @@ -299,34 +324,42 @@ public enum SQLStatementScanner {
let safePosition = cursorPosition.map { min(max(0, $0), length) }

var tracker = SQLStatementBoundaries.makeTracker(for: grammar)
var currentStart = 0
var currentStart = min(max(0, resume?.offset ?? 0), length)
var hasStatementContent = false
var i = 0
var i = currentStart

var lastStatementWithContent: LocatedStatement?
var lastEndedStatement = resume?.endedStatement

/// Reports the segment ending at `end` and starts the next one there. Returns false when the scan is done.
///
/// A caret on a SQL*Plus `/` line stands for the statement the slash ends, which is where a reader who has
/// just typed the slash expects `Cmd+Enter` to act. A caret on a `GO` line stands for the last statement of the
/// batch it ends, and for nothing when that batch is empty: the batch before is not the one the line ends.
// A caret on a SQL*Plus `/` line stands for the statement the slash ends, where `Cmd+Enter` is expected to act.
// A caret on a `GO` line stands for the last statement of its batch, and for nothing when the batch is empty.
func finishSegment(at end: Int, hasContent: Bool, standsForEndedStatement: Bool = false) -> Bool {
let statement = LocatedStatement(
sql: nsQuery.substring(with: NSRange(location: currentStart, length: end - currentStart)),
offset: currentStart,
hasContent: hasContent,
terminator: tracker.terminator,
acceptsBindParameters: tracker.acceptsBindParameters
)
let range = NSRange(location: currentStart, length: end - currentStart)
if hasContent {
lastStatementWithContent = statement
lastEndedStatement = EndedStatement(
range: range,
terminator: tracker.terminator,
acceptsBindParameters: tracker.acceptsBindParameters
)
}
if let cursor = safePosition {
guard cursor >= currentStart, cursor <= end else {
currentStart = end
return true
}
_ = onStatement(standsForEndedStatement ? lastStatementWithContent ?? statement : statement)
if standsForEndedStatement, let ended = lastEndedStatement {
_ = onStatement(ended.located(in: nsQuery))
return false
}
}
let statement = LocatedStatement(
sql: nsQuery.substring(with: range),
offset: currentStart,
hasContent: hasContent,
terminator: tracker.terminator,
acceptsBindParameters: tracker.acceptsBindParameters
)
if safePosition != nil {
_ = onStatement(statement)
return false
}
currentStart = end
Expand Down Expand Up @@ -361,9 +394,10 @@ public enum SQLStatementScanner {
}
let separatorEnd = NSMaxRange(separator.range)
guard finishSegment(at: separatorEnd, hasContent: false, standsForEndedStatement: true) else { return }
lastStatementWithContent = nil
lastEndedStatement = nil
tracker.reset()
hasStatementContent = false
onCheckpoint(Checkpoint(offset: separatorEnd, decidedThrough: separatorEnd, endedStatement: nil))
i = separatorEnd
continue
}
Expand Down Expand Up @@ -392,6 +426,7 @@ public enum SQLStatementScanner {
guard finishSegment(at: i + 1, hasContent: hasStatementContent) else { return }
tracker.reset()
hasStatementContent = false
onCheckpoint(Checkpoint(offset: i + 1, decidedThrough: i, endedStatement: lastEndedStatement))
i += 1
continue
}
Expand All @@ -403,6 +438,11 @@ public enum SQLStatementScanner {
guard finishSegment(at: i + 1, hasContent: false, standsForEndedStatement: true) else { return }
tracker.reset()
hasStatementContent = false
onCheckpoint(Checkpoint(
offset: i + 1,
decidedThrough: endOfSlashLine(nsQuery, at: i, length: length),
endedStatement: lastEndedStatement
))
i += 1
continue
}
Expand Down Expand Up @@ -439,11 +479,17 @@ public enum SQLStatementScanner {
before -= 1
}
guard before < 0 || text.character(at: before) == SqlLexer.newline else { return false }
let after = endOfSlashLine(text, at: offset, length: length)
return after == length || text.character(at: after) == SqlLexer.newline
}

// The last offset `isSlashLine` reads after the slash: its line break, or the end of the text.
private static func endOfSlashLine(_ text: NSString, at offset: Int, length: Int) -> Int {
var after = offset + 1
while after < length, isLineBlank(text.character(at: after)) {
after += 1
}
return after == length || text.character(at: after) == SqlLexer.newline
return after
}

private static func isLineBlank(_ character: UInt16) -> Bool {
Expand Down
Loading
Loading