18 using System.Collections.Generic;
39 private const int Magic = 0x0BAC8001;
43 throw new ArgumentNullException(
"store");
48 public IStore Store {
get;
private set; }
51 using (var reader =
new BinaryReader(Store.GetAreaInputStream(pointer), Encoding.Unicode)) {
54 int count = (int) reader.ReadInt64();
55 for (
int i = 0; i < count; ++i) {
56 long tableId = reader.ReadInt64();
57 string name = reader.ReadString();
65 using (var stream =
new MemoryStream()) {
66 using (var writer =
new BinaryWriter(stream, Encoding.Unicode)) {
68 int sz = list.Count();
69 writer.Write((
long) sz);
70 foreach (var state
in list) {
71 writer.Write((
long)state.TableId);
72 writer.Write(state.SourceName);
77 return stream.ToArray();
83 var bytes = SerializeResources(list);
85 var area = Store.CreateArea(bytes.Length);
87 area.Write(bytes, 0, bytes.Length);
96 var visTablesArea = Store.CreateArea(12);
97 var delTablesArea = Store.CreateArea(12);
98 visAreaPointer = visTablesArea.Id;
99 delAreaPointer = delTablesArea.Id;
102 visTablesArea.WriteInt4(1);
103 visTablesArea.WriteInt8(0);
104 visTablesArea.Flush();
105 delTablesArea.WriteInt4(1);
106 delTablesArea.WriteInt8(0);
107 delTablesArea.Flush();
110 var headerWriter = Store.CreateArea(32);
111 long headerP = headerWriter.Id;
112 headerWriter.WriteInt4(Magic);
113 headerWriter.WriteInt4(0);
114 headerWriter.WriteInt8(0);
115 headerWriter.WriteInt8(visAreaPointer);
116 headerWriter.WriteInt8(delAreaPointer);
117 headerWriter.Flush();
119 headerArea = Store.GetArea(headerP,
false);
124 visibleList =
new List<TableState>();
125 deleteList =
new List<TableState>();
132 public void Open(
long offset) {
134 headerArea = Store.GetArea(offset);
135 int magicValue = headerArea.ReadInt4();
136 if (magicValue != Magic)
137 throw new IOException(
"Magic value for state header area is incorrect.");
139 if (headerArea.ReadInt4() != 0)
140 throw new IOException(
"Unknown version for state header area.");
142 currentTableId = (int)headerArea.ReadInt8();
143 visAreaPointer = headerArea.ReadInt8();
144 delAreaPointer = headerArea.ReadInt8();
147 visibleList =
new List<TableState>();
148 deleteList =
new List<TableState>();
151 ReadStateResourceList(visibleList, visAreaPointer);
152 ReadStateResourceList(deleteList, delAreaPointer);
158 int curCounter = currentTableId;
166 headerArea.WriteInt8(currentTableId);
180 return visibleList.AsEnumerable();
186 return deleteList.AsEnumerable();
192 foreach (var resource
in visibleList) {
193 if (resource.TableId == tableId)
202 visibleList.Add(resource);
203 visListChange =
true;
209 deleteList.Add(resource);
210 delListChange =
true;
216 RemoveState(visibleList, name);
217 visListChange =
true;
223 RemoveState(deleteList, name);
224 delListChange =
true;
228 private static void RemoveState(IList<TableState> list, String name) {
230 for (
int i = 0; i < sz; ++i) {
232 if (name.Equals(state.SourceName)) {
237 throw new Exception(
"Couldn't find resource '" + name +
"' in list.");
242 bool changes =
false;
243 long newVisP = visAreaPointer;
244 long newDelP = delAreaPointer;
251 newVisP = WriteListToStore(visibleList);
252 visListChange =
false;
256 newDelP = WriteListToStore(deleteList);
257 delListChange =
false;
264 headerArea.WriteInt8(newVisP);
265 headerArea.WriteInt8(newDelP);
268 if (visAreaPointer != newVisP) {
269 Store.DeleteArea(visAreaPointer);
270 visAreaPointer = newVisP;
273 if (delAreaPointer != newDelP) {
274 Store.DeleteArea(delAreaPointer);
275 delAreaPointer = newDelP;
289 SourceName = sourceName;
292 public int TableId {
get;
private set; }
294 public string SourceName {
get;
private set; }
long WriteListToStore(IEnumerable< TableState > list)
void AddDeleteResource(TableState resource)
long Position
Returns or sets the current position of the pointer within the area.
TableState(int tableId, string sourceName)
void RemoveDeleteResource(string name)
IEnumerable< TableState > GetVisibleList()
bool ContainsVisibleResource(int tableId)
TableStateStore(IStore store)
List< TableState > deleteList
void AddVisibleResource(TableState resource)
void RemoveVisibleResource(string name)
An interface for access the contents of an area of a store.
IEnumerable< TableState > GetDeleteList()
static byte[] SerializeResources(IEnumerable< TableState > list)
List< TableState > visibleList
static void RemoveState(IList< TableState > list, String name)
A store is a resource where areas can be allocated and freed to store information (a memory allocator...
void ReadStateResourceList(IList< TableState > list, long pointer)