From f20a270fcac8407da097264ef10f1da9ecf4dae2 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 16 Mar 2017 00:22:34 +0300 Subject: [PATCH] no message --- Postbox/ItemCollection.swift | 29 +++++++++++++++++++++++++++ Postbox/ItemCollectionInfosView.swift | 10 +++++---- Postbox/ItemCollectionItemTable.swift | 10 +++++++++ Postbox/Peer.swift | 2 -- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Postbox/ItemCollection.swift b/Postbox/ItemCollection.swift index b2f5673e58..fc05a634cb 100644 --- a/Postbox/ItemCollection.swift +++ b/Postbox/ItemCollection.swift @@ -27,6 +27,35 @@ public struct ItemCollectionId: Comparable, Hashable { public var hashValue: Int { return self.id.hashValue } + + public static func encodeArrayToBuffer(_ array: [ItemCollectionId], buffer: WriteBuffer) { + var length: Int32 = Int32(array.count) + buffer.write(&length, offset: 0, length: 4) + for id in array { + var idNamespace = id.namespace + buffer.write(&idNamespace, offset: 0, length: 4) + var idId = id.id + buffer.write(&idId, offset: 0, length: 8) + } + } + + public static func decodeArrayFromBuffer(_ buffer: ReadBuffer) -> [ItemCollectionId] { + var length: Int32 = 0 + memcpy(&length, buffer.memory, 4) + buffer.offset += 4 + var i = 0 + var array: [ItemCollectionId] = [] + array.reserveCapacity(Int(length)) + while i < Int(length) { + var idNamespace: Int32 = 0 + buffer.read(&idNamespace, offset: 0, length: 4) + var idId: Int64 = 0 + buffer.read(&idId, offset: 0, length: 8) + array.append(ItemCollectionId(namespace: idNamespace, id: idId)) + i += 1 + } + return array + } } public protocol ItemCollectionInfo: Coding { diff --git a/Postbox/ItemCollectionInfosView.swift b/Postbox/ItemCollectionInfosView.swift index b9ff98ae4b..922440425f 100644 --- a/Postbox/ItemCollectionInfosView.swift +++ b/Postbox/ItemCollectionInfosView.swift @@ -3,11 +3,13 @@ import Foundation public final class ItemCollectionInfoEntry { public let id: ItemCollectionId public let info: ItemCollectionInfo + public let count: Int32 public let firstItem: ItemCollectionItem? - init(id: ItemCollectionId, info: ItemCollectionInfo, firstItem: ItemCollectionItem?) { + init(id: ItemCollectionId, info: ItemCollectionInfo, count: Int32, firstItem: ItemCollectionItem?) { self.id = id self.info = info + self.count = count self.firstItem = firstItem } } @@ -25,7 +27,7 @@ final class MutableItemCollectionInfosView: MutablePostboxView { var entries: [ItemCollectionInfoEntry] = [] for (_, id, info) in infos { let firstItem = postbox.itemCollectionItemTable.higherItems(collectionId: id, itemIndex: ItemCollectionItemIndex.lowerBound, count: 1).first - entries.append(ItemCollectionInfoEntry(id: id, info: info, firstItem: firstItem)) + entries.append(ItemCollectionInfoEntry(id: id, info: info, count: postbox.itemCollectionItemTable.itemCount(collectionId: id), firstItem: firstItem)) } entriesByNamespace[namespace] = entries } @@ -64,7 +66,7 @@ final class MutableItemCollectionInfosView: MutablePostboxView { var entries: [ItemCollectionInfoEntry] = [] for (_, id, info) in infos { let firstItem = postbox.itemCollectionItemTable.higherItems(collectionId: id, itemIndex: ItemCollectionItemIndex.lowerBound, count: 1).first - entries.append(ItemCollectionInfoEntry(id: id, info: info, firstItem: firstItem)) + entries.append(ItemCollectionInfoEntry(id: id, info: info, count: postbox.itemCollectionItemTable.itemCount(collectionId: id), firstItem: firstItem)) } entriesByNamespace[namespace] = entries } @@ -75,7 +77,7 @@ final class MutableItemCollectionInfosView: MutablePostboxView { if reloadTopItemCollectionIds.contains(entries[i].id) { updated = true let firstItem = postbox.itemCollectionItemTable.higherItems(collectionId: entries[i].id, itemIndex: ItemCollectionItemIndex.lowerBound, count: 1).first - self.entriesByNamespace[namespace]![i] = ItemCollectionInfoEntry(id: entries[i].id, info: entries[i].info, firstItem: firstItem) + self.entriesByNamespace[namespace]![i] = ItemCollectionInfoEntry(id: entries[i].id, info: entries[i].info, count: postbox.itemCollectionItemTable.itemCount(collectionId: entries[i].id), firstItem: firstItem) } } } diff --git a/Postbox/ItemCollectionItemTable.swift b/Postbox/ItemCollectionItemTable.swift index 6c52c6ee69..416e086943 100644 --- a/Postbox/ItemCollectionItemTable.swift +++ b/Postbox/ItemCollectionItemTable.swift @@ -123,6 +123,16 @@ final class ItemCollectionItemTable: Table { return items } + func itemCount(collectionId: ItemCollectionId) -> Int32 { + var count: Int32 = 0 + self.valueBox.range(self.table, start: self.upperBound(collectionId: collectionId), end: self.lowerBound(collectionId: collectionId), keys: { key in + let itemIndex = ItemCollectionItemIndex(index: key.getInt32(4 + 8), id: key.getInt64(4 + 8 + 4)) + count = itemIndex.index + 1 + return true + }, limit: 1) + return count + } + func higherItems(collectionId: ItemCollectionId, itemIndex: ItemCollectionItemIndex, count: Int) -> [ItemCollectionItem] { var items: [ItemCollectionItem] = [] self.valueBox.range(self.table, start: self.key(collectionId: collectionId, index: itemIndex), end: self.upperBound(collectionId: collectionId), values: { _, value in diff --git a/Postbox/Peer.swift b/Postbox/Peer.swift index 44dc53fef6..90bc134dc8 100644 --- a/Postbox/Peer.swift +++ b/Postbox/Peer.swift @@ -18,8 +18,6 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable { } public func toInt64() -> Int64 { - - return (Int64(self.namespace) << 32) | unsafeBitCast(UInt64(unsafeBitCast(self.id, to: UInt32.self)), to: Int64.self) }