| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: xtract.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package main |
| 32 |
|
| 33 |
import ( |
| 34 |
"bufio" |
| 35 |
"cmp" |
| 36 |
"eutils" |
| 37 |
"fmt" |
| 38 |
"io" |
| 39 |
"maps" |
| 40 |
"math" |
| 41 |
"os" |
| 42 |
"runtime" |
| 43 |
"runtime/debug" |
| 44 |
"runtime/pprof" |
| 45 |
"slices" |
| 46 |
"strconv" |
| 47 |
"strings" |
| 48 |
"time" |
| 49 |
) |
| 50 |
|
| 51 |
// GLOBAL VARIABLES |
| 52 |
|
| 53 |
var ( |
| 54 |
doStem bool |
| 55 |
deStop bool |
| 56 |
) |
| 57 |
|
| 58 |
/* |
| 59 |
// UTILITIES |
| 60 |
|
| 61 |
func parseMarkup(str, cmd string) int { |
| 62 |
|
| 63 |
switch str { |
| 64 |
case "fuse", "fused": |
| 65 |
return eutils.FUSE |
| 66 |
case "space", "spaces": |
| 67 |
return eutils.SPACE |
| 68 |
case "period", "periods": |
| 69 |
return eutils.PERIOD |
| 70 |
case "concise": |
| 71 |
return eutils.CONCISE |
| 72 |
case "bracket", "brackets": |
| 73 |
return eutils.BRACKETS |
| 74 |
case "markdown": |
| 75 |
return eutils.MARKDOWN |
| 76 |
case "slash": |
| 77 |
return eutils.SLASH |
| 78 |
case "tag", "tags": |
| 79 |
return eutils.TAGS |
| 80 |
case "terse": |
| 81 |
return eutils.TERSE |
| 82 |
default: |
| 83 |
if str != "" { |
| 84 |
eutils.DisplayError("Unrecognized %s value '%s'", cmd, str) |
| 85 |
os.Exit(1) |
| 86 |
} |
| 87 |
} |
| 88 |
return eutils.NOMARKUP |
| 89 |
} |
| 90 |
*/ |
| 91 |
|
| 92 |
// MAIN FUNCTION |
| 93 |
|
| 94 |
// e.g., xtract -pattern PubmedArticle -element MedlineCitation/PMID -block Author -sep " " -element Initials,LastName |
| 95 |
|
| 96 |
func main() { |
| 97 |
|
| 98 |
// skip past executable name |
| 99 |
args := os.Args[1:] |
| 100 |
|
| 101 |
if len(args) < 1 { |
| 102 |
eutils.DisplayError("No command-line arguments supplied to xtract") |
| 103 |
os.Exit(1) |
| 104 |
} |
| 105 |
|
| 106 |
// performance arguments |
| 107 |
chanDepth := 0 |
| 108 |
farmSize := 0 |
| 109 |
heapSize := 0 |
| 110 |
numServe := 0 |
| 111 |
goGc := 0 |
| 112 |
|
| 113 |
// processing option arguments |
| 114 |
doCompress := false |
| 115 |
doCleanup := false |
| 116 |
doStrict := false |
| 117 |
doMixed := false |
| 118 |
doSelf := false |
| 119 |
deAccent := false |
| 120 |
deSymbol := false |
| 121 |
doASCII := false |
| 122 |
doStem = false |
| 123 |
deStop = true |
| 124 |
|
| 125 |
/* |
| 126 |
doUnicode := false |
| 127 |
doScript := false |
| 128 |
doMathML := false |
| 129 |
*/ |
| 130 |
|
| 131 |
// CONCURRENCY, CLEANUP, AND DEBUGGING FLAGS |
| 132 |
|
| 133 |
// do these first because -defcpu and -maxcpu can be sent from wrapper before other arguments |
| 134 |
|
| 135 |
ncpu := runtime.NumCPU() |
| 136 |
if ncpu < 1 { |
| 137 |
ncpu = 1 |
| 138 |
} |
| 139 |
|
| 140 |
// wrapper can limit maximum number of processors to use (undocumented) |
| 141 |
maxProcs := ncpu |
| 142 |
defProcs := 0 |
| 143 |
|
| 144 |
// concurrent performance tuning parameters, can be overridden by -proc and -cons |
| 145 |
numProcs := 0 |
| 146 |
serverRatio := 4 |
| 147 |
|
| 148 |
// -flag sets -strict or -mixed cleanup flags from argument |
| 149 |
flgs := "" |
| 150 |
|
| 151 |
/* |
| 152 |
unicodePolicy := "" |
| 153 |
scriptPolicy := "" |
| 154 |
mathmlPolicy := "" |
| 155 |
*/ |
| 156 |
|
| 157 |
// read data from file instead of stdin |
| 158 |
fileName := "" |
| 159 |
|
| 160 |
// flag for indexed input file |
| 161 |
turbo := false |
| 162 |
|
| 163 |
// debugging |
| 164 |
mpty := false |
| 165 |
idnt := false |
| 166 |
stts := false |
| 167 |
timr := false |
| 168 |
|
| 169 |
// profiling |
| 170 |
prfl := false |
| 171 |
|
| 172 |
// repeat the specified extraction 5 times for each -proc from 1 to nCPU |
| 173 |
trial := false |
| 174 |
|
| 175 |
inSwitch := true |
| 176 |
|
| 177 |
// get concurrency, cleanup, and debugging flags in any order |
| 178 |
for { |
| 179 |
|
| 180 |
inSwitch = true |
| 181 |
|
| 182 |
switch args[0] { |
| 183 |
// concurrency override arguments can be passed in by local wrapper script (undocumented) |
| 184 |
case "-maxcpu": |
| 185 |
maxProcs = eutils.GetNumericArg(args, "Maximum number of processors", 1, 1, ncpu) |
| 186 |
args = args[1:] |
| 187 |
case "-defcpu": |
| 188 |
defProcs = eutils.GetNumericArg(args, "Default number of processors", ncpu, 1, ncpu) |
| 189 |
args = args[1:] |
| 190 |
// performance tuning flags |
| 191 |
case "-proc": |
| 192 |
numProcs = eutils.GetNumericArg(args, "Number of processors", ncpu, 1, ncpu) |
| 193 |
args = args[1:] |
| 194 |
case "-cons": |
| 195 |
serverRatio = eutils.GetNumericArg(args, "Parser to processor ratio", 4, 1, 32) |
| 196 |
args = args[1:] |
| 197 |
case "-serv": |
| 198 |
numServe = eutils.GetNumericArg(args, "Concurrent parser count", 0, 1, 128) |
| 199 |
args = args[1:] |
| 200 |
case "-chan": |
| 201 |
chanDepth = eutils.GetNumericArg(args, "Communication channel depth", 0, ncpu, 128) |
| 202 |
args = args[1:] |
| 203 |
case "-heap": |
| 204 |
heapSize = eutils.GetNumericArg(args, "Unshuffler heap size", 8, 8, 64) |
| 205 |
args = args[1:] |
| 206 |
case "-farm": |
| 207 |
farmSize = eutils.GetNumericArg(args, "Node buffer length", 4, 4, 2048) |
| 208 |
args = args[1:] |
| 209 |
case "-gogc": |
| 210 |
goGc = eutils.GetNumericArg(args, "Garbage collection percentage", 0, 50, 1000) |
| 211 |
args = args[1:] |
| 212 |
|
| 213 |
// read data from file |
| 214 |
case "-input": |
| 215 |
fileName = eutils.GetStringArg(args, "Input file name") |
| 216 |
args = args[1:] |
| 217 |
|
| 218 |
// input is indexed with <NEXT_RECORD_SIZE> objects |
| 219 |
case "-turbo": |
| 220 |
turbo = true |
| 221 |
|
| 222 |
// data cleanup flags |
| 223 |
case "-compress", "-compressed": |
| 224 |
doCompress = true |
| 225 |
case "-spaces", "-cleanup": |
| 226 |
doCleanup = true |
| 227 |
case "-strict": |
| 228 |
doStrict = true |
| 229 |
case "-mixed": |
| 230 |
doMixed = true |
| 231 |
case "-self": |
| 232 |
doSelf = true |
| 233 |
case "-accent": |
| 234 |
deAccent = true |
| 235 |
case "-symbol": |
| 236 |
deSymbol = true |
| 237 |
case "-ascii": |
| 238 |
doASCII = true |
| 239 |
|
| 240 |
// previously visible processing flags (undocumented) |
| 241 |
case "-stems", "-stem": |
| 242 |
doStem = true |
| 243 |
case "-stops", "-stop": |
| 244 |
deStop = false |
| 245 |
|
| 246 |
// allow setting of unicode, script, and mathml flags (undocumented) |
| 247 |
case "-unicode": |
| 248 |
// unicodePolicy = GetStringArg(args, "Unicode argument") |
| 249 |
args = args[1:] |
| 250 |
case "-script": |
| 251 |
// scriptPolicy = GetStringArg(args, "Script argument") |
| 252 |
args = args[1:] |
| 253 |
case "-mathml": |
| 254 |
// mathmlPolicy = GetStringArg(args, "MathML argument") |
| 255 |
args = args[1:] |
| 256 |
|
| 257 |
case "-flag", "-flags": |
| 258 |
flgs = eutils.GetStringArg(args, "Flags argument") |
| 259 |
args = args[1:] |
| 260 |
|
| 261 |
// debugging flags |
| 262 |
case "-debug": |
| 263 |
// dbug = true |
| 264 |
case "-empty": |
| 265 |
mpty = true |
| 266 |
case "-ident": |
| 267 |
idnt = true |
| 268 |
case "-stats", "-stat": |
| 269 |
stts = true |
| 270 |
case "-timer": |
| 271 |
timr = true |
| 272 |
case "-profile": |
| 273 |
prfl = true |
| 274 |
case "-trial", "-trials": |
| 275 |
trial = true |
| 276 |
|
| 277 |
default: |
| 278 |
// if not any of the controls, set flag to break out of for loop |
| 279 |
inSwitch = false |
| 280 |
} |
| 281 |
|
| 282 |
if !inSwitch { |
| 283 |
break |
| 284 |
} |
| 285 |
|
| 286 |
// skip past argument |
| 287 |
args = args[1:] |
| 288 |
|
| 289 |
if len(args) < 1 { |
| 290 |
break |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// -flag allows script to set -strict or -mixed (or -stems, or -stops) from argument |
| 295 |
switch flgs { |
| 296 |
case "strict": |
| 297 |
doStrict = true |
| 298 |
case "mixed": |
| 299 |
doMixed = true |
| 300 |
case "stems", "stem": |
| 301 |
doStem = true |
| 302 |
case "stops", "stop": |
| 303 |
deStop = false |
| 304 |
case "none", "default": |
| 305 |
default: |
| 306 |
if flgs != "" { |
| 307 |
eutils.DisplayError("Unrecognized -flag value '%s'", flgs) |
| 308 |
os.Exit(1) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
UnicodeFix = parseMarkup(unicodePolicy, "-unicode") |
| 314 |
ScriptFix = parseMarkup(scriptPolicy, "-script") |
| 315 |
MathMLFix = parseMarkup(mathmlPolicy, "-mathml") |
| 316 |
|
| 317 |
if UnicodeFix != NOMARKUP { |
| 318 |
doUnicode = true |
| 319 |
} |
| 320 |
|
| 321 |
if ScriptFix != NOMARKUP { |
| 322 |
doScript = true |
| 323 |
} |
| 324 |
|
| 325 |
if MathMLFix != NOMARKUP { |
| 326 |
doMathML = true |
| 327 |
} |
| 328 |
*/ |
| 329 |
|
| 330 |
if numProcs == 0 { |
| 331 |
if defProcs > 0 { |
| 332 |
numProcs = defProcs |
| 333 |
} else if maxProcs > 0 { |
| 334 |
numProcs = maxProcs |
| 335 |
} |
| 336 |
} |
| 337 |
if numProcs > ncpu { |
| 338 |
numProcs = ncpu |
| 339 |
} |
| 340 |
if numProcs > maxProcs { |
| 341 |
numProcs = maxProcs |
| 342 |
} |
| 343 |
|
| 344 |
eutils.SetTunings(numProcs, numServe, serverRatio, chanDepth, farmSize, heapSize, goGc, turbo) |
| 345 |
|
| 346 |
eutils.SetOptions(doStrict, doMixed, doSelf, deAccent, deSymbol, doASCII, doCompress, doCleanup, doStem, deStop) |
| 347 |
|
| 348 |
// pretty-printing of xtract arguments |
| 349 |
if args[0] == "-pretty" { |
| 350 |
|
| 351 |
eutils.PrettyArguments(args[1:]) |
| 352 |
|
| 353 |
return |
| 354 |
} |
| 355 |
|
| 356 |
// -stats prints number of CPUs and performance tuning values if no other arguments (undocumented) |
| 357 |
if stts && len(args) < 1 { |
| 358 |
|
| 359 |
eutils.PrintStats() |
| 360 |
|
| 361 |
return |
| 362 |
} |
| 363 |
|
| 364 |
if len(args) < 1 { |
| 365 |
eutils.DisplayError("Insufficient command-line arguments supplied to xtract") |
| 366 |
os.Exit(1) |
| 367 |
} |
| 368 |
|
| 369 |
// DOCUMENTATION COMMANDS |
| 370 |
|
| 371 |
inSwitch = true |
| 372 |
|
| 373 |
switch args[0] { |
| 374 |
case "-version": |
| 375 |
fmt.Printf("%s\n", eutils.EDirectVersion) |
| 376 |
case "-help", "help", "--help": |
| 377 |
eutils.PrintHelp("xtract", "xtract-help.txt") |
| 378 |
case "-examples", "-example": |
| 379 |
eutils.PrintHelp("xtract", "xtract-examples.txt") |
| 380 |
case "-extras", "-extra", "-advanced": |
| 381 |
fmt.Printf("Please run rchive -help for local record indexing information\n") |
| 382 |
case "-internal", "-internals": |
| 383 |
eutils.PrintHelp("xtract", "xtract-internal.txt") |
| 384 |
case "-keys": |
| 385 |
eutils.PrintHelp("xtract", "xtract-keys.txt") |
| 386 |
case "-unix": |
| 387 |
eutils.PrintHelp("xtract", "xtract-unix.txt") |
| 388 |
default: |
| 389 |
// if not any of the documentation commands, keep going |
| 390 |
inSwitch = false |
| 391 |
} |
| 392 |
|
| 393 |
if inSwitch { |
| 394 |
return |
| 395 |
} |
| 396 |
|
| 397 |
// FILE NAME CAN BE SUPPLIED WITH -input COMMAND |
| 398 |
|
| 399 |
in := os.Stdin |
| 400 |
|
| 401 |
// check for data being piped into stdin |
| 402 |
isPipe := false |
| 403 |
fi, err := os.Stdin.Stat() |
| 404 |
if err == nil { |
| 405 |
isPipe = bool((fi.Mode() & os.ModeNamedPipe) != 0) |
| 406 |
} |
| 407 |
|
| 408 |
usingFile := false |
| 409 |
|
| 410 |
if fileName != "" { |
| 411 |
|
| 412 |
inFile, err := os.Open(fileName) |
| 413 |
if err != nil { |
| 414 |
eutils.DisplayError("Unable to open input file '%s'", fileName) |
| 415 |
os.Exit(1) |
| 416 |
} |
| 417 |
|
| 418 |
defer inFile.Close() |
| 419 |
|
| 420 |
// use indicated file instead of stdin |
| 421 |
in = inFile |
| 422 |
usingFile = true |
| 423 |
|
| 424 |
if isPipe && runtime.GOOS != "windows" { |
| 425 |
mode := fi.Mode().String() |
| 426 |
eutils.DisplayError("Input data from both stdin and file '%s', mode is '%s'", fileName, mode) |
| 427 |
os.Exit(1) |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
// check for -input command after extraction arguments |
| 432 |
for _, str := range args { |
| 433 |
if str == "-input" { |
| 434 |
eutils.DisplayError("Misplaced -input command") |
| 435 |
os.Exit(1) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
// START PROFILING IF REQUESTED |
| 440 |
|
| 441 |
if prfl { |
| 442 |
|
| 443 |
f, err := os.Create("cpu.pprof") |
| 444 |
if err != nil { |
| 445 |
eutils.DisplayError("Unable to create profile output file") |
| 446 |
os.Exit(1) |
| 447 |
} |
| 448 |
|
| 449 |
pprof.StartCPUProfile(f) |
| 450 |
|
| 451 |
defer pprof.StopCPUProfile() |
| 452 |
} |
| 453 |
|
| 454 |
// INITIALIZE RECORD COUNT |
| 455 |
|
| 456 |
recordCount := 0 |
| 457 |
byteCount := 0 |
| 458 |
|
| 459 |
// print processing rate and program duration |
| 460 |
printDuration := func(name string) { |
| 461 |
|
| 462 |
eutils.PrintDuration(name, recordCount, byteCount) |
| 463 |
} |
| 464 |
|
| 465 |
// NAME OF OUTPUT STRING TRANSFORMATION FILE |
| 466 |
|
| 467 |
tform := "" |
| 468 |
transform := make(map[string]string) |
| 469 |
|
| 470 |
populateTx := func(tf string, special bool) { |
| 471 |
|
| 472 |
inFile, err := os.Open(tf) |
| 473 |
if err != nil { |
| 474 |
eutils.DisplayError("Unable to open transformation file %s", err.Error()) |
| 475 |
os.Exit(1) |
| 476 |
} |
| 477 |
defer inFile.Close() |
| 478 |
|
| 479 |
scanr := bufio.NewScanner(inFile) |
| 480 |
|
| 481 |
// populate transformation map for -translate, -keywords, and -matrix output |
| 482 |
for scanr.Scan() { |
| 483 |
|
| 484 |
line := scanr.Text() |
| 485 |
|
| 486 |
if special && strings.HasPrefix(line, "#") { |
| 487 |
continue |
| 488 |
} |
| 489 |
|
| 490 |
frst, scnd := eutils.SplitInTwoLeft(line, "\t") |
| 491 |
|
| 492 |
if special && scnd == "-" { |
| 493 |
delete(transform, frst) |
| 494 |
} else { |
| 495 |
transform[frst] = scnd |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
forClassify := false |
| 501 |
|
| 502 |
if len(args) > 2 { |
| 503 |
if args[0] == "-transform" || args[0] == "-aliases" || args[0] == "-transfigure" { |
| 504 |
special := false |
| 505 |
if args[0] == "-aliases" { |
| 506 |
forClassify = true |
| 507 |
} else if args[0] == "-transfigure" { |
| 508 |
special = true |
| 509 |
} |
| 510 |
tform = args[1] |
| 511 |
args = args[2:] |
| 512 |
if tform != "" { |
| 513 |
populateTx(tform, special) |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
// SEQUENCE RECORD EXTRACTION COMMAND GENERATOR |
| 519 |
|
| 520 |
// -insd simplifies extraction of INSDSeq qualifiers |
| 521 |
if args[0] == "-insd" || args[0] == "-insd-" || args[0] == "-insd-idx" || args[0] == "-insd-xml" { |
| 522 |
|
| 523 |
addDash := true |
| 524 |
doIndex := false |
| 525 |
makeXML := false |
| 526 |
|
| 527 |
// -insd- variant suppresses use of dash as placeholder for missing qualifiers (undocumented) |
| 528 |
if args[0] == "-insd-" { |
| 529 |
addDash = false |
| 530 |
} |
| 531 |
// -insd-idx variant creates word index using the -wrp TIAB -indexer command (undocumented) |
| 532 |
if args[0] == "-insd-idx" { |
| 533 |
doIndex = true |
| 534 |
addDash = false |
| 535 |
} |
| 536 |
// -insd-xml variant used for -insdx and subsequent conversion from table to XML (undocumented) |
| 537 |
if args[0] == "-insd-xml" { |
| 538 |
makeXML = true |
| 539 |
} |
| 540 |
|
| 541 |
args = args[1:] |
| 542 |
|
| 543 |
insd := eutils.ProcessINSD(args, isPipe || usingFile, addDash, doIndex, makeXML) |
| 544 |
|
| 545 |
if !isPipe && !usingFile { |
| 546 |
// no piped input, so write output instructions |
| 547 |
fmt.Printf("xtract") |
| 548 |
for _, str := range insd { |
| 549 |
fmt.Printf(" %s", str) |
| 550 |
} |
| 551 |
fmt.Printf("\n") |
| 552 |
return |
| 553 |
} |
| 554 |
|
| 555 |
// data in pipe, so replace arguments, execute dynamically |
| 556 |
args = insd |
| 557 |
} |
| 558 |
|
| 559 |
// BIOTHINGS EXTRACTION COMMAND GENERATOR |
| 560 |
|
| 561 |
// -biopath takes a parent object and a dotted exploration path for BioThings resources (undocumented) |
| 562 |
if args[0] == "-biopath" { |
| 563 |
|
| 564 |
args = args[1:] |
| 565 |
|
| 566 |
biopath := eutils.ProcessBiopath(args, isPipe || usingFile) |
| 567 |
|
| 568 |
if !isPipe && !usingFile { |
| 569 |
// no piped input, so write output instructions |
| 570 |
fmt.Printf("xtract") |
| 571 |
for _, str := range biopath { |
| 572 |
fmt.Printf(" %s", str) |
| 573 |
} |
| 574 |
fmt.Printf("\n") |
| 575 |
return |
| 576 |
} |
| 577 |
|
| 578 |
// data in pipe, so replace arguments, execute dynamically |
| 579 |
args = biopath |
| 580 |
} |
| 581 |
|
| 582 |
// SPECIFY STRINGS TO GO BEFORE AND AFTER ENTIRE OUTPUT OR EACH RECORD |
| 583 |
|
| 584 |
head := "" |
| 585 |
tail := "" |
| 586 |
|
| 587 |
hd := "" |
| 588 |
tl := "" |
| 589 |
|
| 590 |
xmltype := "" |
| 591 |
doctype := "" |
| 592 |
|
| 593 |
for { |
| 594 |
|
| 595 |
inSwitch = true |
| 596 |
|
| 597 |
switch args[0] { |
| 598 |
case "-head": |
| 599 |
if len(args) < 2 { |
| 600 |
eutils.DisplayError("Pattern missing after -head command") |
| 601 |
os.Exit(1) |
| 602 |
} |
| 603 |
head = eutils.ConvertSlash(args[1]) |
| 604 |
// allow splitting of -head argument, keep appending until next command (undocumented) |
| 605 |
ofs, nxt := 0, args[2:] |
| 606 |
for { |
| 607 |
if len(nxt) < 1 { |
| 608 |
break |
| 609 |
} |
| 610 |
tmp := nxt[0] |
| 611 |
if strings.HasPrefix(tmp, "-") { |
| 612 |
break |
| 613 |
} |
| 614 |
ofs++ |
| 615 |
txt := eutils.ConvertSlash(tmp) |
| 616 |
if head != "" && !strings.HasSuffix(head, "\t") { |
| 617 |
head += "\t" |
| 618 |
} |
| 619 |
head += txt |
| 620 |
nxt = nxt[1:] |
| 621 |
} |
| 622 |
if ofs > 0 { |
| 623 |
args = args[ofs:] |
| 624 |
} |
| 625 |
case "-tail": |
| 626 |
if len(args) < 2 { |
| 627 |
eutils.DisplayError("Pattern missing after -tail command") |
| 628 |
os.Exit(1) |
| 629 |
} |
| 630 |
tail = eutils.ConvertSlash(args[1]) |
| 631 |
case "-hd": |
| 632 |
if len(args) < 2 { |
| 633 |
eutils.DisplayError("Pattern missing after -hd command") |
| 634 |
os.Exit(1) |
| 635 |
} |
| 636 |
hd = eutils.ConvertSlash(args[1]) |
| 637 |
case "-tl": |
| 638 |
if len(args) < 2 { |
| 639 |
eutils.DisplayError("Pattern missing after -tl command") |
| 640 |
os.Exit(1) |
| 641 |
} |
| 642 |
tl = eutils.ConvertSlash(args[1]) |
| 643 |
case "-wrp": |
| 644 |
// shortcut to wrap records in XML tags |
| 645 |
if len(args) < 2 { |
| 646 |
eutils.DisplayError("Pattern missing after -wrp command") |
| 647 |
os.Exit(1) |
| 648 |
} |
| 649 |
tmp := eutils.ConvertSlash(args[1]) |
| 650 |
lft, rgt := eutils.SplitInTwoLeft(tmp, ",") |
| 651 |
if lft != "" { |
| 652 |
head = "<" + lft + ">" |
| 653 |
tail = "</" + lft + ">" |
| 654 |
} |
| 655 |
if rgt != "" { |
| 656 |
hd = "<" + rgt + ">" |
| 657 |
tl = "</" + rgt + ">" |
| 658 |
} |
| 659 |
case "-set": |
| 660 |
if len(args) < 2 { |
| 661 |
eutils.DisplayError("Pattern missing after -set command") |
| 662 |
os.Exit(1) |
| 663 |
} |
| 664 |
tmp := eutils.ConvertSlash(args[1]) |
| 665 |
if tmp != "" { |
| 666 |
head = "<" + tmp + ">" |
| 667 |
tail = "</" + tmp + ">" |
| 668 |
} |
| 669 |
case "-rec": |
| 670 |
if len(args) < 2 { |
| 671 |
eutils.DisplayError("Pattern missing after -rec command") |
| 672 |
os.Exit(1) |
| 673 |
} |
| 674 |
tmp := eutils.ConvertSlash(args[1]) |
| 675 |
if tmp != "" { |
| 676 |
hd = "<" + tmp + ">" |
| 677 |
tl = "</" + tmp + ">" |
| 678 |
} |
| 679 |
case "-xml": |
| 680 |
if len(args) < 2 { |
| 681 |
eutils.DisplayError("Pattern missing after -xml command") |
| 682 |
os.Exit(1) |
| 683 |
} |
| 684 |
xmltype = eutils.CleanXmltype(args[1]) |
| 685 |
case "-doctype": |
| 686 |
if len(args) < 2 { |
| 687 |
eutils.DisplayError("Pattern missing after -doctype command") |
| 688 |
os.Exit(1) |
| 689 |
} |
| 690 |
doctype = eutils.CleanDoctype(args[1]) |
| 691 |
default: |
| 692 |
// if not any of the controls, set flag to break out of for loop |
| 693 |
inSwitch = false |
| 694 |
} |
| 695 |
|
| 696 |
if !inSwitch { |
| 697 |
break |
| 698 |
} |
| 699 |
|
| 700 |
// skip past arguments |
| 701 |
args = args[2:] |
| 702 |
|
| 703 |
if len(args) < 1 { |
| 704 |
eutils.DisplayError("Insufficient command-line arguments supplied to xtract") |
| 705 |
os.Exit(1) |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
if head != "" { |
| 710 |
// first prepend doctype |
| 711 |
if doctype != "" { |
| 712 |
head = doctype + "\n" + head |
| 713 |
} |
| 714 |
// then prepend xml line |
| 715 |
if xmltype != "" { |
| 716 |
head = xmltype + "\n" + head |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
// CREATE XML BLOCK READER FROM STDIN OR FILE |
| 721 |
|
| 722 |
const FirstBuffSize = 4096 |
| 723 |
|
| 724 |
getFirstBlock := func() string { |
| 725 |
|
| 726 |
buffer := make([]byte, FirstBuffSize) |
| 727 |
n, err := in.Read(buffer) |
| 728 |
if err != nil && err != io.EOF { |
| 729 |
eutils.DisplayError("Unable to read first block: %s", err.Error()) |
| 730 |
// os.Exit(1) |
| 731 |
} |
| 732 |
bufr := buffer[:n] |
| 733 |
return string(bufr) |
| 734 |
} |
| 735 |
|
| 736 |
first := getFirstBlock() |
| 737 |
|
| 738 |
// trim leading byte order mark, if present |
| 739 |
first = eutils.TrimBOM(first) |
| 740 |
|
| 741 |
mlt := io.MultiReader(strings.NewReader(first), in) |
| 742 |
|
| 743 |
isJsn := false |
| 744 |
isAsn := false |
| 745 |
isGbf := false |
| 746 |
matched := 0 |
| 747 |
|
| 748 |
// auto-detect XML, JSON, or ASN.1 format |
| 749 |
if first != "" { |
| 750 |
posJ1 := strings.Index(first, "{") |
| 751 |
posJ2 := strings.Index(first, "\":") |
| 752 |
posJ3 := strings.Index(first, "\" :") |
| 753 |
if posJ1 >= 0 && posJ2 >= 0 && posJ1 < posJ2 { |
| 754 |
isJsn = true |
| 755 |
matched++ |
| 756 |
} else if posJ1 >= 0 && posJ3 >= 0 && posJ1 < posJ3 { |
| 757 |
isJsn = true |
| 758 |
matched++ |
| 759 |
} else { |
| 760 |
posJ1 = FirstBuffSize |
| 761 |
posJ2 = FirstBuffSize |
| 762 |
} |
| 763 |
posA1 := strings.Index(first, "::=") |
| 764 |
posA2 := strings.Index(first, "{") |
| 765 |
if posA1 >= 0 && posA2 >= 0 && posA1 < posA2 { |
| 766 |
isAsn = true |
| 767 |
matched++ |
| 768 |
} else { |
| 769 |
posA1 = FirstBuffSize |
| 770 |
posA2 = FirstBuffSize |
| 771 |
} |
| 772 |
posG1 := strings.Index(first, "LOCUS") |
| 773 |
posG2 := strings.Index(first, "DEFINITION") |
| 774 |
if posG1 >= 0 && posG2 >= 0 && posG1 < posG2 { |
| 775 |
isGbf = true |
| 776 |
matched++ |
| 777 |
} else { |
| 778 |
posG1 = FirstBuffSize |
| 779 |
posG2 = FirstBuffSize |
| 780 |
} |
| 781 |
posX1 := strings.Index(first, "<") |
| 782 |
posX2 := strings.Index(first, ">") |
| 783 |
if posX1 >= 0 && posX2 >= 0 && posX1 < posX2 { |
| 784 |
matched++ |
| 785 |
} else { |
| 786 |
posX1 = FirstBuffSize |
| 787 |
posX2 = FirstBuffSize |
| 788 |
} |
| 789 |
if matched > 1 { |
| 790 |
if posX1 < posJ1 && posX1 < posA1 && posX1 < posG1 { |
| 791 |
isJsn = false |
| 792 |
isAsn = false |
| 793 |
isGbf = false |
| 794 |
} else if posJ1 < posA1 && posJ1 < posG1 { |
| 795 |
isAsn = false |
| 796 |
isGbf = false |
| 797 |
} else if posA1 < posJ1 && posA1 < posG1 { |
| 798 |
isJsn = false |
| 799 |
isGbf = false |
| 800 |
} else if posG1 < posJ1 && posG1 < posA1 { |
| 801 |
isJsn = false |
| 802 |
isAsn = false |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
var xmlChan <-chan string |
| 808 |
|
| 809 |
if isJsn { |
| 810 |
xmlChan = eutils.JSONConverter(mlt, "root", "", "element") |
| 811 |
mlt = nil |
| 812 |
} else if isAsn { |
| 813 |
xmlChan = eutils.ASN1Converter(mlt, "", "") |
| 814 |
mlt = nil |
| 815 |
} else if isGbf { |
| 816 |
xmlChan = eutils.GenBankConverter(mlt) |
| 817 |
mlt = nil |
| 818 |
} |
| 819 |
|
| 820 |
rdr := eutils.CreateXMLStreamer(mlt, xmlChan) |
| 821 |
if rdr == nil { |
| 822 |
eutils.DisplayError("Unable to create XML Block Reader") |
| 823 |
os.Exit(1) |
| 824 |
} |
| 825 |
|
| 826 |
// CONFIRM INPUT DATA AVAILABILITY AFTER RUNNING COMMAND GENERATORS |
| 827 |
|
| 828 |
if fileName == "" && runtime.GOOS != "windows" { |
| 829 |
|
| 830 |
fromStdin := bool((fi.Mode() & os.ModeCharDevice) == 0) |
| 831 |
if !isPipe || !fromStdin { |
| 832 |
mode := fi.Mode().String() |
| 833 |
eutils.DisplayError("No data supplied to xtract from stdin or file, mode is '%s'", mode) |
| 834 |
os.Exit(1) |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if !usingFile && !isPipe { |
| 839 |
|
| 840 |
eutils.DisplayError("No XML input data supplied to xtract") |
| 841 |
os.Exit(1) |
| 842 |
} |
| 843 |
|
| 844 |
// XML VALIDATION |
| 845 |
|
| 846 |
nextArg := func() (string, bool) { |
| 847 |
|
| 848 |
if len(args) < 1 { |
| 849 |
return "", false |
| 850 |
} |
| 851 |
|
| 852 |
// remove next token from slice |
| 853 |
nxt := args[0] |
| 854 |
args = args[1:] |
| 855 |
|
| 856 |
return nxt, true |
| 857 |
} |
| 858 |
|
| 859 |
if args[0] == "-verify" || args[0] == "-validate" { |
| 860 |
|
| 861 |
// skip past command name |
| 862 |
args = args[1:] |
| 863 |
|
| 864 |
find := "" |
| 865 |
html := false |
| 866 |
max := 0 |
| 867 |
|
| 868 |
// look for optional arguments |
| 869 |
for { |
| 870 |
arg, ok := nextArg() |
| 871 |
if !ok { |
| 872 |
break |
| 873 |
} |
| 874 |
|
| 875 |
switch arg { |
| 876 |
case "-find": |
| 877 |
// set identifier object |
| 878 |
find, _ = nextArg() |
| 879 |
case "-html": |
| 880 |
html = true |
| 881 |
case "-max": |
| 882 |
// override reportable depth |
| 883 |
cutoff, _ := nextArg() |
| 884 |
val, err := strconv.Atoi(cutoff) |
| 885 |
if err == nil && val > 0 { |
| 886 |
max = val |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
recordCount = eutils.ValidateXML(rdr, find, html, max) |
| 892 |
|
| 893 |
debug.FreeOSMemory() |
| 894 |
|
| 895 |
// suppress printing of lines if not properly counted |
| 896 |
if recordCount == 1 { |
| 897 |
recordCount = 0 |
| 898 |
} |
| 899 |
|
| 900 |
if timr { |
| 901 |
printDuration("lines") |
| 902 |
} |
| 903 |
|
| 904 |
return |
| 905 |
} |
| 906 |
|
| 907 |
// MISCELLANEOUS TIMING COMMANDS |
| 908 |
|
| 909 |
if args[0] == "-chunk" { |
| 910 |
|
| 911 |
for str := range rdr { |
| 912 |
recordCount++ |
| 913 |
byteCount += len(str) |
| 914 |
} |
| 915 |
|
| 916 |
printDuration("blocks") |
| 917 |
|
| 918 |
return |
| 919 |
} |
| 920 |
|
| 921 |
if args[0] == "-split" { |
| 922 |
|
| 923 |
if len(args) > 1 { |
| 924 |
if args[1] == "-pattern" { |
| 925 |
// skip past -split if followed by -pattern |
| 926 |
args = args[1:] |
| 927 |
} |
| 928 |
} |
| 929 |
if len(args) < 2 { |
| 930 |
eutils.DisplayError("Pattern missing after -split command") |
| 931 |
os.Exit(1) |
| 932 |
} |
| 933 |
pat := args[1] |
| 934 |
|
| 935 |
eutils.PartitionXML(pat, "", turbo, rdr, |
| 936 |
func(str string) { |
| 937 |
recordCount++ |
| 938 |
byteCount += len(str) |
| 939 |
}) |
| 940 |
|
| 941 |
printDuration("patterns") |
| 942 |
|
| 943 |
return |
| 944 |
} |
| 945 |
|
| 946 |
if args[0] == "-token" { |
| 947 |
|
| 948 |
eutils.StreamTokens(rdr, |
| 949 |
func(tkn eutils.XMLToken) { |
| 950 |
recordCount++ |
| 951 |
byteCount += len(tkn.Name) + len(tkn.Attr) |
| 952 |
}) |
| 953 |
|
| 954 |
printDuration("tokens") |
| 955 |
|
| 956 |
return |
| 957 |
} |
| 958 |
|
| 959 |
// INDEXED XML FILE PREPARATION |
| 960 |
|
| 961 |
// cat carotene.xml | xtract -timer -index -pattern PubmedArticle > carindex.txt |
| 962 |
// xtract -timer -turbo -input carindex.txt -pattern PubmedArticle -element LastName |
| 963 |
if args[0] == "-index" { |
| 964 |
|
| 965 |
if len(args) > 1 { |
| 966 |
if args[1] == "-pattern" { |
| 967 |
// skip past -index if followed by -pattern |
| 968 |
args = args[1:] |
| 969 |
} |
| 970 |
} |
| 971 |
if len(args) < 2 { |
| 972 |
eutils.DisplayError("Pattern missing after -index command") |
| 973 |
os.Exit(1) |
| 974 |
} |
| 975 |
pat := args[1] |
| 976 |
|
| 977 |
retlength := len("\n") |
| 978 |
|
| 979 |
if head != "" { |
| 980 |
os.Stdout.WriteString(head) |
| 981 |
os.Stdout.WriteString("\n") |
| 982 |
} |
| 983 |
|
| 984 |
eutils.PartitionXML(pat, "", false, rdr, |
| 985 |
func(str string) { |
| 986 |
recordCount++ |
| 987 |
nxt := len(str) |
| 988 |
byteCount += nxt |
| 989 |
newln := false |
| 990 |
|
| 991 |
if !strings.HasSuffix(str, "\n") { |
| 992 |
nxt += retlength |
| 993 |
newln = true |
| 994 |
} |
| 995 |
|
| 996 |
os.Stdout.WriteString("<NEXT_RECORD_SIZE>") |
| 997 |
val := strconv.Itoa(nxt) |
| 998 |
os.Stdout.WriteString(val) |
| 999 |
os.Stdout.WriteString("</NEXT_RECORD_SIZE>\n") |
| 1000 |
|
| 1001 |
os.Stdout.WriteString(str) |
| 1002 |
if newln { |
| 1003 |
os.Stdout.WriteString("\n") |
| 1004 |
} |
| 1005 |
}) |
| 1006 |
|
| 1007 |
if tail != "" { |
| 1008 |
os.Stdout.WriteString(tail) |
| 1009 |
os.Stdout.WriteString("\n") |
| 1010 |
} |
| 1011 |
|
| 1012 |
debug.FreeOSMemory() |
| 1013 |
|
| 1014 |
if timr { |
| 1015 |
printDuration("records") |
| 1016 |
} |
| 1017 |
|
| 1018 |
return |
| 1019 |
} |
| 1020 |
|
| 1021 |
// ENSURE PRESENCE OF PATTERN ARGUMENT |
| 1022 |
|
| 1023 |
if len(args) < 1 { |
| 1024 |
eutils.DisplayError("Insufficient command-line arguments supplied to xtract") |
| 1025 |
os.Exit(1) |
| 1026 |
} |
| 1027 |
|
| 1028 |
// allow -record as synonym of -pattern (undocumented) |
| 1029 |
if args[0] == "-record" || args[0] == "-Record" { |
| 1030 |
args[0] = "-pattern" |
| 1031 |
} |
| 1032 |
|
| 1033 |
// make sure top-level -pattern command is next |
| 1034 |
if args[0] != "-pattern" && args[0] != "-Pattern" { |
| 1035 |
eutils.DisplayError("No -pattern in command-line arguments") |
| 1036 |
os.Exit(1) |
| 1037 |
} |
| 1038 |
if len(args) < 2 { |
| 1039 |
eutils.DisplayError("Item missing after -pattern command") |
| 1040 |
os.Exit(1) |
| 1041 |
} |
| 1042 |
|
| 1043 |
topPat := args[1] |
| 1044 |
if topPat == "" { |
| 1045 |
eutils.DisplayError("Item missing after -pattern command") |
| 1046 |
os.Exit(1) |
| 1047 |
} |
| 1048 |
if strings.HasPrefix(topPat, "-") { |
| 1049 |
eutils.DisplayError("Misplaced %s command", topPat) |
| 1050 |
os.Exit(1) |
| 1051 |
} |
| 1052 |
|
| 1053 |
// look for -pattern Parent/* construct for heterogeneous data, e.g., -pattern PubmedArticleSet/* |
| 1054 |
topPattern, star := eutils.SplitInTwoLeft(topPat, "/") |
| 1055 |
if topPattern == "" { |
| 1056 |
return |
| 1057 |
} |
| 1058 |
|
| 1059 |
parent := "" |
| 1060 |
if star == "*" { |
| 1061 |
parent = topPattern |
| 1062 |
} else if star != "" { |
| 1063 |
eutils.DisplayError("-pattern Parent/Child construct is not supported") |
| 1064 |
os.Exit(1) |
| 1065 |
} |
| 1066 |
|
| 1067 |
// SAVE ONLY RECORDS WITH NON-ASCII CHARACTERS |
| 1068 |
|
| 1069 |
// -pattern record_name -select -nonascii |
| 1070 |
if len(args) == 4 && args[2] == "-select" && args[3] == "-nonascii" { |
| 1071 |
|
| 1072 |
xmlq := eutils.CreateXMLProducer(topPattern, star, false, rdr) |
| 1073 |
fchq := eutils.CreateUnicoders(xmlq) |
| 1074 |
unsq := eutils.CreateXMLUnshuffler(fchq) |
| 1075 |
|
| 1076 |
if xmlq == nil || fchq == nil || unsq == nil { |
| 1077 |
eutils.DisplayError("Unable to create selector") |
| 1078 |
os.Exit(1) |
| 1079 |
} |
| 1080 |
|
| 1081 |
if head != "" { |
| 1082 |
os.Stdout.WriteString(head) |
| 1083 |
os.Stdout.WriteString("\n") |
| 1084 |
} |
| 1085 |
|
| 1086 |
// drain output channel |
| 1087 |
for curr := range unsq { |
| 1088 |
|
| 1089 |
str := curr.Text |
| 1090 |
|
| 1091 |
if str == "" { |
| 1092 |
continue |
| 1093 |
} |
| 1094 |
|
| 1095 |
if hd != "" { |
| 1096 |
os.Stdout.WriteString(hd) |
| 1097 |
os.Stdout.WriteString("\n") |
| 1098 |
} |
| 1099 |
|
| 1100 |
// send result to output |
| 1101 |
os.Stdout.WriteString(str) |
| 1102 |
if !strings.HasSuffix(str, "\n") { |
| 1103 |
os.Stdout.WriteString("\n") |
| 1104 |
} |
| 1105 |
|
| 1106 |
if tl != "" { |
| 1107 |
os.Stdout.WriteString(tl) |
| 1108 |
os.Stdout.WriteString("\n") |
| 1109 |
} |
| 1110 |
|
| 1111 |
recordCount++ |
| 1112 |
runtime.Gosched() |
| 1113 |
} |
| 1114 |
|
| 1115 |
if tail != "" { |
| 1116 |
os.Stdout.WriteString(tail) |
| 1117 |
os.Stdout.WriteString("\n") |
| 1118 |
} |
| 1119 |
|
| 1120 |
debug.FreeOSMemory() |
| 1121 |
|
| 1122 |
if timr { |
| 1123 |
printDuration("records") |
| 1124 |
} |
| 1125 |
|
| 1126 |
return |
| 1127 |
} |
| 1128 |
|
| 1129 |
// READ FILE OF IDENTIFIERS AND CONCURRENTLY EXTRACT SELECTED RECORDS |
| 1130 |
|
| 1131 |
// -pattern record_name -select parent/element@attribute^version -in file_of_identifiers |
| 1132 |
if len(args) == 6 && args[2] == "-select" && (args[4] == "-in" || args[4] == "-retaining") { |
| 1133 |
|
| 1134 |
indx := args[3] |
| 1135 |
unqe := args[5] |
| 1136 |
|
| 1137 |
// read file of identifiers to use for filtering |
| 1138 |
fl, err := os.Open(unqe) |
| 1139 |
if err != nil { |
| 1140 |
eutils.DisplayError("Unable to open identifier file '%s'", unqe) |
| 1141 |
os.Exit(1) |
| 1142 |
} |
| 1143 |
|
| 1144 |
// create map that records each UID |
| 1145 |
order := make(map[string]bool) |
| 1146 |
|
| 1147 |
scanr := bufio.NewScanner(fl) |
| 1148 |
|
| 1149 |
// read lines of identifiers |
| 1150 |
for scanr.Scan() { |
| 1151 |
|
| 1152 |
line := scanr.Text() |
| 1153 |
id, _ := eutils.SplitInTwoLeft(line, "\t") |
| 1154 |
|
| 1155 |
id = eutils.SortStringByWords(id) |
| 1156 |
|
| 1157 |
// add identifier to map |
| 1158 |
order[id] = true |
| 1159 |
} |
| 1160 |
|
| 1161 |
fl.Close() |
| 1162 |
|
| 1163 |
xmlq := eutils.CreateXMLProducer(topPattern, star, false, rdr) |
| 1164 |
fchq := eutils.CreateSelectors(topPattern, indx, order, xmlq) |
| 1165 |
unsq := eutils.CreateXMLUnshuffler(fchq) |
| 1166 |
|
| 1167 |
if xmlq == nil || fchq == nil || unsq == nil { |
| 1168 |
eutils.DisplayError("Unable to create selector") |
| 1169 |
os.Exit(1) |
| 1170 |
} |
| 1171 |
|
| 1172 |
if head != "" { |
| 1173 |
os.Stdout.WriteString(head) |
| 1174 |
os.Stdout.WriteString("\n") |
| 1175 |
} |
| 1176 |
|
| 1177 |
// drain output channel |
| 1178 |
for curr := range unsq { |
| 1179 |
|
| 1180 |
str := curr.Text |
| 1181 |
|
| 1182 |
if str == "" { |
| 1183 |
continue |
| 1184 |
} |
| 1185 |
|
| 1186 |
if hd != "" { |
| 1187 |
os.Stdout.WriteString(hd) |
| 1188 |
os.Stdout.WriteString("\n") |
| 1189 |
} |
| 1190 |
|
| 1191 |
// send result to output |
| 1192 |
os.Stdout.WriteString(str) |
| 1193 |
if !strings.HasSuffix(str, "\n") { |
| 1194 |
os.Stdout.WriteString("\n") |
| 1195 |
} |
| 1196 |
|
| 1197 |
if tl != "" { |
| 1198 |
os.Stdout.WriteString(tl) |
| 1199 |
os.Stdout.WriteString("\n") |
| 1200 |
} |
| 1201 |
|
| 1202 |
recordCount++ |
| 1203 |
runtime.Gosched() |
| 1204 |
} |
| 1205 |
|
| 1206 |
if tail != "" { |
| 1207 |
os.Stdout.WriteString(tail) |
| 1208 |
os.Stdout.WriteString("\n") |
| 1209 |
} |
| 1210 |
|
| 1211 |
debug.FreeOSMemory() |
| 1212 |
|
| 1213 |
if timr { |
| 1214 |
printDuration("records") |
| 1215 |
} |
| 1216 |
|
| 1217 |
return |
| 1218 |
} |
| 1219 |
|
| 1220 |
// READ FILE OF IDENTIFIERS AND EXCLUDE SELECTED RECORDS |
| 1221 |
|
| 1222 |
// -pattern record_name -exclude element -excluding file_of_identifiers (undocumented) |
| 1223 |
if len(args) == 6 && args[2] == "-select" && args[4] == "-excluding" { |
| 1224 |
|
| 1225 |
indx := args[3] |
| 1226 |
unqe := args[5] |
| 1227 |
|
| 1228 |
// read file of identifiers to use for filtering |
| 1229 |
fl, err := os.Open(unqe) |
| 1230 |
if err != nil { |
| 1231 |
eutils.DisplayError("Unable to open identifier file '%s'", unqe) |
| 1232 |
os.Exit(1) |
| 1233 |
} |
| 1234 |
|
| 1235 |
// create map that records each UID |
| 1236 |
order := make(map[string]bool) |
| 1237 |
|
| 1238 |
scanr := bufio.NewScanner(fl) |
| 1239 |
|
| 1240 |
// read lines of identifiers |
| 1241 |
for scanr.Scan() { |
| 1242 |
|
| 1243 |
line := scanr.Text() |
| 1244 |
id, _ := eutils.SplitInTwoLeft(line, "\t") |
| 1245 |
id = strings.ToLower(id) |
| 1246 |
|
| 1247 |
// add identifier to map |
| 1248 |
order[id] = true |
| 1249 |
} |
| 1250 |
|
| 1251 |
fl.Close() |
| 1252 |
|
| 1253 |
find := eutils.ParseIndex(indx) |
| 1254 |
|
| 1255 |
if head != "" { |
| 1256 |
os.Stdout.WriteString(head) |
| 1257 |
os.Stdout.WriteString("\n") |
| 1258 |
} |
| 1259 |
|
| 1260 |
eutils.PartitionXML(topPattern, star, false, rdr, |
| 1261 |
func(str string) { |
| 1262 |
recordCount++ |
| 1263 |
|
| 1264 |
id := eutils.FindIdentifier(str[:], parent, find) |
| 1265 |
if id != "" { |
| 1266 |
id = strings.ToLower(id) |
| 1267 |
_, ok := order[id] |
| 1268 |
if ok { |
| 1269 |
// in exclusion list, skip |
| 1270 |
return |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
if hd != "" { |
| 1275 |
os.Stdout.WriteString(hd) |
| 1276 |
os.Stdout.WriteString("\n") |
| 1277 |
} |
| 1278 |
|
| 1279 |
// write selected record |
| 1280 |
os.Stdout.WriteString(str[:]) |
| 1281 |
os.Stdout.WriteString("\n") |
| 1282 |
|
| 1283 |
if tl != "" { |
| 1284 |
os.Stdout.WriteString(tl) |
| 1285 |
os.Stdout.WriteString("\n") |
| 1286 |
} |
| 1287 |
}) |
| 1288 |
|
| 1289 |
if tail != "" { |
| 1290 |
os.Stdout.WriteString(tail) |
| 1291 |
os.Stdout.WriteString("\n") |
| 1292 |
} |
| 1293 |
|
| 1294 |
debug.FreeOSMemory() |
| 1295 |
|
| 1296 |
if timr { |
| 1297 |
printDuration("records") |
| 1298 |
} |
| 1299 |
|
| 1300 |
return |
| 1301 |
} |
| 1302 |
|
| 1303 |
// READ ORDERED FILE OF IDENTIFIERS AND XML STRINGS, APPEND XML JUST INSIDE CLOSING TAG OF APPROPRIATE RECORD |
| 1304 |
|
| 1305 |
// -pattern record_name -select element -appending file_of_identifiers_and_metadata (undocumented) |
| 1306 |
if len(args) == 6 && args[2] == "-select" && args[4] == "-appending" { |
| 1307 |
|
| 1308 |
indx := args[3] |
| 1309 |
apnd := args[5] |
| 1310 |
|
| 1311 |
fl, err := os.Open(apnd) |
| 1312 |
if err != nil { |
| 1313 |
eutils.DisplayError("Unable to open transformation file '%s'", apnd) |
| 1314 |
os.Exit(1) |
| 1315 |
} |
| 1316 |
|
| 1317 |
scanr := bufio.NewScanner(fl) |
| 1318 |
|
| 1319 |
find := eutils.ParseIndex(indx) |
| 1320 |
|
| 1321 |
if head != "" { |
| 1322 |
os.Stdout.WriteString(head) |
| 1323 |
os.Stdout.WriteString("\n") |
| 1324 |
} |
| 1325 |
|
| 1326 |
rgt := "</" + topPattern + ">" |
| 1327 |
|
| 1328 |
eutils.PartitionXML(topPattern, star, false, rdr, |
| 1329 |
func(str string) { |
| 1330 |
recordCount++ |
| 1331 |
|
| 1332 |
id := eutils.FindIdentifier(str[:], parent, find) |
| 1333 |
if id == "" { |
| 1334 |
return |
| 1335 |
} |
| 1336 |
id = strings.ToLower(id) |
| 1337 |
|
| 1338 |
for scanr.Scan() { |
| 1339 |
|
| 1340 |
line := scanr.Text() |
| 1341 |
frst, scnd := eutils.SplitInTwoLeft(line, "\t") |
| 1342 |
frst = strings.ToLower(frst) |
| 1343 |
|
| 1344 |
if id != frst { |
| 1345 |
return |
| 1346 |
} |
| 1347 |
if !strings.HasSuffix(str, rgt) { |
| 1348 |
return |
| 1349 |
} |
| 1350 |
|
| 1351 |
lft := strings.TrimSuffix(str, rgt) |
| 1352 |
str = lft + " " + scnd + "\n" + rgt |
| 1353 |
|
| 1354 |
if hd != "" { |
| 1355 |
os.Stdout.WriteString(hd) |
| 1356 |
os.Stdout.WriteString("\n") |
| 1357 |
} |
| 1358 |
|
| 1359 |
os.Stdout.WriteString(str[:]) |
| 1360 |
os.Stdout.WriteString("\n") |
| 1361 |
|
| 1362 |
if tl != "" { |
| 1363 |
os.Stdout.WriteString(tl) |
| 1364 |
os.Stdout.WriteString("\n") |
| 1365 |
} |
| 1366 |
|
| 1367 |
break |
| 1368 |
} |
| 1369 |
}) |
| 1370 |
|
| 1371 |
if tail != "" { |
| 1372 |
os.Stdout.WriteString(tail) |
| 1373 |
os.Stdout.WriteString("\n") |
| 1374 |
} |
| 1375 |
|
| 1376 |
fl.Close() |
| 1377 |
|
| 1378 |
debug.FreeOSMemory() |
| 1379 |
|
| 1380 |
if timr { |
| 1381 |
printDuration("records") |
| 1382 |
} |
| 1383 |
|
| 1384 |
return |
| 1385 |
} |
| 1386 |
|
| 1387 |
// SORT XML RECORDS BY IDENTIFIER |
| 1388 |
|
| 1389 |
// -pattern record_name -sort parent/element@attribute^version |
| 1390 |
if len(args) == 4 && (args[2] == "-sort" || args[2] == "-sort-fwd" || args[2] == "-sort-rev") { |
| 1391 |
|
| 1392 |
sortInReverse := false |
| 1393 |
if args[2] == "-sort-rev" { |
| 1394 |
sortInReverse = true |
| 1395 |
} |
| 1396 |
|
| 1397 |
indx := args[3] |
| 1398 |
|
| 1399 |
// create map that records each UID |
| 1400 |
order := make(map[string][]string) |
| 1401 |
|
| 1402 |
find := eutils.ParseIndex(indx) |
| 1403 |
|
| 1404 |
eutils.PartitionXML(topPattern, star, false, rdr, |
| 1405 |
func(str string) { |
| 1406 |
recordCount++ |
| 1407 |
|
| 1408 |
id := eutils.FindIdentifier(str[:], parent, find) |
| 1409 |
if id == "" { |
| 1410 |
return |
| 1411 |
} |
| 1412 |
|
| 1413 |
data, ok := order[id] |
| 1414 |
if !ok { |
| 1415 |
data = make([]string, 0, 1) |
| 1416 |
} |
| 1417 |
data = append(data, str) |
| 1418 |
// always need to update order, since data may be reallocated |
| 1419 |
order[id] = data |
| 1420 |
}) |
| 1421 |
|
| 1422 |
// sort fields in alphabetical or numeric order, modified from sort.Slice (Less) to slices.SortedFunc (Compare) style |
| 1423 |
xtractSort := func(i, j string) int { |
| 1424 |
|
| 1425 |
// check for numeric (integer or decimal) representation in both string arguments |
| 1426 |
if eutils.IsAllDigitsOrPeriod(i) && eutils.IsAllDigitsOrPeriod(j) { |
| 1427 |
|
| 1428 |
// isolate integer and fractional component if decimal number |
| 1429 |
ni, _ := eutils.SplitInTwoLeft(i, ".") |
| 1430 |
nj, _ := eutils.SplitInTwoLeft(j, ".") |
| 1431 |
// get lengths of whole number |
| 1432 |
lni, lnj := len(ni), len(nj) |
| 1433 |
// shorter string is numerically lower, assuming no leading zeros |
| 1434 |
if lni < lnj { |
| 1435 |
return -1 |
| 1436 |
} |
| 1437 |
if lni > lnj { |
| 1438 |
return 1 |
| 1439 |
} |
| 1440 |
// integer components are same length, thus right (or decimal point) aligned for text |
| 1441 |
// comparison of numeric values without needing expensive string-to-integer conversion |
| 1442 |
} |
| 1443 |
|
| 1444 |
// same length integers or non-numeric, can now do string comparison on contents |
| 1445 |
return cmp.Compare(i, j) |
| 1446 |
} |
| 1447 |
|
| 1448 |
sortKeys := xtractSort |
| 1449 |
if sortInReverse { |
| 1450 |
sortKeys = func(i, j string) int { |
| 1451 |
// sends arguments in reverse order |
| 1452 |
return xtractSort(j, i) |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
keys := slices.SortedFunc(maps.Keys(order), sortKeys) |
| 1457 |
|
| 1458 |
if head != "" { |
| 1459 |
os.Stdout.WriteString(head) |
| 1460 |
os.Stdout.WriteString("\n") |
| 1461 |
} |
| 1462 |
|
| 1463 |
for _, id := range keys { |
| 1464 |
|
| 1465 |
strs := order[id] |
| 1466 |
for _, str := range strs { |
| 1467 |
os.Stdout.WriteString(str) |
| 1468 |
os.Stdout.WriteString("\n") |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
if tail != "" { |
| 1473 |
os.Stdout.WriteString(tail) |
| 1474 |
os.Stdout.WriteString("\n") |
| 1475 |
} |
| 1476 |
|
| 1477 |
debug.FreeOSMemory() |
| 1478 |
|
| 1479 |
if timr { |
| 1480 |
printDuration("records") |
| 1481 |
} |
| 1482 |
|
| 1483 |
return |
| 1484 |
} |
| 1485 |
|
| 1486 |
// SPLIT FILE BY RECORD COUNT |
| 1487 |
|
| 1488 |
// split XML record into subfiles by count |
| 1489 |
if len(args) == 8 && args[2] == "-split" && args[4] == "-prefix" && args[6] == "-suffix" { |
| 1490 |
|
| 1491 |
// e.g., -head "<IdxDocumentSet>" -tail "</IdxDocumentSet>" -pattern IdxDocument -split 250000 -prefix "biocon" -suffix "e2x" |
| 1492 |
|
| 1493 |
count := 0 |
| 1494 |
fnum := 0 |
| 1495 |
|
| 1496 |
var ( |
| 1497 |
fl *os.File |
| 1498 |
err error |
| 1499 |
) |
| 1500 |
|
| 1501 |
chunk, err := strconv.Atoi(args[3]) |
| 1502 |
if err != nil { |
| 1503 |
eutils.DisplayError("-split argument '%s' is not an integer", err.Error()) |
| 1504 |
return |
| 1505 |
} |
| 1506 |
|
| 1507 |
prefix := args[5] |
| 1508 |
suffix := args[7] |
| 1509 |
|
| 1510 |
eutils.PartitionXML(topPattern, star, false, rdr, |
| 1511 |
func(str string) { |
| 1512 |
recordCount++ |
| 1513 |
|
| 1514 |
if count >= chunk { |
| 1515 |
if tail != "" { |
| 1516 |
fl.WriteString(tail) |
| 1517 |
fl.WriteString("\n") |
| 1518 |
} |
| 1519 |
fl.Close() |
| 1520 |
count = 0 |
| 1521 |
} |
| 1522 |
if count == 0 { |
| 1523 |
fpath := fmt.Sprintf("%s%03d.%s", prefix, fnum, suffix) |
| 1524 |
fl, err = os.Create(fpath) |
| 1525 |
if err != nil { |
| 1526 |
eutils.DisplayError("Unable to create path '%s'", err.Error()) |
| 1527 |
return |
| 1528 |
} |
| 1529 |
os.Stderr.WriteString(fpath + "\n") |
| 1530 |
fnum++ |
| 1531 |
if head != "" { |
| 1532 |
fl.WriteString(head) |
| 1533 |
fl.WriteString("\n") |
| 1534 |
} |
| 1535 |
} |
| 1536 |
count++ |
| 1537 |
|
| 1538 |
fl.WriteString(str[:]) |
| 1539 |
fl.WriteString("\n") |
| 1540 |
}) |
| 1541 |
|
| 1542 |
if count > 0 { |
| 1543 |
if tail != "" { |
| 1544 |
fl.WriteString(tail) |
| 1545 |
fl.WriteString("\n") |
| 1546 |
} |
| 1547 |
fl.Close() |
| 1548 |
} |
| 1549 |
|
| 1550 |
debug.FreeOSMemory() |
| 1551 |
|
| 1552 |
if timr { |
| 1553 |
printDuration("records") |
| 1554 |
} |
| 1555 |
|
| 1556 |
return |
| 1557 |
} |
| 1558 |
|
| 1559 |
// ALLOT RECORDS BY BYTE SIZE |
| 1560 |
|
| 1561 |
// allot XML record into subfiles by size |
| 1562 |
if len(args) == 8 && args[2] == "-allot" && args[4] == "-prefix" && args[6] == "-suffix" { |
| 1563 |
|
| 1564 |
// e.g., -head "<IdxDocumentSet>" -tail "</IdxDocumentSet>" -pattern IdxDocument -allot 10000 -prefix "biocon" -suffix "e2x" |
| 1565 |
|
| 1566 |
retlen := len("\n") |
| 1567 |
cumulative := 0 |
| 1568 |
fnum := 0 |
| 1569 |
|
| 1570 |
headlen := 0 |
| 1571 |
taillen := 0 |
| 1572 |
if head != "" { |
| 1573 |
headlen += len(head) + retlen |
| 1574 |
} |
| 1575 |
if tail != "" { |
| 1576 |
taillen += len(tail) + retlen |
| 1577 |
} |
| 1578 |
padlen := retlen + headlen + taillen |
| 1579 |
|
| 1580 |
var ( |
| 1581 |
fl *os.File |
| 1582 |
err error |
| 1583 |
) |
| 1584 |
|
| 1585 |
chunk, err := strconv.Atoi(args[3]) |
| 1586 |
if err != nil { |
| 1587 |
eutils.DisplayError("-allot argument '%s' is not an integer", err.Error()) |
| 1588 |
return |
| 1589 |
} |
| 1590 |
|
| 1591 |
prefix := args[5] |
| 1592 |
suffix := args[7] |
| 1593 |
|
| 1594 |
eutils.PartitionXML(topPattern, star, false, rdr, |
| 1595 |
func(str string) { |
| 1596 |
recordCount++ |
| 1597 |
|
| 1598 |
if cumulative >= chunk { |
| 1599 |
if tail != "" { |
| 1600 |
fl.WriteString(tail) |
| 1601 |
fl.WriteString("\n") |
| 1602 |
} |
| 1603 |
fl.Close() |
| 1604 |
cumulative = 0 |
| 1605 |
} |
| 1606 |
if cumulative == 0 { |
| 1607 |
fpath := fmt.Sprintf("%s%03d.%s", prefix, fnum, suffix) |
| 1608 |
fl, err = os.Create(fpath) |
| 1609 |
if err != nil { |
| 1610 |
eutils.DisplayError("Unable to create path '%s'", err.Error()) |
| 1611 |
return |
| 1612 |
} |
| 1613 |
os.Stderr.WriteString(fpath + "\n") |
| 1614 |
fnum++ |
| 1615 |
if head != "" { |
| 1616 |
fl.WriteString(head) |
| 1617 |
fl.WriteString("\n") |
| 1618 |
} |
| 1619 |
} |
| 1620 |
cumulative += len(str) + padlen |
| 1621 |
|
| 1622 |
fl.WriteString(str[:]) |
| 1623 |
fl.WriteString("\n") |
| 1624 |
}) |
| 1625 |
|
| 1626 |
if cumulative > 0 { |
| 1627 |
if tail != "" { |
| 1628 |
fl.WriteString(tail) |
| 1629 |
fl.WriteString("\n") |
| 1630 |
} |
| 1631 |
fl.Close() |
| 1632 |
} |
| 1633 |
|
| 1634 |
debug.FreeOSMemory() |
| 1635 |
|
| 1636 |
if timr { |
| 1637 |
printDuration("records") |
| 1638 |
} |
| 1639 |
|
| 1640 |
return |
| 1641 |
} |
| 1642 |
|
| 1643 |
// PARSE AND VALIDATE EXTRACTION ARGUMENTS |
| 1644 |
|
| 1645 |
// parse nested exploration instruction from command-line arguments |
| 1646 |
cmds := eutils.ParseArguments(args, topPattern) |
| 1647 |
if cmds == nil { |
| 1648 |
eutils.DisplayError("Problem parsing command-line arguments") |
| 1649 |
os.Exit(1) |
| 1650 |
} |
| 1651 |
|
| 1652 |
// GLOBAL MAP FOR SORT-UNIQ-COUNT HISTOGRAM ARGUMENT |
| 1653 |
|
| 1654 |
histogram := make(map[string]int) |
| 1655 |
|
| 1656 |
// PERFORMANCE TIMING COMMAND |
| 1657 |
|
| 1658 |
// -stats with an extraction command prints XML size and processing time for each record |
| 1659 |
if stts { |
| 1660 |
|
| 1661 |
legend := "REC\tOFST\tSIZE\tTIME" |
| 1662 |
|
| 1663 |
rec := 0 |
| 1664 |
|
| 1665 |
// find := eutils.ParseIndex("MedlineCitation/PMID") |
| 1666 |
|
| 1667 |
eutils.PartitionXML(topPattern, star, turbo, rdr, |
| 1668 |
func(str string) { |
| 1669 |
rec++ |
| 1670 |
// id := eutils.FindIdentifier(str[:], parent, find) |
| 1671 |
// if eutils.HasCombiningAccent(str[:]) && id != "" { fmt.Printf("%s\n", id) } |
| 1672 |
beginTime := time.Now() |
| 1673 |
eutils.ProcessExtract(str[:], parent, rec, hd, tl, transform, nil, histogram, cmds) |
| 1674 |
endTime := time.Now() |
| 1675 |
duration := endTime.Sub(beginTime) |
| 1676 |
micro := int(float64(duration.Nanoseconds()) / 1e3) |
| 1677 |
if legend != "" { |
| 1678 |
fmt.Printf("%s\n", legend) |
| 1679 |
legend = "" |
| 1680 |
} |
| 1681 |
fmt.Printf("%d\t%d\t%d\n", rec, len(str), micro) |
| 1682 |
}) |
| 1683 |
|
| 1684 |
return |
| 1685 |
} |
| 1686 |
|
| 1687 |
// PERFORMANCE OPTIMIZATION FUNCTION |
| 1688 |
|
| 1689 |
// -trial -input fileName runs the specified extraction for each -proc from 1 to nCPU |
| 1690 |
if trial && fileName != "" { |
| 1691 |
|
| 1692 |
legend := "CPU\tRATE\tDEV" |
| 1693 |
|
| 1694 |
for numServ := 1; numServ <= ncpu; numServ++ { |
| 1695 |
|
| 1696 |
numServe = numServ |
| 1697 |
|
| 1698 |
eutils.SetTunings(numProcs, numServe, serverRatio, chanDepth, farmSize, heapSize, goGc, turbo) |
| 1699 |
|
| 1700 |
runtime.GOMAXPROCS(numServ) |
| 1701 |
|
| 1702 |
sum := 0 |
| 1703 |
count := 0 |
| 1704 |
mean := 0.0 |
| 1705 |
m2 := 0.0 |
| 1706 |
|
| 1707 |
// calculate mean and standard deviation of processing rate |
| 1708 |
for range 5 { |
| 1709 |
|
| 1710 |
inFile, err := os.Open(fileName) |
| 1711 |
if err != nil { |
| 1712 |
eutils.DisplayError("Unable to open input file '%s'", fileName) |
| 1713 |
os.Exit(1) |
| 1714 |
} |
| 1715 |
|
| 1716 |
trdr := eutils.CreateXMLStreamer(inFile, nil) |
| 1717 |
if trdr == nil { |
| 1718 |
eutils.DisplayError("Unable to read input file") |
| 1719 |
os.Exit(1) |
| 1720 |
} |
| 1721 |
|
| 1722 |
xmlq := eutils.CreateXMLProducer(topPattern, star, turbo, trdr) |
| 1723 |
tblq := eutils.CreateXMLConsumers(cmds, parent, hd, tl, transform, false, histogram, xmlq) |
| 1724 |
|
| 1725 |
if xmlq == nil || tblq == nil { |
| 1726 |
eutils.DisplayError("Unable to create servers") |
| 1727 |
os.Exit(1) |
| 1728 |
} |
| 1729 |
|
| 1730 |
begTime := time.Now() |
| 1731 |
recordCount = 0 |
| 1732 |
|
| 1733 |
for range tblq { |
| 1734 |
recordCount++ |
| 1735 |
runtime.Gosched() |
| 1736 |
} |
| 1737 |
|
| 1738 |
inFile.Close() |
| 1739 |
|
| 1740 |
debug.FreeOSMemory() |
| 1741 |
|
| 1742 |
endTime := time.Now() |
| 1743 |
expended := endTime.Sub(begTime) |
| 1744 |
secs := float64(expended.Nanoseconds()) / 1e9 |
| 1745 |
|
| 1746 |
if secs >= 0.000001 && recordCount > 0 { |
| 1747 |
speed := int(float64(recordCount) / secs) |
| 1748 |
sum += speed |
| 1749 |
count++ |
| 1750 |
x := float64(speed) |
| 1751 |
delta := x - mean |
| 1752 |
mean += delta / float64(count) |
| 1753 |
m2 += delta * (x - mean) |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
if legend != "" { |
| 1758 |
fmt.Printf("%s\n", legend) |
| 1759 |
legend = "" |
| 1760 |
} |
| 1761 |
if count > 1 { |
| 1762 |
vrc := m2 / float64(count-1) |
| 1763 |
dev := int(math.Sqrt(vrc)) |
| 1764 |
fmt.Printf("%d\t%d\t%d\n", numServ, sum/count, dev) |
| 1765 |
} |
| 1766 |
} |
| 1767 |
|
| 1768 |
return |
| 1769 |
} |
| 1770 |
|
| 1771 |
// PROCESS SINGLE SELECTED RECORD IF -pattern ARGUMENT IS IMMEDIATELY FOLLOWED BY -position COMMAND |
| 1772 |
|
| 1773 |
posn := "" |
| 1774 |
if cmds.Visit == topPat { |
| 1775 |
if cmds.Position == "outer" || |
| 1776 |
cmds.Position == "inner" || |
| 1777 |
cmds.Position == "even" || |
| 1778 |
cmds.Position == "odd" || |
| 1779 |
cmds.Position == "all" { |
| 1780 |
// filter by record position when draining unshuffler channel |
| 1781 |
posn = cmds.Position |
| 1782 |
cmds.Position = "" |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
if cmds.Visit == topPat && cmds.Position != "" && cmds.Position != "select" { |
| 1787 |
|
| 1788 |
qry := "" |
| 1789 |
idx := 0 |
| 1790 |
rec := 0 |
| 1791 |
|
| 1792 |
if cmds.Position == "first" { |
| 1793 |
|
| 1794 |
eutils.PartitionXML(topPattern, star, turbo, rdr, |
| 1795 |
func(str string) { |
| 1796 |
rec++ |
| 1797 |
if rec == 1 { |
| 1798 |
qry = str |
| 1799 |
idx = rec |
| 1800 |
} |
| 1801 |
}) |
| 1802 |
|
| 1803 |
} else if cmds.Position == "last" { |
| 1804 |
|
| 1805 |
eutils.PartitionXML(topPattern, star, turbo, rdr, |
| 1806 |
func(str string) { |
| 1807 |
qry = str |
| 1808 |
idx = rec |
| 1809 |
}) |
| 1810 |
|
| 1811 |
} else { |
| 1812 |
|
| 1813 |
// use numeric position |
| 1814 |
number, err := strconv.Atoi(cmds.Position) |
| 1815 |
if err != nil { |
| 1816 |
eutils.DisplayError("Unrecognized position '%s'", cmds.Position) |
| 1817 |
os.Exit(1) |
| 1818 |
} |
| 1819 |
|
| 1820 |
eutils.PartitionXML(topPattern, star, turbo, rdr, |
| 1821 |
func(str string) { |
| 1822 |
rec++ |
| 1823 |
if rec == number { |
| 1824 |
qry = str |
| 1825 |
idx = rec |
| 1826 |
} |
| 1827 |
}) |
| 1828 |
} |
| 1829 |
|
| 1830 |
if qry == "" { |
| 1831 |
return |
| 1832 |
} |
| 1833 |
|
| 1834 |
// clear position on top node to prevent condition test failure |
| 1835 |
cmds.Position = "" |
| 1836 |
|
| 1837 |
// process single selected record |
| 1838 |
res := eutils.ProcessExtract(qry[:], parent, idx, hd, tl, transform, nil, histogram, cmds) |
| 1839 |
|
| 1840 |
if res != "" { |
| 1841 |
fmt.Printf("%s", res) |
| 1842 |
} |
| 1843 |
|
| 1844 |
return |
| 1845 |
} |
| 1846 |
|
| 1847 |
// LAUNCH PRODUCER, CONSUMER, AND UNSHUFFLER GOROUTINES |
| 1848 |
|
| 1849 |
// launch producer goroutine to partition XML by pattern |
| 1850 |
xmlq := eutils.CreateXMLProducer(topPattern, star, turbo, rdr) |
| 1851 |
|
| 1852 |
// launch consumer goroutines to parse and explore partitioned XML objects |
| 1853 |
tblq := eutils.CreateXMLConsumers(cmds, parent, hd, tl, transform, forClassify, histogram, xmlq) |
| 1854 |
|
| 1855 |
// launch unshuffler goroutine to restore order of results |
| 1856 |
unsq := eutils.CreateXMLUnshuffler(tblq) |
| 1857 |
|
| 1858 |
if xmlq == nil || tblq == nil || unsq == nil { |
| 1859 |
eutils.DisplayError("Unable to create servers") |
| 1860 |
os.Exit(1) |
| 1861 |
} |
| 1862 |
|
| 1863 |
// PERFORMANCE SUMMARY |
| 1864 |
|
| 1865 |
/* |
| 1866 |
if dbug { |
| 1867 |
|
| 1868 |
// drain results, but suppress extraction output |
| 1869 |
for ext := range unsq { |
| 1870 |
byteCount += len(ext.Text) |
| 1871 |
recordCount++ |
| 1872 |
runtime.Gosched() |
| 1873 |
} |
| 1874 |
|
| 1875 |
// force garbage collection, return memory to operating system |
| 1876 |
debug.FreeOSMemory() |
| 1877 |
|
| 1878 |
// print processing parameters as XML object |
| 1879 |
stopTime := time.Now() |
| 1880 |
duration := stopTime.Sub(StartTime) |
| 1881 |
seconds := float64(duration.Nanoseconds()) / 1e9 |
| 1882 |
|
| 1883 |
// Threads is a more easily explained concept than GOMAXPROCS |
| 1884 |
fmt.Printf("<Xtract>\n") |
| 1885 |
fmt.Printf(" <Threads>%d</Threads>\n", numProcs) |
| 1886 |
fmt.Printf(" <Parsers>%d</Parsers>\n", NumServe) |
| 1887 |
fmt.Printf(" <Time>%.3f</Time>\n", seconds) |
| 1888 |
if seconds >= 0.001 && recordCount > 0 { |
| 1889 |
rate := int(float64(recordCount) / seconds) |
| 1890 |
fmt.Printf(" <Rate>%d</Rate>\n", rate) |
| 1891 |
} |
| 1892 |
fmt.Printf("</Xtract>\n") |
| 1893 |
|
| 1894 |
return |
| 1895 |
} |
| 1896 |
*/ |
| 1897 |
|
| 1898 |
// DRAIN OUTPUT CHANNEL TO EXECUTE EXTRACTION COMMANDS, RESTORE OUTPUT ORDER WITH HEAP |
| 1899 |
|
| 1900 |
recordCount, byteCount = eutils.DrainExtractions(head, tail, posn, mpty, idnt, histogram, unsq) |
| 1901 |
|
| 1902 |
if timr { |
| 1903 |
printDuration("records") |
| 1904 |
} |
| 1905 |
} |