summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-14 03:08:07 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-14 03:08:07 -0400
commit2492014051e5cd4c37287a09639d18f85e1349dc (patch)
treefa3af5377e10f6f6748390d1969b5879bc21593a
parenta8ba50b53f45e06af03f5ef98e759539394a3c0c (diff)
downloadgetwtxt-2492014051e5cd4c37287a09639d18f85e1349dc.tar.gz
moved query functions to own file, made SortByTime query-agnostic
-rw-r--r--registry/index.go87
-rw-r--r--registry/query.go93
-rw-r--r--registry/types.go10
3 files changed, 98 insertions, 92 deletions
diff --git a/registry/index.go b/registry/index.go
index 85277fc..4d82ebb 100644
--- a/registry/index.go
+++ b/registry/index.go
@@ -2,8 +2,6 @@ package registry
 
 import (
 	"log"
-	"sort"
-	"strings"
 	"time"
 )
 
@@ -30,88 +28,3 @@ func (index UserIndex) DelUser(url string) {
 	delete(index, url)
 	imutex.Unlock()
 }
-
-// QueryUser checks the user index for nicknames that contain the
-// nickname provided as an argument. Entries are returned sorted
-// by the date they were added to the index.
-func (index UserIndex) QueryUser(name string) []string {
-	var timekey = map[time.Time]string{}
-	var sortedkeys TimeSlice
-	var users []string
-	imutex.RLock()
-	for k, v := range index {
-		if strings.Contains(v.nick, name) {
-			timekey[v.date] = v.nick + "\t" + k + "\t" + string(v.apidate)
-			sortedkeys = append(sortedkeys, v.date)
-		}
-	}
-	imutex.RUnlock()
-	sort.Sort(sortedkeys)
-	for _, e := range sortedkeys {
-		users = append(users, timekey[e])
-	}
-
-	return users
-}
-
-// QueryTag returns all the known statuses that
-// contain the provided tag.
-func (index UserIndex) QueryTag(tag string) []string {
-	var statusmap StatusMapSlice
-	i := 0
-	imutex.RLock()
-	for _, v := range index {
-		statusmap[i] = v.FindTag(tag)
-		i++
-	}
-	imutex.RUnlock()
-
-	return statusmap.SortByTime()
-}
-
-// FindTag takes a user's tweets and looks for a given tag.
-// Returns the tweets with the tag as a []string.
-func (userdata *Data) FindTag(tag string) StatusMap {
-	var statuses StatusMap
-	for k, e := range userdata.status {
-		parts := strings.Split(e, "\t")
-		statusslice := strings.Split(parts[3], " ")
-		for _, v := range statusslice {
-			if v[1:] == tag {
-				statuses[k] = e
-				break
-			}
-		}
-	}
-
-	return statuses
-}
-
-// SortByTime returns a string slice of the statuses sorted by time
-func (sm StatusMapSlice) SortByTime() []string {
-	var tagmap StatusMap
-	var times TimeSlice
-	var statuses []string
-	for _, e := range sm {
-		for k, v := range e {
-			tagmap[k] = v
-		}
-	}
-	for k := range tagmap {
-		times = append(times, k)
-	}
-	sort.Sort(times)
-	for _, e := range times {
-		statuses = append(statuses, tagmap[e])
-	}
-
-	return statuses
-}
-
-// GetStatuses returns the string slice containing a user's statuses
-func (index UserIndex) GetStatuses(url string) StatusMap {
-	imutex.RLock()
-	status := index[url].status
-	imutex.RUnlock()
-	return status
-}
diff --git a/registry/query.go b/registry/query.go
new file mode 100644
index 0000000..e024bc6
--- /dev/null
+++ b/registry/query.go
@@ -0,0 +1,93 @@
+package registry
+
+import (
+	"sort"
+	"strings"
+	"time"
+)
+
+// QueryUser checks the user index for nicknames that contain the
+// nickname provided as an argument. Entries are returned sorted
+// by the date they were added to the index.
+func (index UserIndex) QueryUser(name string) []string {
+	var timekey = map[time.Time]string{}
+	var keys TimeSlice
+	var users []string
+	imutex.RLock()
+	for k, v := range index {
+		if strings.Contains(v.nick, name) {
+			timekey[v.date] = v.nick + "\t" + k + "\t" + string(v.apidate)
+			keys = append(keys, v.date)
+		}
+	}
+	imutex.RUnlock()
+	sort.Sort(keys)
+	for _, e := range keys {
+		users = append(users, timekey[e])
+	}
+
+	return users
+}
+
+// QueryTag returns all the known statuses that
+// contain the provided tag.
+func (index UserIndex) QueryTag(tag string) []string {
+	var statusmap TimeMapSlice
+	i := 0
+	imutex.RLock()
+	for _, v := range index {
+		statusmap[i] = v.FindTag(tag)
+		i++
+	}
+	imutex.RUnlock()
+
+	return statusmap.SortByTime()
+}
+
+// FindTag takes a user's tweets and looks for a given tag.
+// Returns the tweets with the tag as a []string.
+func (userdata *Data) FindTag(tag string) TimeMap {
+	var statuses TimeMap
+	for k, e := range userdata.status {
+		parts := strings.Split(e, "\t")
+		statusslice := strings.Split(parts[3], " ")
+		for _, v := range statusslice {
+			if v[1:] == tag {
+				statuses[k] = e
+				break
+			}
+		}
+	}
+
+	return statuses
+}
+
+// SortByTime returns a string slice of the query results
+// sorted by time.Time
+func (tm TimeMapSlice) SortByTime() []string {
+	var unionmap TimeMap
+	var times TimeSlice
+	var data []string
+	for _, e := range tm {
+		for k, v := range e {
+			unionmap[k] = v
+		}
+	}
+	for k := range unionmap {
+		times = append(times, k)
+	}
+	sort.Sort(times)
+	for _, e := range times {
+		data = append(data, unionmap[e])
+	}
+
+	return data
+}
+
+// GetStatuses returns the string slice containing a user's statuses
+func (index UserIndex) GetStatuses(url string) TimeMap {
+	imutex.RLock()
+	status := index[url].status
+	imutex.RUnlock()
+	return status
+}
diff --git a/registry/types.go b/registry/types.go
index d3e421a..43b323f 100644
--- a/registry/types.go
+++ b/registry/types.go
@@ -19,16 +19,16 @@ type Data struct {
 	nick    string
 	date    time.Time
 	apidate []byte
-	status  StatusMap
+	status  TimeMap
 }
 
-// StatusMap holds the statuses posted by a given user. A standard
-// time.Time value is used as the key, with the status as a string.
-type StatusMap map[time.Time]string
+// TimeMap holds extracted and processed user data as a
+// string. A standard time.Time value is used as the key.
+type TimeMap map[time.Time]string
 
 // StatusMapSlice is a slice of StatusMaps. Useful for sorting the
 // output of queries.
-type StatusMapSlice []StatusMap
+type TimeMapSlice []TimeMap
 
 // Mutex to control access to the User Index.
 var imutex = sync.RWMutex{}
' href='#n459'>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