Skip to content

Commit 908d7a3

Browse files
author
hoangnh
committed
Add some method.
1 parent ac34a9e commit 908d7a3

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ see [CHANGES.md](CHANGES.md)
7474
+ prepare couchbase for testing
7575
```shell script
7676
docker run -d --name cb -p 8091-8094:8091-8094 -p 11210:11210 couchbase:5.0.1
77-
docker cp travis-cb-prepare.sh cb:/tmp
78-
docker exec -i cb /tmp/travis-cb-prepare.sh
77+
docker cp cb-test-prepare.sh cb:/tmp
78+
docker exec -i cb /tmp/cb-test-prepare.sh
7979
```
8080
or, if you have prepared before => only run `docker start cb`
8181

core/src/main/scala/com/sandinh/couchbase/access/WithCaoKey1.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.sandinh.couchbase.access
22

33
import com.couchbase.client.java.document.Document
4-
import com.couchbase.client.java.error.DocumentDoesNotExistException
4+
import com.couchbase.client.java.error.{CASMismatchException, DocumentDoesNotExistException}
55

66
import scala.concurrent.Future
77
import scala.concurrent.ExecutionContext.Implicits.global
@@ -38,12 +38,29 @@ private[access] trait WithCaoKey1[T, A, U, D <: Document[U]]
3838
final def getWithCAS(a: A): Future[(T, Long)] = self.getWithCAS(key(a))
3939

4040
final def set(a: A, t: T): Future[D] = self.set(key(a), t)
41+
4142
final def update(a: A, t: T, cas: Long = 0): Future[D] =
4243
self.update(key(a), t, cas)
4344

4445
/** @deprecated use `update` instead */
45-
final def updateWithCAS(a: A, t: T, cas: Long = 0): Future[D] =
46-
self.update(key(a), t, cas)
46+
final def updateWithCAS(a: A, t: T, cas: Long = 0): Future[D] = {
47+
cas match {
48+
case -1 => set(a, t)
49+
case _ => self.update(key(a), t, cas)
50+
}
51+
}
52+
53+
final def updateWithCASNew(a: A, t: T): Future[T] = {
54+
getWithCAS(a).flatMap { case (_, cas) =>
55+
updateWithCAS(a, t, cas).map { _ => t }
56+
}.recoverWith { case _: CASMismatchException => updateWithCASNew(a, t) }
57+
}
58+
59+
final def getOrSetDefaultWithCAS(a: A)(default: T): Future[(T, Long)] = {
60+
getWithCAS(a) recoverWith {
61+
case _: DocumentDoesNotExistException => set(a, default).map(d => (default, d.cas()))
62+
}
63+
}
4764

4865
final def remove(a: A): Future[D] = self.remove(key(a))
4966
}

core/src/main/scala/com/sandinh/couchbase/access/WithCaoKey2.scala

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.sandinh.couchbase.access
22

33
import com.couchbase.client.java.document.Document
4-
import com.couchbase.client.java.error.DocumentDoesNotExistException
4+
import com.couchbase.client.java.error.{CASMismatchException, DocumentDoesNotExistException}
5+
56
import scala.concurrent.Future
67
import scala.concurrent.ExecutionContext.Implicits.global
78

@@ -25,8 +26,27 @@ private[access] trait WithCaoKey2[T, A, B, U, D <: Document[U]] {
2526
self.getOrElseWithCAS(key(a, b))(default)
2627

2728
final def set(a: A, b: B, t: T): Future[D] = self.set(key(a, b), t)
29+
2830
final def updateWithCAS(a: A, b: B, t: T, cas: Long = 0): Future[D] =
29-
self.update(key(a, b), t, cas)
31+
cas match {
32+
case -1 => set(key(a, b), t)
33+
case _ => self.update(key(a, b), t, cas)
34+
}
35+
36+
final def update(a: A, b: B, t: T): Future[D] =
37+
self.update(key(a, b), t)
38+
39+
final def updateWithCASNew(a: A, b: B, t: T): Future[T] = {
40+
getWithCAS(key(a, b)).flatMap { case (_, cas) =>
41+
updateWithCAS(a, b, t, cas).map { _ => t }
42+
}.recoverWith { case _: CASMismatchException => updateWithCASNew(a, b, t) }
43+
}
44+
45+
final def getOrSetDefaultWithCAS(a: A, b: B)(default: T): Future[(T, Long)] = {
46+
getWithCAS(key(a, b)) recoverWith {
47+
case _: DocumentDoesNotExistException => set(key(a, b), default).map(d => (default, d.cas()))
48+
}
49+
}
3050

3151
/** convenient method. = set(..).map(_ => t) */
3252
final def setT(a: A, b: B, t: T): Future[T] =

0 commit comments

Comments
 (0)