This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
/
ref.go
135 lines (110 loc) · 3.29 KB
/
ref.go
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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package types
import (
"bytes"
"github.com/attic-labs/noms/go/hash"
)
type Ref struct {
valueImpl
}
type refPart uint32
const (
refPartKind refPart = iota
refPartTargetHash
refPartTargetType
refPartHeight
refPartEnd
)
func NewRef(v Value) Ref {
return constructRef(v.Hash(), TypeOf(v), maxChunkHeight(v)+1)
}
// ToRefOfValue returns a new Ref that points to the same target as |r|, but
// with the type 'Ref<Value>'.
func ToRefOfValue(r Ref) Ref {
return constructRef(r.TargetHash(), ValueType, r.Height())
}
func constructRef(targetHash hash.Hash, targetType *Type, height uint64) Ref {
w := newBinaryNomsWriter()
offsets := make([]uint32, refPartEnd)
offsets[refPartKind] = w.offset
RefKind.writeTo(&w)
offsets[refPartTargetHash] = w.offset
w.writeHash(targetHash)
offsets[refPartTargetType] = w.offset
targetType.writeToAsType(&w, map[string]*Type{})
offsets[refPartHeight] = w.offset
w.writeCount(height)
return Ref{valueImpl{nil, w.data(), offsets}}
}
func writeRefPartsTo(w nomsWriter, targetHash hash.Hash, targetType *Type, height uint64) {
RefKind.writeTo(w)
w.writeHash(targetHash)
targetType.writeToAsType(w, map[string]*Type{})
w.writeCount(height)
}
// readRef reads the data provided by a reader and moves the reader forward.
func readRef(dec *typedBinaryNomsReader) Ref {
start := dec.pos()
offsets := skipRef(dec)
end := dec.pos()
return Ref{valueImpl{nil, dec.byteSlice(start, end), offsets}}
}
// skipRef moves the reader forward, past the data representing the Ref, and returns the offsets of the component parts.
func skipRef(dec *typedBinaryNomsReader) []uint32 {
offsets := make([]uint32, refPartEnd)
offsets[refPartKind] = dec.pos()
dec.skipKind()
offsets[refPartTargetHash] = dec.pos()
dec.skipHash() // targetHash
offsets[refPartTargetType] = dec.pos()
dec.skipType() // targetType
offsets[refPartHeight] = dec.pos()
dec.skipCount() // height
return offsets
}
func maxChunkHeight(v Value) (max uint64) {
v.WalkRefs(func(r Ref) {
if height := r.Height(); height > max {
max = height
}
})
return
}
func (r Ref) offsetAtPart(part refPart) uint32 {
return r.offsets[part] - r.offsets[refPartKind]
}
func (r Ref) decoderAtPart(part refPart) valueDecoder {
offset := r.offsetAtPart(part)
return newValueDecoder(r.buff[offset:], nil)
}
func (r Ref) TargetHash() hash.Hash {
dec := r.decoderAtPart(refPartTargetHash)
return dec.readHash()
}
func (r Ref) Height() uint64 {
dec := r.decoderAtPart(refPartHeight)
return dec.readCount()
}
func (r Ref) TargetValue(vr ValueReader) Value {
return vr.ReadValue(r.TargetHash())
}
func (r Ref) TargetType() *Type {
dec := r.decoderAtPart(refPartTargetType)
return dec.readType()
}
// Value interface
func (r Ref) Value() Value {
return r
}
func (r Ref) WalkValues(cb ValueCallback) {
}
func (r Ref) typeOf() *Type {
return makeCompoundType(RefKind, r.TargetType())
}
func (r Ref) isSameTargetType(other Ref) bool {
targetTypeBytes := r.buff[r.offsetAtPart(refPartTargetType):r.offsetAtPart(refPartHeight)]
otherTargetTypeBytes := other.buff[other.offsetAtPart(refPartTargetType):other.offsetAtPart(refPartHeight)]
return bytes.Equal(targetTypeBytes, otherTargetTypeBytes)
}