Debian Codegraph @

examples/distribution/storage.go

raw
1 package main
2
3 import (
4 "context"
5 "log"
6 )
7
8 func newDistributedStorage() *distributedStorage {
9 return &distributedStorage{
10 records: make(map[string][]byte),
11 }
12 }
13
14 type distributedStorage struct {
15 records map[string][]byte
16 }
17
18 func (d *distributedStorage) Get(_ context.Context, key string) ([]byte, bool) {
19 log.Printf("Getting key %s from the distributed storage\n", key)
20 value, ok := d.records[key]
21 return value, ok
22 }
23
24 func (d *distributedStorage) Set(_ context.Context, key string, value []byte) {
25 log.Printf("Writing key %s to the distributed storage\n", key)
26 d.records[key] = value
27 }
28
29 func (d *distributedStorage) GetBatch(_ context.Context, keys []string) map[string][]byte {
30 records := make(map[string][]byte)
31 for _, key := range keys {
32 if value, ok := d.records[key]; ok {
33 records[key] = value
34 }
35 }
36 return records
37 }
38
39 func (d *distributedStorage) SetBatch(_ context.Context, records map[string][]byte) {
40 for key, value := range records {
41 d.records[key] = value
42 }
43 }