Merge branch '19566-glm'
[lightning.git] / gob.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package lightning
6
7 import (
8         "bufio"
9         "encoding/gob"
10         "io"
11         "io/ioutil"
12         _ "net/http/pprof"
13
14         "github.com/klauspost/pgzip"
15         "golang.org/x/crypto/blake2b"
16 )
17
18 type CompactGenome struct {
19         Name     string
20         Variants []tileVariantID
21         StartTag tagID
22         EndTag   tagID
23 }
24
25 type CompactSequence struct {
26         Name          string
27         TileSequences map[string][]tileLibRef
28 }
29
30 type TileVariant struct {
31         Tag      tagID
32         Ref      bool
33         Variant  tileVariantID
34         Blake2b  [blake2b.Size256]byte
35         Sequence []byte
36 }
37
38 type LibraryEntry struct {
39         TagSet           [][]byte
40         CompactGenomes   []CompactGenome
41         CompactSequences []CompactSequence
42         TileVariants     []TileVariant
43 }
44
45 func ReadCompactGenomes(rdr io.Reader, gz bool) ([]CompactGenome, error) {
46         var ret []CompactGenome
47         err := DecodeLibrary(rdr, gz, func(ent *LibraryEntry) error {
48                 ret = append(ret, ent.CompactGenomes...)
49                 return nil
50         })
51         return ret, err
52 }
53
54 func DecodeLibrary(rdr io.Reader, gz bool, cb func(*LibraryEntry) error) error {
55         zrdr := ioutil.NopCloser(rdr)
56         var err error
57         if gz {
58                 zrdr, err = pgzip.NewReader(bufio.NewReaderSize(rdr, 1<<20))
59                 if err != nil {
60                         return err
61                 }
62                 defer zrdr.Close()
63         }
64         dec := gob.NewDecoder(zrdr)
65         for {
66                 var ent LibraryEntry
67                 err = dec.Decode(&ent)
68                 if err == io.EOF {
69                         return zrdr.Close()
70                 } else if err != nil {
71                         return err
72                 }
73                 err = cb(&ent)
74                 if err != nil {
75                         return err
76                 }
77         }
78 }