Skip to content

Instantly share code, notes, and snippets.

@tuhuynh27
Last active July 22, 2019 05:31
Show Gist options
  • Save tuhuynh27/6c5107dc1a05c5961cc45c1d568cd3f5 to your computer and use it in GitHub Desktop.
Save tuhuynh27/6c5107dc1a05c5961cc45c1d568cd3f5 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Matching ...
type Matching struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Email string `json:"email" bson:"email"`
BookDetailID int `json:"bookDetailId,omitempty" bson:"bookDetailId,omitempty"`
BookID *string `json:"bookId,omitempty" bson:"bookId,omitempty"`
Matched bool `json:"matched" bson:"matched"`
Time int `json:"time" bson:"time"`
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal(err)
}
collection := client.Database("librarianblockchain").Collection("matchings")
cur, err := collection.Find(ctx, bson.M{"matched": false})
if err != nil {
log.Fatal(err)
}
var matchingSlice []Matching
for cur.Next(ctx) {
var result Matching
err := cur.Decode(&result)
if err != nil {
log.Fatal(err)
}
matchingSlice = append(matchingSlice, result)
}
uniqueBookIDs := distinctSlice(matchingSlice)
queues := createQueueSlice(uniqueBookIDs, matchingSlice)
checkQueues(queues)
if err := cur.Err(); err != nil {
log.Fatal(err)
}
}
func distinctSlice(slice []Matching) []int {
var unique []int
for _, v := range slice {
skip := false
for _, u := range unique {
if v.BookDetailID == u {
skip = true
break
}
}
if !skip {
unique = append(unique, v.BookDetailID)
}
}
return unique
}
func createBookDetailIDSlice(slice []Matching, bookDetailID int) []Matching {
var result []Matching
for _, v := range slice {
if v.BookDetailID == bookDetailID {
result = append(result, v)
}
}
return result
}
func createQueueSlice(bookDetailIds []int, slice []Matching) []([]Matching) {
var result []([]Matching)
for _, v := range bookDetailIds {
queue := createBookDetailIDSlice(slice, v)
result = append(result, queue)
}
return result
}
func createRequestSlice(slice []Matching, isReturning bool) []Matching {
var result []Matching
for _, v := range slice {
if isReturning {
if v.BookID != nil {
result = append(result, v)
}
} else {
if v.BookID == nil {
result = append(result, v)
}
}
}
return result
}
func checkQueues(queues []([]Matching)) {
for _, v := range queues {
returnSlice := createRequestSlice(v, true)
requestSlice := createRequestSlice(v, false)
var shorterLength int
if len(returnSlice) > len(requestSlice) {
shorterLength = len(requestSlice)
} else {
shorterLength = len(returnSlice)
}
if shorterLength == 0 {
log.Println("No match!")
return
}
for i := 0; i < shorterLength; i++ {
matchedReturner := returnSlice[i]
matchedRequester := requestSlice[i]
log.Println("Matched!")
log.Println("Returner: ", matchedReturner)
log.Println("Requester: ", matchedRequester)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment