Skip to content

Commit

Permalink
Merge pull request #20 from singhala/develop
Browse files Browse the repository at this point in the history
add logic for replicating writes and reads to stores
sritchie committed Feb 8, 2013
2 parents 7295c67 + 621962a commit 853e476
Showing 2 changed files with 63 additions and 0 deletions.
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(_) }
}
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))
}

0 comments on commit 853e476

Please sign in to comment.