Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ListStore.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 
7 namespace SiliconStudio.Core.IO
8 {
9  /// <summary>
10  /// A value store that will be incrementally saved on HDD.
11  /// Thread-safe and process-safe.
12  /// </summary>
13  /// <typeparam name="T">The type of elements in the store.</typeparam>
14  public class ListStore<T> : Store<T> where T : new()
15  {
16  protected readonly List<T> loadedIdMap = new List<T>();
17  protected readonly LinkedList<UnsavedEntry> unsavedIdMap = new LinkedList<UnsavedEntry>();
18 
19  private IEqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
20 
21  public ListStore(Stream stream) : base(stream)
22  {
23  }
24 
25  public T[] GetValues()
26  {
27  lock (lockObject)
28  {
29  var result = new T[loadedIdMap.Count + unsavedIdMap.Count];
30  int i;
31  for (i = 0; i < loadedIdMap.Count; ++i)
32  {
33  result[i] = loadedIdMap[i];
34  }
35  foreach (var item in unsavedIdMap)
36  {
37  result[i++] = item.Value;
38  }
39 
40  return result;
41  }
42  }
43 
44  protected override void AddUnsaved(T item, int currentTransaction)
45  {
46  unsavedIdMap.AddLast(new UnsavedEntry { Value = item, Transaction = currentTransaction });
47  }
48 
49  protected override void RemoveUnsaved(T item, int currentTransaction)
50  {
51  RemoveUnsaved(null, currentTransaction);
52  }
53 
54  protected override void RemoveUnsaved(IEnumerable<T> items, int currentTransaction)
55  {
56  var node = unsavedIdMap.First;
57 
58  while (node != null)
59  {
60  var next = node.Next;
61  var nodeTransaction = node.Value.Transaction;
62  if (nodeTransaction == currentTransaction)
63  unsavedIdMap.Remove(node);
64  else if (nodeTransaction > currentTransaction)
65  break; // No need to test further since transaction are ordered
66  node = next;
67  }
68  }
69 
70  protected override void AddLoaded(T item)
71  {
72  loadedIdMap.Add(item);
73  }
74 
75  protected override IEnumerable<T> GetPendingItems(int currentTransaction)
76  {
77  var transactionIds = new List<T>();
78 
79  foreach (var item in unsavedIdMap)
80  {
81  if (item.Transaction < currentTransaction)
82  continue;
83  if (item.Transaction > currentTransaction)
84  break; // No need to test further since transaction are ordered
85 
86  transactionIds.Add(item.Value);
87  }
88 
89  return transactionIds;
90  }
91 
92  protected class UnsavedEntry
93  {
94  public int Transaction;
95  public T Value;
96  }
97  }
98 }
override void AddLoaded(T item)
Adds a value that has already been saved in the store (saved state).
Definition: ListStore.cs:70
int Transaction
Definition: ListStore.cs:94
override void RemoveUnsaved(T item, int currentTransaction)
Removes a value that has not yet been saved (pending state).
Definition: ListStore.cs:49
T Value
Definition: ListStore.cs:95
override IEnumerable< T > GetPendingItems(int currentTransaction)
Gets the list of pending items for a given transaction index.
Definition: ListStore.cs:75
override void RemoveUnsaved(IEnumerable< T > items, int currentTransaction)
Removes values that have not yet been saved (pending state).
Definition: ListStore.cs:54
override void AddUnsaved(T item, int currentTransaction)
Adds a value that has not yet been saved in the store (pending state).
Definition: ListStore.cs:44