Example #1
0
// 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
}
Example #2
0
File: filter.go Project: xuy/srclib
// withEmptyImpliedValues returns the RefDefKey with empty field
// values for fields whose value in f.def matches the implied value.
//
// This is useful because in the index, the RefDefKey keys use the
// standard implicit values: a ref to a def in the same repo has an
// empty DefRepo, etc. But the ByRefDefFilter might have the def repo
// specified, since it was created at a higher level. Only set those
// values if they are not the same as the implicit ones (because the
// implicit ones should be blank).
func (f *byRefDefFilter) withEmptyImpliedValues() graph.RefDefKey {
	def := graph.RefDefKey{DefPath: f.ByDefPath()}
	if f.ByDefRepo() != f.impliedRepo {
		def.DefRepo = f.ByDefRepo()
	}
	if f.ByDefUnitType() != f.impliedUnit.Type {
		def.DefUnitType = f.ByDefUnitType()
	}
	if f.ByDefUnit() != f.impliedUnit.Name {
		def.DefUnit = f.ByDefUnit()
	}
	return def
}