Skip to content

Commit e3b5630

Browse files
committed
fix sql stats in case multiples values with the same objectId are passed in input
1 parent 2958b17 commit e3b5630

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Currently available profiles (see the class `ch.derlin.bbdata.api.Profiles`):
6868
* `input`: will only register the "input" endpoint (`POST /objects/values`);
6969
* `output`: will only register the "output" endpoints (everything BUT the one above);
7070
* `noc`: short for "_No Cassandra_". It won't register endpoints needing a Cassandra connection (input and values);
71-
* `sqlstats`: use MySQL to store objects statistics, instead of Cassandra
71+
* `sqlstats`: use MySQL to store objects statistics, instead of Cassandra **BEWARE** this is waaaay slower !!
7272

7373
Profiles can be combined (when it makes sense).
7474

src/main/kotlin/ch/derlin/bbdata/common/stats/StatsLogic.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,22 @@ class SqlStatsLogic(private val statsRepository: SqlStatsRepository) : StatsLogi
9494
}
9595

9696
override fun updateAllStats(vs: List<NewValue>) {
97+
// get all unique objects whose objectIds are in vs
98+
val stats = vs.map { it.objectId!! }.toSet().map{ objectId ->
99+
objectId to statsRepository.findById(objectId).orElse(SqlStats(objectId = objectId))
100+
}.toMap()
101+
102+
// update each: this will also work when multiple values target the same object
103+
vs.forEach {
104+
stats[it.objectId]!!.updateWithNewValue(it)
105+
}
106+
97107
// use the bulk save option of MySQL repositories to speed up the process
98-
statsRepository.saveAll(vs.map { v ->
99-
val stats = statsRepository.findById(v.objectId!!).orElse(SqlStats(objectId = v.objectId))
100-
stats.updateWithNewValue(v)
101-
stats
102-
})
108+
statsRepository.saveAll(stats.values)
103109
}
104110

105111
override fun incrementReadCounter(objectId: Long) {
106-
val stats = statsRepository.findById(objectId).orElseGet{ SqlStats(objectId = objectId) }
112+
val stats = statsRepository.findById(objectId).orElse(SqlStats(objectId = objectId))
107113
stats.nReads += 1
108114
statsRepository.save(stats)
109115
}

0 commit comments

Comments
 (0)