forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapterRepo.scala
More file actions
301 lines (250 loc) · 10.4 KB
/
ChapterRepo.scala
File metadata and controls
301 lines (250 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package lila.study
import scala.collection.immutable.SeqMap
import akka.stream.scaladsl.*
import chess.format.UciPath
import chess.format.pgn.Tags
import chess.FideId
import reactivemongo.akkastream.cursorProducer
import reactivemongo.api.bson.*
import lila.db.AsyncColl
import lila.db.dsl.{ *, given }
import lila.tree.{ Branch, Branches, Clock }
import Node.BsonFields as F
final class ChapterRepo(val coll: AsyncColl)(using Executor, akka.stream.Materializer):
import BSONHandlers.{ writeBranch, given }
val $sortOrder = $sort.asc("order")
val $sortOrderDesc = $sort.desc("order")
def byId(id: StudyChapterId): Fu[Option[Chapter]] = coll(_.byId[Chapter](id))
def studyIdOf(chapterId: StudyChapterId): Fu[Option[StudyId]] =
coll(_.primitiveOne[StudyId]($id(chapterId), "studyId"))
def deleteByStudy(s: Study): Funit = coll(_.delete.one($studyId(s.id))).void
def deleteByStudyIds(ids: List[StudyId]): Funit = ids.nonEmpty.so:
coll(_.delete.one($doc("studyId".$in(ids)))).void
// studyId is useful to ensure that the chapter belongs to the study
def byIdAndStudy(id: StudyChapterId, studyId: StudyId): Fu[Option[Chapter]] =
coll(_.one($id(id) ++ $studyId(studyId)))
def firstByStudy(studyId: StudyId): Fu[Option[Chapter]] =
coll(_.find($studyId(studyId)).sort($sortOrder).one[Chapter])
private[study] def lastByStudy(studyId: StudyId): Fu[Option[Chapter]] =
coll(_.find($studyId(studyId)).sort($sortOrderDesc).one[Chapter])
def existsByStudy(studyId: StudyId): Fu[Boolean] =
coll(_.exists($studyId(studyId)))
def orderedByStudySource(studyId: StudyId): Source[Chapter, ?] =
Source.futureSource:
coll.map:
_.find($studyId(studyId))
.sort($sortOrder)
.cursor[Chapter]()
.documentSource()
def byIdsSource(ids: Iterable[StudyChapterId]): Source[Chapter, ?] =
Source.futureSource:
coll.map:
_.find($inIds(ids))
.cursor[Chapter]()
.documentSource()
def byStudiesSource(studyIds: Seq[StudyId]): Source[Chapter, ?] =
Source.futureSource:
coll.map:
_.find($doc("studyId".$in(studyIds))).cursor[Chapter]().documentSource()
// loads all study chapters in memory!
def orderedByStudyLoadingAllInMemory(studyId: StudyId): Fu[List[Chapter]] =
coll:
_.find($studyId(studyId))
.sort($sortOrder)
.cursor[Chapter]()
.list(256)
def studyIdsByRelayFideId(fideId: chess.FideId): Fu[List[StudyId]] =
coll(_.distinctEasy[StudyId, List]("studyId", $doc("relay.fideIds" -> fideId)))
def fideIdsOf(studyIds: List[StudyId]): Fu[Set[FideId]] =
coll:
_.aggregateOne(): framework =>
import framework.*
Match($doc("studyId".$in(studyIds), "relay.fideIds".$exists(true))) -> List(
Project($doc("_id" -> false, "ids" -> "$relay.fideIds")),
UnwindField("ids"),
Group(BSONNull)("ids" -> AddFieldToSet("ids"))
)
.map:
_.flatMap(_.getAsOpt[Set[FideId]]("ids")).orZero
def sort(study: Study, ids: List[StudyChapterId]): Funit =
coll: c =>
ids
.mapWithIndex: (id, index) =>
c.updateField($studyId(study.id) ++ $id(id), "order", index + 1)
.parallelVoid
def nextOrderByStudy(studyId: StudyId): Fu[Chapter.Order] =
coll(_.primitiveOne[Chapter.Order]($studyId(studyId), $sort.desc("order"), "order")).dmap { ~_ + 1 }
def setConceal(chapterId: StudyChapterId, conceal: chess.Ply) =
coll(_.updateField($id(chapterId), "conceal", conceal)).void
def removeConceal(chapterId: StudyChapterId) =
coll(_.unsetField($id(chapterId), "conceal")).void
def setRelay(chapterId: StudyChapterId, relay: Chapter.Relay) =
coll(_.updateField($id(chapterId), "relay", relay)).void
def setRelayPath(chapterId: StudyChapterId, path: UciPath) =
coll(_.updateField($id(chapterId) ++ $doc("relay.lastMoveAt".$exists(true)), "relay.path", path)).void
def setTagsFor(chapter: Chapter) =
coll(_.updateField($id(chapter.id), "tags", chapter.tags)).void
def setShapes(shapes: lila.tree.Node.Shapes) =
setNodeValue(F.shapes, shapes.value.nonEmpty.option(shapes))
def setComments(comments: lila.tree.Node.Comments) =
setNodeValue(F.comments, comments.value.nonEmpty.option(comments))
def setGamebook(gamebook: lila.tree.Node.Gamebook) =
setNodeValue(F.gamebook, gamebook.nonEmpty.option(gamebook))
def setGlyphs(glyphs: chess.format.pgn.Glyphs) = setNodeValue(F.glyphs, glyphs.nonEmpty)
def setClockAndDenorm(
chapter: Chapter,
path: UciPath,
clock: Option[Clock],
denorm: Option[Chapter.BothClocks]
) =
val modifier = clock match
case None =>
$unset(pathToField(path, F.clock)) ++ denorm.fold($empty)(clocks => $set("denorm.clocks" -> clocks))
case Some(c) =>
val updateNode = $doc(pathToField(path, F.clock) -> c)
val updateDenorm = denorm.map(clocks => $doc("denorm.clocks" -> clocks))
$set(updateDenorm.foldLeft(updateNode)(_ ++ _))
coll:
_.update
.one($id(chapter.id) ++ $doc(path.toDbField.$exists(true)), modifier)
.void
def forceVariation(force: Boolean) = setNodeValue(F.forceVariation, force.option(true))
def setName(id: StudyChapterId, name: StudyChapterName) = coll(_.updateField($id(id), "name", name)).void
// insert node and its children
// and updates chapter denormalization
private[study] def addSubTree(
chapter: Chapter,
subTree: Branch,
parentPath: UciPath,
relay: Option[Chapter.Relay]
): Funit =
val set = $doc(subTreeToBsonElements(parentPath, subTree)) ++
$doc("denorm" -> chapter.denorm) ++
relay.flatMap(toBdoc).so(r => $doc("relay" -> r))
coll(_.update.one($id(chapter.id), $set(set))).void
private def subTreeToBsonElements(parentPath: UciPath, subTree: Branch): List[(String, Bdoc)] =
(parentPath.depth < Node.MAX_PLIES).so:
val path = parentPath + subTree.id
subTree.children.toList
.flatMap(subTreeToBsonElements(path, _))
.appended:
path.toDbField -> writeBranch(subTree)
// overrides all children sub-nodes in DB! Make the tree merge beforehand.
def setChildren(children: Branches)(chapter: Chapter, path: UciPath): Funit =
val set: Bdoc = $doc(childrenTreeToBsonElements(path, children))
coll(_.update.one($id(chapter.id), $set(set))).void
private def childrenTreeToBsonElements(
parentPath: UciPath,
children: Branches
): List[(String, Bdoc)] =
(parentPath.depth < Node.MAX_PLIES).so:
for
node <- children.toList
path = parentPath + node.id
elems <- childrenTreeToBsonElements(path, node.children).appended(path.toDbField -> writeBranch(node))
yield elems
private def setNodeValue[A: BSONWriter](
field: String,
value: Option[A]
)(chapter: Chapter, path: UciPath): Funit =
coll:
_.updateOrUnsetField(
$id(chapter.id) ++ $doc(path.toDbField.$exists(true)),
pathToField(path, field),
value
).void
private[study] def setNodeValues(
chapter: Chapter,
path: UciPath,
values: List[(String, Option[BSONValue])]
): Funit =
values.collect { case (field, Some(v)) =>
pathToField(path, field) -> v
} match
case Nil => funit
case sets =>
coll:
_.update
.one(
$id(chapter.id) ++ $doc(path.toDbField.$exists(true)),
$set($doc(sets))
)
.void
// root.path.subField
private def pathToField(path: UciPath, subField: String): String = s"${path.toDbField}.$subField"
private[study] def idNamesByStudyIds(
studyIds: Seq[StudyId],
nbChaptersPerStudy: Int
): Fu[Map[StudyId, Vector[Chapter.IdName]]] =
studyIds.nonEmpty.so:
coll:
_.find(
$doc("studyId".$in(studyIds)),
$doc("studyId" -> true, "_id" -> true, "name" -> true).some
)
.sort($sortOrder)
.cursor[Bdoc]()
.list(nbChaptersPerStudy * studyIds.size)
.map: docs =>
docs.foldLeft(Map.empty[StudyId, Vector[Chapter.IdName]]): (hash, doc) =>
doc.getAsOpt[StudyId]("studyId").fold(hash) { studyId =>
hash.get(studyId) match
case Some(chapters) if chapters.sizeIs >= nbChaptersPerStudy => hash
case maybe =>
val chapters = ~maybe
hash + (studyId -> chapterIdNameHandler.readOpt(doc).fold(chapters)(chapters :+ _))
}
def idNames(studyId: StudyId): Fu[List[Chapter.IdName]] =
coll:
_.find($studyId(studyId), $doc("_id" -> true, "name" -> true).some)
.sort($sortOrder)
.cursor[Chapter.IdName]()
.list(Study.maxChapters.value)
// ordered like studyIds, then tags with the chapter order field
def tagsByStudyIds(studyIds: Iterable[StudyId]): Fu[SeqMap[StudyId, SeqMap[StudyChapterId, Tags]]] =
studyIds.nonEmpty.so:
coll:
_.find($doc("studyId".$in(studyIds)), $doc("studyId" -> true, "tags" -> true).some)
.sort($sortOrder)
.cursor[Bdoc]()
.listAll()
.map: docs =>
for
doc <- docs
chapterId <- doc.getAsOpt[StudyChapterId]("_id")
studyId <- doc.getAsOpt[StudyId]("studyId")
tags <- doc.getAsOpt[Tags]("tags")
yield (studyId, chapterId, tags)
.map:
_.groupBy(_._1).view
.mapValues:
_.view
.map: (_, chapterId, tags) =>
chapterId -> tags
.to(SeqMap)
.toMap
.map: hash =>
studyIds.view
.map: id =>
id -> ~hash.get(id)
.to(SeqMap)
def startServerEval(chapter: Chapter) =
coll:
_.updateField(
$id(chapter.id),
"serverEval",
Chapter.ServerEval(
path = chapter.root.mainlinePath,
done = false
)
)
.void
def completeServerEval(chapter: Chapter) =
coll(_.updateField($id(chapter.id), "serverEval.done", true)).void
def countByStudyId(studyId: StudyId): Fu[Int] =
coll(_.countSel($studyId(studyId)))
def insert(s: Chapter): Funit = coll(_.insert.one(s.updateDenorm)).void
def update(c: Chapter): Funit = coll(_.update.one($id(c.id), c.updateDenorm)).void
def delete(id: StudyChapterId): Funit = coll(_.delete.one($id(id))).void
def delete(c: Chapter): Funit = delete(c.id)
def $studyId(id: StudyId) = $doc("studyId" -> id)