blob: 241169d92b08a4ad1b42886413af6008509d2d49 (
about) (
plain) (
blame)
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
|
package memory
import (
"bytes"
objectheader "codeberg.org/lindenii/furgit/object/header"
objectid "codeberg.org/lindenii/furgit/object/id"
objecttype "codeberg.org/lindenii/furgit/object/type"
)
// WriteBytesContent writes one typed object content byte slice.
func (store *Store) WriteBytesContent(ty objecttype.Type, content []byte) (objectid.ObjectID, error) {
id := store.algo.Sum(buildRawObject(ty, content))
store.objects[id] = storedObject{ty: ty, content: append([]byte(nil), content...)}
return id, nil
}
// WriteBytesFull writes one full serialized object byte slice as "type size\0content".
func (store *Store) WriteBytesFull(raw []byte) (objectid.ObjectID, error) {
return store.WriteReaderFull(bytes.NewReader(raw))
}
func buildRawObject(ty objecttype.Type, body []byte) []byte {
header, ok := objectheader.Encode(ty, int64(len(body)))
if !ok {
panic("failed to encode object header")
}
raw := make([]byte, len(header)+len(body))
copy(raw, header)
copy(raw[len(header):], body)
return raw
}
|