| 59 | } |
| 60 | |
| 61 | func (i *Idx) Part(part string, idxFilePath string, idxLength int64) (pos int64, length int64, err error) { |
| 62 | // this function is for returning a single pos and length for a given range |
| 63 | // used for non-subset indices where the records are contiguous for the data file |
| 64 | f, err := os.Open(idxFilePath) |
| 65 | if err != nil { |
| 66 | err = errors.New(e.IndexNoFile) |
| 67 | return |
| 68 | } |
| 69 | defer f.Close() |
| 70 | |
| 71 | if strings.Contains(part, "-") { |
| 72 | startend := strings.Split(part, "-") |
| 73 | start, startEr := strconv.ParseInt(startend[0], 10, 64) |
| 74 | end, endEr := strconv.ParseInt(startend[1], 10, 64) |
| 75 | if startEr != nil || endEr != nil || start <= 0 || start > int64(idxLength) || end <= 0 || end > int64(idxLength) { |
| 76 | err = errors.New(e.InvalidIndexRange) |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | // read start offset and length from index file |
| 81 | sr := io.NewSectionReader(f, (start-1)*16, 16) |
| 82 | srec := make([]int64, 2) |
| 83 | binary.Read(sr, binary.LittleEndian, &srec[0]) |
| 84 | binary.Read(sr, binary.LittleEndian, &srec[1]) |
| 85 | |
| 86 | // read end offset and length from index file |
| 87 | sr = io.NewSectionReader(f, (end-1)*16, 16) |
| 88 | erec := make([]int64, 2) |
| 89 | binary.Read(sr, binary.LittleEndian, &erec[0]) |
| 90 | binary.Read(sr, binary.LittleEndian, &erec[1]) |
| 91 | |
| 92 | pos = srec[0] |
| 93 | length = (erec[0] - srec[0]) + erec[1] |
| 94 | } else { |
| 95 | p, er := strconv.ParseInt(part, 10, 64) |
| 96 | if er != nil || p <= 0 || p > int64(idxLength) { |
| 97 | err = errors.New(e.IndexOutBounds) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | // read offset and length from index file |
| 102 | sr := io.NewSectionReader(f, (p-1)*16, 16) |
| 103 | rec := make([]int64, 2) |
| 104 | binary.Read(sr, binary.LittleEndian, &rec[0]) |
| 105 | binary.Read(sr, binary.LittleEndian, &rec[1]) |
| 106 | |
| 107 | pos = rec[0] |
| 108 | length = rec[1] |
| 109 | } |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | func (i *Idx) Range(part string, idxFilePath string, idxLength int64) (recs [][]int64, err error) { |
| 114 | // this function is for returning an array of [pos, length] for a given range |