Home telegram - peerTable
Post
Cancel

telegram - peerTable

PeerTable

class PeerTable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
static func tableSpec(_ id: Int32) -> ValueBoxTable {
    return ValueBoxTable(id: id, keyType: .int64, compactValuesOnCreation: false)
}

private let reverseAssociatedTable: ReverseAssociatedPeerTable

private let sharedEncoder = PostboxEncoder()
private let sharedKey = ValueBoxKey(length: 8)

private var cachedPeers: [PeerId: Peer] = [:]
private var updatedInitialPeers: [PeerId: Peer?] = [:]

private func key(_ id: PeerId) -> ValueBoxKey {
    self.sharedKey.setInt64(0, value: id.toInt64())
    return self.sharedKey
}

// set
func set(_ peer: Peer) {
    let previous = self.get(peer.id)
  // update cache
    self.cachedPeers[peer.id] = peer
    if self.updatedInitialPeers[peer.id] == nil {// update only when cache is nil
        self.updatedInitialPeers[peer.id] = previous
    }
}

// get
func get(_ id: PeerId) -> Peer? {
    if let peer = self.cachedPeers[id] {// from cache
        return peer
    }
  // from db in disk
    if let value = self.valueBox.get(self.table, key: self.key(id)) {
        if let peer = PostboxDecoder(buffer: value).decodeRootObject() as? Peer {
            self.cachedPeers[id] = peer
            return peer
        }
    }
    return nil
}

// clearMemoryCache
override func clearMemoryCache() {
    self.cachedPeers.removeAll()
    assert(self.updatedInitialPeers.isEmpty)
}

// transaction update peers
func transactionUpdatedPeers(contactsTable: ContactTable) -> [((Peer, Bool)?, (Peer, Bool))] {
    var result: [((Peer, Bool)?, (Peer, Bool))] = []
    for (peerId, initialPeer) in self.updatedInitialPeers {
        if let peer = self.get(peerId) {
            let isContact = contactsTable.isContact(peerId: peerId)
            result.append((initialPeer.flatMap { ($0, isContact) }, (peer, isContact)))
        } else {
            assertionFailure()
        }
    }
    return result
}

// before commit
override func beforeCommit() {
    if !self.updatedInitialPeers.isEmpty {
        for (peerId, previousPeer) in self.updatedInitialPeers {
            if let peer = self.cachedPeers[peerId] {
                self.sharedEncoder.reset()
                self.sharedEncoder.encodeRootObject(peer)
                
                self.valueBox.set(self.table, key: self.key(peerId), value: self.sharedEncoder.readBufferNoCopy())
                
              // update reverse associated peer
                let previousAssociation = previousPeer?.associatedPeerId
                if previousAssociation != peer.associatedPeerId {
                    if let previousAssociation = previousAssociation {
                        self.reverseAssociatedTable.removeReverseAssociation(target: previousAssociation, from: peerId)
                    }
                    if let associatedPeerId = peer.associatedPeerId {
                        self.reverseAssociatedTable.addReverseAssociation(target: associatedPeerId, from: peerId)
                    }
                }
            } else {
                assertionFailure()
            }
        }
        
        self.updatedInitialPeers.removeAll()
        if !self.useCaches {
            self.cachedPeers.removeAll()
        }
    }
}
This post is licensed under CC BY 4.0 by the author.