mirror of https://github.com/oxen-io/session-ios
Fix database usage
parent
0ac2aaf8bb
commit
21c5875ef5
@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
@objc(LKDeviceLinkIndex)
|
||||||
|
public final class DeviceLinkIndex : NSObject {
|
||||||
|
|
||||||
|
private static let name = "loki_device_link_index"
|
||||||
|
|
||||||
|
@objc public static let masterHexEncodedPublicKey = "master_hex_encoded_public_key"
|
||||||
|
@objc public static let slaveHexEncodedPublicKey = "slave_hex_encoded_public_key"
|
||||||
|
@objc public static let isAuthorized = "is_authorized"
|
||||||
|
|
||||||
|
@objc public static let indexDatabaseExtension: YapDatabaseSecondaryIndex = {
|
||||||
|
let setup = YapDatabaseSecondaryIndexSetup()
|
||||||
|
setup.addColumn(masterHexEncodedPublicKey, with: .text)
|
||||||
|
setup.addColumn(slaveHexEncodedPublicKey, with: .text)
|
||||||
|
setup.addColumn(isAuthorized, with: .integer)
|
||||||
|
let handler = YapDatabaseSecondaryIndexHandler.withObjectBlock { _, map, _, _, object in
|
||||||
|
guard let deviceLink = object as? LokiDeviceLink else { return }
|
||||||
|
map[masterHexEncodedPublicKey] = deviceLink.master.hexEncodedPublicKey
|
||||||
|
map[slaveHexEncodedPublicKey] = deviceLink.slave.hexEncodedPublicKey
|
||||||
|
map[isAuthorized] = deviceLink.isAuthorized
|
||||||
|
}
|
||||||
|
return YapDatabaseSecondaryIndex(setup: setup, handler: handler)
|
||||||
|
}()
|
||||||
|
|
||||||
|
@objc public static var databaseExtensionName: String { return name }
|
||||||
|
|
||||||
|
@objc public static func asyncRegisterDatabaseExtensions(_ storage: OWSStorage) {
|
||||||
|
storage.register(indexDatabaseExtension, withName: name)
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc public static func getDeviceLinks(for query: YapDatabaseQuery, in transaction: YapDatabaseReadTransaction) -> [LokiDeviceLink] {
|
||||||
|
guard let ext = transaction.ext(DeviceLinkIndex.name) as? YapDatabaseSecondaryIndexTransaction else { return [] }
|
||||||
|
var result: [LokiDeviceLink] = []
|
||||||
|
ext.enumerateKeysAndObjects(matching: query) { _, _, object, _ in
|
||||||
|
guard let deviceLink = object as? LokiDeviceLink else { return }
|
||||||
|
result.append(deviceLink)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
@ -1,53 +0,0 @@
|
|||||||
//@objc(LKPairingAuthorisationsIndex)
|
|
||||||
//public final class PairingAuthorisationsIndex : NSObject {
|
|
||||||
// private static let name = "loki_index_pairing_authorisations"
|
|
||||||
//
|
|
||||||
// // Fields
|
|
||||||
// @objc public static let primaryDevicePubKey = "pairing_primary_device_pub_key"
|
|
||||||
// @objc public static let secondaryDevicePubKey = "pairing_secondary_device_pub_key"
|
|
||||||
// @objc public static let isGranted = "pairing_is_granted"
|
|
||||||
//
|
|
||||||
// // MARK: Database Extension
|
|
||||||
//
|
|
||||||
// @objc public static var indexDatabaseExtension: YapDatabaseSecondaryIndex {
|
|
||||||
// let setup = YapDatabaseSecondaryIndexSetup()
|
|
||||||
// setup.addColumn(primaryDevicePubKey, with: .text)
|
|
||||||
// setup.addColumn(secondaryDevicePubKey, with: .text)
|
|
||||||
// setup.addColumn(isGranted, with: .integer)
|
|
||||||
//
|
|
||||||
// let handler = YapDatabaseSecondaryIndexHandler.withObjectBlock { (transaction, dict, collection, key, object) in
|
|
||||||
// guard let pairing = object as? LokiPairingAuthorisation else { return }
|
|
||||||
// dict[primaryDevicePubKey] = pairing.primaryDevicePubKey
|
|
||||||
// dict[secondaryDevicePubKey] = pairing.secondaryDevicePubKey
|
|
||||||
// dict[isGranted] = pairing.isGranted
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return YapDatabaseSecondaryIndex(setup: setup, handler: handler)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @objc public static var databaseExtensionName: String {
|
|
||||||
// return name
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @objc public static func asyncRegisterDatabaseExtensions(_ storage: OWSStorage) {
|
|
||||||
// storage.register(indexDatabaseExtension, withName: name)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // MARK: Helper
|
|
||||||
//
|
|
||||||
// public static func enumeratePairingAuthorisations(with query: YapDatabaseQuery, transaction: YapDatabaseReadTransaction, block: @escaping (LokiPairingAuthorisation, UnsafeMutablePointer<ObjCBool>) -> Void) {
|
|
||||||
// let ext = transaction.ext(PairingAuthorisationsIndex.name) as? YapDatabaseSecondaryIndexTransaction
|
|
||||||
// ext?.enumerateKeysAndObjects(matching: query) { (collection, key, object, stop) in
|
|
||||||
// guard let authorisation = object as? LokiPairingAuthorisation else { return }
|
|
||||||
// block(authorisation, stop)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static func getPairingAuthorisations(with query: YapDatabaseQuery, transaction: YapDatabaseReadTransaction) -> [LokiPairingAuthorisation] {
|
|
||||||
// var authorisations = [LokiPairingAuthorisation]()
|
|
||||||
// enumeratePairingAuthorisations(with: query, transaction: transaction) { (authorisation, _) in
|
|
||||||
// authorisations.append(authorisation)
|
|
||||||
// }
|
|
||||||
// return authorisations
|
|
||||||
// }
|
|
||||||
//}
|
|
Loading…
Reference in New Issue