Ejemplo n.º 1
0
func testRepoStore_Defs_ByCommitIDs(t *testing.T, rs RepoStoreImporter) {
	const numCommits = 3
	for c := 1; c <= numCommits; c++ {
		unit := &unit.SourceUnit{Type: "t", Name: "u"}
		data := graph.Output{Defs: []*graph.Def{{DefKey: graph.DefKey{Path: "p"}}}}
		commitID := fmt.Sprintf("c%d", c)
		if err := rs.Import(commitID, unit, data); err != nil {
			t.Errorf("%s: Import(%s, %v, data): %s", rs, commitID, unit, err)
		}
		if rs, ok := rs.(RepoIndexer); ok {
			if err := rs.Index(commitID); err != nil {
				t.Fatalf("%s: Index: %s", rs, err)
			}
		}
	}

	want := []*graph.Def{
		{DefKey: graph.DefKey{CommitID: "c1", UnitType: "t", Unit: "u", Path: "p"}},
		{DefKey: graph.DefKey{CommitID: "c3", UnitType: "t", Unit: "u", Path: "p"}},
	}

	defs, err := rs.Defs(ByCommitIDs("c1", "c3"))
	if err != nil {
		t.Fatalf("%s: Defs: %s", rs, err)
	}
	sort.Sort(graph.Defs(defs))
	sort.Sort(graph.Defs(want))
	if !reflect.DeepEqual(defs, want) {
		t.Errorf("%s: Defs: got defs %v, want %v", rs, defs, want)
	}
}
Ejemplo n.º 2
0
func testMultiRepoStore_Defs_ByRepoCommitIDs(t *testing.T, mrs MultiRepoStoreImporter) {
	repos := []string{"r1", "r2", "r3"}
	commitIDs := []string{"c1", "c2"}
	for _, repo := range repos {
		for _, commitID := range commitIDs {
			if err := mrs.Import(repo, commitID, &unit.SourceUnit{Type: "t", Name: "u"}, graph.Output{Defs: []*graph.Def{
				{DefKey: graph.DefKey{Path: "p"}},
			}}); err != nil {
				t.Errorf("%s: Import: %s", mrs, err)
			}
			if mrs, ok := mrs.(MultiRepoIndexer); ok {
				if err := mrs.Index(repo, commitID); err != nil {
					t.Fatalf("%s: Index: %s", mrs, err)
				}
			}
		}
	}

	want := []*graph.Def{
		{DefKey: graph.DefKey{Repo: "r1", CommitID: "c2", UnitType: "t", Unit: "u", Path: "p"}},
		{DefKey: graph.DefKey{Repo: "r3", CommitID: "c1", UnitType: "t", Unit: "u", Path: "p"}},
	}

	defs, err := mrs.Defs(ByRepoCommitIDs(Version{Repo: "r1", CommitID: "c2"}, Version{Repo: "r3", CommitID: "c1"}))
	if err != nil {
		t.Errorf("%s: Defs: %s", mrs, err)
	}
	sort.Sort(graph.Defs(defs))
	sort.Sort(graph.Defs(want))
	if !reflect.DeepEqual(defs, want) {
		t.Errorf("%s: Defs: got defs %v, want %v", mrs, defs, want)
	}
}
Ejemplo n.º 3
0
func sortedOutput(o *graph.Output) *graph.Output {
	sort.Sort(graph.Defs(o.Defs))
	sort.Sort(graph.Refs(o.Refs))
	sort.Sort(graph.Docs(o.Docs))
	sort.Sort(ann.Anns(o.Anns))
	return o
}
Ejemplo n.º 4
0
// defsAtOffsets reads the defs at the given serialized byte offsets
// from the def data file and returns them in arbitrary order.
func (s *fsUnitStore) defsAtOffsets(ofs byteOffsets, fs []DefFilter) (defs []*graph.Def, err error) {
	vlog.Printf("%s: reading defs at %d offsets with filters %v...", s, len(ofs), fs)
	f, err := openFetcherOrOpen(s.fs, unitDefsFilename)
	if err != nil {
		return nil, err
	}
	defer func() {
		err2 := f.Close()
		if err == nil {
			err = err2
		}
	}()

	ffs := DefFilters(fs)

	p := parFetches(s.fs, fs)
	if p == 0 {
		return nil, nil
	}

	var defsLock sync.Mutex
	par := parallel.NewRun(p)
	for _, ofs_ := range ofs {
		ofs := ofs_
		par.Acquire()
		go func() {
			defer par.Release()

			if _, moreOK := LimitRemaining(fs); !moreOK {
				return
			}

			// Guess how many bytes this def is. The s3vfs (if that's the
			// VFS impl in use) will autofetch beyond that if needed.
			const byteEstimate = 2 * decodeBufSize
			r, err := rangeReader(s.fs, unitDefsFilename, f, ofs, byteEstimate)
			if err != nil {
				par.Error(err)
				return
			}
			dec := Codec.NewDecoder(r)
			var def graph.Def
			if _, err := dec.Decode(&def); err != nil {
				par.Error(err)
				return
			}
			if ffs.SelectDef(&def) {
				defsLock.Lock()
				defs = append(defs, &def)
				defsLock.Unlock()
			}
		}()
	}
	if err := par.Wait(); err != nil {
		return defs, err
	}
	sort.Sort(graph.Defs(defs))
	vlog.Printf("%s: read %v defs at %d offsets with filters %v.", s, len(defs), len(ofs), fs)
	return defs, nil
}
Ejemplo n.º 5
0
func testMultiRepoStore_Defs_ByRepoCommitIDs_ByDefQuery(t *testing.T, mrs MultiRepoStoreImporter) {
	repos := []string{"r1", "r2", "r3"}
	commitIDs := []string{"c1", "c2"}
	for _, repo := range repos {
		for _, commitID := range commitIDs {
			data := graph.Output{
				Defs: []*graph.Def{
					{DefKey: graph.DefKey{Path: "p1"}, Name: "abc-" + repo},
					{DefKey: graph.DefKey{Path: "p2"}, Name: "xyz-" + repo},
				},
			}
			if err := mrs.Import(repo, commitID, &unit.SourceUnit{Key: unit.Key{Type: "t", Name: "u"}}, data); err != nil {
				t.Errorf("%s: Import: %s", mrs, err)
			}
			if mrs, ok := mrs.(MultiRepoIndexer); ok {
				if err := mrs.Index(repo, commitID); err != nil {
					t.Fatalf("%s: Index: %s", mrs, err)
				}
			}
			if err := mrs.CreateVersion(repo, commitID); err != nil {
				t.Errorf("%s: CreateVersion: %s", mrs, err)
			}
		}
	}

	c_defQueryTreeIndex_getByQuery.set(0)

	want := []*graph.Def{
		{DefKey: graph.DefKey{Repo: "r1", CommitID: "c2", UnitType: "t", Unit: "u", Path: "p1"}, Name: "abc-r1"},
		{DefKey: graph.DefKey{Repo: "r3", CommitID: "c1", UnitType: "t", Unit: "u", Path: "p1"}, Name: "abc-r3"},
	}

	defs, err := mrs.Defs(ByRepoCommitIDs(Version{Repo: "r1", CommitID: "c2"}, Version{Repo: "r3", CommitID: "c1"}), ByDefQuery("abc"))
	if err != nil {
		t.Errorf("%s: Defs: %s", mrs, err)
	}
	sort.Sort(graph.Defs(defs))
	sort.Sort(graph.Defs(want))
	if !deepEqual(defs, want) {
		t.Errorf("%s: Defs: got defs %v, want %v", mrs, defs, want)
	}
	if isIndexedStore(mrs) {
		if want := 2; c_defQueryTreeIndex_getByQuery.get() != want {
			t.Errorf("%s: Defs: got %d index hits on tree def query index, want %d", mrs, c_defQueryTreeIndex_getByQuery.get(), want)
		}
	}
}
Ejemplo n.º 6
0
func testTreeStore_Defs_ByUnits(t *testing.T, ts TreeStoreImporter) {
	units := []*unit.SourceUnit{
		{Key: unit.Key{Type: "t1", Name: "u1"}},
		{Key: unit.Key{Type: "t2", Name: "u2"}},
		{Key: unit.Key{Type: "t3", Name: "u3"}},
	}
	for i, unit := range units {
		data := graph.Output{
			Defs: []*graph.Def{{DefKey: graph.DefKey{Path: fmt.Sprintf("p%d", i+1)}}},
		}
		if err := ts.Import(unit, data); err != nil {
			t.Errorf("%s: Import(%v, data): %s", ts, unit, err)
		}
	}
	if ts, ok := ts.(TreeIndexer); ok {
		if err := ts.Index(); err != nil {
			t.Fatalf("%s: Index: %s", ts, err)
		}
	}

	want := []*graph.Def{
		{DefKey: graph.DefKey{UnitType: "t1", Unit: "u1", Path: "p1"}},
		{DefKey: graph.DefKey{UnitType: "t3", Unit: "u3", Path: "p3"}},
	}

	c_fsTreeStore_unitsOpened.set(0)
	defs, err := ts.Defs(ByUnits(unit.ID2{Type: "t3", Name: "u3"}, unit.ID2{Type: "t1", Name: "u1"}))
	if err != nil {
		t.Errorf("%s: Defs(ByUnits): %s", ts, err)
	}
	sort.Sort(graph.Defs(defs))
	sort.Sort(graph.Defs(want))
	if !deepEqual(defs, want) {
		t.Errorf("%s: Defs(ByUnits): got defs %v, want %v", ts, defs, want)
	}
	if isIndexedStore(ts) {
		if c_fsTreeStore_unitsOpened.get() != 0 {
			t.Errorf("%s: Defs(ByUnits): got %d units opened, want none (should be able to use ByUnits filter to avoid needing to open any units)", ts, c_fsTreeStore_unitsOpened.get())
		}
	}
}
Ejemplo n.º 7
0
Archivo: filter.go Proyecto: xuy/srclib
func (ds DefsSortByKey) DefsSort(defs []*graph.Def) {
	sort.Sort(graph.Defs(defs))
}