no message

This commit is contained in:
Peter
2017-03-16 00:22:34 +03:00
parent 1ce4c9f832
commit f20a270fca
4 changed files with 45 additions and 6 deletions

View File

@@ -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 {

View File

@@ -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)
}
}
}

View File

@@ -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

View File

@@ -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)
}