/*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
package gr.grnet.aquarium.store.memory
-import gr.grnet.aquarium.user.UserState
-import gr.grnet.aquarium.Configurable
import com.ckkloverdos.props.Props
import com.ckkloverdos.maybe.{NoVal, Just, Maybe}
import gr.grnet.aquarium.store._
import scala.collection.JavaConversions._
import java.util.Date
import collection.mutable.ConcurrentMap
-import gr.grnet.aquarium.logic.events.{WalletEntry, ResourceEvent, UserEvent, PolicyEntry}
import java.util.concurrent.ConcurrentHashMap
+import gr.grnet.aquarium.uid.ConcurrentVMLocalUIDGenerator
+import gr.grnet.aquarium.Configurable
+import gr.grnet.aquarium.event.model.{WalletEntry, PolicyEntry}
+import gr.grnet.aquarium.event.model.im.{StdIMEvent, IMEventModel}
+import org.bson.types.ObjectId
+import gr.grnet.aquarium.event.model.resource.{StdResourceEvent, ResourceEventModel}
+import gr.grnet.aquarium.computation.UserState
/**
* An implementation of various stores that persists data in memory.
class MemStore extends UserStateStore
with Configurable with PolicyStore
- with ResourceEventStore with UserEventStore
+ with ResourceEventStore with IMEventStore
with WalletEntryStore
with StoreProvider {
- private[this] var _userStates = List[UserState]()
- private var policies: List[PolicyEntry] = List()
+ override type IMEvent = MemIMEvent
+ override type ResourceEvent = MemResourceEvent
+
+ private[this] val idGen = new ConcurrentVMLocalUIDGenerator(1000)
+
+ private[this] var _userStates = List[UserState]()
+ private[this] var _policyEntries = List[PolicyEntry]()
+ private[this] var _resourceEvents = List[ResourceEvent]()
+
private[this] val walletEntriesById: ConcurrentMap[String, WalletEntry] = new ConcurrentHashMap[String, WalletEntry]()
- private val userEventById: ConcurrentMap[String, UserEvent] = new ConcurrentHashMap[String, UserEvent]()
- private[this] val resourceEventsById: ConcurrentMap[String, ResourceEvent] = new ConcurrentHashMap[String, ResourceEvent]()
+ private[this] val imEventById: ConcurrentMap[String, MemIMEvent] = new ConcurrentHashMap[String, MemIMEvent]()
def configure(props: Props) = {
}
override def toString = {
val map = Map(
"UserState" -> _userStates.size,
- "ResourceEvent" -> resourceEventsById.size,
- "UserEvent" -> userEventById.size,
- "PolicyEntry" -> policies.size,
+ "ResourceEvent" -> _resourceEvents.size,
+ "IMEvent" -> imEventById.size,
+ "PolicyEntry" -> _policyEntries.size,
"WalletEntry" -> walletEntriesById.size
)
def walletEntryStore = this
- def userEventStore = this
+ def imEventStore = this
def policyStore = this
//- StoreProvider
//+ UserStateStore
- def storeUserState(userState: UserState): Maybe[RecordID] = {
- _userStates = userState :: _userStates
- Just(RecordID(userState.userId))
+ def insertUserState(userState: UserState): UserState = {
+ _userStates = userState.copy(_id = new ObjectId()) :: _userStates
+ userState
}
- def findUserStateByUserId(userId: String) = {
- _userStates.find(_.userId == userId) match {
- case Some(userState) ⇒
- Just(userState)
- case None ⇒
- NoVal
+ def findUserStateByUserID(userID: String) = {
+ _userStates.find(_.userID == userID)
+ }
+
+ def findLatestUserStateByUserID(userID: String) = {
+ val goodOnes = _userStates.filter(_.userID == userID)
+
+ goodOnes.sortWith {
+ case (us1, us2) ⇒
+ us1.occurredMillis > us2.occurredMillis
+ } match {
+ case head :: _ ⇒
+ Some(head)
+ case _ ⇒
+ None
}
}
- def findLatestUserStateForEndOfBillingMonth(userId: String,
+ def findLatestUserStateForEndOfBillingMonth(userID: String,
yearOfBillingMonth: Int,
- billingMonth: Int): Maybe[UserState] = {
+ billingMonth: Int): Option[UserState] = {
val goodOnes = _userStates.filter { userState ⇒
- val f1 = userState.userId == userId
+ val f1 = userState.userID == userID
val f2 = userState.isFullBillingMonthState
val bm = userState.theFullBillingMonth
val f3 = (bm ne null) && {
goodOnes.sortWith {
case (us1, us2) ⇒
- us1.oldestSnapshotTime > us2.oldestSnapshotTime
+ us1.occurredMillis > us2.occurredMillis
} match {
case head :: _ ⇒
- Just(head)
+ Some(head)
case _ ⇒
- NoVal
+ None
}
}
def deleteUserState(userId: String) {
- _userStates.filterNot(_.userId == userId)
+ _userStates.filterNot(_.userID == userId)
}
//- UserStateStore
//- WalletEntryStore
//+ ResourceEventStore
- def storeResourceEvent(event: ResourceEvent) = {
- resourceEventsById(event.id) = event
- Just(RecordID(event.id))
+ def createResourceEventFromOther(event: ResourceEventModel): ResourceEvent = {
+ if(event.isInstanceOf[MemResourceEvent]) event.asInstanceOf[MemResourceEvent]
+ else {
+ import event._
+ new StdResourceEvent(
+ id,
+ occurredMillis,
+ receivedMillis,
+ userID,
+ clientID,
+ resource,
+ instanceID,
+ value,
+ eventVersion,
+ details
+ ): MemResourceEvent
+ }
+ }
+
+ override def clearResourceEvents() = {
+ _resourceEvents = Nil
+ }
+
+ def insertResourceEvent(event: ResourceEventModel) = {
+ val localEvent = createResourceEventFromOther(event)
+ _resourceEvents ::= localEvent
+ localEvent
}
def findResourceEventById(id: String) = {
- Maybe(resourceEventsById(id))
+ _resourceEvents.find(ev ⇒ ev.id == id)
}
def findResourceEventsByUserId(userId: String)
(sortWith: Option[(ResourceEvent, ResourceEvent) => Boolean]): List[ResourceEvent] = {
- val byUserId = resourceEventsById.valuesIterator.filter(_.userId == userId).toArray
+ val byUserId = _resourceEvents.filter(_.userID == userId).toArray
val sorted = sortWith match {
case Some(sorter) ⇒
byUserId.sortWith(sorter)
}
def findResourceEventsByUserIdAfterTimestamp(userId: String, timestamp: Long): List[ResourceEvent] = {
- resourceEventsById.valuesIterator.filter { ev ⇒
- ev.userId == userId &&
+ _resourceEvents.filter { ev ⇒
+ ev.userID == userId &&
(ev.occurredMillis > timestamp)
}.toList
}
def findResourceEventsForReceivedPeriod(userId: String,
startTimeMillis: Long,
stopTimeMillis: Long): List[ResourceEvent] = {
- resourceEventsById.valuesIterator.filter { ev ⇒
- ev.userId == userId &&
+ _resourceEvents.filter { ev ⇒
+ ev.userID == userId &&
ev.isReceivedWithinMillis(startTimeMillis, stopTimeMillis)
}.toList
}
- def countOutOfSyncEventsForBillingPeriod(userId: String, startMillis: Long, stopMillis: Long): Maybe[Long] = Maybe {
- resourceEventsById.valuesIterator.filter { case ev ⇒
+ def countOutOfSyncEventsForBillingPeriod(userId: String, startMillis: Long, stopMillis: Long): Long = {
+ _resourceEvents.filter { case ev ⇒
// out of sync events are those that were received in the billing month but occurred in previous (or next?)
// months
ev.isOutOfSyncForBillingPeriod(startMillis, stopMillis)
override def findAllRelevantResourceEventsForBillingPeriod(userId: String,
startMillis: Long,
stopMillis: Long): List[ResourceEvent] = {
- resourceEventsById.valuesIterator.filter { case ev ⇒
+ _resourceEvents.filter { case ev ⇒
ev.isOccurredOrReceivedWithinMillis(startMillis, stopMillis)
}.toList sortWith { case (ev1, ev2) ⇒ ev1.occurredMillis <= ev2.occurredMillis }
}
//- ResourceEventStore
- def storeUserEvent(event: UserEvent) = {userEventById += (event.id -> event); Just(RecordID(event.id))}
+ //+ IMEventStore
+ def createIMEventFromJson(json: String) = {
+ StdIMEvent.fromJsonString(json)
+ }
+
+ def createIMEventFromOther(event: IMEventModel) = {
+ StdIMEvent.fromOther(event)
+ }
- def findUserEventById(id: String) = Maybe{userEventById.getOrElse(id, null)}
+ def insertIMEvent(event: IMEventModel) = {
+ val localEvent = createIMEventFromOther(event)
+ imEventById += (event.id -> localEvent)
+ localEvent
+ }
- def findUserEventsByUserId(userId: String) = userEventById.valuesIterator.filter{v => v.userId == userId}.toList
+ def findIMEventById(id: String) = imEventById.get(id)
+ //- IMEventStore
- def loadPolicies(after: Long) =
- policies.filter(p => p.validFrom > after)
+ def loadPolicyEntriesAfter(after: Long) =
+ _policyEntries.filter(p => p.validFrom > after)
.sortWith((a,b) => a.validFrom < b.validFrom)
- def storePolicy(policy: PolicyEntry) = {policies = policy :: policies; Just(RecordID(policy.id))}
+ def storePolicyEntry(policy: PolicyEntry) = {_policyEntries = policy :: _policyEntries; Just(RecordID(policy.id))}
- def updatePolicy(policy: PolicyEntry) =
- policies = policies.foldLeft(List[PolicyEntry]()){
+ def updatePolicyEntry(policy: PolicyEntry) =
+ _policyEntries = _policyEntries.foldLeft(List[PolicyEntry]()){
(acc, p) =>
if (p.id == policy.id)
policy :: acc
p :: acc
}
- def findPolicy(id: String) = policies.find(p => p.id == id) match {
+ def findPolicyEntry(id: String) = _policyEntries.find(p => p.id == id) match {
case Some(x) => Just(x)
case None => NoVal
}
+}
+
+object MemStore {
+ final def isLocalIMEvent(event: IMEventModel) = event match {
+ case _: MemIMEvent ⇒ true
+ case _ ⇒ false
+ }
}
\ No newline at end of file