an atproto pds written in F# (.NET 9) 🦒
pds
fsharp
giraffe
dotnet
atproto
1namespace PDSharp.Core
2
3open System.Collections.Concurrent
4
5/// Block storage interface for CID → byte[] mappings
6module BlockStore =
7
8 /// Interface for content-addressed block storage
9 type IBlockStore =
10 abstract member Get : Cid -> Async<byte[] option>
11 abstract member Put : byte[] -> Async<Cid>
12 abstract member Has : Cid -> Async<bool>
13 abstract member GetAllCidsAndData : unit -> Async<(Cid * byte[]) list>
14
15 /// In-memory implementation of IBlockStore for testing
16 type MemoryBlockStore() =
17 let store = ConcurrentDictionary<string, (Cid * byte[])>()
18
19 let cidKey (cid : Cid) =
20 System.Convert.ToBase64String(cid.Bytes)
21
22 interface IBlockStore with
23 member _.Get(cid : Cid) = async {
24 let key = cidKey cid
25
26 match store.TryGetValue(key) with
27 | true, (_, data) -> return Some data
28 | false, _ -> return None
29 }
30
31 member _.Put(data : byte[]) = async {
32 let hash = Crypto.sha256 data
33 let cid = Cid.FromHash hash
34 let key = cidKey cid
35 store.[key] <- (cid, data)
36 return cid
37 }
38
39 member _.Has(cid : Cid) = async {
40 let key = cidKey cid
41 return store.ContainsKey(key)
42 }
43
44 member _.GetAllCidsAndData() = async { return store.Values |> Seq.toList }
45
46 /// Get the number of blocks stored (for testing)
47 member _.Count = store.Count
48
49 /// Clear all blocks (for testing)
50 member _.Clear() = store.Clear()