|
3 | 3 |
|
4 | 4 | package node
|
5 | 5 |
|
| 6 | +var ( |
| 7 | + // DefaultCopySettings contains the following copy settings: |
| 8 | + // - children are NOT deep copied recursively |
| 9 | + // - the HashDigest field is left empty on the copy |
| 10 | + // - the Encoding field is left empty on the copy |
| 11 | + // - the key field is deep copied |
| 12 | + // - the value field is deep copied |
| 13 | + DefaultCopySettings = CopySettings{ |
| 14 | + CopyKey: true, |
| 15 | + CopyValue: true, |
| 16 | + } |
| 17 | + |
| 18 | + // DeepCopySettings returns the following copy settings: |
| 19 | + // - children are deep copied recursively |
| 20 | + // - the HashDigest field is deep copied |
| 21 | + // - the Encoding field is deep copied |
| 22 | + // - the key field is deep copied |
| 23 | + // - the value field is deep copied |
| 24 | + DeepCopySettings = CopySettings{ |
| 25 | + CopyChildren: true, |
| 26 | + CopyCached: true, |
| 27 | + CopyKey: true, |
| 28 | + CopyValue: true, |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +// CopySettings contains settings to configure the deep copy |
| 33 | +// of a node. |
| 34 | +type CopySettings struct { |
| 35 | + // CopyChildren can be set to true to recursively deep copy the eventual |
| 36 | + // children of the node. This is false by default and should only be used |
| 37 | + // in tests since it is quite inefficient. |
| 38 | + CopyChildren bool |
| 39 | + // CopyCached can be set to true to deep copy the cached digest |
| 40 | + // and encoding fields on the current node copied. |
| 41 | + // This is false by default because in production, a node is copied |
| 42 | + // when it is about to be mutated, hence making its cached fields |
| 43 | + // no longer valid. |
| 44 | + CopyCached bool |
| 45 | + // CopyKey can be set to true to deep copy the key field of |
| 46 | + // the node. This is useful when false if the key is about to |
| 47 | + // be assigned after the Copy operation, to save a memory operation. |
| 48 | + CopyKey bool |
| 49 | + // CopyValue can be set to true to deep copy the value field of |
| 50 | + // the node. This is useful when false if the value is about to |
| 51 | + // be assigned after the Copy operation, to save a memory operation. |
| 52 | + CopyValue bool |
| 53 | +} |
| 54 | + |
6 | 55 | // Copy deep copies the branch.
|
7 | 56 | // Setting copyChildren to true will deep copy
|
8 | 57 | // children as well.
|
9 |
| -func (b *Branch) Copy(copyChildren bool) Node { |
| 58 | +func (b *Branch) Copy(settings CopySettings) Node { |
10 | 59 | cpy := &Branch{
|
11 | 60 | Dirty: b.Dirty,
|
12 | 61 | Generation: b.Generation,
|
13 | 62 | }
|
14 | 63 |
|
15 |
| - if copyChildren { |
| 64 | + if settings.CopyChildren { |
| 65 | + // Copy all fields of children if we deep copy children |
| 66 | + childSettings := settings |
| 67 | + childSettings.CopyKey = true |
| 68 | + childSettings.CopyValue = true |
| 69 | + childSettings.CopyCached = true |
16 | 70 | for i, child := range b.Children {
|
17 | 71 | if child == nil {
|
18 | 72 | continue
|
19 | 73 | }
|
20 |
| - cpy.Children[i] = child.Copy(copyChildren) |
| 74 | + cpy.Children[i] = child.Copy(childSettings) |
21 | 75 | }
|
22 | 76 | } else {
|
23 | 77 | cpy.Children = b.Children // copy interface pointers only
|
24 | 78 | }
|
25 | 79 |
|
26 |
| - if b.Key != nil { |
| 80 | + if settings.CopyKey && b.Key != nil { |
27 | 81 | cpy.Key = make([]byte, len(b.Key))
|
28 | 82 | copy(cpy.Key, b.Key)
|
29 | 83 | }
|
30 | 84 |
|
31 | 85 | // nil and []byte{} are encoded differently, watch out!
|
32 |
| - if b.Value != nil { |
| 86 | + if settings.CopyValue && b.Value != nil { |
33 | 87 | cpy.Value = make([]byte, len(b.Value))
|
34 | 88 | copy(cpy.Value, b.Value)
|
35 | 89 | }
|
36 | 90 |
|
37 |
| - if b.HashDigest != nil { |
38 |
| - cpy.HashDigest = make([]byte, len(b.HashDigest)) |
39 |
| - copy(cpy.HashDigest, b.HashDigest) |
40 |
| - } |
| 91 | + if settings.CopyCached { |
| 92 | + if b.HashDigest != nil { |
| 93 | + cpy.HashDigest = make([]byte, len(b.HashDigest)) |
| 94 | + copy(cpy.HashDigest, b.HashDigest) |
| 95 | + } |
41 | 96 |
|
42 |
| - if b.Encoding != nil { |
43 |
| - cpy.Encoding = make([]byte, len(b.Encoding)) |
44 |
| - copy(cpy.Encoding, b.Encoding) |
| 97 | + if b.Encoding != nil { |
| 98 | + cpy.Encoding = make([]byte, len(b.Encoding)) |
| 99 | + copy(cpy.Encoding, b.Encoding) |
| 100 | + } |
45 | 101 | }
|
46 | 102 |
|
47 | 103 | return cpy
|
48 | 104 | }
|
49 | 105 |
|
50 | 106 | // Copy deep copies the leaf.
|
51 |
| -func (l *Leaf) Copy(_ bool) Node { |
| 107 | +func (l *Leaf) Copy(settings CopySettings) Node { |
52 | 108 | cpy := &Leaf{
|
53 | 109 | Dirty: l.Dirty,
|
54 | 110 | Generation: l.Generation,
|
55 | 111 | }
|
56 | 112 |
|
57 |
| - if l.Key != nil { |
| 113 | + if settings.CopyKey && l.Key != nil { |
58 | 114 | cpy.Key = make([]byte, len(l.Key))
|
59 | 115 | copy(cpy.Key, l.Key)
|
60 | 116 | }
|
61 | 117 |
|
62 | 118 | // nil and []byte{} are encoded differently, watch out!
|
63 |
| - if l.Value != nil { |
| 119 | + if settings.CopyValue && l.Value != nil { |
64 | 120 | cpy.Value = make([]byte, len(l.Value))
|
65 | 121 | copy(cpy.Value, l.Value)
|
66 | 122 | }
|
67 | 123 |
|
68 |
| - if l.HashDigest != nil { |
69 |
| - cpy.HashDigest = make([]byte, len(l.HashDigest)) |
70 |
| - copy(cpy.HashDigest, l.HashDigest) |
71 |
| - } |
| 124 | + if settings.CopyCached { |
| 125 | + if l.HashDigest != nil { |
| 126 | + cpy.HashDigest = make([]byte, len(l.HashDigest)) |
| 127 | + copy(cpy.HashDigest, l.HashDigest) |
| 128 | + } |
72 | 129 |
|
73 |
| - if l.Encoding != nil { |
74 |
| - cpy.Encoding = make([]byte, len(l.Encoding)) |
75 |
| - copy(cpy.Encoding, l.Encoding) |
| 130 | + if l.Encoding != nil { |
| 131 | + cpy.Encoding = make([]byte, len(l.Encoding)) |
| 132 | + copy(cpy.Encoding, l.Encoding) |
| 133 | + } |
76 | 134 | }
|
77 | 135 |
|
78 | 136 | return cpy
|
|
0 commit comments