Skip to content

Commit

Permalink
re-extract tags and text
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesvangurp committed Dec 24, 2023
1 parent 87574c8 commit ddbdb4e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/com/tryformation/pgdocstore/DocStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class DocStore<T : Any>(
VALUES (?,?,?,?,?,to_tsvector(coalesce(?)))
""".trimIndent(), listOf(id, timestamp, timestamp, txt, tags, text)
)

}

suspend fun getById(id: String): T? {
Expand Down Expand Up @@ -410,5 +409,9 @@ class DocStore<T : Any>(
}
}
}

suspend fun reExtract() {
bulkInsert(documentsByRecencyScrolling())
}
}

25 changes: 25 additions & 0 deletions src/test/kotlin/com/tryformation/pgdocstore/RetagTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tryformation.pgdocstore

import io.kotest.matchers.shouldBe
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.flow
import org.junit.jupiter.api.Test

class RetagTest: DbTestBase() {
@Test
fun `should retag everything with updated tag extractor`() = coRun {
val ds = DocStore(db, TaggedModel.serializer(), tableName)

flow {
repeat(200) {
emit(TaggedModel(title = "doc $it", tags = listOf("foo")))
}
}.let {
ds.bulkInsert(it)
}
ds.documentsByRecencyScrolling(tags = listOf("foo")).count() shouldBe 0
val ds2 = DocStore(db, TaggedModel.serializer(), tableName, tagExtractor = TaggedModel::tags)
ds2.reExtract()
ds2.documentsByRecencyScrolling(tags = listOf("foo")).count() shouldBe 200
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/com/tryformation/pgdocstore/TaggingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test
import java.util.UUID

@Serializable
data class TaggedModel(val title: String, val tags: List<String>, val id:String = UUID.randomUUID().toString())
data class TaggedModel(val title: String, val tags: List<String> = listOf(), val id:String = UUID.randomUUID().toString())

class TaggingTest : DbTestBase() {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package com.tryformation.pgdocstore.docs

import com.github.jasync.sql.db.ConnectionPoolConfiguration
import com.github.jasync.sql.db.asSuspending
import com.github.jasync.sql.db.interceptor.LoggingInterceptorSupplier
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
import com.jillesvangurp.kotlin4example.SourceRepository
import com.tryformation.pgdocstore.*
import kotlinx.coroutines.Dispatchers
import com.tryformation.pgdocstore.DocStore
import com.tryformation.pgdocstore.db
import com.tryformation.pgdocstore.reCreateDocStoreSchema
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.flow
import kotlinx.serialization.Serializable
import org.junit.jupiter.api.Test
import java.io.File
import java.util.*
import kotlin.random.Random
import kotlin.random.nextULong
import kotlin.time.Duration.Companion.milliseconds

const val githubLink = "https://github.com/formation-res/pg-docstore"
Expand Down Expand Up @@ -101,9 +99,6 @@ val readmeMd = sourceGitRepository.md {
store.getById(doc1.id)?.let {
println("Retrieved ${it.title}")
}
// delete it
store.delete(doc1)
println("now it's gone: ${store.getById(doc1.id)}")

// you can also do bulk inserts using flows or lists
flow {
Expand Down Expand Up @@ -147,6 +142,11 @@ val readmeMd = sourceGitRepository.md {
}"
)

// delete a document
store.delete(doc1)
println("now it's gone: ${store.getById(doc1.id)}")


store.create(MyModel("The quick brown fox"))
// or search on the extracted text
println(
Expand All @@ -155,15 +155,6 @@ val readmeMd = sourceGitRepository.md {
}"
)

// if you like sql, just use the connection
store.connection
.sendQuery("SELECT COUNT(*) as total FROM docs")
.let { res ->
res.rows.first().let { row ->
println("Count query total: ${row["total"]}")
}
}

// the whole point of dbs is transactions
store.transact { tStore ->
// everything you do with tStore is
Expand Down Expand Up @@ -211,8 +202,23 @@ val readmeMd = sourceGitRepository.md {
}
// prints null because the transaction was rolled back
println("after the rollback ${store.getById(another.id)?.title}")

// if you like sql, just use the connection
store.connection
.sendQuery("SELECT COUNT(*) as total FROM docs")
.let { res ->
res.rows.first().let { row ->
println("Count query total: ${row["total"]}")
}
}

}
}
+"""
This shows off most of the features. And more importantly, it shows how
simple interactions with the database are. Mostly, it's just simple idiomatic Kotlin; nice
little one liners.
""".trimIndent()

includeMdFile("outro.md")

Expand Down

0 comments on commit ddbdb4e

Please sign in to comment.