forked from grailbio/bio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_mate_shard.go
67 lines (57 loc) · 1.48 KB
/
mem_mate_shard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package bampair
import (
"fmt"
"sync"
"github.com/Schaudge/hts/sam"
)
// memMateShard is an in-memory shard of the distant mates.
//
// add(), openReader() and closeReader() are threadsafe. The call to
// closeWriter() should occur after all calls to add() are finished.
// Calls to getMate() should occur only between a call to openReader()
// and a call to closeReader().
type memMateShard struct {
m map[string][]*indexedRecord
mutex sync.Mutex
}
func newMemMateShard() *memMateShard {
return &memMateShard{
m: map[string][]*indexedRecord{},
}
}
func (s *memMateShard) add(mate *sam.Record, fileIdx uint64) error {
s.mutex.Lock()
defer s.mutex.Unlock()
e, found := s.m[mate.Name]
if found {
for _, iRecord := range e {
if recordsEqual(iRecord.r, mate) {
return nil
}
}
s.m[mate.Name] = append(s.m[mate.Name], &indexedRecord{mate, fileIdx})
if len(e) > 2 {
return fmt.Errorf("Got 3rd read, right is not nil! %v %d", mate, fileIdx)
}
} else {
s.m[mate.Name] = []*indexedRecord{&indexedRecord{mate, fileIdx}}
}
return nil
}
func (s *memMateShard) closeWriter() error {
return nil
}
func (s *memMateShard) openReader() error {
return nil
}
func (s *memMateShard) getMate(shardInfo *ShardInfo, r *sam.Record) (*sam.Record, uint64) {
e := s.m[r.Name]
for _, iRecord := range e {
if !recordsEqual(iRecord.r, r) {
return iRecord.r, iRecord.fileIdx + shardInfo.getShardStartFileIdx(iRecord.r)
}
}
return nil, 0
}
func (s *memMateShard) closeReader() {
}