Debian Codegraph @

store_test.go

raw
1 package desync
2
3 var _ WriteStore = &TestStore{}
4
5 type TestStore struct {
6 Chunks map[ChunkID][]byte
7
8 // Override the default behavior by setting these functions
9 GetChunkFunc func(ChunkID) (*Chunk, error)
10 HasChunkFunc func(ChunkID) (bool, error)
11 StoreChunkFunc func(chunk *Chunk) error
12 }
13
14 func (s *TestStore) GetChunk(id ChunkID) (*Chunk, error) {
15 if s.GetChunkFunc != nil {
16 return s.GetChunkFunc(id)
17 }
18 b, ok := s.Chunks[id]
19 if !ok {
20 return nil, ChunkMissing{id}
21 }
22 return NewChunk(b), nil
23 }
24
25 func (s *TestStore) HasChunk(id ChunkID) (bool, error) {
26 if s.HasChunkFunc != nil {
27 return s.HasChunkFunc(id)
28 }
29 _, ok := s.Chunks[id]
30 return ok, nil
31 }
32
33 func (s *TestStore) StoreChunk(chunk *Chunk) error {
34 if s.StoreChunkFunc != nil {
35 return s.StoreChunkFunc(chunk)
36 }
37 if s.Chunks == nil {
38 s.Chunks = make(map[ChunkID][]byte)
39 }
40 b, _ := chunk.Data()
41 s.Chunks[chunk.ID()] = b
42 return nil
43 }
44
45 func (s *TestStore) String() string { return "TestStore" }
46
47 func (s *TestStore) Close() error { return nil }