-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from singhala/develop
add logic for replicating writes and reads to stores
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
storehaus-core/src/main/scala/com/twitter/storehaus/ReplicatedStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.twitter.storehaus | ||
|
||
import com.twitter.util.Future | ||
|
||
import Store.{selectFirstSuccessfulTrial => selectFirst} | ||
|
||
/** | ||
* Replicates writes to all stores, and takes the first successful read. | ||
*/ | ||
class ReplicatedStore[StoreType <: Store[StoreType, K, V], K, V](stores: Seq[StoreType]) | ||
extends Store[ReplicatedStore[StoreType, K, V], K, V] { | ||
override def get(k: K) = selectFirst(stores.map { _.get(k) }) | ||
override def multiGet(ks: Set[K]) = selectFirst(stores.map { _.multiGet(ks) }) | ||
override def update(k: K)(fn: Option[V] => Option[V]) = | ||
Future.collect(stores.map { _.update(k)(fn) }).map { new ReplicatedStore(_) } | ||
override def -(k: K) = | ||
Future.collect(stores.map { _ - k }).map { new ReplicatedStore(_) } | ||
override def +(pair: (K,V)) = | ||
Future.collect(stores.map { _ + pair }).map { new ReplicatedStore(_) } | ||
} |
27 changes: 27 additions & 0 deletions
27
storehaus-core/src/test/scala/com/twitter/storehaus/ReplicatedStoreProperties.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. You may obtain | ||
* a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.twitter.storehaus | ||
|
||
import org.scalacheck.Properties | ||
|
||
object ReplicatedStoreProperties extends Properties("ReplicatedStore") { | ||
import StoreProperties.storeTest | ||
|
||
property("ReplicatedStore test") = | ||
storeTest[ReplicatedStore[MapStore[String, Int], String, Int], String, Int](new ReplicatedStore( | ||
Stream.continually(new MapStore[String, Int]()).take(100).toSeq)) | ||
} |