|
| 1 | +// Copyright 2021 the Dupi authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + "io/ioutil" |
| 21 | + "log" |
| 22 | + "os" |
| 23 | + "sort" |
| 24 | + |
| 25 | + "github.com/go-air/dupi" |
| 26 | + "github.com/go-air/dupi/token" |
| 27 | +) |
| 28 | + |
| 29 | +type likeCmd struct { |
| 30 | + subCmd |
| 31 | +} |
| 32 | + |
| 33 | +func newLikeCmd() *likeCmd { |
| 34 | + sc := &subCmd{flags: flag.NewFlagSet("like", flag.ExitOnError)} |
| 35 | + lc := &likeCmd{subCmd: *sc} |
| 36 | + return lc |
| 37 | +} |
| 38 | + |
| 39 | +func (lc *likeCmd) Usage() string { |
| 40 | + return "[file path]" |
| 41 | +} |
| 42 | + |
| 43 | +func (lc *likeCmd) Run(args []string) error { |
| 44 | + lc.flags.Parse(args) |
| 45 | + root := getIndexRoot() |
| 46 | + idx, err := dupi.OpenIndex(root) |
| 47 | + if err != nil { |
| 48 | + log.Fatalf("couldn't open dupi index at '%s': %s", root, err) |
| 49 | + } |
| 50 | + defer idx.Close() |
| 51 | + for _, fname := range args { |
| 52 | + if err := doFilename(idx, fname); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +type docKey struct { |
| 60 | + Path string |
| 61 | + start, end uint32 |
| 62 | +} |
| 63 | + |
| 64 | +func dkey(doc *dupi.Doc) docKey { |
| 65 | + return docKey{ |
| 66 | + Path: doc.Path, |
| 67 | + start: doc.Start, |
| 68 | + end: doc.End} |
| 69 | +} |
| 70 | + |
| 71 | +func doFilename(idx *dupi.Index, fname string) error { |
| 72 | + f, e := os.Open(fname) |
| 73 | + if e != nil { |
| 74 | + return e |
| 75 | + } |
| 76 | + defer f.Close() |
| 77 | + dat, err := ioutil.ReadAll(f) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + doc := &dupi.Doc{Path: fname, Dat: dat, End: uint32(len(dat))} |
| 82 | + blots := idx.BlotDoc(nil, doc) |
| 83 | + var toks []token.T |
| 84 | + toks = idx.TokenFunc()(nil, doc.Dat, 0) |
| 85 | + j := 0 |
| 86 | + for i := range toks { |
| 87 | + if toks[i].Tag != token.Word { |
| 88 | + continue |
| 89 | + } |
| 90 | + toks[j] = toks[i] |
| 91 | + j++ |
| 92 | + } |
| 93 | + N := uint32(idx.NumShards()) * (1 << 16) |
| 94 | + seqLen := idx.SeqLen() |
| 95 | + bm := make(map[uint32][]byte, len(blots)) |
| 96 | + for i, b := range blots { |
| 97 | + beg := toks[i].Pos |
| 98 | + end := toks[i+seqLen].Pos + uint32(len(toks[i+seqLen].Lit)) |
| 99 | + bm[b%N] = dat[beg:end] |
| 100 | + } |
| 101 | + query := idx.StartQuery(dupi.QueryMaxBlot) |
| 102 | + var db dupi.Blot |
| 103 | + found := make(map[docKey]int) |
| 104 | + for _, blot := range blots { |
| 105 | + b := blot % N |
| 106 | + db.Blot = b |
| 107 | + db.Docs = nil |
| 108 | + if err := query.Get(&db); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + for i := range db.Docs { |
| 112 | + doc := &db.Docs[i] |
| 113 | + dk := dkey(doc) |
| 114 | + if found[dk] != 0 { |
| 115 | + continue |
| 116 | + } |
| 117 | + rm, err := idx.FindBlots(bm, doc) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + if len(rm) == 0 { |
| 122 | + continue |
| 123 | + } |
| 124 | + found[dk]++ |
| 125 | + } |
| 126 | + } |
| 127 | + keys := make([]docKey, 0, len(found)) |
| 128 | + for k, _ := range found { |
| 129 | + keys = append(keys, k) |
| 130 | + } |
| 131 | + sort.Slice(keys, func(i, j int) bool { |
| 132 | + return found[keys[i]] < found[keys[j]] |
| 133 | + }) |
| 134 | + fmt.Printf("like %s:\n", fname) |
| 135 | + for _, dk := range keys { |
| 136 | + fmt.Printf("\t%s %d:%d\n", dk.Path, dk.start, dk.end) |
| 137 | + } |
| 138 | + return nil |
| 139 | +} |
0 commit comments