aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/document/document.go
blob: e2a77f7b12614e175de0111452c64d11157a6b21 (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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package document

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"math/rand"
	"strconv"
	"strings"
	"sync"

	"time"

	// Why do we use an experimental parser? This appears to be the only JSON library that satisfies the following
	// requirements:
	// - Faster than the std parser
	// - Supports parsing from a io.Reader
	// - Supports parsing token-by-token
	// - Few allocations during parsing (especially for large objects)
	"github.com/go-json-experiment/json"
)

type Operation int

const (
	OperationPut Operation = iota
	OperationUpdate
	OperationRemove

	jsonArrayStart  json.Kind = '['
	jsonArrayEnd    json.Kind = ']'
	jsonObjectStart json.Kind = '{'
	jsonObjectEnd   json.Kind = '}'
	jsonString      json.Kind = '"'
)

var (
	ErrMissingId = errors.New("no id specified")
	fieldsPrefix = []byte(`{"fields":`)
	fieldsSuffix = []byte("}")
)

func (o Operation) String() string {
	switch o {
	case OperationPut:
		return "put"
	case OperationUpdate:
		return "update"
	case OperationRemove:
		return "remove"
	}
	return ""
}

// Id represents a Vespa document ID.
type Id struct {
	id string

	Type         string
	Namespace    string
	Number       *int64
	Group        string
	UserSpecific string
}

func (d Id) Equal(o Id) bool {
	return d.Type == o.Type &&
		d.Namespace == o.Namespace &&
		((d.Number == nil && o.Number == nil) || *d.Number == *o.Number) &&
		d.Group == o.Group &&
		d.UserSpecific == o.UserSpecific
}

func (d Id) String() string { return d.id }

// ParseId parses a serialized document ID string.
func ParseId(serialized string) (Id, error) {
	parts := strings.SplitN(serialized, ":", 4)
	if len(parts) < 4 || parts[0] != "id" {
		return Id{}, parseError(serialized)
	}
	namespace := parts[1]
	if namespace == "" {
		return Id{}, parseError(serialized)
	}
	docType := parts[2]
	if docType == "" {
		return Id{}, parseError(serialized)
	}
	rest := strings.SplitN(parts[3], ":", 2)
	if len(rest) < 2 {
		return Id{}, parseError(serialized)
	}
	options := rest[0]
	userSpecific := rest[1]
	var number *int64
	group := ""
	if strings.HasPrefix(options, "n=") {
		n, err := strconv.ParseInt(options[2:], 10, 64)
		if err != nil {
			return Id{}, parseError(serialized)
		}
		number = &n
	} else if strings.HasPrefix(options, "g=") {
		group = options[2:]
		if len(group) == 0 {
			return Id{}, parseError(serialized)
		}
	} else if options != "" {
		return Id{}, parseError(serialized)
	}
	if userSpecific == "" {
		return Id{}, parseError(serialized)
	}
	return Id{
		id:           serialized,
		Namespace:    namespace,
		Type:         docType,
		Number:       number,
		Group:        group,
		UserSpecific: userSpecific,
	}, nil
}

// Document represents a Vespa document operation.
type Document struct {
	Id        Id
	Condition string
	Body      []byte
	Operation Operation
	Create    bool

	resetFunc func()
}

func (d Document) Equal(o Document) bool {
	return d.Id.Equal(o.Id) &&
		d.Condition == o.Condition &&
		bytes.Equal(d.Body, o.Body) &&
		d.Operation == o.Operation &&
		d.Create == o.Create
}

// Reset discards the body of this document.
func (d *Document) Reset() {
	d.Body = nil
	if d.resetFunc != nil {
		d.resetFunc()
	}
}

// Decoder decodes documents from a JSON structure which is either an array of objects, or objects separated by newline.
type Decoder struct {
	dec *json.Decoder
	buf bytes.Buffer

	array bool
	jsonl bool

	fieldsEnd int64

	documentBuffers sync.Pool
}

func (d Document) String() string {
	var sb strings.Builder
	sb.WriteString(d.Operation.String())
	sb.WriteString(" ")
	sb.WriteString(d.Id.String())
	if d.Condition != "" {
		sb.WriteString(", condition=")
		sb.WriteString(d.Condition)
	}
	if d.Create {
		sb.WriteString(", create=true")
	}
	if d.Body != nil {
		sb.WriteString(", body=")
		sb.WriteString(string(d.Body))
	}
	return sb.String()
}

func (d *Decoder) guessMode() error {
	if d.array || d.jsonl {
		return nil
	}
	kind := d.dec.PeekKind()
	switch kind {
	case jsonArrayStart:
		if _, err := d.readNext(jsonArrayStart); err != nil {
			return err
		}
		d.array = true
	case jsonObjectStart:
		d.jsonl = true
	default:
		return fmt.Errorf("expected %s or %s, got %s", jsonArrayStart, jsonObjectStart, kind)
	}
	return nil
}

func (d *Decoder) readNext(kind json.Kind) (json.Token, error) {
	t, err := d.dec.ReadToken()
	if err != nil {
		return json.Token{}, err
	}
	if t.Kind() != kind {
		return json.Token{}, fmt.Errorf("unexpected json kind: %q: want %q", t, kind)
	}
	return t, nil
}

func (d *Decoder) readString() (string, error) {
	t, err := d.readNext(jsonString)
	if err != nil {
		return "", err
	}
	return t.String(), nil
}

