CreateSheets creates sheet blobs using content-addressed storage. Each sheet is identified by the SHA256 hash of its statement. Duplicate statements share the same blob (ON CONFLICT DO NOTHING).
(ctx context.Context, creates ...*SheetMessage)
| 109 | // Each sheet is identified by the SHA256 hash of its statement. |
| 110 | // Duplicate statements share the same blob (ON CONFLICT DO NOTHING). |
| 111 | func (s *Store) CreateSheets(ctx context.Context, creates ...*SheetMessage) ([]*SheetMessage, error) { |
| 112 | var statements []string |
| 113 | var sha256s [][]byte |
| 114 | |
| 115 | for _, c := range creates { |
| 116 | statements = append(statements, c.Statement) |
| 117 | h := sha256.Sum256([]byte(c.Statement)) |
| 118 | c.Sha256 = hex.EncodeToString(h[:]) |
| 119 | sha256s = append(sha256s, h[:]) |
| 120 | c.Size = int64(len(c.Statement)) |
| 121 | } |
| 122 | |
| 123 | q := qb.Q().Space(` |
| 124 | INSERT INTO sheet_blob ( |
| 125 | sha256, |
| 126 | content |
| 127 | ) SELECT |
| 128 | unnest(CAST(? AS BYTEA[])), |
| 129 | unnest(CAST(? AS TEXT[])) |
| 130 | ON CONFLICT DO NOTHING |
| 131 | `, sha256s, statements) |
| 132 | |
| 133 | query, args, err := q.ToSQL() |
| 134 | if err != nil { |
| 135 | return nil, errors.Wrapf(err, "failed to build sql") |
| 136 | } |
| 137 | |
| 138 | if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil { |
| 139 | return nil, errors.Wrapf(err, "failed to exec") |
| 140 | } |
| 141 | |
| 142 | return creates, nil |
| 143 | } |
| 144 | |
| 145 | // HasSheets checks if all sheets exist by SHA256 hashes. |
| 146 | func (s *Store) HasSheets(ctx context.Context, sha256Hexes ...string) (bool, error) { |
no test coverage detected