import java.util.Date
import collection.mutable.ConcurrentMap
import java.util.concurrent.ConcurrentHashMap
-import gr.grnet.aquarium.user.UserState
-import gr.grnet.aquarium.simulation.uid.ConcurrentVMLocalUIDGenerator
-import gr.grnet.aquarium.{AquariumException, Configurable}
-import gr.grnet.aquarium.events.{IMEvent, WalletEntry, ResourceEvent, PolicyEntry}
+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.
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 _resourceEvents = List[ResourceEvent]()
private[this] val walletEntriesById: ConcurrentMap[String, WalletEntry] = new ConcurrentHashMap[String, WalletEntry]()
- private[this] val imEventById: ConcurrentMap[String, IMEvent] = new ConcurrentHashMap[String, IMEvent]()
+ private[this] val imEventById: ConcurrentMap[String, MemIMEvent] = new ConcurrentHashMap[String, MemIMEvent]()
def configure(props: Props) = {
}
//+ UserStateStore
- def storeUserState(userState: UserState): Maybe[RecordID] = {
- _userStates = userState.copy(id = idGen.nextUID()) :: _userStates
- Just(RecordID(_userStates.head._id))
+ 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)
}.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
//+ IMEventStore
- def storeUnparsed(json: String) = throw new AquariumException("Not implemented")
+ def createIMEventFromJson(json: String) = {
+ StdIMEvent.fromJsonString(json)
+ }
- def storeIMEvent(event: IMEvent) = {imEventById += (event.id -> event); RecordID(event.id)}
+ def createIMEventFromOther(event: IMEventModel) = {
+ StdIMEvent.fromOther(event)
+ }
- def findIMEventById(id: String) = Maybe{imEventById.getOrElse(id, null)}
+ def insertIMEvent(event: IMEventModel) = {
+ val localEvent = createIMEventFromOther(event)
+ imEventById += (event.id -> localEvent)
+ localEvent
+ }
- def findIMEventsByUserId(userId: String) = imEventById.valuesIterator.filter{v => v.userID == userId}.toList
+ def findIMEventById(id: String) = imEventById.get(id)
//- IMEventStore
def loadPolicyEntriesAfter(after: Long) =
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