Add inserts a path into the index, tombstoning any previous entry.
(path string, flags uint16)
| 50 | |
| 51 | // Add inserts a path into the index, tombstoning any previous entry. |
| 52 | func (idx *Index) Add(path string, flags uint16) uint32 { |
| 53 | norm := string(normalizePathBytes([]byte(path))) |
| 54 | if oldID, ok := idx.byPath[norm]; ok { |
| 55 | idx.deleted[oldID] = true |
| 56 | } |
| 57 | id := uint32(len(idx.docs)) //nolint:gosec // Index will never exceed 2^32 docs. |
| 58 | baseOff, baseLen := extractBasename([]byte(norm)) |
| 59 | idx.docs = append(idx.docs, doc{ |
| 60 | path: norm, baseOff: baseOff, baseLen: baseLen, |
| 61 | depth: strings.Count(norm, "/"), flags: flags, |
| 62 | }) |
| 63 | idx.byPath[norm] = id |
| 64 | for _, g := range extractTrigrams([]byte(norm)) { |
| 65 | idx.byGram[g] = append(idx.byGram[g], id) |
| 66 | } |
| 67 | if baseLen > 0 { |
| 68 | basename := []byte(norm[baseOff : baseOff+baseLen]) |
| 69 | p1 := prefix1(basename) |
| 70 | idx.byPrefix1[p1] = append(idx.byPrefix1[p1], id) |
| 71 | p2 := prefix2(basename) |
| 72 | idx.byPrefix2[p2] = append(idx.byPrefix2[p2], id) |
| 73 | } |
| 74 | return id |
| 75 | } |
| 76 | |
| 77 | // Remove marks the entry for path as deleted. |
| 78 | func (idx *Index) Remove(path string) bool { |