Ejemplo n.º 1
0
// 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
}
Ejemplo n.º 2
0
// Build creates the defRefsIndex.
func (x *defRefsIndex) Build(refs []*graph.Ref, fbr fileByteRanges, ofs byteOffsets) error {
	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
}
Ejemplo n.º 3
0
// 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
}
Ejemplo n.º 4
0
// Write implements persistedIndex.
func (x *defQueryTreeIndex) 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
}
Ejemplo n.º 5
0
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)
	}
}