func (d *Decoder) readBool() (bool, error) {
	t, err := d.dec.ReadToken()
	if err != nil {
		return false, err
	}
	kind := t.Kind()
	if kind != 't' && kind != 'f' {
		return false, fmt.Errorf("unexpected json kind: %q: want %q or %q", t, 't', 'f')
	}
	return t.Bool(), nil
}

func (d *Decoder) Decode() (Document, error) {
	doc, err := d.decode()
	if err != nil && err != io.EOF {
		return doc, fmt.Errorf("invalid operation at byte offset %d: %w", d.dec.InputOffset(), err)
	}
	return doc, err
}

func (d *Decoder) buffer() *bytes.Buffer {
	buf := d.documentBuffers.Get().(*bytes.Buffer)
	buf.Reset()
	return buf
}

func (d *Decoder) readField(name string, offset int64, doc *Document) error {
	readId := false
	switch name {
	case "id", "put":
		readId = true
		doc.Operation = OperationPut
	case "update":
		readId = true
		doc.Operation = OperationUpdate
	case "remove":
		readId = true
		doc.Operation = OperationRemove
	case "condition":
		condition, err := d.readString()
		if err != nil {
			return err
		}
		doc.Condition = condition
	case "create":
		create, err := d.readBool()
		if err != nil {
			return err
		}
		doc.Create = create
	case "fields":
		if _, err := d.readNext(jsonObjectStart); err != nil {
			return err
		}
		// Skip data between start of operation and start of fields
		fieldsStart := d.dec.InputOffset() - 1
		d.buf.Next(int(fieldsStart - offset))
		depth := 1
		for depth > 0 {
			t, err := d.dec.ReadToken()
			if err != nil {
				return err
			}
			switch t.Kind() {
			case jsonObjectStart:
				depth++
			case jsonObjectEnd:
				depth--
			}
		}
		d.fieldsEnd = d.dec.InputOffset()
		fields := d.buf.Next(int(d.fieldsEnd - fieldsStart))
		// Try to re-use buffers holding the document body. The buffer is released by document.Reset()
		bodyBuf := d.buffer()
		bodyBuf.Grow(len(fieldsPrefix) + len(fields) + len(fieldsSuffix))
		bodyBuf.Write(fieldsPrefix)
		bodyBuf.Write(fields)
		bodyBuf.Write(fieldsSuffix)
		doc.Body = bodyBuf.Bytes()
		doc.resetFunc = func() { d.documentBuffers.Put(bodyBuf) }
	}
	if readId {
		s, err := d.readString()
		if err != nil {
			return err
		}
		id, err := ParseId(s)
		if err != nil {
			return err
		}
		doc.Id = id
	}
	return nil
}

func (d *Decoder) decode() (Document, error) {
	start := d.dec.InputOffset()
	if err := d.guessMode(); err != nil {
		return Document{}, err
	}
	if d.array && d.dec.PeekKind() == jsonArrayEnd {
		// Reached end of the array holding document operations
		if _, err := d.readNext(jsonArrayEnd); err != nil {
			return Document{}, err
		}
		return Document{}, io.EOF
	}
	// Start of document operation
	if _, err := d.readNext(jsonObjectStart); err != nil {
		return Document{}, err
	}
	var doc Document
loop:
	for {
		switch d.dec.PeekKind() {
		case jsonString:
			t, err := d.dec.ReadToken()
			if err != nil {
				return Document{}, err
			}
			if err := d.readField(t.String(), start, &doc); err != nil {
				return Document{}, err
			}
		default:
			if _, err := d.readNext(jsonObjectEnd); err != nil {
				return Document{}, err
			}
			// Drop operation from the buffer
			start = max(start, d.fieldsEnd)
			end := d.dec.InputOffset()
			d.buf.Next(int(end - start))
			break loop
		}
	}
	if doc.Id.id == "" {
		return doc, ErrMissingId
	}
	return doc, nil
}

func NewDecoder(r io.Reader) *Decoder {
	d := &Decoder{}
	d.documentBuffers.New = func() any { return &bytes.Buffer{} }
	d.dec = json.NewDecoder(io.TeeReader(r, &d.buf))
	return d
}

func parseError(value string) error {
	return fmt.Errorf("invalid document: expected id:<namespace>:<document-type>:[n=<number>|g=<group>]:<user-specific>, got %q", value)
}

// Generator is a reader that returns synthetic documents until a given deadline.
type Generator struct {
	Size     int
	Deadline time.Time

	buf     bytes.Buffer
	nowFunc func() time.Time
}

func NewGenerator(size int, deadline time.Time) *Generator {
	return &Generator{Size: size, Deadline: deadline, nowFunc: time.Now}
}

func randString(size int) string {
	b := make([]byte, size)
	for i := range b {
		b[i] = byte('a' + rand.Intn('z'-'a'+1))
	}
	return string(b)
}

func (g *Generator) Read(p []byte) (int, error) {
	if g.buf.Len() == 0 {
		if !g.nowFunc().Before(g.Deadline) {
			return 0, io.EOF
		}
		fmt.Fprintf(&g.buf, "{\"put\": \"id:test:test::%s\", \"fields\": {\"test\": \"%s\"}}\n", randString(8), randString(g.Size))
	}
	return g.buf.Read(p)
}

type number interface{ float64 | int64 | int }

func min[T number](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T number](x, y T) T {
	if x > y {
		return x
	}
	return y
}