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

add logic for replicating writes and reads to stores #20

Merged
merged 6 commits into from
Feb 8, 2013
Merged
Show file tree
Hide file tree
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
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))
}