/*
- * 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 {
+ 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 imEventById: ConcurrentMap[String, MemIMEvent] = new ConcurrentHashMap[String, MemIMEvent]()
def configure(props: Props) = {
}
val map = Map(
"UserState" -> _userStates.size,
"ResourceEvent" -> _resourceEvents.size,
- "UserEvent" -> userEventById.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 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 storeResourceEvent(event: ResourceEvent) = {
- _resourceEvents ::= event
- Just(RecordID(event.id))
+ def insertResourceEvent(event: ResourceEventModel) = {
+ val localEvent = createResourceEventFromOther(event)
+ _resourceEvents ::= localEvent
+ localEvent
}
def findResourceEventById(id: String) = {
- _resourceEvents.find(ev ⇒ ev.id == id) match {
- case Some(ev) ⇒ Just(ev)
- case None ⇒ NoVal
- }
+ _resourceEvents.find(ev ⇒ ev.id == id)
}
def findResourceEventsByUserId(userId: String)
(sortWith: Option[(ResourceEvent, ResourceEvent) => Boolean]): List[ResourceEvent] = {
- val byUserId = _resourceEvents.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] = {
_resourceEvents.filter { ev ⇒
- ev.userId == userId &&
+ ev.userID == userId &&
(ev.occurredMillis > timestamp)
}.toList
}
startTimeMillis: Long,
stopTimeMillis: Long): List[ResourceEvent] = {
_resourceEvents.filter { ev ⇒
- ev.userId == userId &&
+ ev.userID == userId &&
ev.isReceivedWithinMillis(startTimeMillis, stopTimeMillis)
}.toList
}
- def countOutOfSyncEventsForBillingPeriod(userId: String, startMillis: Long, stopMillis: Long): Maybe[Long] = Maybe {
+ 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
}
//- 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 loadPolicyEntriesAfter(after: Long) =
_policyEntries.filter(p => p.validFrom > after)
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