File tree Expand file tree Collapse file tree
src/main/kotlin/ch/derlin/bbdata/common/stats Expand file tree Collapse file tree Original file line number Diff line number Diff 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
7373Profiles can be combined (when it makes sense).
7474
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments