| 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: index.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"bufio" |
| 35 |
"compress/gzip" |
| 36 |
"fmt" |
| 37 |
"html" |
| 38 |
"io" |
| 39 |
"maps" |
| 40 |
"os" |
| 41 |
"path/filepath" |
| 42 |
"regexp" |
| 43 |
"runtime" |
| 44 |
"slices" |
| 45 |
"strings" |
| 46 |
"sync" |
| 47 |
"unicode" |
| 48 |
) |
| 49 |
|
| 50 |
// PUBMED INDEXED AND INVERTED FILE FORMATS |
| 51 |
|
| 52 |
// Local archive indexing reads PubmedArticle XML records and produces IdxDocument records. |
| 53 |
// Title and Title/Abstract fields include term positions as XML attributes: |
| 54 |
|
| 55 |
/* |
| 56 |
|
| 57 |
... |
| 58 |
<IdxDocument> |
| 59 |
<IdxUid>2539356</IdxUid> |
| 60 |
<IdxSearchFields> |
| 61 |
<UID>02539356</UID> |
| 62 |
<SIZE>13727</SIZE> |
| 63 |
<YEAR>1989</YEAR> |
| 64 |
<DATE>1989 04</DATE> |
| 65 |
<RDAT>2019 05 08</RDAT> |
| 66 |
<JOUR>J Bacteriol</JOUR> |
| 67 |
<JOUR>2985120R</JOUR> |
| 68 |
<JOUR>0021-9193</JOUR> |
| 69 |
<JOUR>Journal of Bacteriology</JOUR> |
| 70 |
<JOUR>J Bacteriol</JOUR> |
| 71 |
<JOUR>0021-9193</JOUR> |
| 72 |
<VOL>171</VOL> |
| 73 |
<ISS>4</ISS> |
| 74 |
<PAGE>1904</PAGE> |
| 75 |
<LANG>eng</LANG> |
| 76 |
<ANUM>2</ANUM> |
| 77 |
<FAUT>Kans JA</FAUT> |
| 78 |
<LAUT>Casadaban MJ</LAUT> |
| 79 |
<AUTH>Kans JA</AUTH> |
| 80 |
<AUTH>Casadaban MJ</AUTH> |
| 81 |
<TITL pos="7">immunity</TITL> |
| 82 |
<TITL pos="1">nucleotide</TITL> |
| 83 |
<TITL pos="3">required</TITL> |
| 84 |
<TITL pos="2">sequences</TITL> |
| 85 |
<TITL pos="5">tn3</TITL> |
| 86 |
<TITL pos="6">transposition</TITL> |
| 87 |
<TIAB pos="145">38</TIAB> |
| 88 |
<TIAB pos="126">acting</TIAB> |
| 89 |
<TIAB pos="188">additional</TIAB> |
| 90 |
<TIAB pos="146">base</TIAB> |
| 91 |
<TIAB pos="125">cis</TIAB> |
| 92 |
<TIAB pos="172,178,187">conferred</TIAB> |
| 93 |
... |
| 94 |
<PAIR>nucleotide sequences</PAIR> |
| 95 |
<PAIR>sequences required</PAIR> |
| 96 |
<PAIR>tn3 transposition</PAIR> |
| 97 |
<PAIR>transposition immunity</PAIR> |
| 98 |
<PTYP>Journal Article</PTYP> |
| 99 |
<PTYP>Research Support, U.S. Gov't, P.H.S.</PTYP> |
| 100 |
<PROP>Published In Print</PROP> |
| 101 |
<PROP>Has Abstract</PROP> |
| 102 |
<DOI>9891 4191 4091 4 171 bj 8211 01</DOI> |
| 103 |
<PMCID>209839</PMCID> |
| 104 |
<CODE>d001483</CODE> |
| 105 |
<CODE>d002874</CODE> |
| 106 |
... |
| 107 |
<MESH>Plasmids</MESH> |
| 108 |
<MESH>Recombination, Genetic</MESH> |
| 109 |
</IdxSearchFields> |
| 110 |
</IdxDocument> |
| 111 |
... |
| 112 |
|
| 113 |
*/ |
| 114 |
|
| 115 |
// Inversion reads a set of indexed documents and generated InvDocument records: |
| 116 |
|
| 117 |
/* |
| 118 |
|
| 119 |
... |
| 120 |
<InvDocument> |
| 121 |
<InvKey>tn3</InvKey> |
| 122 |
<InvIDs> |
| 123 |
<TIAB pos="5,102,117,129,157,194">2539356</TIAB> |
| 124 |
<TITL pos="5">2539356</TITL> |
| 125 |
</InvIDs> |
| 126 |
</InvDocument> |
| 127 |
<InvDocument> |
| 128 |
<InvKey>tn3 transposition</InvKey> |
| 129 |
<InvIDs> |
| 130 |
<PAIR>2539356</PAIR> |
| 131 |
</InvIDs> |
| 132 |
</InvDocument> |
| 133 |
<InvDocument> |
| 134 |
<InvKey>transposition</InvKey> |
| 135 |
<InvIDs> |
| 136 |
<TIAB pos="6,122">2539356</TIAB> |
| 137 |
<TITL pos="6">2539356</TITL> |
| 138 |
</InvIDs> |
| 139 |
</InvDocument> |
| 140 |
<InvDocument> |
| 141 |
<InvKey>transposition immunity</InvKey> |
| 142 |
<InvIDs> |
| 143 |
<PAIR>2539356</PAIR> |
| 144 |
</InvIDs> |
| 145 |
</InvDocument> |
| 146 |
... |
| 147 |
|
| 148 |
*/ |
| 149 |
|
| 150 |
// Separate inversion runs are merged and used to produce term lists and postings file. |
| 151 |
// These can then be searched by passing commands to EDirect's "phrase-search" script. |
| 152 |
|
| 153 |
// ENTREZ2INDEX COMMAND GENERATOR |
| 154 |
|
| 155 |
// MakeE2Commands generates extraction commands to create input for Entrez2Index |
| 156 |
func MakeE2Commands(tform, idxargs string) []string { |
| 157 |
|
| 158 |
var acc []string |
| 159 |
|
| 160 |
// idxargs file contains one command or argument per line |
| 161 |
if idxargs == "" { |
| 162 |
return acc |
| 163 |
} |
| 164 |
|
| 165 |
inFile, err := os.Open(idxargs) |
| 166 |
if err != nil { |
| 167 |
DisplayError("Unable to open index argument array file: %s\n", err.Error()) |
| 168 |
return acc |
| 169 |
} |
| 170 |
defer inFile.Close() |
| 171 |
|
| 172 |
scanr := bufio.NewScanner(inFile) |
| 173 |
if scanr == nil { |
| 174 |
DisplayError("Unable to create NewScanner") |
| 175 |
return acc |
| 176 |
} |
| 177 |
|
| 178 |
for scanr.Scan() { |
| 179 |
|
| 180 |
line := scanr.Text() |
| 181 |
|
| 182 |
// do NOT skip empty line or trim spaces, to allow "" or " " arguments |
| 183 |
|
| 184 |
acc = append(acc, line) |
| 185 |
} |
| 186 |
|
| 187 |
return acc |
| 188 |
} |
| 189 |
|
| 190 |
// UPDATE CACHED INDEXED AND INVERTED-INDEX FILES FROM LOCAL ARCHIVE FOLDERS |
| 191 |
|
| 192 |
// examineFolder collects two-digit subdirectories, xml files, e2x files, and inv files |
| 193 |
func examineFolder(base, path string) ([]string, []string, []string, []string) { |
| 194 |
|
| 195 |
dir := filepath.Join(base, path) |
| 196 |
|
| 197 |
contents, err := os.ReadDir(dir) |
| 198 |
if err != nil { |
| 199 |
return nil, nil, nil, nil |
| 200 |
} |
| 201 |
|
| 202 |
isTwoDigits := func(str string) bool { |
| 203 |
|
| 204 |
if len(str) != 2 { |
| 205 |
return false |
| 206 |
} |
| 207 |
|
| 208 |
ch := str[0] |
| 209 |
if ch < '0' || ch > '9' { |
| 210 |
return false |
| 211 |
} |
| 212 |
|
| 213 |
ch = str[1] |
| 214 |
if ch < '0' || ch > '9' { |
| 215 |
return false |
| 216 |
} |
| 217 |
|
| 218 |
return true |
| 219 |
} |
| 220 |
|
| 221 |
var dirs []string |
| 222 |
var xmls []string |
| 223 |
var e2xs []string |
| 224 |
var invs []string |
| 225 |
|
| 226 |
for _, item := range contents { |
| 227 |
name := item.Name() |
| 228 |
if name == "" { |
| 229 |
continue |
| 230 |
} |
| 231 |
if item.IsDir() { |
| 232 |
if isTwoDigits(name) { |
| 233 |
dirs = append(dirs, name) |
| 234 |
} |
| 235 |
} else if strings.HasSuffix(name, ".xml.gz") { |
| 236 |
xmls = append(xmls, name) |
| 237 |
} else if strings.HasSuffix(name, ".e2x.gz") { |
| 238 |
e2xs = append(e2xs, name) |
| 239 |
} else if strings.HasSuffix(name, ".inv.gz") { |
| 240 |
invs = append(invs, name) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return dirs, xmls, e2xs, invs |
| 245 |
} |
| 246 |
|
| 247 |
// gzFileToString reads selected gzipped file, uncompressing and saving contents as string |
| 248 |
func gzFileToString(fpath string) string { |
| 249 |
|
| 250 |
file, err := os.Open(fpath) |
| 251 |
if err != nil { |
| 252 |
return "" |
| 253 |
} |
| 254 |
defer file.Close() |
| 255 |
|
| 256 |
var rdr io.Reader |
| 257 |
|
| 258 |
gz, err := gzip.NewReader(file) |
| 259 |
if err != nil { |
| 260 |
return "" |
| 261 |
} |
| 262 |
defer gz.Close() |
| 263 |
rdr = gz |
| 264 |
|
| 265 |
byt, err := io.ReadAll(rdr) |
| 266 |
if err != nil { |
| 267 |
return "" |
| 268 |
} |
| 269 |
|
| 270 |
str := string(byt) |
| 271 |
if str == "" { |
| 272 |
return "" |
| 273 |
} |
| 274 |
|
| 275 |
if !strings.HasSuffix(str, "\n") { |
| 276 |
str += "\n" |
| 277 |
} |
| 278 |
|
| 279 |
return str |
| 280 |
} |
| 281 |
|
| 282 |
func stringToGzFile(base, path, file, str string) { |
| 283 |
|
| 284 |
if str == "" { |
| 285 |
return |
| 286 |
} |
| 287 |
|
| 288 |
dpath := filepath.Join(base, path) |
| 289 |
if dpath == "" { |
| 290 |
return |
| 291 |
} |
| 292 |
err := os.MkdirAll(dpath, os.ModePerm) |
| 293 |
if err != nil { |
| 294 |
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 295 |
return |
| 296 |
} |
| 297 |
fpath := filepath.Join(dpath, file) |
| 298 |
if fpath == "" { |
| 299 |
return |
| 300 |
} |
| 301 |
|
| 302 |
// overwrites and truncates existing file |
| 303 |
fl, err := os.Create(fpath) |
| 304 |
if err != nil { |
| 305 |
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
// for small files, use regular gzip |
| 310 |
zpr, err := gzip.NewWriterLevel(fl, gzip.BestSpeed) |
| 311 |
if err != nil { |
| 312 |
DisplayError("Unable to create compressor") |
| 313 |
os.Exit(1) |
| 314 |
} |
| 315 |
|
| 316 |
wrtr := bufio.NewWriter(zpr) |
| 317 |
|
| 318 |
// write contents |
| 319 |
wrtr.WriteString(str) |
| 320 |
if !strings.HasSuffix(str, "\n") { |
| 321 |
wrtr.WriteString("\n") |
| 322 |
} |
| 323 |
|
| 324 |
err = wrtr.Flush() |
| 325 |
if err != nil { |
| 326 |
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 327 |
return |
| 328 |
} |
| 329 |
|
| 330 |
err = zpr.Close() |
| 331 |
if err != nil { |
| 332 |
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 333 |
return |
| 334 |
} |
| 335 |
|
| 336 |
// fl.Sync() |
| 337 |
|
| 338 |
err = fl.Close() |
| 339 |
if err != nil { |
| 340 |
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 341 |
return |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// e2IndexConsumer callbacks have access to application-specific data as closures |
| 346 |
type e2IndexConsumer func(inp <-chan XMLRecord) <-chan XMLRecord |
| 347 |
|
| 348 |
// IncrementalIndex creates or updates missing cached .e2x.gz indexed files, |
| 349 |
// e.g., /Index/02/53/025393.e2x.gz for /Archive/02/53/93/*.xml.gz |
| 350 |
func IncrementalIndex(db, pfx, ptrn string, dotMax int, csmr e2IndexConsumer) <-chan string { |
| 351 |
|
| 352 |
if csmr == nil { |
| 353 |
return nil |
| 354 |
} |
| 355 |
|
| 356 |
if db == "" { |
| 357 |
db = "pubmed" |
| 358 |
} |
| 359 |
|
| 360 |
// obtain paths from environment variable(s) |
| 361 |
archiveBase := GetLocalArchiveFolder(db, "Archive", true) |
| 362 |
indexBase := GetLocalArchiveFolder(db, "Index", true) |
| 363 |
|
| 364 |
if archiveBase == "" || indexBase == "" { |
| 365 |
DisplayError("Unable to get local archive or index paths") |
| 366 |
os.Exit(1) |
| 367 |
} |
| 368 |
|
| 369 |
// check to make sure local archive is mounted |
| 370 |
_, err := os.Stat(archiveBase) |
| 371 |
if err != nil && os.IsNotExist(err) { |
| 372 |
DisplayError("Local archive and search index is not mounted") |
| 373 |
os.Exit(1) |
| 374 |
} |
| 375 |
|
| 376 |
// check to make sure local index is mounted |
| 377 |
_, err = os.Stat(indexBase) |
| 378 |
if err != nil && os.IsNotExist(err) { |
| 379 |
DisplayError("Local incremental index is not mounted") |
| 380 |
os.Exit(1) |
| 381 |
} |
| 382 |
|
| 383 |
// visitArchiveFolders sends an Archive leaf folder path plus the file base names |
| 384 |
// contained in it down a channel, e.g., [ "02/53/93", "2539300", "2539301", ..., ] for |
| 385 |
// Archive/02/53/93/*.xml.gz |
| 386 |
visitArchiveFolders := func(archiveBase string) <-chan []string { |
| 387 |
|
| 388 |
out := make(chan []string, chanDepth) |
| 389 |
if out == nil { |
| 390 |
DisplayError("Unable to create archive explorer channel") |
| 391 |
os.Exit(1) |
| 392 |
} |
| 393 |
|
| 394 |
// recursive definition |
| 395 |
var visitSubFolders func(base, path string, out chan<- []string) |
| 396 |
|
| 397 |
// visitSubFolders recurses to leaf directories |
| 398 |
visitSubFolders = func(base, path string, out chan<- []string) { |
| 399 |
|
| 400 |
dirs, xmls, _, _ := examineFolder(base, path) |
| 401 |
|
| 402 |
// recursively explore subdirectories |
| 403 |
if dirs != nil { |
| 404 |
for _, dr := range dirs { |
| 405 |
sub := filepath.Join(path, dr) |
| 406 |
visitSubFolders(base, sub, out) |
| 407 |
} |
| 408 |
return |
| 409 |
} |
| 410 |
|
| 411 |
// looking for leaf Archive directory with at least one *.xml.gz file |
| 412 |
if xmls == nil { |
| 413 |
return |
| 414 |
} |
| 415 |
|
| 416 |
// remove ".xml.gz" suffixes, leaving unpadded PMID |
| 417 |
for i, file := range xmls { |
| 418 |
pos := strings.Index(file, ".") |
| 419 |
if pos >= 0 { |
| 420 |
file = file[:pos] |
| 421 |
} |
| 422 |
xmls[i] = file |
| 423 |
} |
| 424 |
|
| 425 |
if len(xmls) > 1 { |
| 426 |
// sort fields in alphabetical or numeric order |
| 427 |
slices.SortFunc(xmls, CompareAlphaOrNumericKeys) |
| 428 |
} |
| 429 |
|
| 430 |
var res []string |
| 431 |
res = append(res, path) |
| 432 |
res = append(res, xmls...) |
| 433 |
|
| 434 |
out <- res |
| 435 |
} |
| 436 |
|
| 437 |
visitArchiveSubset := func(base string, out chan<- []string) { |
| 438 |
|
| 439 |
defer close(out) |
| 440 |
|
| 441 |
dirs, _, _, _ := examineFolder(base, "") |
| 442 |
|
| 443 |
// iterate through top directories |
| 444 |
for _, top := range dirs { |
| 445 |
// skip Sentinels folder |
| 446 |
if IsAllDigits(top) && len(top) == 2 { |
| 447 |
visitSubFolders(base, top, out) |
| 448 |
} |
| 449 |
|
| 450 |
// force garbage collection |
| 451 |
runtime.GC() |
| 452 |
|
| 453 |
runtime.Gosched() |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
// launch single archive visitor goroutine |
| 458 |
go visitArchiveSubset(archiveBase, out) |
| 459 |
|
| 460 |
return out |
| 461 |
} |
| 462 |
|
| 463 |
// filterIndexFolders checks for presence of an Index file for an archive folder, |
| 464 |
// only passing those files that need to be (re)indexed |
| 465 |
filterIndexFolders := func(indexBase string, inp <-chan []string) <-chan XMLRecord { |
| 466 |
|
| 467 |
out := make(chan XMLRecord, chanDepth) |
| 468 |
if out == nil { |
| 469 |
DisplayError("Unable to create index filter channel") |
| 470 |
os.Exit(1) |
| 471 |
} |
| 472 |
|
| 473 |
filterIndexSubset := func(indBase string, inp <-chan []string, out chan<- XMLRecord) { |
| 474 |
|
| 475 |
defer close(out) |
| 476 |
|
| 477 |
idx := 0 |
| 478 |
|
| 479 |
for data := range inp { |
| 480 |
|
| 481 |
// path is first element in slice |
| 482 |
path := data[0] |
| 483 |
// "02/53/93/" |
| 484 |
|
| 485 |
// followed by xml file base names (PMIDs) |
| 486 |
pmids := data[1:] |
| 487 |
|
| 488 |
indPath := path[:6] |
| 489 |
// "02/53/" |
| 490 |
|
| 491 |
indFile := strings.Replace(path, "/", "", -1) |
| 492 |
// "025393" |
| 493 |
|
| 494 |
target := filepath.Join(indBase, indPath, indFile+".e2x.gz") |
| 495 |
|
| 496 |
_, err := os.Stat(target) |
| 497 |
if err == nil { |
| 498 |
// skip if first-level incremental Entrez index file exists for current set of 100 archive records |
| 499 |
continue |
| 500 |
} |
| 501 |
|
| 502 |
for _, pmid := range pmids { |
| 503 |
// increment index so unshuffler can restore order of results |
| 504 |
idx++ |
| 505 |
|
| 506 |
// send PMID (unindexed file base name) down channel |
| 507 |
out <- XMLRecord{Index: idx, Ident: indFile, Text: pmid} |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
// launch single index filter goroutine |
| 513 |
go filterIndexSubset(indexBase, inp, out) |
| 514 |
|
| 515 |
return out |
| 516 |
} |
| 517 |
|
| 518 |
cleanIndexFiles := func(inp <-chan XMLRecord) <-chan XMLRecord { |
| 519 |
|
| 520 |
out := make(chan XMLRecord, chanDepth) |
| 521 |
if out == nil { |
| 522 |
DisplayError("Unable to create index cleaner channel") |
| 523 |
os.Exit(1) |
| 524 |
} |
| 525 |
|
| 526 |
indexCleaner := func(wg *sync.WaitGroup, indBase string, inp <-chan XMLRecord, out chan<- XMLRecord) { |
| 527 |
|
| 528 |
defer wg.Done() |
| 529 |
|
| 530 |
re := regexp.MustCompile(">[ \n\r\t]*<") |
| 531 |
|
| 532 |
for curr := range inp { |
| 533 |
|
| 534 |
str := curr.Text |
| 535 |
|
| 536 |
if str == "" { |
| 537 |
continue |
| 538 |
} |
| 539 |
|
| 540 |
// clean up white space between stop tag and next start tag, replacing with a single newline |
| 541 |
str = re.ReplaceAllString(str, ">\n<") |
| 542 |
|
| 543 |
if !strings.HasSuffix(str, "\n") { |
| 544 |
str += "\n" |
| 545 |
} |
| 546 |
|
| 547 |
out <- XMLRecord{Index: curr.Index, Ident: curr.Ident, Text: str} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
var wg sync.WaitGroup |
| 552 |
|
| 553 |
// launch multiple index cleaner goroutines |
| 554 |
for range numProcs { |
| 555 |
wg.Add(1) |
| 556 |
go indexCleaner(&wg, indexBase, inp, out) |
| 557 |
} |
| 558 |
|
| 559 |
// launch separate anonymous goroutine to wait until all index cleaners are done |
| 560 |
go func() { |
| 561 |
wg.Wait() |
| 562 |
close(out) |
| 563 |
}() |
| 564 |
|
| 565 |
return out |
| 566 |
} |
| 567 |
|
| 568 |
combineIndexFiles := func(indexBase string, dotMax int, inp <-chan XMLRecord) <-chan string { |
| 569 |
|
| 570 |
out := make(chan string, chanDepth) |
| 571 |
if out == nil { |
| 572 |
DisplayError("Unable to create index combiner channel") |
| 573 |
os.Exit(1) |
| 574 |
} |
| 575 |
|
| 576 |
// mutex to protect access to rollingCount and rollingColumn variables |
| 577 |
var vlock sync.Mutex |
| 578 |
|
| 579 |
rollingCount := 0 |
| 580 |
rollingColumn := 0 |
| 581 |
|
| 582 |
countSuccess := func() { |
| 583 |
|
| 584 |
vlock.Lock() |
| 585 |
|
| 586 |
rollingCount++ |
| 587 |
if rollingCount >= dotMax { |
| 588 |
rollingCount = 0 |
| 589 |
// print dot (progress monitor) |
| 590 |
fmt.Fprintf(os.Stderr, ".") |
| 591 |
rollingColumn++ |
| 592 |
if rollingColumn > 49 { |
| 593 |
// print newline after 50 dots |
| 594 |
fmt.Fprintf(os.Stderr, "\n") |
| 595 |
rollingColumn = 0 |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
vlock.Unlock() |
| 600 |
} |
| 601 |
|
| 602 |
indexCombiner := func(indBase string, inp <-chan XMLRecord, out chan<- string) { |
| 603 |
|
| 604 |
defer close(out) |
| 605 |
|
| 606 |
currentIdent := "" |
| 607 |
|
| 608 |
var buffer strings.Builder |
| 609 |
|
| 610 |
verbose := false |
| 611 |
// set verbose flag from environment variable |
| 612 |
env := os.Getenv("EDIRECT_LOCAL_VERBOSE") |
| 613 |
if env == "Y" || env == "y" || env == "true" { |
| 614 |
verbose = true |
| 615 |
} |
| 616 |
|
| 617 |
for curr := range inp { |
| 618 |
|
| 619 |
str := curr.Text |
| 620 |
|
| 621 |
if str == "" { |
| 622 |
continue |
| 623 |
} |
| 624 |
|
| 625 |
ident := curr.Ident |
| 626 |
|
| 627 |
if ident != currentIdent && currentIdent != "" { |
| 628 |
txt := buffer.String() |
| 629 |
indPath, _ := IndexTrie(currentIdent + "00") |
| 630 |
stringToGzFile(indBase, indPath, currentIdent+".e2x.gz", txt) |
| 631 |
buffer.Reset() |
| 632 |
|
| 633 |
if verbose { |
| 634 |
fmt.Fprintf(os.Stderr, "IDX %s/%s%s.e2x.gz\n", indBase, indPath, currentIdent) |
| 635 |
} else { |
| 636 |
// progress monitor |
| 637 |
countSuccess() |
| 638 |
} |
| 639 |
|
| 640 |
out <- currentIdent |
| 641 |
} |
| 642 |
|
| 643 |
currentIdent = ident |
| 644 |
|
| 645 |
buffer.WriteString(str) |
| 646 |
|
| 647 |
} |
| 648 |
|
| 649 |
if currentIdent != "" { |
| 650 |
txt := buffer.String() |
| 651 |
indPath, _ := IndexTrie(currentIdent + "00") |
| 652 |
stringToGzFile(indBase, indPath, currentIdent+".e2x.gz", txt) |
| 653 |
buffer.Reset() |
| 654 |
|
| 655 |
if verbose { |
| 656 |
fmt.Fprintf(os.Stderr, "IDX %s/%s%s.e2x.gz\n", indBase, indPath, currentIdent) |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
if rollingColumn > 0 { |
| 661 |
vlock.Lock() |
| 662 |
fmt.Fprintf(os.Stderr, "\n") |
| 663 |
vlock.Unlock() |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
// launch single index combiner goroutine |
| 668 |
go indexCombiner(indexBase, inp, out) |
| 669 |
|
| 670 |
return out |
| 671 |
} |
| 672 |
|
| 673 |
if dotMax == 0 { |
| 674 |
// show a dot every 200 .e2x files, generated from up to 20000 .xml files |
| 675 |
dotMax = 200 |
| 676 |
if db == "pmc" { |
| 677 |
dotMax = 50 |
| 678 |
} else if db == "taxonomy" { |
| 679 |
dotMax = 500 |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
vrfq := visitArchiveFolders(archiveBase) |
| 684 |
vifq := filterIndexFolders(indexBase, vrfq) |
| 685 |
strq := CreateFetchers(archiveBase, db, pfx, ".xml", ptrn, true, vifq) |
| 686 |
// callback passes cmds and transform values as closures to xtract createConsumers |
| 687 |
tblq := csmr(strq) |
| 688 |
// clean up XML (no measured benefit to adding next record size prefix) |
| 689 |
sifq := cleanIndexFiles(tblq) |
| 690 |
// restore original order, so indexed results are grouped by archive folder |
| 691 |
unsq := CreateXMLUnshuffler(sifq) |
| 692 |
cifq := combineIndexFiles(indexBase, dotMax, unsq) |
| 693 |
|
| 694 |
if vrfq == nil || vifq == nil || strq == nil || tblq == nil || sifq == nil || unsq == nil || cifq == nil { |
| 695 |
return nil |
| 696 |
} |
| 697 |
|
| 698 |
return cifq |
| 699 |
} |
| 700 |
|
| 701 |
// InvertIndexedFile reads IdxDocument XML strings and writes a combined InvDocument XML record |
| 702 |
func InvertIndexedFile(inp <-chan string) <-chan string { |
| 703 |
|
| 704 |
if inp == nil { |
| 705 |
return nil |
| 706 |
} |
| 707 |
|
| 708 |
indexDispenser := func(inp <-chan string) <-chan []string { |
| 709 |
|
| 710 |
if inp == nil { |
| 711 |
return nil |
| 712 |
} |
| 713 |
|
| 714 |
out := make(chan []string, chanDepth) |
| 715 |
if out == nil { |
| 716 |
DisplayError("Unable to create dispenser channel") |
| 717 |
os.Exit(1) |
| 718 |
} |
| 719 |
|
| 720 |
type Inverter struct { |
| 721 |
ilock sync.Mutex |
| 722 |
// map for inverted index |
| 723 |
inverted map[string][]string |
| 724 |
} |
| 725 |
|
| 726 |
inverters := make(map[rune]*Inverter) |
| 727 |
|
| 728 |
prefixes := "01234567890abcdefghijklmnopqrstuvwxyz" |
| 729 |
|
| 730 |
for _, ch := range prefixes { |
| 731 |
inverters[ch] = &Inverter{inverted: make(map[string][]string)} |
| 732 |
} |
| 733 |
|
| 734 |
// add single posting |
| 735 |
addPost := func(fld, term, pos, uid string) { |
| 736 |
|
| 737 |
ch := rune(term[0]) |
| 738 |
inv := inverters[ch] |
| 739 |
|
| 740 |
// protect map with mutex |
| 741 |
inv.ilock.Lock() |
| 742 |
|
| 743 |
data, ok := inv.inverted[term] |
| 744 |
if !ok { |
| 745 |
data = make([]string, 0, 4) |
| 746 |
// first entry on new slice is term |
| 747 |
data = append(data, term) |
| 748 |
} |
| 749 |
data = append(data, fld) |
| 750 |
data = append(data, uid) |
| 751 |
data = append(data, pos) |
| 752 |
// always need to update inverted, since data may be reallocated |
| 753 |
inv.inverted[term] = data |
| 754 |
|
| 755 |
inv.ilock.Unlock() |
| 756 |
} |
| 757 |
|
| 758 |
// xmlDispenser prepares UID, term, and position strings for inversion |
| 759 |
xmlDispenser := func(wg *sync.WaitGroup, inp <-chan string, out chan<- []string) { |
| 760 |
|
| 761 |
defer wg.Done() |
| 762 |
|
| 763 |
currUID := "" |
| 764 |
|
| 765 |
doDispense := func(tag, attr, content string) { |
| 766 |
|
| 767 |
if tag == "IdxUid" { |
| 768 |
currUID = content |
| 769 |
} else { |
| 770 |
|
| 771 |
content = html.UnescapeString(content) |
| 772 |
|
| 773 |
// expand Greek letters, anglicize characters in other alphabets |
| 774 |
if IsNotASCII(content) { |
| 775 |
|
| 776 |
content = TransformAccents(content, true, true) |
| 777 |
|
| 778 |
if HasAdjacentSpacesOrNewline(content) { |
| 779 |
content = CompressRunsOfSpaces(content) |
| 780 |
} |
| 781 |
|
| 782 |
content = UnicodeToASCII(content) |
| 783 |
|
| 784 |
if HasFlankingSpace(content) { |
| 785 |
content = strings.TrimSpace(content) |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
content = strings.ToLower(content) |
| 790 |
|
| 791 |
// remove punctuation from term |
| 792 |
content = strings.Map(func(c rune) rune { |
| 793 |
if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != ' ' && c != '-' && c != '_' { |
| 794 |
return -1 |
| 795 |
} |
| 796 |
return c |
| 797 |
}, content) |
| 798 |
|
| 799 |
content = strings.Replace(content, "_", " ", -1) |
| 800 |
content = strings.Replace(content, "-", " ", -1) |
| 801 |
|
| 802 |
if HasAdjacentSpacesOrNewline(content) { |
| 803 |
content = CompressRunsOfSpaces(content) |
| 804 |
} |
| 805 |
|
| 806 |
if HasFlankingSpace(content) { |
| 807 |
content = strings.TrimSpace(content) |
| 808 |
} |
| 809 |
|
| 810 |
if content != "" && currUID != "" { |
| 811 |
addPost(tag, content, attr, currUID) |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
// read partitioned XML from producer channel |
| 817 |
for str := range inp { |
| 818 |
StreamValues(str[:], "IdxDocument", doDispense) |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
var wg sync.WaitGroup |
| 823 |
|
| 824 |
// launch multiple dispenser goroutines |
| 825 |
for range numProcs { |
| 826 |
wg.Add(1) |
| 827 |
go xmlDispenser(&wg, inp, out) |
| 828 |
} |
| 829 |
|
| 830 |
// launch separate anonymous goroutine to wait until all dispensers are done |
| 831 |
go func() { |
| 832 |
wg.Wait() |
| 833 |
|
| 834 |
// send results to inverters |
| 835 |
for _, ch := range prefixes { |
| 836 |
inv := inverters[ch] |
| 837 |
for _, data := range inv.inverted { |
| 838 |
out <- data |
| 839 |
|
| 840 |
runtime.Gosched() |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
close(out) |
| 845 |
}() |
| 846 |
|
| 847 |
return out |
| 848 |
} |
| 849 |
|
| 850 |
indexInverter := func(inp <-chan []string) <-chan XMLRecord { |
| 851 |
|
| 852 |
if inp == nil { |
| 853 |
return nil |
| 854 |
} |
| 855 |
|
| 856 |
out := make(chan XMLRecord, chanDepth) |
| 857 |
if out == nil { |
| 858 |
DisplayError("Unable to create inverter channel") |
| 859 |
os.Exit(1) |
| 860 |
} |
| 861 |
|
| 862 |
// xmlInverter sorts and prints one posting list |
| 863 |
xmlInverter := func(wg *sync.WaitGroup, inp <-chan []string, out chan<- XMLRecord) { |
| 864 |
|
| 865 |
defer wg.Done() |
| 866 |
|
| 867 |
var buffer strings.Builder |
| 868 |
|
| 869 |
printPosting := func(key string, data []string) string { |
| 870 |
|
| 871 |
fields := make(map[string]map[string]string) |
| 872 |
|
| 873 |
for len(data) > 1 { |
| 874 |
fld := data[0] |
| 875 |
uid := data[1] |
| 876 |
att := data[2] |
| 877 |
positions, ok := fields[fld] |
| 878 |
if !ok { |
| 879 |
positions = make(map[string]string) |
| 880 |
fields[fld] = positions |
| 881 |
} |
| 882 |
// store position attribute string by uid |
| 883 |
positions[uid] = att |
| 884 |
// skip to next position |
| 885 |
data = data[3:] |
| 886 |
} |
| 887 |
|
| 888 |
buffer.Reset() |
| 889 |
|
| 890 |
buffer.WriteString("<InvDocument>\n") |
| 891 |
buffer.WriteString("<InvKey>") |
| 892 |
buffer.WriteString(key) |
| 893 |
buffer.WriteString("</InvKey>\n") |
| 894 |
buffer.WriteString("<InvIDs>\n") |
| 895 |
|
| 896 |
// sort fields in alphabetical order |
| 897 |
keys := slices.Sorted(maps.Keys(fields)) |
| 898 |
|
| 899 |
for _, fld := range keys { |
| 900 |
|
| 901 |
positions := fields[fld] |
| 902 |
|
| 903 |
arry := slices.SortedFunc(maps.Keys(positions), CompareAlphaOrNumericKeys) |
| 904 |
|
| 905 |
// print list of UIDs, skipping duplicates |
| 906 |
prev := "" |
| 907 |
for _, uid := range arry { |
| 908 |
if uid == prev { |
| 909 |
continue |
| 910 |
} |
| 911 |
|
| 912 |
buffer.WriteString("<") |
| 913 |
buffer.WriteString(fld) |
| 914 |
atr := positions[uid] |
| 915 |
if atr != "" { |
| 916 |
buffer.WriteString(" ") |
| 917 |
buffer.WriteString(atr) |
| 918 |
} |
| 919 |
buffer.WriteString(">") |
| 920 |
buffer.WriteString(uid) |
| 921 |
buffer.WriteString("</") |
| 922 |
buffer.WriteString(fld) |
| 923 |
buffer.WriteString(">\n") |
| 924 |
|
| 925 |
prev = uid |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
buffer.WriteString("</InvIDs>\n") |
| 930 |
buffer.WriteString("</InvDocument>\n") |
| 931 |
|
| 932 |
str := buffer.String() |
| 933 |
|
| 934 |
return str |
| 935 |
} |
| 936 |
|
| 937 |
for inv := range inp { |
| 938 |
|
| 939 |
key := inv[0] |
| 940 |
data := inv[1:] |
| 941 |
|
| 942 |
str := printPosting(key, data) |
| 943 |
|
| 944 |
out <- XMLRecord{Ident: key, Text: str} |
| 945 |
|
| 946 |
runtime.Gosched() |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
var wg sync.WaitGroup |
| 951 |
|
| 952 |
// launch multiple inverter goroutines |
| 953 |
for range numProcs { |
| 954 |
wg.Add(1) |
| 955 |
go xmlInverter(&wg, inp, out) |
| 956 |
} |
| 957 |
|
| 958 |
// launch separate anonymous goroutine to wait until all inverters are done |
| 959 |
go func() { |
| 960 |
wg.Wait() |
| 961 |
close(out) |
| 962 |
}() |
| 963 |
|
| 964 |
return out |
| 965 |
} |
| 966 |
|
| 967 |
indexResolver := func(inp <-chan XMLRecord) <-chan string { |
| 968 |
|
| 969 |
if inp == nil { |
| 970 |
return nil |
| 971 |
} |
| 972 |
|
| 973 |
out := make(chan string, chanDepth) |
| 974 |
if out == nil { |
| 975 |
DisplayError("Unable to create resolver channel") |
| 976 |
os.Exit(1) |
| 977 |
} |
| 978 |
|
| 979 |
// xmlResolver prints inverted postings alphabetized by identifier prefix |
| 980 |
xmlResolver := func(inp <-chan XMLRecord, out chan<- string) { |
| 981 |
|
| 982 |
// close channel when all records have been processed |
| 983 |
defer close(out) |
| 984 |
|
| 985 |
// map for inverted index |
| 986 |
inverted := make(map[string]string) |
| 987 |
|
| 988 |
// drain channel, populate map for alphabetizing |
| 989 |
for curr := range inp { |
| 990 |
|
| 991 |
inverted[curr.Ident] = curr.Text |
| 992 |
} |
| 993 |
|
| 994 |
ordered := slices.Sorted(maps.Keys(inverted)) |
| 995 |
|
| 996 |
// iterate through alphabetized results |
| 997 |
for _, curr := range ordered { |
| 998 |
|
| 999 |
txt := inverted[curr] |
| 1000 |
|
| 1001 |
// send result to output |
| 1002 |
out <- txt |
| 1003 |
|
| 1004 |
runtime.Gosched() |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
// launch single resolver goroutine |
| 1009 |
go xmlResolver(inp, out) |
| 1010 |
|
| 1011 |
return out |
| 1012 |
} |
| 1013 |
|
| 1014 |
idsq := indexDispenser(inp) |
| 1015 |
invq := indexInverter(idsq) |
| 1016 |
idrq := indexResolver(invq) |
| 1017 |
|
| 1018 |
if idsq == nil || invq == nil || idrq == nil { |
| 1019 |
return nil |
| 1020 |
} |
| 1021 |
|
| 1022 |
return idrq |
| 1023 |
} |
| 1024 |
|
| 1025 |
// IncrementalInvert creates or updates missing cached .inv.gz inverted index files |
| 1026 |
func IncrementalInvert(db string, dotMax int) <-chan string { |
| 1027 |
|
| 1028 |
if db == "" { |
| 1029 |
db = "pubmed" |
| 1030 |
} |
| 1031 |
|
| 1032 |
// obtain paths from environment variable(s) |
| 1033 |
indexBase := GetLocalArchiveFolder(db, "Index", true) |
| 1034 |
invertBase := GetLocalArchiveFolder(db, "Invert", true) |
| 1035 |
|
| 1036 |
if indexBase == "" || invertBase == "" { |
| 1037 |
DisplayError("Unable to get local index or invert paths") |
| 1038 |
os.Exit(1) |
| 1039 |
} |
| 1040 |
|
| 1041 |
// check to make sure local index directory is mounted |
| 1042 |
_, err := os.Stat(indexBase) |
| 1043 |
if err != nil && os.IsNotExist(err) { |
| 1044 |
DisplayError("Local incremental index is not mounted") |
| 1045 |
os.Exit(1) |
| 1046 |
} |
| 1047 |
|
| 1048 |
// check to make sure local invert directory is mounted |
| 1049 |
_, err = os.Stat(invertBase) |
| 1050 |
if err != nil && os.IsNotExist(err) { |
| 1051 |
DisplayError("Local invert directory is not mounted") |
| 1052 |
os.Exit(1) |
| 1053 |
} |
| 1054 |
|
| 1055 |
indexFetchers := func(inp <-chan string) <-chan string { |
| 1056 |
|
| 1057 |
if inp == nil { |
| 1058 |
return nil |
| 1059 |
} |
| 1060 |
|
| 1061 |
out := make(chan string, chanDepth) |
| 1062 |
if out == nil { |
| 1063 |
DisplayError("Unable to create index fetcher channel") |
| 1064 |
os.Exit(1) |
| 1065 |
} |
| 1066 |
|
| 1067 |
// e2xFetcher reads indexed XML from file |
| 1068 |
e2xFetcher := func(wg *sync.WaitGroup, inp <-chan string, out chan<- string) { |
| 1069 |
|
| 1070 |
defer wg.Done() |
| 1071 |
|
| 1072 |
for file := range inp { |
| 1073 |
|
| 1074 |
txt := gzFileToString(file) |
| 1075 |
|
| 1076 |
out <- txt |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
var wg sync.WaitGroup |
| 1081 |
|
| 1082 |
// launch multiple fetcher goroutines |
| 1083 |
for range numProcs { |
| 1084 |
wg.Add(1) |
| 1085 |
go e2xFetcher(&wg, inp, out) |
| 1086 |
} |
| 1087 |
|
| 1088 |
// launch separate anonymous goroutine to wait until all fetchers are done |
| 1089 |
go func() { |
| 1090 |
wg.Wait() |
| 1091 |
close(out) |
| 1092 |
}() |
| 1093 |
|
| 1094 |
return out |
| 1095 |
} |
| 1096 |
|
| 1097 |
invertIndexFiles := func(invBase string, inp <-chan []string) <-chan string { |
| 1098 |
|
| 1099 |
out := make(chan string, chanDepth) |
| 1100 |
if out == nil { |
| 1101 |
DisplayError("Unable to create index inverter channel") |
| 1102 |
os.Exit(1) |
| 1103 |
} |
| 1104 |
|
| 1105 |
invertIndexSubset := func(wg *sync.WaitGroup, invBase string, inp <-chan []string, out chan<- string) { |
| 1106 |
|
| 1107 |
defer wg.Done() |
| 1108 |
|
| 1109 |
for data := range inp { |
| 1110 |
|
| 1111 |
// path is first element in slice |
| 1112 |
path := data[0] |
| 1113 |
// "02/53" |
| 1114 |
|
| 1115 |
// followed by e2x file paths |
| 1116 |
filenames := data[1:] |
| 1117 |
|
| 1118 |
invPath := path[:3] |
| 1119 |
// "02/" |
| 1120 |
|
| 1121 |
invFile := strings.Replace(path, "/", "", -1) |
| 1122 |
// "0253" |
| 1123 |
|
| 1124 |
s2cq := SliceToChan(filenames) |
| 1125 |
idfq := indexFetchers(s2cq) |
| 1126 |
// indexDispenser | indexInverter | indexResolver |
| 1127 |
iifq := InvertIndexedFile(idfq) |
| 1128 |
|
| 1129 |
var buffer strings.Builder |
| 1130 |
|
| 1131 |
for str := range iifq { |
| 1132 |
buffer.WriteString(str) |
| 1133 |
} |
| 1134 |
|
| 1135 |
txt := buffer.String() |
| 1136 |
if txt == "" { |
| 1137 |
fmt.Fprintf(os.Stderr, "Empty %s\n", invPath+invFile) |
| 1138 |
return |
| 1139 |
} |
| 1140 |
|
| 1141 |
// save to target file |
| 1142 |
stringToGzFile(invertBase, invPath, invFile+".inv.gz", txt) |
| 1143 |
|
| 1144 |
out <- invFile + ".inv.gz" |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
var wg sync.WaitGroup |
| 1149 |
|
| 1150 |
// launch inverter goroutines (use 3 to numProcs: with numServe, PMC will eventually overflow memory) |
| 1151 |
for range numProcs { |
| 1152 |
wg.Add(1) |
| 1153 |
go invertIndexSubset(&wg, invBase, inp, out) |
| 1154 |
} |
| 1155 |
|
| 1156 |
// launch separate anonymous goroutine to wait until all inverters are done |
| 1157 |
go func() { |
| 1158 |
wg.Wait() |
| 1159 |
close(out) |
| 1160 |
}() |
| 1161 |
|
| 1162 |
return out |
| 1163 |
} |
| 1164 |
|
| 1165 |
// visitIndexFolders sends an Index leaf folder name, e.g., "02/53", |
| 1166 |
// plus full paths to all component index files, down a channel, but |
| 1167 |
// only if there is no existing Invert file, e.g., Invert/02/0253.inv.gz |
| 1168 |
visitIndexFolders := func(indexBase, invertBase string) <-chan []string { |
| 1169 |
|
| 1170 |
out := make(chan []string, chanDepth) |
| 1171 |
if out == nil { |
| 1172 |
DisplayError("Unable to create index explorer channel") |
| 1173 |
os.Exit(1) |
| 1174 |
} |
| 1175 |
|
| 1176 |
// mutex to protect access to rollingCount and rollingColumn variables |
| 1177 |
var vlock sync.Mutex |
| 1178 |
|
| 1179 |
if dotMax == 0 { |
| 1180 |
// show a dot every 10 .inv files that were deleted for regeneration, |
| 1181 |
// representing from up to 1000 .e2x files, some also just regenerated |
| 1182 |
dotMax = 10 |
| 1183 |
} |
| 1184 |
|
| 1185 |
rollingCount := 0 |
| 1186 |
rollingColumn := 0 |
| 1187 |
|
| 1188 |
countSuccess := func() { |
| 1189 |
|
| 1190 |
vlock.Lock() |
| 1191 |
|
| 1192 |
rollingCount++ |
| 1193 |
if rollingCount >= dotMax { |
| 1194 |
rollingCount = 0 |
| 1195 |
// print dot (progress monitor) |
| 1196 |
fmt.Fprintf(os.Stderr, ".") |
| 1197 |
rollingColumn++ |
| 1198 |
if rollingColumn > 49 { |
| 1199 |
// print newline after 50 dots |
| 1200 |
fmt.Fprintf(os.Stderr, "\n") |
| 1201 |
rollingColumn = 0 |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
vlock.Unlock() |
| 1206 |
} |
| 1207 |
|
| 1208 |
verbose := false |
| 1209 |
// set verbose flag from environment variable |
| 1210 |
env := os.Getenv("EDIRECT_LOCAL_VERBOSE") |
| 1211 |
if env == "Y" || env == "y" || env == "true" { |
| 1212 |
verbose = true |
| 1213 |
} |
| 1214 |
|
| 1215 |
// recursive definition |
| 1216 |
var visitSubFolders func(idxBase, invBase, path string, out chan<- []string) |
| 1217 |
|
| 1218 |
// visitSubFolders recurses to leaf directories |
| 1219 |
visitSubFolders = func(idxBase, invBase, path string, out chan<- []string) { |
| 1220 |
|
| 1221 |
dirs, _, e2xs, _ := examineFolder(idxBase, path) |
| 1222 |
|
| 1223 |
// recursively explore subdirectories |
| 1224 |
if dirs != nil { |
| 1225 |
for _, dr := range dirs { |
| 1226 |
sub := filepath.Join(path, dr) |
| 1227 |
visitSubFolders(idxBase, invBase, sub, out) |
| 1228 |
} |
| 1229 |
return |
| 1230 |
} |
| 1231 |
|
| 1232 |
// looking for leaf Index directory with at least one *.e2x.gz file |
| 1233 |
if e2xs == nil { |
| 1234 |
return |
| 1235 |
} |
| 1236 |
|
| 1237 |
// "02/53" |
| 1238 |
|
| 1239 |
invPath := path[:2] |
| 1240 |
// "02" |
| 1241 |
|
| 1242 |
invFile := strings.Replace(path, "/", "", -1) |
| 1243 |
// "0253" |
| 1244 |
|
| 1245 |
target := filepath.Join(invBase, invPath, invFile+".inv.gz") |
| 1246 |
|
| 1247 |
_, err := os.Stat(target) |
| 1248 |
if err == nil { |
| 1249 |
// if inverted index file exists for the indexed folder, no need to recreate |
| 1250 |
return |
| 1251 |
} |
| 1252 |
|
| 1253 |
var res []string |
| 1254 |
|
| 1255 |
// first string is indexed folder path, to become inverted file name |
| 1256 |
res = append(res, path) |
| 1257 |
|
| 1258 |
for _, file := range e2xs { |
| 1259 |
|
| 1260 |
// path for indexed file within current leaf folder |
| 1261 |
fpath := filepath.Join(idxBase, path, file) |
| 1262 |
|
| 1263 |
res = append(res, fpath) |
| 1264 |
} |
| 1265 |
|
| 1266 |
if verbose { |
| 1267 |
fmt.Fprintf(os.Stderr, "INV %s\n", invFile) |
| 1268 |
} else { |
| 1269 |
// progress monitor |
| 1270 |
countSuccess() |
| 1271 |
} |
| 1272 |
|
| 1273 |
out <- res |
| 1274 |
} |
| 1275 |
|
| 1276 |
visitIndexSubset := func(idxBase, invBase string, out chan<- []string) { |
| 1277 |
|
| 1278 |
defer close(out) |
| 1279 |
|
| 1280 |
dirs, _, _, _ := examineFolder(idxBase, "") |
| 1281 |
|
| 1282 |
// iterate through top directories |
| 1283 |
for _, top := range dirs { |
| 1284 |
if IsAllDigits(top) && len(top) == 2 { |
| 1285 |
visitSubFolders(idxBase, invBase, top, out) |
| 1286 |
} |
| 1287 |
|
| 1288 |
// force garbage collection |
| 1289 |
runtime.GC() |
| 1290 |
|
| 1291 |
runtime.Gosched() |
| 1292 |
} |
| 1293 |
|
| 1294 |
if rollingColumn > 0 { |
| 1295 |
vlock.Lock() |
| 1296 |
fmt.Fprintf(os.Stderr, "\n") |
| 1297 |
vlock.Unlock() |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
// launch single index visitor goroutine |
| 1302 |
go visitIndexSubset(indexBase, invertBase, out) |
| 1303 |
|
| 1304 |
return out |
| 1305 |
} |
| 1306 |
|
| 1307 |
vafq := visitIndexFolders(indexBase, invertBase) |
| 1308 |
out := invertIndexFiles(invertBase, vafq) |
| 1309 |
if vafq == nil || out == nil { |
| 1310 |
DisplayError("Unable to create visitIndexFolders channel") |
| 1311 |
os.Exit(1) |
| 1312 |
} |
| 1313 |
|
| 1314 |
return out |
| 1315 |
} |