Skip to content

Commit bfbedb8

Browse files
committed
Load less data when getting versions. Getting OOM otherwise.
1 parent 269be05 commit bfbedb8

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/main/kotlin/no/nav/klage/document/api/DocumentController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import no.nav.klage.document.api.views.DocumentVersionView
77
import no.nav.klage.document.api.views.DocumentView
88
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
99
import no.nav.klage.document.domain.DocumentVersion
10+
import no.nav.klage.document.domain.ShortDocumentVersion
1011
import no.nav.klage.document.service.DocumentService
1112
import no.nav.klage.document.util.TokenUtil
1213
import no.nav.klage.document.util.getLogger
@@ -132,7 +133,7 @@ class DocumentController(
132133
modified = documentVersion.modified
133134
)
134135

135-
private fun mapToDocumentVersionView(documentVersion: DocumentVersion): DocumentVersionView =
136+
private fun mapToDocumentVersionView(documentVersion: ShortDocumentVersion): DocumentVersionView =
136137
DocumentVersionView(
137138
documentId = documentVersion.documentId,
138139
version = documentVersion.version,

src/main/kotlin/no/nav/klage/document/domain/DocumentVersion.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,16 @@ class DocumentVersion(
4343
return "DocumentVersion(documentId=$documentId, version=$version, json='$json', data=$data, created=$created, modified=$modified, authorNavIdent='$authorNavIdent')"
4444
}
4545

46-
4746
}
47+
48+
/**
49+
* Using this when we don't need the full DocumentVersion object, just a subset of the fields.
50+
* Otherwise, we would have to fetch the full object from the database and that's too much data.
51+
*/
52+
data class ShortDocumentVersion(
53+
val documentId: UUID,
54+
val version: Int,
55+
val authorNavIdent: String,
56+
val created: LocalDateTime,
57+
val modified: LocalDateTime,
58+
)

src/main/kotlin/no/nav/klage/document/repositories/DocumentVersionRepository.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ package no.nav.klage.document.repositories
22

33
import no.nav.klage.document.domain.DocumentVersion
44
import no.nav.klage.document.domain.DocumentVersionId
5+
import no.nav.klage.document.domain.ShortDocumentVersion
56
import org.springframework.data.jpa.repository.JpaRepository
7+
import org.springframework.data.jpa.repository.Query
68
import java.util.*
79

810
interface DocumentVersionRepository : JpaRepository<DocumentVersion, DocumentVersionId> {
911

1012
fun findByDocumentId(documentId: UUID): List<DocumentVersion>
1113

14+
@Query(
15+
"""
16+
SELECT new no.nav.klage.document.domain.ShortDocumentVersion(documentId, version, authorNavIdent, created, modified)
17+
FROM DocumentVersion
18+
WHERE documentId = :documentId
19+
ORDER BY version
20+
"""
21+
)
22+
fun findVersionsByDocumentId(documentId: UUID): List<ShortDocumentVersion>
23+
1224
fun deleteByDocumentId(documentId: UUID)
1325
}

src/main/kotlin/no/nav/klage/document/service/DocumentService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package no.nav.klage.document.service
33
import no.nav.klage.document.domain.Document
44
import no.nav.klage.document.domain.DocumentVersion
55
import no.nav.klage.document.domain.DocumentVersionId
6+
import no.nav.klage.document.domain.ShortDocumentVersion
67
import no.nav.klage.document.repositories.CommentRepository
78
import no.nav.klage.document.repositories.DocumentRepository
89
import no.nav.klage.document.repositories.DocumentVersionRepository
@@ -113,8 +114,8 @@ class DocumentService(
113114
documentRepository.deleteById(documentId)
114115
}
115116

116-
fun getDocumentVersions(documentId: UUID): List<DocumentVersion> {
117-
return documentVersionRepository.findByDocumentId(documentId = documentId).sortedBy { it.version }
117+
fun getDocumentVersions(documentId: UUID): List<ShortDocumentVersion> {
118+
return documentVersionRepository.findVersionsByDocumentId(documentId = documentId)
118119
}
119120

120121
}

0 commit comments

Comments
 (0)