GroupStateStore.cpp 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. state = cache.set(id, GroupState::defaultState(id.deviceType));
  10. }
  11. return state;
  12. }
  13. GroupState* GroupStateStore::set(const GroupId &id, const GroupState& state) {
  14. *(get(id)) = state;
  15. }
  16. void GroupStateStore::trackEviction() {
  17. if (cache.isFull()) {
  18. evictedIds.add(cache.getLru());
  19. }
  20. }
  21. void GroupStateStore::flush() {
  22. ListNode<GroupCacheNode*>* curr = cache.getHead();
  23. while (curr != NULL && curr->data->state.isDirty()) {
  24. persistence.set(curr->data->id, curr->data->state);
  25. curr->data->state.clearDirty();
  26. curr = curr->next;
  27. }
  28. while (evictedIds.size() > 0) {
  29. persistence.clear(evictedIds.shift());
  30. }
  31. }