/usr/share/gocode/src/github.com/ctdk/goiardi/indexer/file_index.go is in golang-github-ctdk-goiardi-dev 0.11.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | /*
* Copyright (c) 2013-2016, Jeremy Bingham (<jeremy@goiardi.gl>), Zsolt Takács
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package indexer
import (
"bytes"
"compress/zlib"
"encoding/gob"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"sort"
"strings"
"sync"
"github.com/ctdk/go-trie/gtrie"
"github.com/ctdk/goiardi/util"
"github.com/tideland/golib/logger"
"github.com/tinylib/msgp/msgp"
)
type FileIndex struct {
file string
m sync.RWMutex
idxmap map[string]IndexCollection
updated bool
}
type IndexCollection interface {
addDoc(Indexable)
delDoc(string)
allDocs() map[string]Document
searchCollection(string, bool) (map[string]Document, error)
searchTextCollection(string, bool) (map[string]Document, error)
searchRange(string, string, string, bool) (map[string]Document, error)
}
// IdxCollection holds a map of documents.
type IdxCollection struct {
m sync.RWMutex
docs map[string]*IdxDoc
}
// IdxDoc is the indexed documents that are actually searched.
type IdxDoc struct {
m sync.RWMutex
trie []byte
docText []byte
}
type searchRes struct {
key string
doc *IdxDoc
}
/* Index methods */
func (i *FileIndex) Initialize() error {
in := new(FileIndex)
ic := new(IdxCollection)
id := new(IdxDoc)
gob.Register(in)
gob.Register(ic)
gob.Register(id)
i.makeDefaultCollections()
return nil
}
func (i *FileIndex) CreateCollection(idxName string) error {
if _, ok := i.idxmap[idxName]; !ok {
coll := new(IdxCollection)
coll.docs = make(map[string]*IdxDoc)
casted := IndexCollection(coll)
i.idxmap[idxName] = casted
}
return nil
}
func (i *FileIndex) CreateNewCollection(idxName string) error {
i.m.Lock()
defer i.m.Unlock()
i.updated = true
return i.CreateCollection(idxName)
}
func (i *FileIndex) DeleteCollection(idxName string) error {
i.m.Lock()
defer i.m.Unlock()
/* Don't try and delete built-in indexes */
if idxName == "node" || idxName == "client" || idxName == "environment" || idxName == "role" {
err := fmt.Errorf("%s is a default search index, cannot be deleted.", idxName)
return err
}
i.updated = true
delete(i.idxmap, idxName)
return nil
}
func (i *FileIndex) SaveItem(object Indexable) error {
/* Have to check to see if data bag indexes exist */
i.m.Lock()
defer i.m.Unlock()
i.updated = true
if _, found := i.idxmap[object.Index()]; !found {
i.CreateCollection(object.Index())
}
i.idxmap[object.Index()].addDoc(object)
return nil
}
func (i *FileIndex) DeleteItem(idxName string, doc string) error {
i.m.Lock()
defer i.m.Unlock()
i.updated = true
if _, found := i.idxmap[idxName]; !found {
err := fmt.Errorf("Index collection %s not found", idxName)
return err
}
i.idxmap[idxName].delDoc(doc)
return nil
}
func (i *FileIndex) Search(idx string, term string, notop bool) (map[string]Document, error) {
i.m.RLock()
defer i.m.RUnlock()
idc, found := i.idxmap[idx]
if !found {
err := fmt.Errorf("I don't know how to search for %s data objects.", idx)
return nil, err
}
// Special case - if term is '*:*', just return all of the keys
if term == "*:*" {
return idc.allDocs(), nil
}
results, err := idc.searchCollection(unescape(term), notop)
return results, err
}
func (i *FileIndex) SearchText(idx string, term string, notop bool) (map[string]Document, error) {
i.m.RLock()
defer i.m.RUnlock()
idc, found := i.idxmap[idx]
if !found {
err := fmt.Errorf("I don't know how to search for %s data objects.", idx)
return nil, err
}
results, err := idc.searchTextCollection(unescape(term), notop)
return results, err
}
func (i *FileIndex) SearchRange(idx string, field string, start string, end string, inclusive bool) (map[string]Document, error) {
i.m.RLock()
defer i.m.RUnlock()
idc, found := i.idxmap[idx]
if !found {
err := fmt.Errorf("I don't know how to search for %s data objects.", idx)
return nil, err
}
results, err := idc.searchRange(field, start, end, inclusive)
return results, err
}
// SearchResults does a basic search from an existing collection of documents,
// rather than the full index.
func (i *FileIndex) SearchResults(term string, notop bool, docs map[string]Document) (map[string]Document, error) {
i.m.RLock()
defer i.m.RUnlock()
d := docToIdxDoc(docs)
idc := &IdxCollection{docs: d}
if term == "*:*" {
if notop {
d := make(map[string]Document)
return d, nil
}
return docs, nil
}
res, err := idc.searchCollection(unescape(term), notop)
return res, err
}
func docToIdxDoc(docs map[string]Document) map[string]*IdxDoc {
d := make(map[string]*IdxDoc, len(docs))
for k, v := range docs {
d[k] = v.(*IdxDoc)
}
return d
}
// SearchResultsRange does a range search on a collection of search results,
// rather than the full index.
func (i *FileIndex) SearchResultsRange(field string, start string, end string, inclusive bool, docs map[string]Document) (map[string]Document, error) {
i.m.RLock()
defer i.m.RUnlock()
d := docToIdxDoc(docs)
idc := &IdxCollection{docs: d}
res, err := idc.searchRange(field, start, end, inclusive)
return res, err
}
// SearchResultsText does a text searc on a collection of search results,
// rather than the full index.
func (i *FileIndex) SearchResultsText(term string, notop bool, docs map[string]Document) (map[string]Document, error) {
i.m.RLock()
defer i.m.RUnlock()
d := docToIdxDoc(docs)
idc := &IdxCollection{docs: d}
res, err := idc.searchTextCollection(unescape(term), notop)
return res, err
}
// Endpoints returns a list of currently indexed endpoints.
func (i *FileIndex) Endpoints() ([]string, error) {
i.m.RLock()
defer i.m.RUnlock()
endpoints := make([]string, len(i.idxmap))
n := 0
for k := range i.idxmap {
endpoints[n] = k
n++
}
sort.Strings(endpoints)
return endpoints, nil
}
func (i *FileIndex) Clear() error {
i.makeDefaultCollections()
return nil
}
func (i *FileIndex) makeDefaultCollections() {
defaults := [...]string{"client", "environment", "node", "role"}
i.m.Lock()
defer i.m.Unlock()
i.updated = true
i.idxmap = make(map[string]IndexCollection)
for _, d := range defaults {
i.CreateCollection(d)
}
}
/* IdxCollection methods */
func (ic *IdxCollection) addDoc(object Indexable) {
ic.m.Lock()
if _, found := ic.docs[object.DocID()]; !found {
ic.docs[object.DocID()] = new(IdxDoc)
}
ic.m.Unlock()
ic.m.RLock()
defer ic.m.RUnlock()
ic.docs[object.DocID()].update(object)
}
func (ic *IdxCollection) delDoc(doc string) {
ic.m.Lock()
defer ic.m.Unlock()
delete(ic.docs, doc)
}
/* Search for an exact key/value match */
func (ic *IdxCollection) searchCollection(term string, notop bool) (map[string]Document, error) {
results := make(map[string]Document)
ic.m.RLock()
defer ic.m.RUnlock()
l := len(ic.docs)
errCh := make(chan error, l)
resCh := make(chan *searchRes, l)
for k, v := range ic.docs {
go func(k string, v *IdxDoc) {
m, err := v.Examine(term)
if err != nil {
errCh <- err
resCh <- nil
} else {
errCh <- nil
if (m && !notop) || (!m && notop) {
r := &searchRes{k, v}
resCh <- r
} else {
resCh <- nil
}
}
}(k, v)
}
for i := 0; i < l; i++ {
e := <-errCh
if e != nil {
return nil, e
}
}
for i := 0; i < l; i++ {
r := <-resCh
if r != nil {
results[r.key] = Document(r.doc)
}
}
rsafe := safeSearchResults(results)
return rsafe, nil
}
func (ic *IdxCollection) searchTextCollection(term string, notop bool) (map[string]Document, error) {
results := make(map[string]Document)
ic.m.RLock()
defer ic.m.RUnlock()
l := len(ic.docs)
errCh := make(chan error, l)
resCh := make(chan *searchRes, l)
for k, v := range ic.docs {
go func(k string, v *IdxDoc) {
m, err := v.TextSearch(term)
if err != nil {
errCh <- err
resCh <- nil
} else {
errCh <- nil
if (m && !notop) || (!m && notop) {
r := &searchRes{k, v}
logger.Debugf("Adding result %s to channel", k)
resCh <- r
} else {
resCh <- nil
}
}
}(k, v)
}
for i := 0; i < l; i++ {
e := <-errCh
if e != nil {
return nil, e
}
}
for i := 0; i < l; i++ {
r := <-resCh
if r != nil {
logger.Debugf("adding result")
results[r.key] = Document(r.doc)
}
}
rsafe := safeSearchResults(results)
return rsafe, nil
}
func (ic *IdxCollection) searchRange(field string, start string, end string, inclusive bool) (map[string]Document, error) {
results := make(map[string]Document)
ic.m.RLock()
defer ic.m.RUnlock()
l := len(ic.docs)
errCh := make(chan error, l)
resCh := make(chan *searchRes, l)
for k, v := range ic.docs {
go func(k string, v *IdxDoc) {
m, err := v.RangeSearch(field, start, end, inclusive)
if err != nil {
errCh <- err
resCh <- nil
} else {
errCh <- nil
if m {
r := &searchRes{k, v}
logger.Debugf("Adding result %s to channel", k)
resCh <- r
} else {
resCh <- nil
}
}
}(k, v)
}
for i := 0; i < l; i++ {
e := <-errCh
if e != nil {
return nil, e
}
}
for i := 0; i < l; i++ {
r := <-resCh
if r != nil {
logger.Debugf("adding result")
results[r.key] = Document(r.doc)
}
}
rsafe := safeSearchResults(results)
return rsafe, nil
}
func safeSearchResults(results map[string]Document) map[string]Document {
rsafe := make(map[string]Document, len(results))
for k, v := range results {
j := &v
rsafe[k] = *j
}
return rsafe
}
func (ic *IdxCollection) allDocs() map[string]Document {
docs := make(map[string]Document, len(ic.docs))
for k, v := range ic.docs {
docs[k] = Document(v)
}
return docs
}
/* IdxDoc methods */
func (idoc *IdxDoc) update(object Indexable) {
idoc.m.Lock()
defer idoc.m.Unlock()
flattened := util.Indexify(object.Flatten())
flatText := strings.Join(flattened, "\n")
/* recover from horrific trie errors that seem to happen with really
* big values. :-/ */
defer func() {
if e := recover(); e != nil {
logger.Errorf("There was a problem creating the trie: %s", fmt.Sprintln(e))
}
}()
trie, err := gtrie.Create(flattened)
if err != nil {
logger.Errorf(err.Error())
} else {
var err error
idoc.trie, err = compressTrie(trie)
if err != nil {
panic(err)
}
idoc.docText, err = compressText(flatText)
if err != nil {
panic(err)
}
}
}
// Examine searches a document, determining if it needs to do a search for an
// exact term or a regexp search.
func (idoc *IdxDoc) Examine(term string) (bool, error) {
idoc.m.RLock()
defer idoc.m.RUnlock()
r := regexp.MustCompile(`\*|\?`)
if r.MatchString(term) {
m, err := idoc.regexSearch(term)
return m, err
}
m, err := idoc.exactSearch(term)
if err != nil {
return false, err
}
return m, nil
}
// TextSearch performs a text search of an index document.
func (idoc *IdxDoc) TextSearch(term string) (bool, error) {
if term[0] == '*' || term[0] == '?' {
err := fmt.Errorf("Can't start a term with a wildcard character")
return false, err
}
term = strings.Replace(term, "*", ".*", -1)
term = strings.Replace(term, "?", ".?", -1)
re := fmt.Sprintf("(?m):%s$", term)
reComp, err := regexp.Compile(re)
if err != nil {
return false, err
}
idoc.m.RLock()
defer idoc.m.RUnlock()
docText, err := decompressText(idoc.docText)
if err != nil {
return false, err
}
m := reComp.MatchString(docText)
return m, nil
}
// RangeSearch searches for a range of values.
func (idoc *IdxDoc) RangeSearch(field string, start string, end string, inclusive bool) (bool, error) {
// The parser should catch a lot of possible errors, happily
// "*" is permitted as a range that indicates anything bigger or smaller
// than the other range, depending
wildStart := false
wildEnd := false
if start == "*" {
wildStart = true
}
if end == "*" {
wildEnd = true
}
if wildStart && wildEnd {
err := fmt.Errorf("you can't have both start and end be wild in a range search, sadly")
return false, err
}
idoc.m.RLock()
defer idoc.m.RUnlock()
key := fmt.Sprintf("%s:", field)
trie, err := decompressTrie(idoc.trie)
if err != nil {
return false, err
}
if n, _ := trie.HasPrefix(key); n != nil {
kids := n.ChildKeys()
for _, child := range kids {
if inclusive {
if wildStart {
if child <= end {
return true, nil
}
} else if wildEnd {
if child >= start {
return true, nil
}
} else {
if child >= start && child <= end {
return true, nil
}
}
} else {
if wildStart {
if child < end {
return true, nil
}
} else if wildEnd {
if child > start {
return true, nil
}
} else {
if child > start && child < end {
return true, nil
}
}
}
}
}
return false, nil
}
func (idoc *IdxDoc) exactSearch(term string) (bool, error) {
trie, err := decompressTrie(idoc.trie)
if err != nil {
return false, err
}
return trie.Accepts(term), nil
}
func (idoc *IdxDoc) regexSearch(reTerm string) (bool, error) {
z := strings.SplitN(reTerm, ":", 2)
key := fmt.Sprintf("%s:", z[0])
re := z[1]
/* Must add . before any * or ? in the regexp first. Taking the easy way
* out and using strings.Replace. */
re = strings.Replace(re, ".", "\\.", -1)
re = strings.Replace(re, "*", ".*", -1)
re = strings.Replace(re, "?", ".?", -1)
reComp, err := regexp.Compile(re)
if err != nil {
return false, err
}
/* What would be better would be to fetch all of the parts of the key
* before the regexp part starts. Hmmm. */
trie, err := decompressTrie(idoc.trie)
if err != nil {
return false, err
}
if n, _ := trie.HasPrefix(key); n != nil {
kids := n.ChildKeys()
for _, c := range kids {
if reComp.MatchString(c) {
return true, nil
}
}
}
return false, nil
}
/* gob encoding functions for the index */
func (i *FileIndex) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
err := encoder.Encode(i.idxmap)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (i *FileIndex) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)
return decoder.Decode(&i.idxmap)
}
func (ic *IdxCollection) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
ic.m.RLock()
defer ic.m.RUnlock()
err := encoder.Encode(ic.docs)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (ic *IdxCollection) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)
return decoder.Decode(&ic.docs)
}
func (idoc *IdxDoc) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
idoc.m.RLock()
defer idoc.m.RUnlock()
err := encoder.Encode(idoc.trie)
if err != nil {
return nil, err
}
err = encoder.Encode(idoc.docText)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (idoc *IdxDoc) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)
err := decoder.Decode(&idoc.trie)
if err != nil {
return err
}
return decoder.Decode(&idoc.docText)
}
func (i *FileIndex) Save() error {
i.m.RLock()
defer i.m.RUnlock()
idxFile := i.file
if idxFile == "" {
err := fmt.Errorf("Yikes! Cannot save index to disk because no file was specified.")
return err
}
if !i.updated {
return nil
}
logger.Infof("Index has changed, saving to disk")
fp, err := ioutil.TempFile(path.Dir(idxFile), "idx-build")
if err != nil {
return err
}
zfp := zlib.NewWriter(fp)
i.updated = false
enc := gob.NewEncoder(zfp)
err = enc.Encode(i)
zfp.Close()
if err != nil {
fp.Close()
return err
}
err = fp.Close()
if err != nil {
return err
}
return os.Rename(fp.Name(), idxFile)
}
func (i *FileIndex) Load() error {
idxFile := i.file
if idxFile == "" {
err := fmt.Errorf("Yikes! Cannot load index from disk because no file was specified.")
return err
}
fp, err := os.Open(idxFile)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
zfp, zerr := zlib.NewReader(fp)
if zerr != nil {
fp.Close()
return zerr
}
dec := gob.NewDecoder(zfp)
err = dec.Decode(&i)
zfp.Close()
if err != nil {
fp.Close()
return err
}
return fp.Close()
}
func compressTrie(t *gtrie.Node) ([]byte, error) {
b := new(bytes.Buffer)
z := zlib.NewWriter(b)
err := msgp.Encode(z, t)
z.Close()
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func decompressTrie(buf []byte) (*gtrie.Node, error) {
b := bytes.NewBuffer(buf)
z, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
t := new(gtrie.Node)
err = msgp.Decode(z, t)
err2 := z.Close()
if err != nil {
return nil, err
}
if err2 != nil {
return nil, err2
}
return t, nil
}
func compressText(t string) ([]byte, error) {
b := new(bytes.Buffer)
z := zlib.NewWriter(b)
_, err := z.Write([]byte(t))
z.Close()
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
func decompressText(buf []byte) (string, error) {
b := bytes.NewBuffer(buf)
z, err := zlib.NewReader(b)
if err != nil {
return "", err
}
t, err := ioutil.ReadAll(z)
z.Close()
if err != nil {
return "", err
}
return string(t), nil
}
func unescape(term string) string {
re := regexp.MustCompile(`\\(\S)`)
return re.ReplaceAllString(term, "$1")
}
|