forked from dolthub/vitess
/
lookup_hash.go
118 lines (95 loc) · 3.52 KB
/
lookup_hash.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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vindexes
import (
"encoding/json"
)
func init() {
Register("lookup_hash", NewLookupHash)
Register("lookup_hash_unique", NewLookupHashUnique)
}
//====================================================================
// LookupHash defines a vindex that uses a lookup table.
// The table is expected to define the id column as unique. It's
// NonUnique and a Lookup.
type LookupHash struct {
name string
lkp lookup
}
// NewLookupHash creates a LookupHash vindex.
func NewLookupHash(name string, m map[string]string) (Vindex, error) {
lhu := &LookupHash{name: name}
lhu.lkp.Init(m, true)
return lhu, nil
}
// String returns the name of the vindex.
func (vind *LookupHash) String() string {
return vind.name
}
// Cost returns the cost of this vindex as 20.
func (vind *LookupHash) Cost() int {
return 20
}
// Map returns the corresponding KeyspaceId values for the given ids.
func (vind *LookupHash) Map(vcursor VCursor, ids []interface{}) ([][][]byte, error) {
return vind.lkp.MapNonUniqueLookup(vcursor, ids)
}
// Verify returns true if ids maps to ksids.
func (vind *LookupHash) Verify(vcursor VCursor, ids []interface{}, ksids [][]byte) (bool, error) {
return vind.lkp.Verify(vcursor, ids, ksids)
}
// Create reserves the id by inserting it into the vindex table.
func (vind *LookupHash) Create(vcursor VCursor, id []interface{}, ksids [][]byte) error {
return vind.lkp.Create(vcursor, id, ksids)
}
// Delete deletes the entry from the vindex table.
func (vind *LookupHash) Delete(vcursor VCursor, ids []interface{}, ksid []byte) error {
return vind.lkp.Delete(vcursor, ids, ksid)
}
// MarshalJSON returns a JSON representation of LookupHash.
func (vind *LookupHash) MarshalJSON() ([]byte, error) {
return json.Marshal(vind.lkp)
}
//====================================================================
// LookupHashUnique defines a vindex that uses a lookup table.
// The table is expected to define the id column as unique. It's
// Unique and a Lookup.
type LookupHashUnique struct {
name string
lkp lookup
}
// NewLookupHashUnique creates a LookupHashUnique vindex.
func NewLookupHashUnique(name string, m map[string]string) (Vindex, error) {
lhu := &LookupHashUnique{name: name}
lhu.lkp.Init(m, true)
return lhu, nil
}
// String returns the name of the vindex.
func (vind *LookupHashUnique) String() string {
return vind.name
}
// Cost returns the cost of this vindex as 10.
func (vind *LookupHashUnique) Cost() int {
return 10
}
// Map returns the corresponding KeyspaceId values for the given ids.
func (vind *LookupHashUnique) Map(vcursor VCursor, ids []interface{}) ([][]byte, error) {
return vind.lkp.MapUniqueLookup(vcursor, ids)
}
// Verify returns true if ids maps to ksids.
func (vind *LookupHashUnique) Verify(vcursor VCursor, ids []interface{}, ksids [][]byte) (bool, error) {
return vind.lkp.Verify(vcursor, ids, ksids)
}
// Create reserves the id by inserting it into the vindex table.
func (vind *LookupHashUnique) Create(vcursor VCursor, id []interface{}, ksids [][]byte) error {
return vind.lkp.Create(vcursor, id, ksids)
}
// Delete deletes the entry from the vindex table.
func (vind *LookupHashUnique) Delete(vcursor VCursor, ids []interface{}, ksid []byte) error {
return vind.lkp.Delete(vcursor, ids, ksid)
}
// MarshalJSON returns a JSON representation of LookupHashUnique.
func (vind *LookupHashUnique) MarshalJSON() ([]byte, error) {
return json.Marshal(vind.lkp)
}