Skip to content

Commit ded64de

Browse files
committed
sharded dicts
1 parent 71567ea commit ded64de

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

contracts/TopShotIPFSResolver.cdc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ access(all) contract TopShotIPFSResolver {
33
access(all) event CIDSet(setID: UInt32, playID: UInt32, subeditionID: UInt32, mediaType: String, cid: String)
44
access(all) event GatewayUpdated(gateway: String)
55

6-
// Composite key "setID_playID_subeditionID" → { mediaType: CID }
7-
access(contract) let cids: {String: {String: String}}
6+
// Number of buckets to split CID entries across
7+
access(all) let numBuckets: UInt64
8+
9+
// Sharded storage: bucket index -> composite key -> { mediaType: CID }
10+
// Let's us store 32 * 100 k= 3,200 CID entries
11+
access(contract) let shards: {UInt64: {String: {String: String}}}
812

913
// IPFS gateway base URL (e.g., "https://ipfs.dapperlabs.com/ipfs/")
1014
access(all) var gateway: String
@@ -14,9 +18,12 @@ access(all) contract TopShotIPFSResolver {
1418
access(all) resource Admin {
1519
access(all) fun setCID(setID: UInt32, playID: UInt32, subeditionID: UInt32, mediaType: String, cid: String) {
1620
let key = TopShotIPFSResolver.buildKey(setID: setID, playID: playID, subeditionID: subeditionID)
17-
var inner = TopShotIPFSResolver.cids[key] ?? {}
21+
let bucket = TopShotIPFSResolver.getBucket(setID: setID, playID: playID, subeditionID: subeditionID)
22+
var shard = TopShotIPFSResolver.shards[bucket] ?? {}
23+
var inner = shard[key] ?? {}
1824
inner[mediaType] = cid
19-
TopShotIPFSResolver.cids[key] = inner
25+
shard[key] = inner
26+
TopShotIPFSResolver.shards[bucket] = shard
2027
emit CIDSet(setID: setID, playID: playID, subeditionID: subeditionID, mediaType: mediaType, cid: cid)
2128
}
2229

@@ -28,7 +35,13 @@ access(all) contract TopShotIPFSResolver {
2835

2936
access(all) view fun getCIDs(setID: UInt32, playID: UInt32, subeditionID: UInt32): {String: String}? {
3037
let key = self.buildKey(setID: setID, playID: playID, subeditionID: subeditionID)
31-
return self.cids[key]
38+
let bucket = self.getBucket(setID: setID, playID: playID, subeditionID: subeditionID)
39+
let shard = self.shards[bucket] ?? {}
40+
return shard[key]
41+
}
42+
43+
access(all) view fun getBucket(setID: UInt32, playID: UInt32, subeditionID: UInt32): UInt64 {
44+
return (UInt64(setID) + UInt64(playID) + UInt64(subeditionID)) % self.numBuckets
3245
}
3346

3447
access(all) view fun buildKey(setID: UInt32, playID: UInt32, subeditionID: UInt32): String {
@@ -40,7 +53,8 @@ access(all) contract TopShotIPFSResolver {
4053
}
4154

4255
init() {
43-
self.cids = {}
56+
self.numBuckets = 10
57+
self.shards = {}
4458
self.gateway = "https://ipfs.dapperlabs.com/ipfs/"
4559
self.AdminStoragePath = /storage/TopShotIPFSResolverAdmin
4660
self.account.storage.save<@Admin>(<- create Admin(), to: self.AdminStoragePath)

0 commit comments

Comments
 (0)