aboutsummaryrefslogtreecommitdiff
path: root/objects/object.go
blob: 08525a9161101f1f7241b0be28481b7769e9a1f8 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package objects

import (
	"fmt"
	"io"
	"strconv"
	"strings"
)

type ObjectType string

const (
	OTBlob     ObjectType = "blob"
	OTFile     ObjectType = "file"
	OTTree     ObjectType = "tree"
	OTSnapshot ObjectType = "snapshot"
)

var AllObjectTypes = []ObjectType{
	OTBlob,
	OTFile,
	OTTree,
	OTSnapshot,
}

func (ot ObjectType) IsKnown() bool {
	for _, t := range AllObjectTypes {
		if t == ot {
			return true
		}
	}
	return false
}

// RawObject describes a serialized object plus it's type header. This is the content that will be saved.
// It is serialized as the type (ObjectType), a space byte, the size of the payload in bytes (encoded as decimal ASCII number), a newline ('\n') character and the payload.
// The encoding of the payload depends on the type.
type RawObject struct {
	Type    ObjectType
	Payload []byte
}

// Serialize writes the binary representation of an object to a io.Writer
func (o RawObject) Serialize(w io.Writer) error {
	if _, err := fmt.Fprintf(w, "%s %d\n", o.Type, len(o.Payload)); err != nil {
		return err
	}

	_, err := w.Write(o.Payload)
	return err
}

func (o RawObject) SerializeAndId(w io.Writer, algo ObjectIdAlgo) (ObjectId, error) {
	gen := algo.Generator()

	if err := o.Serialize(io.MultiWriter(w, gen)); err != nil {
		return ObjectId{}, err
	}

	return gen.GetId(), nil
}

type UnserializeError struct {
	Reason error
}

func (err UnserializeError) Error() string {
	if err.Reason == nil {
		return "Invalid object"
	} else {
		return fmt.Sprintf("Invalid object: %s", err.Reason)
	}
}

type bytewiseReader struct {
	r   io.Reader
	buf []byte
}

func newBytewiseReader(r io.Reader) io.ByteReader {
	return &bytewiseReader{
		r:   r,
		buf: make([]byte, 1),
	}
}

func (br *bytewiseReader) ReadByte() (b byte, err error) {
	_, err = br.r.Read(br.buf)
	b = br.buf[0]

	return
}

// Unserialize attempts to read an object from a stream.
// It is advisable to pass a buffered reader, if feasible.
func Unserialize(r io.Reader) (RawObject, error) {
	br := newBytewiseReader(r)

	line := []byte{}

	for {
		b, err := br.ReadByte()
		if err != nil {
			return RawObject{}, UnserializeError{err}
		}

		if b == '\n' {
			break
		}
		line = append(line, b)
	}

	parts := strings.SplitN(string(line), " ", 2)
	if len(parts) != 2 {
		return RawObject{}, UnserializeError{}
	}

	size, err := strconv.ParseUint(parts[1], 10, 64)
	if err != nil {
		return RawObject{}, UnserializeError{err}
	}

	o := RawObject{
		Type:    ObjectType(parts[0]),
		Payload: make([]byte, size),
	}

	if _, err := io.ReadFull(r, o.Payload); err != nil {
		return RawObject{}, UnserializeError{err}
	}

	return o, nil
}

type Object interface {
	Type() ObjectType
	Payload() []byte
	FromPayload([]byte) error
}

func (ro RawObject) Object() (o Object, err error) {
	switch ro.Type {
	case OTBlob:
		o = new(Blob)
	case OTFile:
		o = new(File)
	case OTTree:
		o = make(Tree)
	case OTSnapshot:
		o = new(Snapshot)
	default:
		return nil, fmt.Errorf("Unknown object type %s", ro.Type)
	}

	if err = o.FromPayload(ro.Payload); err != nil {
		o = nil
	}
	return
}

func ToRawObject(o Object) RawObject {
	return RawObject{Type: o.Type(), Payload: o.Payload()}
}