From 4b049eefdaa385b58ccf119b618027a11ff6ce7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 16 Sep 2015 21:53:35 +0200 Subject: [PATCH] Row.metalFetch(), the base for future Int.metalFetch() and Person.metalFetch() --- .../Performance/FetchPerformanceTests.swift | 22 +++- GRDB/Core/Row.swift | 107 +++++++++++++++++- GRDB/Core/SelectStatement.swift | 2 +- 3 files changed, 124 insertions(+), 7 deletions(-) diff --git a/GRDB Tests/Performance/FetchPerformanceTests.swift b/GRDB Tests/Performance/FetchPerformanceTests.swift index 3d17f23ec..80e29ca9b 100644 --- a/GRDB Tests/Performance/FetchPerformanceTests.swift +++ b/GRDB Tests/Performance/FetchPerformanceTests.swift @@ -18,7 +18,7 @@ class FetchPerformanceTests: XCTestCase { // // } - func testMetalFetchPerformance() { + func testMetalRowFetchPerformance() { let databasePath = NSBundle(forClass: self.dynamicType).pathForResource("FetchPerformanceTests", ofType: "sqlite")! let dbQueue = try! DatabaseQueue(path: databasePath) @@ -38,6 +38,26 @@ class FetchPerformanceTests: XCTestCase { XCTAssertEqual(sum, 4999990) } + func testRowMetalFetchPerformance() { + let databasePath = NSBundle(forClass: self.dynamicType).pathForResource("FetchPerformanceTests", ofType: "sqlite")! + let dbQueue = try! DatabaseQueue(path: databasePath) + + var sum: Int64 = 0 + self.measureBlock { + dbQueue.inDatabase { db in + for row in Row.metalFetch(db, "SELECT * FROM items") { + let i0: Int64 = row.value(atIndex: 0) + let i1: Int64 = row.value(atIndex: 1) + let i2: Int64 = row.value(atIndex: 2) + let i3: Int64 = row.value(atIndex: 3) + let i4: Int64 = row.value(atIndex: 4) + sum += i0 + i1 + i2 + i3 + i4 + } + } + } + XCTAssertEqual(sum, 4999990) + } + func testRegularFetchPerformance() { let databasePath = NSBundle(forClass: self.dynamicType).pathForResource("FetchPerformanceTests", ofType: "sqlite")! let dbQueue = try! DatabaseQueue(path: databasePath) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 4bfc9b64d..2d316e787 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -92,10 +92,34 @@ public struct Row: CollectionType { return Value.fromDatabaseValue(impl.databaseValue(atIndex: index)) } + public func value>(atIndex index: Int) -> Value? { + let sqliteStatement = impl.sqliteStatement + guard sqliteStatement != nil else { + return Value.fromDatabaseValue(impl.databaseValue(atIndex: index)) + } + + // Metal + if sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL { + return nil + } else { + return Value(sqliteStatement: sqliteStatement, index: Int32(index)) + } + } + public func value(atIndex index: Int) -> Value { return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))! } + public func value>(atIndex index: Int) -> Value { + let sqliteStatement = impl.sqliteStatement + guard sqliteStatement != nil else { + return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))! + } + + // Metal + return Value(sqliteStatement: sqliteStatement, index: Int32(index)) + } + /** Returns the value for the given column. @@ -162,7 +186,12 @@ public struct Row: CollectionType { */ public func value(named columnName: String) -> Value? { let index = impl.indexForColumn(named: columnName)! - return impl.databaseValue(atIndex: index).value() + return Value.fromDatabaseValue(impl.databaseValue(atIndex: index)) + } + + public func value(named columnName: String) -> Value { + let index = impl.indexForColumn(named: columnName)! + return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))! } @@ -302,6 +331,38 @@ public struct Row: CollectionType { // MARK: - Fetching From Database + /** + Returns a row generator that provides fast but unsafe access to database + values: + + for row in MetalRow.fetch(db, "SELECT id, name FROM persons") { + let id = row.int64(atIndex: 0) + let name = row.string(atIndex: 1) + } + + The returned generator is *unsafe* because it must be used with extra care, + and GRDB.swift will not prevent invalid usage. + + - It MUST be generated and iterated in the database queue. + + - It MUST be iterated right away. Do not reserve it, do not wrap it in an + Array. + + Granted with those constraints, the unsafe generator grants extra speed for + the typed row accessors Row.int64(atIndex:), Row.string(atIndex:), etc. + + - parameter db: A Database. + - parameter sql: An SQL query. + - parameter arguments: Optional statement arguments. + - returns: A lazy sequence of rows. + */ + public static func metalFetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnyGenerator { + let statement = db.selectStatement(sql) + return statement.metalGenerate(arguments: arguments) { + Row(metalStatement: statement) + } + } + /** Fetches a lazy sequence of rows. @@ -377,6 +438,10 @@ public struct Row: CollectionType { self.impl = StatementRowImpl(statement: statement) } + init(metalStatement statement: SelectStatement) { + self.impl = MetalRowImpl(sqliteStatement: statement.sqliteStatement) + } + // MARK: - DictionaryRowImpl @@ -444,6 +509,38 @@ public struct Row: CollectionType { return columnNames.indexOf { $0.lowercaseString == lowercaseName } } } + + + // MARK: - MetalRowImpl + + /// See Row.init(metalStatement:) + private struct MetalRowImpl : RowImpl { + let sqliteStatement: SQLiteStatement + + var count: Int { + return Int(sqlite3_column_count(sqliteStatement)) + } + + func databaseValue(atIndex index: Int) -> DatabaseValue { + return DatabaseValue(sqliteStatement: sqliteStatement, index: index) + } + + func columnName(atIndex index: Int) -> String { + return String.fromCString(sqlite3_column_name(sqliteStatement, Int32(index)))! + } + + // This method MUST be case-insensitive. + func indexForColumn(named name: String) -> Int? { + let lowercaseName = name.lowercaseString + for i in 0.. Bool { public struct MetalRow { let sqliteStatement: SQLiteStatement - init(statement: SelectStatement) { - self.sqliteStatement = statement.sqliteStatement + init(sqliteStatement: SQLiteStatement) { + self.sqliteStatement = sqliteStatement } @@ -542,8 +639,8 @@ public struct MetalRow { */ public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnyGenerator { let statement = db.selectStatement(sql) - return statement.unsafeGenerate(arguments: arguments) { - MetalRow(statement: statement) + return statement.metalGenerate(arguments: arguments) { + MetalRow(sqliteStatement: statement.sqliteStatement) } } diff --git a/GRDB/Core/SelectStatement.swift b/GRDB/Core/SelectStatement.swift index b0c278a60..1a2d77c37 100644 --- a/GRDB/Core/SelectStatement.swift +++ b/GRDB/Core/SelectStatement.swift @@ -67,7 +67,7 @@ public final class SelectStatement : Statement { *read* function is called, the statement has just read a row. - returns: A lazy sequence. */ - func unsafeGenerate(arguments arguments: StatementArguments?, read: () -> T) -> AnyGenerator { + func metalGenerate(arguments arguments: StatementArguments?, read: () -> T) -> AnyGenerator { if let arguments = arguments { self.arguments = arguments }