Last active
October 19, 2016 01:17
-
-
Save macalinao/195904dbb41c7339c6cbb78ad05ee601 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (a *matchSumDAO) Get(f *apb.MatchFilters) (*apb.MatchSum, error) { | |
var rawSum []byte | |
if err := a.CQL.Query( | |
stmtGetSum, f.ChampionId, f.EnemyId, f.Patch, | |
f.Tier, int32(f.Region), int32(f.Role), | |
).Scan(&rawSum); err != nil { | |
if err == gocql.ErrNotFound { | |
return nil, nil | |
} | |
return nil, fmt.Errorf("error fetching sum from Cassandra: %v", err) | |
} | |
var sum apb.MatchSum | |
if err := proto.Unmarshal(rawSum, &sum); err != nil { | |
return nil, fmt.Errorf("error unmarshaling sum: %v", err) | |
} | |
return &sum, nil | |
} | |
// Sum derives a sum from a set of filters. | |
func (a *matchSumDAO) Sum(filters []*apb.MatchFilters) (*apb.MatchSum, error) { | |
// Create aggregate sum | |
sum := (*apb.MatchSum)(nil) | |
var wg sync.WaitGroup | |
sums := make(chan *apb.MatchSum) | |
var fetchErr error | |
// Iterate over all filters | |
for _, filter := range filters { | |
wg.Add(1) | |
go func(filter *apb.MatchFilters) { | |
defer wg.Done() | |
s, err := a.Get(filter) | |
if err != nil { | |
fetchErr = err | |
return | |
} | |
if s == nil { | |
return | |
} | |
sums <- s | |
}(filter) | |
} | |
// Close when all are done | |
go func() { | |
wg.Wait() | |
close(sums) | |
}() | |
for s := range sums { | |
normalizeMatchSum(s) | |
if sum == nil { | |
sum = s | |
} else { | |
sum = addMatchSums(sum, s) | |
} | |
} | |
if fetchErr != nil { | |
return nil, fetchErr | |
} | |
// Return sum and error | |
return sum, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get(filters: MatchFilters): Future[Option[MatchSum]] = { | |
select | |
.where(_.championId eqs filters.championId) | |
.and(_.enemyId eqs filters.enemyId) | |
.and(_.patch eqs filters.patch) | |
.and(_.tier eqs filters.tier) | |
.and(_.region eqs filters.region.value) | |
.and(_.role eqs filters.role.value) | |
.consistencyLevel_=(ConsistencyLevel.ONE) | |
.one() | |
} | |
def sum(filters: Seq[MatchFilters]): Future[MatchSum] = { | |
val futures = Future.sequence(filters.map(getFromFilters(_))) | |
futures.map { (list) => | |
list.foldLeft(MatchSum()) { | |
case (acc, v) => v match { | |
case Some(x) => acc + x | |
case None => acc | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment