Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert with retry implemented #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/scala/org/reactivecouchbase/rs/scaladsl/Bucket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.couchbase.client.java.repository.AsyncRepository
import com.couchbase.client.java.error.subdoc.PathNotFoundException
import com.couchbase.client.java.subdoc._
import com.couchbase.client.core.message.kv.subdoc.multi._
import com.couchbase.client.java.util.retry.RetryWhenFunction
import com.typesafe.config.Config
import org.reactivecouchbase.rs.scaladsl.TypeUtils.EnvCustomizer
import org.reactivecouchbase.rs.scaladsl.json.{JsonError, JsonFormat, JsonReads, JsonSuccess, JsonWrites}
Expand Down Expand Up @@ -535,6 +536,28 @@ class Bucket(config: BucketConfig, onStop: () => Unit) {
}
}

def upsertWithRetry[T](key: String, slug: T, settings: WriteSettings = defaultWriteSettings, cas: Option[Long] = None, retryWhen: RetryWhenFunction)(implicit ec: ExecutionContext, format: JsonFormat[T]): Future[T] = _futureBucket.flatMap { bucket =>
val doc = cas match {
case None => RawJsonDocument.create(
key,
settings.expiration.asCouchbaseExpiry,
format.writes(slug).utf8String
)
case Some(casValue) => RawJsonDocument.create(
key,
settings.expiration.asCouchbaseExpiry,
format.writes(slug).utf8String,
casValue
)
}
bucket.upsert(
doc,
settings.persistTo,
settings.replicateTo
).retryWhen(retryWhen)
.asFuture.map(doc => format.reads(ByteString(doc.content())).get)
}

def get[T](key: String)(implicit ec: ExecutionContext, reader: JsonReads[T]): Future[Option[T]] = {
_futureBucket.flatMap(b => b.get(RawJsonDocument.create(key)).asFuture)
.filter(_ != null)
Expand Down