Skip to content

Performance improvements for Blob #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
58 changes: 45 additions & 13 deletions SQLite/Core/Blob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,58 @@
// THE SOFTWARE.
//

public struct Blob {
import Foundation

public let bytes: [UInt8]

public init(bytes: [UInt8]) {
self.bytes = bytes
public final class Blob {

public let data: NSData
public var bytes: UnsafePointer<Void> {
return self.data.bytes
}

public init(bytes: UnsafePointer<Void>, length: Int) {
self.init(bytes: [UInt8](UnsafeBufferPointer(
start: UnsafePointer(bytes), count: length
)))
public var length: Int {
return self.data.length
}

public convenience init(bytes: [UInt8]) {
let buffer = UnsafeMutablePointer<UInt8>.alloc(bytes.count)
for idx in 0..<bytes.count {
buffer.advancedBy(idx).memory = bytes[idx]
}
let data = NSData(
bytesNoCopy: UnsafeMutablePointer<Void>(buffer),
length: bytes.count,
freeWhenDone: true
)
self.init(data: data)
}

public convenience init(bytes: UnsafePointer<Void>, length: Int) {
self.init(data: NSData(bytes: bytes, length: length))
}

public init(data: NSData) {
self.data = data
}

}

extension Blob {

public func toHex() -> String {
return bytes.map {
($0 < 16 ? "0" : "") + String($0, radix: 16, uppercase: false)
}.joinWithSeparator("")
let bytes = UnsafePointer<UInt8>(self.bytes)

var hex = ""
for idx in 0..<self.length {
let byte = bytes.advancedBy(idx).memory
if byte < 16 {
hex += "0"
}
hex += String(byte, radix: 16, uppercase: false)
}
return hex
}

}

extension Blob : CustomStringConvertible {
Expand Down
9 changes: 5 additions & 4 deletions SQLite/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ extension NSData : Value {
public class var declaredDatatype: String {
return Blob.declaredDatatype
}

public class func fromDatatypeValue(dataValue: Blob) -> NSData {
return NSData(bytes: dataValue.bytes, length: dataValue.bytes.count)
return dataValue.data
}

public var datatypeValue: Blob {
return Blob(bytes: bytes, length: length)
return Blob(data: self)
}

}


extension NSDate : Value {

public class var declaredDatatype: String {
Expand Down