// Build implements unitIndexBuilder. func (x *unitFilesIndex) Build(units []*unit.SourceUnit) error { vlog.Printf("unitFilesIndex: building index...") f2u := make(filesToUnits, len(units)*10) for _, u := range units { for _, f := range u.Files { f2u.add(f, u.ID2()) } } b := phtable.Builder(len(f2u)) for file, fileUnits := range f2u { ub, err := binary.Marshal(fileUnits) if err != nil { return err } b.Add([]byte(file), ub) } h, err := b.Build() if err != nil { return err } x.phtable = h x.ready = true vlog.Printf("unitFilesIndex: done building index.") return nil }
// Build creates the defRefsIndex. func (x *defRefsIndex) Build(refs []*graph.Ref, fbr fileByteRanges, ofs byteOffsets) error { x.Lock() defer x.Unlock() vlog.Printf("defRefsIndex: building inverted def->ref index (%d refs)...", len(refs)) defToRefOfs := map[graph.RefDefKey]byteOffsets{} for i, ref := range refs { defToRefOfs[ref.RefDefKey()] = append(defToRefOfs[ref.RefDefKey()], ofs[i]) } vlog.Printf("defRefsIndex: adding %d index phtable keys...", len(defToRefOfs)) b := phtable.Builder(len(fbr)) for def, refOfs := range defToRefOfs { v, err := binary.Marshal(refOfs) if err != nil { return err } k, err := proto.Marshal(&def) if err != nil { return err } b.Add([]byte(k), v) } vlog.Printf("defRefsIndex: building index phtable...") h, err := b.Build() if err != nil { return err } h.StoreKeys = true // so defRefUnitsIndex can enumerate defs pointed to by this unit's refs x.phtable = h x.ready = true vlog.Printf("defRefsIndex: done building index.") return nil }
// Build implements defIndexBuilder. func (x *defFilesIndex) Build(defs []*graph.Def, ofs byteOffsets) error { vlog.Printf("defFilesIndex: building index...") f2ofs := make(filesToDefOfs, len(defs)/50) for i, def := range defs { if len(f2ofs[def.File]) < x.perFile && DefFilters(x.filters).SelectDef(def) { f2ofs.add(def.File, ofs[i], x.perFile) } } b := phtable.Builder(len(f2ofs)) for file, defOfs := range f2ofs { ob, err := binary.Marshal(defOfs) if err != nil { return err } b.Add([]byte(file), ob) } h, err := b.Build() if err != nil { return err } x.phtable = h x.ready = true vlog.Printf("defFilesIndex: done building index.") return nil }
// Write implements persistedIndex. func (x *defQueryIndex) Write(w io.Writer) error { if x.mt == nil { panic("no mafsaTable to write") } b, err := binary.Marshal(x.mt) if err != nil { return err } _, err = w.Write(b) return err }
// Build implements unitRefIndexBuilder. func (x *defRefUnitsIndex) Build(unitRefIndexes map[unit.ID2]*defRefsIndex) error { x.Lock() defer x.Unlock() vlog.Printf("defRefUnitsIndex: building inverted def->units index (%d units)...", len(unitRefIndexes)) defToUnits := map[graph.RefDefKey][]unit.ID2{} for u, x := range unitRefIndexes { it := x.phtable.Iterate() for { if it == nil { break } kb, _ := it.Get() var def graph.RefDefKey if err := proto.Unmarshal(kb, &def); err != nil { return err } // Set implied fields. if def.DefUnit == "" { def.DefUnit = u.Name } if def.DefUnitType == "" { def.DefUnitType = u.Type } defToUnits[def] = append(defToUnits[def], u) it = it.Next() } } vlog.Printf("defRefUnitsIndex: adding %d index phtable keys...", len(defToUnits)) b := phtable.Builder(len(defToUnits)) for def, units := range defToUnits { ub, err := binary.Marshal(units) if err != nil { return err } kb, err := proto.Marshal(&def) if err != nil { return err } b.Add(kb, ub) } vlog.Printf("defRefUnitsIndex: building phtable index...") h, err := b.Build() if err != nil { return err } x.phtable = h x.ready = true vlog.Printf("defRefUnitsIndex: done building index.") return nil }
func TestUnitOffsets_BinaryEncoding(t *testing.T) { uofs := unitOffsets{Unit: 123, byteOffsets: []int64{1, 100, 1000, 200, 2}} b, err := binary.Marshal(&uofs) if err != nil { t.Fatal(err) } var uofs2 unitOffsets if err := binary.Unmarshal(b, &uofs2); err != nil { t.Fatal(err) } if !reflect.DeepEqual(uofs2, uofs) { t.Errorf("got %v, want %v", uofs2, uofs) } }
func (b BinarySerializer) Marshal(o interface{}) []byte { d, _ := binary.Marshal(o) return d }