From 86522e1ffea743afe3c336d41eb5e633f2b5d809 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 17 Nov 2023 04:56:57 -0500 Subject: [PATCH] feat: add getCachedStorageLocations method --- src/node.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/node.ts b/src/node.ts index be77bad..3ee42a4 100644 --- a/src/node.ts +++ b/src/node.ts @@ -85,6 +85,40 @@ export class S5Node { await this.services.p2p.stop(); } + async getCachedStorageLocations( + hash: Multihash, + types: number[], + ): Promise> { + const locations = new Map(); + + const map = await this.readStorageLocationsFromDB(hash); // Assuming this method exists and returns a Map or similar structure + if (map.size === 0) { + return new Map(); + } + + const ts = Math.floor(Date.now() / 1000); + + types.forEach((type) => { + if (!map.has(type)) return; + + map.get(type)!.forEach((value, key) => { + if (value[3] >= ts) { + const storageLocation = new StorageLocation( + type, + value[1].map((v: string) => v), // Assuming value[1] is an array of strings + value[3], + ); + + // Assuming providerMessage is a property of StorageLocation + storageLocation.providerMessage = value[4]; + locations.set(key, storageLocation); + } + }); + }); + + return locations; + } + async readStorageLocationsFromDB( hash: Multihash, ): Promise>>> {