-
Notifications
You must be signed in to change notification settings - Fork 5
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
12 changed files
with
297 additions
and
147 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
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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package output | ||
|
||
import "fmt" | ||
|
||
// ParseResult structure contains parsing output, its place in the | ||
// slice, and an unexpected error, if it happened durin the parsing. | ||
type ParseResult struct { | ||
Index int | ||
Idx int | ||
Parsed Parsed | ||
Error error | ||
} | ||
|
||
func (pr ParseResult) Index() int { | ||
return pr.Idx | ||
} | ||
|
||
func (pr ParseResult) Unpack(v interface{}) error { | ||
if pr.Error != nil { | ||
return pr.Error | ||
} | ||
switch p := v.(type) { | ||
case *Parsed: | ||
*p = pr.Parsed | ||
return nil | ||
default: | ||
return fmt.Errorf("cannot use %T as Parsed", v) | ||
} | ||
} |
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,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"log" | ||
"sync" | ||
|
||
"github.com/gnames/gnlib/format" | ||
"github.com/gnames/gnparser" | ||
"github.com/gnames/gnparser/entity/output" | ||
) | ||
|
||
func parseBatch( | ||
gnp gnparser.GNParser, | ||
f io.Reader, | ||
) { | ||
batch := make([]string, batchSize) | ||
chOut := make(chan []output.Parsed) | ||
var wg sync.WaitGroup | ||
|
||
wg.Add(1) | ||
go processResults(chOut, &wg, gnp.Format()) | ||
|
||
sc := bufio.NewScanner(f) | ||
var i, count int | ||
for sc.Scan() { | ||
batch[count] = sc.Text() | ||
count++ | ||
if count == batchSize { | ||
i++ | ||
log.Printf("Parsing %d-th line\n", count*i) | ||
chOut <- gnp.ParseNames(batch) | ||
batch = make([]string, batchSize) | ||
count = 0 | ||
} | ||
} | ||
chOut <- gnp.ParseNames(batch[:count]) | ||
close(chOut) | ||
if err := sc.Err(); err != nil { | ||
log.Panic(err) | ||
} | ||
wg.Wait() | ||
} | ||
|
||
func processResults( | ||
out <-chan []output.Parsed, | ||
wg *sync.WaitGroup, | ||
f format.Format, | ||
) { | ||
defer wg.Done() | ||
if f == format.CSV { | ||
fmt.Println(output.HeaderCSV()) | ||
} | ||
for pr := range out { | ||
for i := range pr { | ||
fmt.Println(pr[i].Output(f)) | ||
} | ||
} | ||
} |
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,81 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"sync" | ||
|
||
"github.com/gnames/gnlib/format" | ||
"github.com/gnames/gnparser" | ||
"github.com/gnames/gnparser/entity/input" | ||
"github.com/gnames/gnparser/entity/output" | ||
) | ||
|
||
func getNames( | ||
ctx context.Context, | ||
f io.Reader, | ||
) <-chan input.Name { | ||
chIn := make(chan input.Name) | ||
sc := bufio.NewScanner(f) | ||
|
||
go func() { | ||
defer close(chIn) | ||
var count int | ||
for sc.Scan() { | ||
nameString := sc.Text() | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case chIn <- input.Name{Index: count, NameString: nameString}: | ||
} | ||
count++ | ||
} | ||
}() | ||
if err := sc.Err(); err != nil { | ||
log.Panic(err) | ||
} | ||
return chIn | ||
} | ||
|
||
func parseStream( | ||
gnp gnparser.GNParser, | ||
f io.Reader, | ||
) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
chIn := getNames(ctx, f) | ||
chOut := make(chan output.Parsed) | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
if gnp.Format() == format.CSV { | ||
output.HeaderCSV() | ||
} | ||
|
||
go gnp.ParseNameStream(ctx, chIn, chOut) | ||
|
||
go func() { | ||
defer cancel() | ||
defer wg.Done() | ||
var count int | ||
for { | ||
count++ | ||
if count%50_000 == 0 { | ||
log.Printf("Processing %d-th name", count) | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case v, ok := <-chOut: | ||
if !ok { | ||
return | ||
} | ||
fmt.Println(v.Output(gnp.Format())) | ||
} | ||
} | ||
}() | ||
wg.Wait() | ||
} |
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.