-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
436 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
language: go | ||
go: | ||
- 1.x | ||
- 1.6 | ||
- 1.7.x | ||
- 1.8.x | ||
- tip | ||
before_install: | ||
- go get golang.org/x/tools/cmd/cover | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/korfuri/goref" | ||
log "github.com/sirupsen/logrus" | ||
elastic "gopkg.in/olivere/elastic.v5" | ||
) | ||
|
||
const ( | ||
// Max number of errors reported in one call to | ||
// LoadGraphToElastic | ||
maxErrorsReported = 20 | ||
) | ||
|
||
// PackageExists returns whether the provided loadpath + version tuple | ||
// exists in this index. | ||
func PackageExists(loadpath string, version int64, client *elastic.Client) bool { | ||
ctx := context.Background() | ||
docID := fmt.Sprintf("v1@%d@%s", version, loadpath) | ||
pkgDoc, _ := client.Get(). | ||
Index("goref"). | ||
Type("package"). | ||
Id(docID). | ||
Do(ctx) | ||
// TODO: handle errors better. Right now we assume that any | ||
// error is a 404 and can be ignored safely. | ||
return pkgDoc != nil | ||
} | ||
|
||
// LoadGraphToElastic loads all Packages and Refs from a PackageGraph | ||
// to the provided ES index. | ||
func LoadGraphToElastic(pg goref.PackageGraph, client *elastic.Client) ([]*goref.Ref, error) { | ||
ctx := context.Background() | ||
missedRefs := make([]*goref.Ref, 0) | ||
errs := make([]error, 0) | ||
|
||
for _, p := range pg.Packages { | ||
log.Infof("Processing package %s", p.Path) | ||
|
||
if PackageExists(p.Path, p.Version, client) { | ||
log.Infof("Package %s already exists in this index.", p) | ||
continue | ||
} | ||
|
||
log.Infof("Creating Package %s in the index", p) | ||
if _, err := client.Index(). | ||
Index("goref"). | ||
Type("package"). | ||
Id(p.DocumentID()). | ||
BodyJson(p). | ||
Do(ctx); err != nil { | ||
log.Infof("2 %s", err) | ||
return nil, err | ||
} | ||
|
||
for _, r := range p.OutRefs { | ||
log.Infof("Creating Ref document [%s] in the index", r) | ||
refDoc, err := client.Index(). | ||
Index("goref"). | ||
Type("ref"). | ||
BodyJson(r). | ||
Do(ctx) | ||
if err != nil { | ||
missedRefs = append(missedRefs, r) | ||
errs = append(errs, err) | ||
log.Infof("Create Ref document failed with err:[%s] for Ref:[%s]", err, r) | ||
} else { | ||
log.Infof("Created Ref document with docID:[%s] for Ref:[%s]", refDoc.Id, r) | ||
} | ||
} | ||
} | ||
if len(missedRefs) > 0 { | ||
errStr := fmt.Sprintf("%d refs couldn't be imported. Errors were:\n", len(missedRefs)) | ||
c := 0 | ||
for _, e := range errs { | ||
errStr = errStr + e.Error() + "\n" | ||
c = c + 1 | ||
if c >= maxErrorsReported { | ||
break | ||
} | ||
} | ||
return missedRefs, errors.New(errStr) | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/korfuri/goref" | ||
"github.com/korfuri/goref/elasticsearch" | ||
log "github.com/sirupsen/logrus" | ||
elastic "gopkg.in/olivere/elastic.v5" | ||
) | ||
|
||
const ( | ||
Usage = `elastic_goref -version 42 -include_tests <true|false> \\ | ||
-elastic_url http://localhost:9200/ -elastic_user elastic -elastic_password changeme \\ | ||
github.com/korfuri/goref github.com/korfuri/goref/elastic/main` | ||
) | ||
|
||
var ( | ||
version = flag.Int64("version", -1, | ||
"Version of the code being examined. Should increase monotonically when the code is updated.") | ||
includeTests = flag.Bool("include_tests", true, | ||
"Whether XTest packages should be included in the index.") | ||
elasticUrl = flag.String("elastic_url", "http://localhost:9200", | ||
"URL of the ElasticSearch cluster.") | ||
elasticUsername = flag.String("elastic_user", "elastic", | ||
"Username to authenticate with ElasticSearch.") | ||
elasticPassword = flag.String("elastic_password", "changeme", | ||
"Password to authenticate with ElasticSearch.") | ||
) | ||
|
||
func usage() { | ||
log.Fatal(Usage) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
args := flag.Args() | ||
|
||
if *version == -1 || len(args) == 0 { | ||
usage() | ||
} | ||
|
||
// Create a client | ||
client, err := elastic.NewClient( | ||
elastic.SetURL(*elasticUrl), | ||
elastic.SetBasicAuth(*elasticUsername, *elasticPassword)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Filter out packages that already exist at this version in | ||
// the index. | ||
packages := make([]string, 0) | ||
for _, a := range args { | ||
if !elasticsearch.PackageExists(a, *version, client) { | ||
packages = append(packages, a) | ||
} | ||
} | ||
|
||
// Index the requested packages | ||
log.Infof("Indexing packages: %v", packages) | ||
if *includeTests { | ||
log.Info("This index will include XTests.") | ||
} | ||
pg := goref.NewPackageGraph(0) | ||
pg.LoadPrograms(packages, *includeTests) | ||
log.Info("Computing the interface-implementation matrix.") | ||
pg.ComputeInterfaceImplementationMatrix() | ||
|
||
log.Infof("%d packages in the graph.", len(pg.Packages)) | ||
log.Infof("%d files in the graph.", len(pg.Files)) | ||
|
||
// Load the indexed references into ElasticSearch | ||
log.Info("Inserting references into ElasticSearch.") | ||
if missed, err := elasticsearch.LoadGraphToElastic(*pg, client); err != nil { | ||
log.Fatalf("Couldn't load %d references. Error: %s", len(missed), err) | ||
} | ||
log.Info("Done, bye.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/korfuri/goref" | ||
) | ||
|
||
func GraphAsJSON(pg goref.PackageGraph, outch chan<- []byte, errch chan<- error, done chan<- struct{}) { | ||
for _, p := range pg.Packages { | ||
for _, r := range p.InRefs { | ||
if j, err := json.Marshal(r); err == nil { | ||
outch <- j | ||
} else { | ||
errch <- err | ||
} | ||
} | ||
} | ||
done <- struct{}{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.