Initial naive pubsub impl generalising from current setup

This commit is contained in:
eikek
2021-11-02 00:41:16 +01:00
parent ae30a3890e
commit d483d9f176
18 changed files with 864 additions and 2 deletions

View File

@ -0,0 +1,8 @@
CREATE TABLE "pubsub" (
"id" varchar(254) not null primary key,
"node_id" varchar(254) not null,
"url" varchar(254) not null,
"topic" varchar(254) not null,
"counter" int not null,
unique("url", "topic")
)

View File

@ -0,0 +1,8 @@
CREATE TABLE `pubsub` (
`id` varchar(254) not null primary key,
`node_id` varchar(254) not null,
`url` varchar(254) not null,
`topic` varchar(254) not null,
`counter` int not null,
unique(`url`, `topic`)
)

View File

@ -0,0 +1,8 @@
CREATE TABLE "pubsub" (
"id" varchar(254) not null primary key,
"node_id" varchar(254) not null,
"url" varchar(254) not null,
"topic" varchar(254) not null,
"counter" int not null,
unique("url", "topic")
)

View File

@ -0,0 +1,86 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.store.records
import cats.data.NonEmptyList
import cats.implicits._
import docspell.common._
import docspell.store.qb.DSL._
import docspell.store.qb.{Column, DML, TableDef}
import doobie._
import doobie.implicits._
/** A table for supporting naive pubsub across nodes. */
final case class RPubSub(
id: Ident,
nodeId: Ident,
url: LenientUri,
topic: String,
counter: Int
)
object RPubSub {
final case class Table(alias: Option[String]) extends TableDef {
val tableName: String = "pubsub"
val id = Column[Ident]("id", this)
val nodeId = Column[Ident]("node_id", this)
val url = Column[LenientUri]("url", this)
val topic = Column[String]("topic", this)
val counter = Column[Int]("counter", this)
val all: NonEmptyList[Column[_]] =
NonEmptyList.of(id, nodeId, url, topic, counter)
}
def as(alias: String): Table =
Table(Some(alias))
val T: Table = Table(None)
def insert(r: RPubSub): ConnectionIO[Int] =
DML.insert(T, T.all, sql"${r.id}, ${r.nodeId}, ${r.url}, ${r.topic}, ${r.counter}")
/** Insert all topics with counter = 0 */
def initTopics(
nodeId: Ident,
url: LenientUri,
topics: NonEmptyList[String]
): ConnectionIO[Int] =
DML.delete(T, T.nodeId === nodeId) *>
topics.toList
.traverse(t =>
Ident
.randomId[ConnectionIO]
.flatMap(id => insert(RPubSub(id, nodeId, url, t, 0)))
)
.map(_.sum)
def increment(url: LenientUri, topics: NonEmptyList[String]): ConnectionIO[Int] =
DML.update(
T,
T.url === url && T.topic.in(topics),
DML.set(
T.counter.increment(1)
)
)
def decrement(url: LenientUri, topics: NonEmptyList[String]): ConnectionIO[Int] =
DML.update(
T,
T.url === url && T.topic.in(topics),
DML.set(
T.counter.decrement(1)
)
)
def findSubs(topic: String): ConnectionIO[List[LenientUri]] =
run(select(T.url), from(T), T.topic === topic && T.counter > 0)
.query[LenientUri]
.to[List]
}

View File

@ -3,7 +3,7 @@
<withJansi>true</withJansi>
<encoder>
<pattern>%highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
<pattern>%highlight(%-5level) [%t{10}] %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>