// Map2 is for a non-unique vindex. func (lkp *lookup) Map2(vcursor planbuilder.VCursor, ids []interface{}) ([][][]byte, error) { out := make([][][]byte, 0, len(ids)) bq := &tproto.BoundQuery{ Sql: lkp.sel, } for _, id := range ids { bq.BindVariables = map[string]interface{}{ lkp.From: id, } result, err := vcursor.Execute(bq) if err != nil { return nil, fmt.Errorf("lookup.Map: %v", err) } var ksids [][]byte for _, row := range result.Rows { num, err := getNumber(row[0].ToNative()) if err != nil { return nil, fmt.Errorf("lookup.Map: %v", err) } ksids = append(ksids, vhash(num)) } out = append(out, ksids) } return out, nil }
// Map1 is for a unique vindex. func (lkp *lookup) Map1(vcursor planbuilder.VCursor, ids []interface{}) ([][]byte, error) { out := make([][]byte, 0, len(ids)) bq := &tproto.BoundQuery{ Sql: lkp.sel, } for _, id := range ids { bq.BindVariables = map[string]interface{}{ lkp.From: id, } result, err := vcursor.Execute(bq) if err != nil { return nil, fmt.Errorf("lookup.Map: %v", err) } if len(result.Rows) == 0 { out = append(out, []byte{}) continue } if len(result.Rows) != 1 { return nil, fmt.Errorf("lookup.Map: unexpected multiple results from vindex %s: %v", lkp.Table, id) } num, err := getNumber(result.Rows[0][0].ToNative()) if err != nil { return nil, fmt.Errorf("lookup.Map: %v", err) } out = append(out, vhash(num)) } return out, nil }
// Delete deletes the entry from the vindex table. func (vind *HashAuto) Delete(vcursor planbuilder.VCursor, ids []interface{}, _ []byte) error { bq := &tproto.BoundQuery{ Sql: vind.del, BindVariables: map[string]interface{}{ vind.Column: ids, }, } if _, err := vcursor.Execute(bq); err != nil { return fmt.Errorf("hash.Delete: %v", err) } return nil }
// Create reserves the id by inserting it into the vindex table. func (vind *HashAuto) Create(vcursor planbuilder.VCursor, id interface{}) error { bq := &tproto.BoundQuery{ Sql: vind.ins, BindVariables: map[string]interface{}{ vind.Column: id, }, } if _, err := vcursor.Execute(bq); err != nil { return fmt.Errorf("hash.Create: %v", err) } return nil }
// Generate generates a new id by using the autoinc of the vindex table. func (vind *HashAuto) Generate(vcursor planbuilder.VCursor) (id int64, err error) { bq := &tproto.BoundQuery{ Sql: vind.ins, BindVariables: map[string]interface{}{ vind.Column: nil, }, } result, err := vcursor.Execute(bq) if err != nil { return 0, fmt.Errorf("hash.Generate: %v", err) } return int64(result.InsertID), err }
// Delete deletes the association between ids and ksid. func (lkp *lookup) Delete(vcursor planbuilder.VCursor, ids []interface{}, ksid []byte) error { val, err := vunhash(ksid) if err != nil { return fmt.Errorf("lookup.Delete: %v", err) } bq := &tproto.BoundQuery{ Sql: lkp.del, BindVariables: map[string]interface{}{ lkp.From: ids, lkp.To: val, }, } if _, err := vcursor.Execute(bq); err != nil { return fmt.Errorf("lookup.Delete: %v", err) } return nil }
// Create creates an association between id and ksid by inserting a row in the vindex table. func (lkp *lookup) Create(vcursor planbuilder.VCursor, id interface{}, ksid key.KeyspaceId) error { val, err := vunhash(ksid) if err != nil { return fmt.Errorf("lookup.Create: %v", err) } bq := &tproto.BoundQuery{ Sql: lkp.ins, BindVariables: map[string]interface{}{ lkp.From: id, lkp.To: val, }, } if _, err := vcursor.Execute(bq); err != nil { return fmt.Errorf("lookup.Create: %v", err) } return nil }
// Generate generates an id and associates the ksid to the new id. func (lkp *lookup) Generate(vcursor planbuilder.VCursor, ksid []byte) (id int64, err error) { val, err := vunhash(ksid) if err != nil { return 0, fmt.Errorf("lookup.Generate: %v", err) } bq := &tproto.BoundQuery{ Sql: lkp.ins, BindVariables: map[string]interface{}{ lkp.From: nil, lkp.To: val, }, } result, err := vcursor.Execute(bq) if err != nil { return 0, fmt.Errorf("lookup.Generate: %v", err) } return int64(result.InsertID), err }
// Verify returns true if id maps to ksid. func (lkp *lookup) Verify(vcursor planbuilder.VCursor, id interface{}, ksid []byte) (bool, error) { val, err := vunhash(ksid) if err != nil { return false, fmt.Errorf("lookup.Verify: %v", err) } bq := &tproto.BoundQuery{ Sql: lkp.ver, BindVariables: map[string]interface{}{ lkp.From: id, lkp.To: val, }, } result, err := vcursor.Execute(bq) if err != nil { return false, fmt.Errorf("lookup.Verify: %v", err) } if len(result.Rows) == 0 { return false, nil } return true, nil }