NSData is now the type responsible for SQLite blobs.

This commit is contained in:
Gwendal Roué committed 2015-10-13 19:52:40 +02:00
1 parent 990c34e1d9
commit 5c1e320d97
12 files changed
+647 -137

No files matched your search

+5
View File
@@ -7,6 +7,11 @@ Release Notes
- `Row()` initializes an empty row.
**Breaking Change**
- NSData is now the canonical type for blobs. The former intermediate `Blob` type has been removed.
- `DatabaseValue.dataNoCopy()` has turned useless, and has been removed.
## 0.22.0
@@ -0,0 +1,56 @@
import XCTest
@testable import GRDB
class NSDataMemoryTests: GRDBTestCase {
func testMemoryBehavior() {
assertNoError {
try dbQueue.inDatabase { db in
try db.execute("CREATE TABLE datas (data BLOB)")
let data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
try db.execute("INSERT INTO datas (data) VALUES (?)", arguments: [data])
for row in Row.fetch(db, "SELECT * FROM datas") {
let sqliteStatement = row.sqliteStatement
let sqliteBytes = sqlite3_column_blob(sqliteStatement, 0)
do {
// This data should be copied:
let copiedData: NSData = row.value(atIndex: 0)
XCTAssertNotEqual(copiedData.bytes, sqliteBytes)
XCTAssertEqual(copiedData, data)
}
do {
// This data should not be copied
let nonCopiedData = row.dataNoCopy(atIndex: 0)!
XCTAssertEqual(nonCopiedData.bytes, sqliteBytes)
XCTAssertEqual(nonCopiedData, data)
}
}
let row = Row.fetchOne(db, "SELECT * FROM datas")!
let databaseValue = row.first!.1
switch databaseValue.storage {
case .Blob(let data):
do {
// This data should not be copied:
let nonCopiedData: NSData = row.value(atIndex: 0)
XCTAssertEqual(nonCopiedData.bytes, data.bytes)
XCTAssertEqual(nonCopiedData, data)
}
do {
// This data should not be copied:
let nonCopiedData = row.dataNoCopy(atIndex: 0)!
XCTAssertEqual(nonCopiedData.bytes, data.bytes)
XCTAssertEqual(nonCopiedData, data)
}
default:
XCTFail("Not a blob")
}
}
}
}
}
@@ -8,36 +8,4 @@ class NSDataTests: GRDBTestCase {
let databaseValue = DatabaseValue(data: NSData())
XCTAssertEqual(databaseValue, DatabaseValue.Null)
}
func testMemoryBehavior() {
assertNoError {
try dbQueue.inDatabase { db in
try db.execute("CREATE TABLE datas (data BLOB)")
let data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
try db.execute("INSERT INTO datas (data) VALUES (?)", arguments: [data])
// This blob should not copy SQLite data. This can not be tested here: test it at runtime with a breakpoint.
for row in Row.fetch(db, "SELECT * FROM datas") {
// This data should be copied:
let copiedData: NSData = row.value(atIndex: 0)
XCTAssertEqual(copiedData, data)
// This data should not be copied, and extraced raw from the sqliteStatement
let nonCopiedData = row.dataNoCopy(atIndex: 0)
XCTAssertEqual(nonCopiedData, data)
}
let row = Row.fetchOne(db, "SELECT * FROM datas")!
// This data should not be copied:
let nonCopiedData1: NSData = row.value(atIndex: 0)
XCTAssertEqual(nonCopiedData1, data)
// This data should not be copied:
let nonCopiedData2 = row.dataNoCopy(atIndex: 0)
XCTAssertEqual(nonCopiedData2, data)
}
}
}
}
File diff suppressed because it is too large. Load diff
+12 -14
View File
@@ -25,8 +25,6 @@
565D5D741BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565D5D731BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift */; settings = {ASSET_TAGS = (); }; };
565D5D751BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565D5D731BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift */; settings = {ASSET_TAGS = (); }; };
5683C2691B4D445E00296494 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5683C2681B4D445E00296494 /* libsqlite3.dylib */; };
56997A8E1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56997A8D1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift */; };
56997A8F1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56997A8D1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift */; };
56A238021B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */; settings = {ASSET_TAGS = (); }; };
56A238031B9C6A650082EB20 /* DatabaseDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */; settings = {ASSET_TAGS = (); }; };
56A238051B9C6D670082EB20 /* NSDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A238041B9C6D670082EB20 /* NSDate.swift */; settings = {ASSET_TAGS = (); }; };
@@ -175,6 +173,10 @@
56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */; settings = {ASSET_TAGS = (); }; };
56E8CE101BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E8CE0F1BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift */; settings = {ASSET_TAGS = (); }; };
56E8CE111BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E8CE0F1BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift */; settings = {ASSET_TAGS = (); }; };
56EB0AAF1BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EB0AAE1BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift */; settings = {ASSET_TAGS = (); }; };
56EB0AB01BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EB0AAE1BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift */; settings = {ASSET_TAGS = (); }; };
56EB0AB21BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */; settings = {ASSET_TAGS = (); }; };
56EB0AB31BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */; settings = {ASSET_TAGS = (); }; };
56EE573D1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EE573C1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift */; settings = {ASSET_TAGS = (); }; };
56EE573E1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EE573C1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift */; settings = {ASSET_TAGS = (); }; };
56EE57401BB31DBA007A6A95 /* GRDBPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56BB86121BA9886D001F9168 /* GRDBPerformanceTests.swift */; settings = {ASSET_TAGS = (); }; };
@@ -291,7 +293,6 @@
565D5D701BBC694D00DC9BD4 /* Row+FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+FoundationTests.swift"; sourceTree = "<group>"; };
565D5D731BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StatementArguments+FoundationTests.swift"; sourceTree = "<group>"; };
5683C2681B4D445E00296494 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.dylib; sourceTree = DEVELOPER_DIR; };
56997A8D1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseErrorFromStatementCreationTests.swift; sourceTree = "<group>"; };
56A238011B9C6A650082EB20 /* DatabaseDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDateComponents.swift; sourceTree = "<group>"; };
56A238041B9C6D670082EB20 /* NSDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDate.swift; sourceTree = "<group>"; };
56A238081B9C6DD20082EB20 /* StandardLibraryTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardLibraryTypes.swift; sourceTree = "<group>"; };
@@ -379,6 +380,8 @@
56E5D7F91B4D422D00430942 /* GRDBTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
56E8CE0C1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleFetchTests.swift; sourceTree = "<group>"; };
56E8CE0F1BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStatementConvertibleFetchTests.swift; sourceTree = "<group>"; };
56EB0AAE1BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseErrorFromStatementCreationTests.swift; sourceTree = "<group>"; };
56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDataMemoryTests.swift; sourceTree = "<group>"; };
56EE573C1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStatementConversionTests.swift; sourceTree = "<group>"; };
56F0B98D1B6001C600A2F135 /* NSDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponentsTests.swift; sourceTree = "<group>"; };
56F0B98E1B6001C600A2F135 /* NSDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateTests.swift; sourceTree = "<group>"; };
@@ -454,7 +457,8 @@
569978D31B539038005EBEED /* Private */ = {
isa = PBXGroup;
children = (
56997A8C1B5390D4005EBEED /* DatabaseError */,
56EB0AAE1BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift */,
56EB0AB11BCD787300A3DC55 /* NSDataMemoryTests.swift */,
);
path = Private;
sourceTree = "<group>";
@@ -472,14 +476,6 @@
path = Public;
sourceTree = "<group>";
};
56997A8C1B5390D4005EBEED /* DatabaseError */ = {
isa = PBXGroup;
children = (
56997A8D1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift */,
);
path = DatabaseError;
sourceTree = "<group>";
};
56A238001B9C6A1E0082EB20 /* Foundation */ = {
isa = PBXGroup;
children = (
@@ -1126,6 +1122,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
56EB0AB01BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift in Sources */,
56A238601B9C74A90082EB20 /* RecordDescriptionTests.swift in Sources */,
56A238561B9C74A90082EB20 /* PrimaryKeyNoneTests.swift in Sources */,
56A238501B9C74A90082EB20 /* MinimalPrimaryKeyRowIDTests.swift in Sources */,
@@ -1150,6 +1147,7 @@
56FDECE31BB32DFD009AD709 /* MetalRowTests.swift in Sources */,
56BB862A1BA988F2001F9168 /* FMDatabaseQueue.m in Sources */,
56A238681B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
56EB0AB31BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */,
565B0FFC1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */,
56A2385E1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */,
56FC78691BBAEB8C00CA1285 /* ManagedDataControllerTests.swift in Sources */,
@@ -1167,7 +1165,6 @@
56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */,
56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */,
56A2385C1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
56997A8F1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift in Sources */,
56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
56EE57401BB31DBA007A6A95 /* GRDBPerformanceTests.swift in Sources */,
56A2383C1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
@@ -1188,6 +1185,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
56EB0AAF1BCD785C00A3DC55 /* DatabaseErrorFromStatementCreationTests.swift in Sources */,
5614DEF81BAA1B43003163B3 /* DatabaseValueConversionTests.swift in Sources */,
56A2385F1B9C74A90082EB20 /* RecordDescriptionTests.swift in Sources */,
56A238551B9C74A90082EB20 /* PrimaryKeyNoneTests.swift in Sources */,
@@ -1212,6 +1210,7 @@
56A238671B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */,
56A2385D1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */,
56A238431B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */,
56EB0AB21BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */,
565B0FFB1BBC80720098DE03 /* RowConvertible+FoundationTests.swift in Sources */,
56BB86271BA988F2001F9168 /* FMDatabasePool.m in Sources */,
56FC78681BBAEB8C00CA1285 /* ManagedDataControllerTests.swift in Sources */,
@@ -1226,7 +1225,6 @@
56A2385B1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
565B0FEF1BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */,
56DAC70E1BB504400010FE01 /* FMDBPerformanceTests.swift in Sources */,
56997A8E1B5390D4005EBEED /* DatabaseErrorFromStatementCreationTests.swift in Sources */,
56A2383F1B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
56E8CE0D1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */,
56A2383B1B9C74A90082EB20 /* DatabaseErrorTests.swift in Sources */,
+2 -2
View File
@@ -39,7 +39,7 @@ public struct DatabaseValue : Equatable {
}
/**
Returns a DatabaseValue storing a Blob.
Returns a DatabaseValue storing NSData.
*/
public init(data: NSData) {
if data.length == 0 {
@@ -146,7 +146,7 @@ public struct DatabaseValue : Equatable {
/// The TEXT storage class, wrapping a String.
case String(Swift.String)
/// The BLOB storage class, wrapping a Blob.
/// The BLOB storage class, wrapping NSData.
case Blob(NSData)
init(sqliteStatement: SQLiteStatement, index: Int) {
+2 -2
View File
@@ -75,7 +75,7 @@ public final class Row: CollectionType {
righmost column.
If not nil (for the database NULL), its type is guaranteed to be one of the
following: Int64, Double, String, and Blob.
following: Int64, Double, String, and NSData.
let value = row.value(atIndex: 0)
@@ -209,7 +209,7 @@ public final class Row: CollectionType {
Returns the value for the given column.
If not nil (for the database NULL), its type is guaranteed to be one of the
following: Int64, Double, String, and Blob.
following: Int64, Double, String, and NSData.
let value = row.value(named: "name")
+2 -2
View File
@@ -135,8 +135,8 @@ public class Statement {
code = sqlite3_bind_double(sqliteStatement, Int32(index), double)
case .String(let text):
code = sqlite3_bind_text(sqliteStatement, Int32(index), text, -1, SQLITE_TRANSIENT)
case .Blob(let blob):
code = sqlite3_bind_blob(sqliteStatement, Int32(index), blob.bytes, Int32(blob.length), SQLITE_TRANSIENT)
case .Blob(let data):
code = sqlite3_bind_blob(sqliteStatement, Int32(index), data.bytes, Int32(data.length), SQLITE_TRANSIENT)
}
if code != SQLITE_OK {
+3 -2
View File
@@ -8,7 +8,8 @@ extension NSData : DatabaseValueConvertible {
/**
Returns an NSData initialized from *databaseValue*, if it contains a Blob.
The data is *copied*.
Whether the data is copied or not depends on the behavior of
`Self.init(data: NSData)`. For NSData itself, the data is *not copied*.
- parameter databaseValue: A DatabaseValue.
- returns: An optional NSData.
@@ -16,7 +17,7 @@ extension NSData : DatabaseValueConvertible {
public static func fromDatabaseValue(databaseValue: DatabaseValue) -> Self? {
switch databaseValue.storage {
case .Blob(let data):
return self.init(data: data)
return self.init(data: data) // When Self is NSData, the buffer is not copied.
default:
return nil
}
+12 -20
View File
@@ -447,26 +447,21 @@ NSData.fetchOne(db, "SELECT ...") // NSData?
Yet, when extracting NSData from a row, **you have the opportunity to save memory by not copying the data fetched by SQLite**:
```swift
// When the "data" column is know to be there:
let notCopiedData = row.dataNoCopy(named: "data") // NSData?
**The most memory-efficient way** to consume database blobs is the following:
// When the column `data` may not be there:
if row.hasColumn("data") {
let notCopiedData = row.dataNoCopy(named: "data") // NSData?
```swift
for row in Row.fetch(db, "SELECT data, ...") {
let data = row.dataNoCopy(named: "data") // NSData?
// When the column `data` may not be there:
if row.hasColumn("data") {
let data = row.dataNoCopy(named: "data") // NSData?
}
}
```
In this case, make sure that you do not use the non-copied data longer than the row's lifetime.
Unless you want to save data for later use, **the most memory-efficient way** to consume database blobs is the following:
```swift
for row in Row.fetch(db, "SELECT data, ...") {
let data = row.dataNoCopy(named: "data")
}
```
Compare with the **anti-patterns** below:
```swift
@@ -474,10 +469,10 @@ for row in Row.fetch(db, "SELECT data, ...") {
// Data is copied, row after row:
let data: NSData = row.value(named: "data")
// Data is copied, row after row (prefer row.hasColumn("data") in this case):
// Data is copied, row after row:
if let databaseValue = row["data"] {
// Too late to do the right thing:
let data = databaseValue.dataNoCopy
let data: NSData = databaseValue.value()
}
}
@@ -485,9 +480,6 @@ for row in Row.fetch(db, "SELECT data, ...") {
for row in Row.fetchAll(db, "SELECT data, ...") {
// Too late to do the right thing:
let data = row.dataNoCopy(named: "data")
// This data has been copied twice:
let data: NSData = row.value(named: "data")
}
```
@@ -669,7 +661,7 @@ public protocol DatabaseValueConvertible {
All types that adopt this protocol can be used wherever the built-in types `Int`, `String`, etc. are used. without any limitation or caveat. Those built-in types actually adopt it.
The `databaseValue` property returns [DatabaseValue](GRDB/Core/DatabaseValue.swift), a type that wraps the five types supported by SQLite: NULL, Int64, Double, String and Blob.
The `databaseValue` property returns [DatabaseValue](GRDB/Core/DatabaseValue.swift), a type that wraps the five types supported by SQLite: NULL, Int64, Double, String and NSData.
The `fromDatabaseValue()` factory method returns an instance of your custom type, if the databaseValue contains a suitable value.
-1
View File
@@ -7,7 +7,6 @@
- [ ] Provide some sample code for external data storage (investigate UpdateStatement didCommit + didRollback hooks)
- [ ] `IN (?)` sql snippet, with an array argument.
- [ ] Investigate the invalidation of RowSequence.
- [ ] Criticize the Blob type (couldn't we just use NSData?)
Not sure: