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
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strings" | |
) | |
// StringParser parses XML as strings. | |
type StringParser 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
import * as fs from 'fs' | |
import * as es from 'event-stream' | |
async function processFile() { | |
console.log('Processing file started, optimized version') | |
let lineNr = 0 | |
const artNummer: string = '<ns:KoeretoejArtNummer>' | |
const artNummerCar: string = '<ns:KoeretoejArtNummer>1<' | |
const vehicleStart: string = '<ns:KoeretoejBetegnelseStruktur>' |
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
parser | 3 workers | 6 workers | 12 workers | |
---|---|---|---|---|
string | 5m17s | 5m11s | 5m11s | |
xml | 22m35s | 22m52s | 23m46s |
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
func main() { | |
flag.Parse() | |
xmlFileName := filepath.Join("/tmp/", "out.xml") | |
if _, err := os.Stat(*inFile); os.IsNotExist(err) { | |
fmt.Printf("abort: file %q does not seem to exist\n", *inFile) | |
return | |
} | |
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
// ParseExcerpt runs the string parser. | |
func (p *StringParser) ParseExcerpt(id int, lines <-chan []string, parsed chan<- string, done chan<- int) { | |
var isCar bool | |
csv, brand, model := "", "", "" | |
proc := 0 // How many excerpts did we process? | |
for excerpt := range lines { | |
for _, line := range excerpt { | |
if strings.HasPrefix(line, "<ns:KoeretoejArtNummer>") { | |
isCar = strings.HasPrefix(line, "<ns:KoeretoejArtNummer>1<") | |
continue |
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
// Same data structures as before. | |
// ParseExcerpt parses XML file using XML decoding. | |
func (p *XMLParser) ParseExcerpt(id int, lines <-chan []string, parsed chan<- string, done chan<- int) { | |
proc := 0 // How many excerpts did we process? | |
var stat vehicleStat | |
for excerpt := range lines { | |
if err := xml.Unmarshal([]byte(strings.Join(excerpt, "\n")), &stat); err != nil { | |
panic(err) // We _could_ skip it, but it's better to halt execution here. | |
} |
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
// Player represents a player in a game. | |
type Player struct { | |
ID uint32 | |
Handle, Name, Country string | |
Games []string | |
} | |
// cachedPlayer is a Player, but without the Games field. | |
type cachedPlayer struct { | |
ID uint32 |
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
// FindPlayerByID returns the player with the given ID, if exists. | |
// Otherwise, an empty Player is returned. | |
func FindPlayerByID(ID uint32) Player { | |
if cp, ok := crashyGamesPlayers[ID]; ok { | |
// cachedPlayer found. | |
return cp.convertWith(games) // Using globally cached games. | |
} | |
return Player{} | |
} |
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
// convertWith returns the Player that matches cachedPlayer, | |
// with game titles attached. | |
func (c cachedPlayer) convertWith(games []string) Player { | |
return Player{ | |
ID: c.ID, | |
Handle: c.Handle, | |
Name: c.Name, | |
Country: c.Country, | |
Games: games, | |
} |
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
var games = []string{"Flappy Fish", "Ducks'n'Dogs", "Backflip Pro"} | |
var crashyGamesPlayers map[uint32]cachedPlayer |
OlderNewer