GroupStateStore.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <GroupStateStore.h>
  2. GroupStateStore::GroupStateStore(const size_t maxSize)
  3. : cache(GroupStateCache(maxSize))
  4. { }
  5. GroupState& GroupStateStore::get(const GroupId& id) {
  6. GroupState* state = cache.get(id);
  7. if (state == NULL) {
  8. trackEviction();
  9. GroupState loadedState = GroupState::defaultState(id.deviceType);
  10. persistence.get(id, loadedState);
  11. state = cache.set(id, loadedState);
  12. }
  13. return *state;
  14. }
  15. GroupState& GroupStateStore::set(const GroupId &id, const GroupState& state) {
  16. GroupState& storedState = get(id);
  17. storedState = state;
  18. return storedState;
  19. }
  20. void GroupStateStore::trackEviction() {
  21. if (cache.isFull()) {
  22. evictedIds.add(cache.getLru());
  23. }
  24. }
  25. void GroupStateStore::flush() {
  26. ListNode<GroupCacheNode*>* curr = cache.getHead();
  27. while (curr != NULL && curr->data->state.isDirty()) {
  28. persistence.set(curr->data->id, curr->data->state);
  29. curr->data->state.clearDirty();
  30. curr = curr->next;
  31. }
  32. while (evictedIds.size() > 0) {
  33. persistence.clear(evictedIds.shift());
  34. }
  35. }