()
| 26 | } |
| 27 | |
| 28 | func Example_indexRanges() { |
| 29 | |
| 30 | // Index ranges instead of keys: |
| 31 | // In this example at most 4 keys shares one index item. |
| 32 | |
| 33 | data := RangeData("Aaron,1,Agatha,1,Al,2,Albert,3,Alexander,5,Alison,8") |
| 34 | |
| 35 | // keyOffsets is a prebuilt index that stores range start, range end and its offset. |
| 36 | keyOffsets := []index.OffsetIndexItem{ |
| 37 | // Aaron +--> 0 |
| 38 | // Agatha | |
| 39 | // Al | |
| 40 | // Albert | |
| 41 | |
| 42 | // Alexander +--> 31 |
| 43 | // Alison | |
| 44 | |
| 45 | {Key: "Aaron", Offset: 0}, |
| 46 | {Key: "Agatha", Offset: 0}, |
| 47 | {Key: "Al", Offset: 0}, |
| 48 | {Key: "Albert", Offset: 0}, |
| 49 | |
| 50 | {Key: "Alexander", Offset: 31}, |
| 51 | {Key: "Alison", Offset: 31}, |
| 52 | } |
| 53 | |
| 54 | st, err := index.NewSlimIndex(keyOffsets, data) |
| 55 | if err != nil { |
| 56 | panic(err) |
| 57 | } |
| 58 | |
| 59 | v, found := st.RangeGet("Aaron") |
| 60 | fmt.Printf("key: %q\n found: %t\n value: %q\n", "Aaron", found, v) |
| 61 | |
| 62 | v, found = st.RangeGet("Al") |
| 63 | fmt.Printf("key: %q\n found: %t\n value: %q\n", "Al", found, v) |
| 64 | |
| 65 | v, found = st.RangeGet("foo") |
| 66 | fmt.Printf("key: %q\n found: %t\n value: %q\n", "foo", found, v) |
| 67 | |
| 68 | // Output: |
| 69 | // key: "Aaron" |
| 70 | // found: true |
| 71 | // value: "1" |
| 72 | // key: "Al" |
| 73 | // found: true |
| 74 | // value: "2" |
| 75 | // key: "foo" |
| 76 | // found: false |
| 77 | // value: "" |
| 78 | } |
nothing calls this directly
no test coverage detected