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
60 changes: 51 additions & 9 deletions apple/Packages/TableProCore/Sources/TableProModels/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,68 @@ public extension Row {
}
}
}

// A one-line list never needs more than a preview, even of a value the cell holds whole.
func displayValues(maxBytes: Int = Cell.previewByteLimit) -> [String?] {
cells.map { cell -> String? in
switch cell {
case .null:
return nil
case .text(let value):
return Cell.preview(of: value, maxBytes: maxBytes)
case .truncatedText, .binary:
return cell.displayString
}
}
}
}

public extension Cell {
static let previewByteLimit = 4_096

// For text whose kind only the column's declared type tells, such as a row read back as strings.
static func from(legacyValue value: String?, columnTypeName: String?, options: StreamOptions, ref: CellRef? = nil) -> Cell {
guard let value else { return .null }
let bytes = value.utf8.count
let upper = (columnTypeName ?? "").uppercased()
let isBinary = upper.contains("BLOB") || upper.contains("BYTEA") || upper.contains("BINARY") || upper.contains("VARBINARY") || upper.contains("IMAGE")
if let columnTypeName, holdsBytes(typeName: columnTypeName) {
return .binary(byteCount: value.utf8.count, ref: ref)
}
return from(text: value, options: options, ref: ref)
}

if isBinary {
return .binary(byteCount: bytes, ref: ref)
// For a value the driver knows is text, whatever its column's type is called.
static func from(text value: String, options: StreamOptions, ref: CellRef? = nil) -> Cell {
let bytes = value.utf8.count
guard bytes > options.textTruncationBytes else { return .text(value) }
// A value with a ref can be read again by its key, so it never spends the budget.
if ref == nil, let budget = options.textBudget, budget.admit(bytes) {
return .text(value)
}
return .truncatedText(prefix: preview(of: value, maxBytes: options.textTruncationBytes), totalBytes: bytes, ref: ref)
}

if bytes > options.textTruncationBytes {
let prefixSlice = value.prefix(options.textTruncationBytes)
return .truncatedText(prefix: String(prefixSlice), totalBytes: bytes, ref: ref)
// At most `maxBytes` of UTF-8, ending on a Character boundary.
static func preview(of value: String, maxBytes: Int) -> String {
guard value.utf8.count > maxBytes else { return value }
var end = value.startIndex
var used = 0
while end < value.endIndex {
let next = value.index(after: end)
used += value.utf8.distance(from: end, to: next)
guard used <= maxBytes else { break }
end = next
}
return String(value[..<end])
}

return .text(value)
// Whole names only: Oracle's BINARY_DOUBLE, BINARY_FLOAT and BINARY_INTEGER hold numbers.
static func holdsBytes(typeName: String) -> Bool {
let base = typeName.prefix { $0 != "(" }.trimmingCharacters(in: .whitespaces).uppercased()
return byteTypeNames.contains(base)
}

private static let byteTypeNames: Set<String> = [
"BLOB", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB", "BYTEA", "BINARY", "VARBINARY", "IMAGE"
]
}

private let byteCountFormatter = OSAllocatedUnfairLock(uncheckedState: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import os

public enum StreamElement: Sendable {
case columns([ColumnInfo])
Expand All @@ -20,22 +21,55 @@ public struct StreamOptions: Sendable {
public let inlineBinary: Bool
public let maxRows: Int
public let lazyContext: LazyContext?
// Long text with no ref stays whole while this admits it; nil cuts every long value.
public var textBudget: TextBudget?

public init(
textTruncationBytes: Int = 4_096,
textTruncationBytes: Int = Cell.previewByteLimit,
inlineBinary: Bool = false,
maxRows: Int = 100_000,
lazyContext: LazyContext? = nil
lazyContext: LazyContext? = nil,
textBudget: TextBudget? = nil
) {
self.textTruncationBytes = textTruncationBytes
self.inlineBinary = inlineBinary
self.maxRows = maxRows
self.lazyContext = lazyContext
self.textBudget = textBudget
}

public static let `default` = StreamOptions()
}

// Shared by every cell of one request, and read from whatever thread the driver decodes on.
public final class TextBudget: Sendable {
private struct State: Sendable {
var remaining: Int
var refused = false
}

private let state: OSAllocatedUnfairLock<State>

public init(bytes: Int) {
state = OSAllocatedUnfairLock(initialState: State(remaining: max(0, bytes)))
}

public func admit(_ bytes: Int) -> Bool {
state.withLock { state in
guard bytes <= state.remaining else {
state.refused = true
return false
}
state.remaining -= bytes
return true
}
}

public var hasRefused: Bool {
state.withLock { $0.refused }
}
}

public struct LazyContext: Sendable {
public let table: String
public let primaryKeyColumns: [String]
Expand Down
133 changes: 133 additions & 0 deletions apple/Packages/TableProCore/Tests/TableProModelsTests/CellTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import Foundation
import Testing

@testable import TableProModels

struct CellTests {
private static let long = String(repeating: "a", count: 10_000)

@Test("A budget keeps long values whole until it is spent, then the next keeps only a preview")
func budgetKeepsWholeUntilSpent() {
let budget = TextBudget(bytes: 15_000)
let options = StreamOptions(textBudget: budget)

guard case .text(let kept) = Cell.from(text: Self.long, options: options) else {
Issue.record("the first long value should be kept whole")
return
}
#expect(kept == Self.long)
#expect(!budget.hasRefused)

guard case .truncatedText(let prefix, let totalBytes, let ref) = Cell.from(text: Self.long, options: options) else {
Issue.record("the second long value should be cut")
return
}
#expect(prefix.utf8.count == Cell.previewByteLimit)
#expect(totalBytes == 10_000)
#expect(ref == nil)
#expect(budget.hasRefused)
}

@Test("A value within the cut spends nothing")
func shortValuesSpendNothing() {
let budget = TextBudget(bytes: 10_000)
let options = StreamOptions(textBudget: budget)
for _ in 0 ..< 10 {
_ = Cell.from(text: String(repeating: "b", count: Cell.previewByteLimit), options: options)
}

guard case .text = Cell.from(text: Self.long, options: options) else {
Issue.record("the long value should still fit the budget")
return
}
#expect(!budget.hasRefused)
}

@Test("Without a budget every long value is cut")
func noBudgetCuts() {
guard case .truncatedText(_, let totalBytes, .none) = Cell.from(text: Self.long, options: .default) else {
Issue.record("the long value should be cut")
return
}
#expect(totalBytes == 10_000)
}

@Test("A value with a ref stays cut and loadable, and spends none of the budget")
func refValuesSpendNothing() {
let budget = TextBudget(bytes: 10_000)
let options = StreamOptions(textBudget: budget)
let ref = CellRef(table: "notes", column: "body", primaryKey: [PrimaryKeyComponent(column: "id", value: "1")])

guard case .truncatedText(_, _, let kept?) = Cell.from(text: Self.long, options: options, ref: ref) else {
Issue.record("a value with a ref should stay cut")
return
}
#expect(kept == ref)
guard case .text = Cell.from(text: Self.long, options: options) else {
Issue.record("the budget should be untouched")
return
}
}

@Test(
"The preview holds at most the cut in bytes and ends on a whole character",
arguments: ["界", "👍🏽", "e\u{301}", "x"]
)
func previewIsBoundedInBytes(_ unit: String) {
let value = String(repeating: unit, count: 5_000)

let preview = Cell.preview(of: value, maxBytes: 4_096)

#expect(preview.utf8.count <= 4_096)
#expect(preview.utf8.count > 4_096 - unit.utf8.count)
#expect(preview == String(repeating: unit, count: preview.count))
}

@Test("A cut value's preview is bounded in bytes, not characters")
func cutPreviewIsBoundedInBytes() {
let value = String(repeating: "界", count: 5_000)

guard case .truncatedText(let prefix, let totalBytes, _) = Cell.from(text: value, options: .default) else {
Issue.record("the value should be cut")
return
}
#expect(prefix.utf8.count <= Cell.previewByteLimit)
#expect(totalBytes == 15_000)
}

@Test(
"Oracle's BINARY_DOUBLE, BINARY_FLOAT and BINARY_INTEGER hold numbers, not bytes",
arguments: ["binary_double", "BINARY_FLOAT", "binary_integer"]
)
func oracleBinaryNumbersStayText(_ typeName: String) {
guard case .text(let value) = Cell.from(legacyValue: "1.5", columnTypeName: typeName, options: .default) else {
Issue.record("\(typeName) should read as text")
return
}
#expect(value == "1.5")
}

@Test(
"A byte type, with or without a length, reads as binary",
arguments: ["BLOB", "bytea", "VARBINARY(16)", "binary(16)", "LONGBLOB", "image", "BLOB(3)"]
)
func byteTypesAreBinary(_ typeName: String) {
guard case .binary(let byteCount, _) = Cell.from(legacyValue: "0A0B", columnTypeName: typeName, options: .default) else {
Issue.record("\(typeName) should read as binary")
return
}
#expect(byteCount == 4)
}

@Test("Display values cut a long value the cell holds whole")
func displayValuesCutKeptText() {
let row = Row(cells: [.text(Self.long), .null, .truncatedText(prefix: "a", totalBytes: 10_000, ref: nil)])

let shown = row.displayValues()

#expect(shown[0]?.utf8.count == Cell.previewByteLimit)
#expect(shown[1] == nil)
#expect(shown[2] == row.cells[2].displayString)
#expect(row.legacyValues[0] == Self.long)
}
}
4 changes: 4 additions & 0 deletions apple/TableProMobile/TableProMobile/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class AppState {
let backgroundRelease: BackgroundReleaseCoordinator
let queryActivities = QueryActivityController()
let catalogChanges = CatalogChangeBoard()
let sessionModels = ConnectionSessionModels()
let syncCoordinator: IOSSyncCoordinator

@ObservationIgnored private var automaticPresentationOwner: UUID?
Expand Down Expand Up @@ -186,6 +187,7 @@ final class AppState {

private func sessionEnded(_ connectionId: UUID, driver: any DatabaseDriver) {
sessionTransactions.sessionEnded(connectionId, driver: driver)
sessionModels.existing(connectionId)?.sessionEnded(driver)
bumpSessionRevision(connectionId)
}

Expand Down Expand Up @@ -225,6 +227,7 @@ final class AppState {
let removedIds = Set(connections.map(\.id)).subtracting(merged.map(\.id))
guard persist(connections: merged, onlyIfSaved: true) else { return false }
localState.purge(removedIds)
sessionModels.purge(removedIds)
savedQueries.removeRecords(ofConnections: removedIds, tombstoning: false)
publishLibrary()
return true
Expand Down Expand Up @@ -441,6 +444,7 @@ final class AppState {
guard !removed.isEmpty else { return }
persist(connections: connections.filter { !ids.contains($0.id) })
localState.purge(Set(removed.map(\.id)))
sessionModels.purge(Set(removed.map(\.id)))
let syncedIds = Set(removed.filter(\.participatesInSync).map(\.id))
savedQueries.removeRecords(ofConnections: syncedIds, tombstoning: true)
savedQueries.removeRecords(ofConnections: Set(removed.map(\.id)).subtracting(syncedIds), tombstoning: false)
Expand Down
Loading
Loading