func (self *Gatekeeper) load(name string, num uint, counts map[uint]uint) error { log.Printf("Gatekeeper.load(%v, %v)\n", name, num) file, err := util.Open(name) if err != nil { return err } cnt := uint64(0) for { n1, key, err := file.ReadLenval() if err == io.EOF { break } if err != nil { return err } u, err := UrlTransform(string(key)) if err != nil { return err } n2, err := file.SkipLenval() if err != nil { return err } nv := Value{ FNum: num, Offset: cnt, Len: n1 + n2, } old := self.trie.Add([]byte(u), nv) if old != nil { old := old.(Value) if old.Len != 0 { counts[old.FNum] -= 1 } } cnt += n1 + n2 counts[num] += 1 } return nil }
func (self *Gatekeeper) Read(val Value) (string, error) { log.Printf("Gatekeeper.Read(%+v)\n", val) f, err := util.Open(self.dir + "/" + strconv.Itoa(int(val.FNum))) if err != nil { return "", err } if _, err := f.Seek(int64(val.Offset), 0); err != nil { return "", err } if _, err := f.SkipLenval(); err != nil { return "", err } _, res, err := f.ReadLenval() if err != nil { return "", err } log.Printf("Gatekeeper.Read(%+v) OK\n", val) return string(res), nil }