v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/cmd/bfbuild/bfbuild.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "bytes" 6 "flag" 7 "log" 8 "os" 9 10 "v8.run/go/exp/hash/hashutil/bloom" 11 ) 12 13 var ( 14 FalsePositiveRate = flag.Float64("p", 0.001, "false positive rate") 15 CaseSensitive = flag.Bool("c", false, "case sensitive") 16 ) 17 18 func main() { 19 flag.Parse() 20 if flag.CommandLine.Arg(0) == "" { 21 println("usage: bfbuild <file>") 22 return 23 } 24 25 f, err := os.Open(flag.CommandLine.Arg(0)) 26 if err != nil { 27 log.Fatalln(err) 28 return 29 } 30 defer f.Close() 31 32 br := bufio.NewReader(f) 33 var lines uint64 34 for { 35 _, _, err := br.ReadLine() 36 if err != nil { 37 break 38 } 39 lines++ 40 } 41 42 // Seek to the beginning of the file 43 f.Seek(0, 0) 44 br.Reset(f) 45 46 // Allocate bloom filter 47 bf := bloom.NewBloom(lines, *FalsePositiveRate) 48 for { 49 line, _, err := br.ReadLine() 50 if err != nil { 51 break 52 } 53 if *CaseSensitive { 54 bf.Set(line) 55 } else { 56 bf.Set(bytes.ToLower(line)) 57 } 58 } 59 60 // Open output file 61 f, err = os.Create(flag.CommandLine.Arg(0) + ".blf") 62 if err != nil { 63 log.Fatalln(err) 64 return 65 } 66 defer f.Close() 67 68 // Write bloom filter to file 69 b := bf.Bytes() 70 if _, err := f.Write(b); err != nil { 71 log.Fatalln(err) 72 return 73 } 74 }