getIndexValue is used to get the IndexSchema and the value used to scan the index given the parameters. This handles prefix based scans when the index has the "_prefix" suffix. The index must support prefix iteration.
(table, index string, args ...interface{})
| 658 | // scans when the index has the "_prefix" suffix. The index must support |
| 659 | // prefix iteration. |
| 660 | func (txn *Txn) getIndexValue(table, index string, args ...interface{}) (*IndexSchema, []byte, error) { |
| 661 | // Get the table schema |
| 662 | tableSchema, ok := txn.db.schema.Tables[table] |
| 663 | if !ok { |
| 664 | return nil, nil, fmt.Errorf("invalid table '%s'", table) |
| 665 | } |
| 666 | |
| 667 | // Check for a prefix scan |
| 668 | prefixScan := false |
| 669 | if strings.HasSuffix(index, "_prefix") { |
| 670 | index = strings.TrimSuffix(index, "_prefix") |
| 671 | prefixScan = true |
| 672 | } |
| 673 | |
| 674 | // Get the index schema |
| 675 | indexSchema, ok := tableSchema.Indexes[index] |
| 676 | if !ok { |
| 677 | return nil, nil, fmt.Errorf("invalid index '%s'", index) |
| 678 | } |
| 679 | |
| 680 | // Hot-path for when there are no arguments |
| 681 | if len(args) == 0 { |
| 682 | return indexSchema, nil, nil |
| 683 | } |
| 684 | |
| 685 | // Special case the prefix scanning |
| 686 | if prefixScan { |
| 687 | prefixIndexer, ok := indexSchema.Indexer.(PrefixIndexer) |
| 688 | if !ok { |
| 689 | return indexSchema, nil, |
| 690 | fmt.Errorf("index '%s' does not support prefix scanning", index) |
| 691 | } |
| 692 | |
| 693 | val, err := prefixIndexer.PrefixFromArgs(args...) |
| 694 | if err != nil { |
| 695 | return indexSchema, nil, fmt.Errorf("index error: %v", err) |
| 696 | } |
| 697 | return indexSchema, val, err |
| 698 | } |
| 699 | |
| 700 | // Get the exact match index |
| 701 | val, err := indexSchema.Indexer.FromArgs(args...) |
| 702 | if err != nil { |
| 703 | return indexSchema, nil, fmt.Errorf("index error: %v", err) |
| 704 | } |
| 705 | return indexSchema, val, err |
| 706 | } |
| 707 | |
| 708 | // ResultIterator is used to iterate over a list of results from a query on a table. |
| 709 | // |
no test coverage detected