| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package triegen |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"strings" |
| 12 |
"text/template" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
func (b *builder) print(w io.Writer) error { |
| 18 |
b.Stats.NValueEntries = len(b.ValueBlocks) * blockSize |
| 19 |
b.Stats.NValueBytes = len(b.ValueBlocks) * blockSize * b.ValueSize |
| 20 |
b.Stats.NIndexEntries = len(b.IndexBlocks) * blockSize |
| 21 |
b.Stats.NIndexBytes = len(b.IndexBlocks) * blockSize * b.IndexSize |
| 22 |
b.Stats.NHandleBytes = len(b.Trie) * 2 * b.IndexSize |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
if len(b.Trie) == 1 { |
| 27 |
|
| 28 |
b.ASCIIBlock = b.Name + "Values" |
| 29 |
b.StarterBlock = b.Name + "Index" |
| 30 |
} else { |
| 31 |
|
| 32 |
|
| 33 |
b.ASCIIBlock = "t.ascii" |
| 34 |
b.StarterBlock = "t.utf8Start" |
| 35 |
} |
| 36 |
|
| 37 |
b.SourceType = "[]byte" |
| 38 |
if err := lookupGen.Execute(w, b); err != nil { |
| 39 |
return err |
| 40 |
} |
| 41 |
|
| 42 |
if err := trieGen.Execute(w, b); err != nil { |
| 43 |
return err |
| 44 |
} |
| 45 |
|
| 46 |
for _, c := range b.Compactions { |
| 47 |
if err := c.c.Print(w); err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func printValues(n int, values []uint64) string { |
| 56 |
w := &bytes.Buffer{} |
| 57 |
boff := n * blockSize |
| 58 |
fmt.Fprintf(w, "\t// Block %#x, offset %#x", n, boff) |
| 59 |
var newline bool |
| 60 |
for i, v := range values { |
| 61 |
if i%6 == 0 { |
| 62 |
newline = true |
| 63 |
} |
| 64 |
if v != 0 { |
| 65 |
if newline { |
| 66 |
fmt.Fprintf(w, "\n") |
| 67 |
newline = false |
| 68 |
} |
| 69 |
fmt.Fprintf(w, "\t%#02x:%#04x, ", boff+i, v) |
| 70 |
} |
| 71 |
} |
| 72 |
return w.String() |
| 73 |
} |
| 74 |
|
| 75 |
func printIndex(b *builder, nr int, n *node) string { |
| 76 |
w := &bytes.Buffer{} |
| 77 |
boff := nr * blockSize |
| 78 |
fmt.Fprintf(w, "\t// Block %#x, offset %#x", nr, boff) |
| 79 |
var newline bool |
| 80 |
for i, c := range n.children { |
| 81 |
if i%8 == 0 { |
| 82 |
newline = true |
| 83 |
} |
| 84 |
if c != nil { |
| 85 |
v := b.Compactions[c.index.compaction].Offset + uint32(c.index.index) |
| 86 |
if v != 0 { |
| 87 |
if newline { |
| 88 |
fmt.Fprintf(w, "\n") |
| 89 |
newline = false |
| 90 |
} |
| 91 |
fmt.Fprintf(w, "\t%#02x:%#02x, ", boff+i, v) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
return w.String() |
| 96 |
} |
| 97 |
|
| 98 |
var ( |
| 99 |
trieGen = template.Must(template.New("trie").Funcs(template.FuncMap{ |
| 100 |
"printValues": printValues, |
| 101 |
"printIndex": printIndex, |
| 102 |
"title": strings.Title, |
| 103 |
"dec": func(x int) int { return x - 1 }, |
| 104 |
"psize": func(n int) string { |
| 105 |
return fmt.Sprintf("%d bytes (%.2f KiB)", n, float64(n)/1024) |
| 106 |
}, |
| 107 |
}).Parse(trieTemplate)) |
| 108 |
lookupGen = template.Must(template.New("lookup").Parse(lookupTemplate)) |
| 109 |
) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
const trieTemplate = `{{$b := .}}{{$multi := gt (len .Trie) 1}} |
| 115 |
// {{.Name}}Trie. Total size: {{psize .Size}}. Checksum: {{printf "%08x" .Checksum}}. |
| 116 |
type {{.Name}}Trie struct { {{if $multi}} |
| 117 |
ascii []{{.ValueType}} // index for ASCII bytes |
| 118 |
utf8Start []{{.IndexType}} // index for UTF-8 bytes >= 0xC0 |
| 119 |
{{end}}} |
| 120 |
|
| 121 |
// func new{{title .Name}}Trie(i int) *{{.Name}}Trie { {{if $multi}} |
| 122 |
// h := {{.Name}}TrieHandles[i] |
| 123 |
// return &{{.Name}}Trie{ {{.Name}}Values[uint32(h.ascii)<<6:], {{.Name}}Index[uint32(h.multi)<<6:] } |
| 124 |
// } |
| 125 |
// |
| 126 |
// type {{.Name}}TrieHandle struct { |
| 127 |
// ascii, multi {{.IndexType}} |
| 128 |
// } |
| 129 |
// |
| 130 |
// // {{.Name}}TrieHandles: {{len .Trie}} handles, {{.Stats.NHandleBytes}} bytes |
| 131 |
// var {{.Name}}TrieHandles = [{{len .Trie}}]{{.Name}}TrieHandle{ |
| 132 |
// {{range .Trie}} { {{.ASCIIIndex}}, {{.StarterIndex}} }, // {{printf "%08x" .Checksum}}: {{.Name}} |
| 133 |
// {{end}}}{{else}} |
| 134 |
// return &{{.Name}}Trie{} |
| 135 |
// } |
| 136 |
{{end}} |
| 137 |
// lookupValue determines the type of block n and looks up the value for b. |
| 138 |
func (t *{{.Name}}Trie) lookupValue(n uint32, b byte) {{.ValueType}}{{$last := dec (len .Compactions)}} { |
| 139 |
switch { {{range $i, $c := .Compactions}} |
| 140 |
{{if eq $i $last}}default{{else}}case n < {{$c.Cutoff}}{{end}}:{{if ne $i 0}} |
| 141 |
n -= {{$c.Offset}}{{end}} |
| 142 |
return {{print $b.ValueType}}({{$c.Handler}}){{end}} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// {{.Name}}Values: {{len .ValueBlocks}} blocks, {{.Stats.NValueEntries}} entries, {{.Stats.NValueBytes}} bytes |
| 147 |
// The third block is the zero block. |
| 148 |
var {{.Name}}Values = [{{.Stats.NValueEntries}}]{{.ValueType}} { |
| 149 |
{{range $i, $v := .ValueBlocks}}{{printValues $i $v}} |
| 150 |
{{end}}} |
| 151 |
|
| 152 |
// {{.Name}}Index: {{len .IndexBlocks}} blocks, {{.Stats.NIndexEntries}} entries, {{.Stats.NIndexBytes}} bytes |
| 153 |
// Block 0 is the zero block. |
| 154 |
var {{.Name}}Index = [{{.Stats.NIndexEntries}}]{{.IndexType}} { |
| 155 |
{{range $i, $v := .IndexBlocks}}{{printIndex $b $i $v}} |
| 156 |
{{end}}} |
| 157 |
` |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
const lookupTemplate = ` |
| 162 |
// lookup{{if eq .SourceType "string"}}String{{end}} returns the trie value for the first UTF-8 encoding in s and |
| 163 |
// the width in bytes of this encoding. The size will be 0 if s does not |
| 164 |
// hold enough bytes to complete the encoding. len(s) must be greater than 0. |
| 165 |
func (t *{{.Name}}Trie) lookup{{if eq .SourceType "string"}}String{{end}}(s {{.SourceType}}) (v {{.ValueType}}, sz int) { |
| 166 |
c0 := s[0] |
| 167 |
switch { |
| 168 |
case c0 < 0x80: // is ASCII |
| 169 |
return {{.ASCIIBlock}}[c0], 1 |
| 170 |
case c0 < 0xC2: |
| 171 |
return 0, 1 // Illegal UTF-8: not a starter, not ASCII. |
| 172 |
case c0 < 0xE0: // 2-byte UTF-8 |
| 173 |
if len(s) < 2 { |
| 174 |
return 0, 0 |
| 175 |
} |
| 176 |
i := {{.StarterBlock}}[c0] |
| 177 |
c1 := s[1] |
| 178 |
if c1 < 0x80 || 0xC0 <= c1 { |
| 179 |
return 0, 1 // Illegal UTF-8: not a continuation byte. |
| 180 |
} |
| 181 |
return t.lookupValue(uint32(i), c1), 2 |
| 182 |
case c0 < 0xF0: // 3-byte UTF-8 |
| 183 |
if len(s) < 3 { |
| 184 |
return 0, 0 |
| 185 |
} |
| 186 |
i := {{.StarterBlock}}[c0] |
| 187 |
c1 := s[1] |
| 188 |
if c1 < 0x80 || 0xC0 <= c1 { |
| 189 |
return 0, 1 // Illegal UTF-8: not a continuation byte. |
| 190 |
} |
| 191 |
o := uint32(i)<<6 + uint32(c1) |
| 192 |
i = {{.Name}}Index[o] |
| 193 |
c2 := s[2] |
| 194 |
if c2 < 0x80 || 0xC0 <= c2 { |
| 195 |
return 0, 2 // Illegal UTF-8: not a continuation byte. |
| 196 |
} |
| 197 |
return t.lookupValue(uint32(i), c2), 3 |
| 198 |
case c0 < 0xF8: // 4-byte UTF-8 |
| 199 |
if len(s) < 4 { |
| 200 |
return 0, 0 |
| 201 |
} |
| 202 |
i := {{.StarterBlock}}[c0] |
| 203 |
c1 := s[1] |
| 204 |
if c1 < 0x80 || 0xC0 <= c1 { |
| 205 |
return 0, 1 // Illegal UTF-8: not a continuation byte. |
| 206 |
} |
| 207 |
o := uint32(i)<<6 + uint32(c1) |
| 208 |
i = {{.Name}}Index[o] |
| 209 |
c2 := s[2] |
| 210 |
if c2 < 0x80 || 0xC0 <= c2 { |
| 211 |
return 0, 2 // Illegal UTF-8: not a continuation byte. |
| 212 |
} |
| 213 |
o = uint32(i)<<6 + uint32(c2) |
| 214 |
i = {{.Name}}Index[o] |
| 215 |
c3 := s[3] |
| 216 |
if c3 < 0x80 || 0xC0 <= c3 { |
| 217 |
return 0, 3 // Illegal UTF-8: not a continuation byte. |
| 218 |
} |
| 219 |
return t.lookupValue(uint32(i), c3), 4 |
| 220 |
} |
| 221 |
// Illegal rune |
| 222 |
return 0, 1 |
| 223 |
} |
| 224 |
` |