DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.TableStateStore Class Reference

Classes

class  TableState
 

Public Member Functions

 TableStateStore (IStore store)
 
long Create ()
 
void Open (long offset)
 
int NextTableId ()
 
IEnumerable< TableStateGetVisibleList ()
 
IEnumerable< TableStateGetDeleteList ()
 
bool ContainsVisibleResource (int tableId)
 
void AddVisibleResource (TableState resource)
 
void AddDeleteResource (TableState resource)
 
void RemoveVisibleResource (string name)
 
void RemoveDeleteResource (string name)
 
void Flush ()
 

Properties

IStore Store [get, private set]
 

Private Member Functions

void ReadStateResourceList (IList< TableState > list, long pointer)
 
long WriteListToStore (IEnumerable< TableState > list)
 

Static Private Member Functions

static byte[] SerializeResources (IEnumerable< TableState > list)
 
static void RemoveState (IList< TableState > list, String name)
 

Private Attributes

IArea headerArea
 
long delAreaPointer
 
List< TableStatedeleteList
 
bool delListChange
 
long visAreaPointer
 
List< TableStatevisibleList
 
bool visListChange
 
int currentTableId
 
const int Magic = 0x0BAC8001
 

Detailed Description

Definition at line 26 of file TableStateStore.cs.

Constructor & Destructor Documentation

Deveel.Data.TableStateStore.TableStateStore ( IStore  store)
inline

Definition at line 41 of file TableStateStore.cs.

41  {
42  if (store == null)
43  throw new ArgumentNullException("store");
44 
45  Store = store;
46  }

Member Function Documentation

void Deveel.Data.TableStateStore.AddDeleteResource ( TableState  resource)
inline

Definition at line 207 of file TableStateStore.cs.

207  {
208  lock (this) {
209  deleteList.Add(resource);
210  delListChange = true;
211  }
212  }
List< TableState > deleteList
void Deveel.Data.TableStateStore.AddVisibleResource ( TableState  resource)
inline

Definition at line 200 of file TableStateStore.cs.

200  {
201  lock (this) {
202  visibleList.Add(resource);
203  visListChange = true;
204  }
205  }
List< TableState > visibleList
bool Deveel.Data.TableStateStore.ContainsVisibleResource ( int  tableId)
inline

Definition at line 190 of file TableStateStore.cs.

190  {
191  lock (this) {
192  foreach (var resource in visibleList) {
193  if (resource.TableId == tableId)
194  return true;
195  }
196  return false;
197  }
198  }
List< TableState > visibleList
long Deveel.Data.TableStateStore.Create ( )
inline

Definition at line 93 of file TableStateStore.cs.

93  {
94  lock (this) {
95  // Allocate empty visible and deleted tables area
96  var visTablesArea = Store.CreateArea(12);
97  var delTablesArea = Store.CreateArea(12);
98  visAreaPointer = visTablesArea.Id;
99  delAreaPointer = delTablesArea.Id;
100 
101  // Write empty entries for both of these
102  visTablesArea.WriteInt4(1);
103  visTablesArea.WriteInt8(0);
104  visTablesArea.Flush();
105  delTablesArea.WriteInt4(1);
106  delTablesArea.WriteInt8(0);
107  delTablesArea.Flush();
108 
109  // Now allocate an empty state header
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();
118 
119  headerArea = Store.GetArea(headerP, false);
120 
121  // Reset currentTableId
122  currentTableId = 0;
123 
124  visibleList = new List<TableState>();
125  deleteList = new List<TableState>();
126 
127  // Return pointer to the header area
128  return headerP;
129  }
130  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
List< TableState > deleteList
List< TableState > visibleList
IArea GetArea(long id, bool readOnly)
Returns an object that allows for the contents of an area (represented by the id parameter) to be Re...
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31
void Deveel.Data.TableStateStore.Flush ( )
inline

Definition at line 240 of file TableStateStore.cs.

240  {
241  lock (this) {
242  bool changes = false;
243  long newVisP = visAreaPointer;
244  long newDelP = delAreaPointer;
245 
246  try {
247  Store.Lock();
248 
249  // If the lists changed, then Write new state areas to the store.
250  if (visListChange) {
251  newVisP = WriteListToStore(visibleList);
252  visListChange = false;
253  changes = true;
254  }
255  if (delListChange) {
256  newDelP = WriteListToStore(deleteList);
257  delListChange = false;
258  changes = true;
259  }
260 
261  // Commit the changes,
262  if (changes) {
263  headerArea.Position = 16;
264  headerArea.WriteInt8(newVisP);
265  headerArea.WriteInt8(newDelP);
266  headerArea.Flush();
267 
268  if (visAreaPointer != newVisP) {
270  visAreaPointer = newVisP;
271  }
272 
273  if (delAreaPointer != newDelP) {
275  delAreaPointer = newDelP;
276  }
277  }
278  } finally {
279  Store.Unlock();
280  }
281  }
282  }
long WriteListToStore(IEnumerable< TableState > list)
long Position
Returns or sets the current position of the pointer within the area.
Definition: IArea.cs:48
void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
void DeleteArea(long id)
Deletes an area that was previously allocated by the CreateArea method by the area id...
List< TableState > deleteList
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
List< TableState > visibleList
IEnumerable<TableState> Deveel.Data.TableStateStore.GetDeleteList ( )
inline

Definition at line 184 of file TableStateStore.cs.

184  {
185  lock (this) {
186  return deleteList.AsEnumerable();
187  }
188  }
List< TableState > deleteList
IEnumerable<TableState> Deveel.Data.TableStateStore.GetVisibleList ( )
inline

Definition at line 178 of file TableStateStore.cs.

178  {
179  lock (this) {
180  return visibleList.AsEnumerable();
181  }
182  }
List< TableState > visibleList
int Deveel.Data.TableStateStore.NextTableId ( )
inline

Definition at line 156 of file TableStateStore.cs.

156  {
157  lock (this) {
158  int curCounter = currentTableId;
159  ++currentTableId;
160 
161  try {
162  Store.Lock();
163 
164  // Update the state in the file
165  headerArea.Position = 8;
166  headerArea.WriteInt8(currentTableId);
167 
168  // Check out the change
169  headerArea.Flush();
170  } finally {
171  Store.Unlock();
172  }
173 
174  return curCounter;
175  }
176  }
long Position
Returns or sets the current position of the pointer within the area.
Definition: IArea.cs:48
void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...
void Deveel.Data.TableStateStore.Open ( long  offset)
inline

Definition at line 132 of file TableStateStore.cs.

132  {
133  lock (this) {
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.");
138 
139  if (headerArea.ReadInt4() != 0)
140  throw new IOException("Unknown version for state header area.");
141 
142  currentTableId = (int)headerArea.ReadInt8();
143  visAreaPointer = headerArea.ReadInt8();
144  delAreaPointer = headerArea.ReadInt8();
145 
146  // Setup the visible and delete list
147  visibleList = new List<TableState>();
148  deleteList = new List<TableState>();
149 
150  // Read the resource list for the visible and delete list.
153  }
154  }
List< TableState > deleteList
List< TableState > visibleList
IArea GetArea(long id, bool readOnly)
Returns an object that allows for the contents of an area (represented by the id parameter) to be Re...
void ReadStateResourceList(IList< TableState > list, long pointer)
void Deveel.Data.TableStateStore.ReadStateResourceList ( IList< TableState list,
long  pointer 
)
inlineprivate

Definition at line 50 of file TableStateStore.cs.

50  {
51  using (var reader = new BinaryReader(Store.GetAreaInputStream(pointer), Encoding.Unicode)) {
52  reader.ReadInt32(); // version
53 
54  int count = (int) reader.ReadInt64();
55  for (int i = 0; i < count; ++i) {
56  long tableId = reader.ReadInt64();
57  string name = reader.ReadString();
58 
59  list.Add(new TableState((int)tableId, name));
60  }
61  }
62  }
void Deveel.Data.TableStateStore.RemoveDeleteResource ( string  name)
inline

Definition at line 221 of file TableStateStore.cs.

221  {
222  lock (this) {
223  RemoveState(deleteList, name);
224  delListChange = true;
225  }
226  }
List< TableState > deleteList
static void RemoveState(IList< TableState > list, String name)
static void Deveel.Data.TableStateStore.RemoveState ( IList< TableState list,
String  name 
)
inlinestaticprivate

Definition at line 228 of file TableStateStore.cs.

228  {
229  int sz = list.Count;
230  for (int i = 0; i < sz; ++i) {
231  var state = list[i];
232  if (name.Equals(state.SourceName)) {
233  list.RemoveAt(i);
234  return;
235  }
236  }
237  throw new Exception("Couldn't find resource '" + name + "' in list.");
238  }
void Deveel.Data.TableStateStore.RemoveVisibleResource ( string  name)
inline

Definition at line 214 of file TableStateStore.cs.

214  {
215  lock (this) {
216  RemoveState(visibleList, name);
217  visListChange = true;
218  }
219  }
List< TableState > visibleList
static void RemoveState(IList< TableState > list, String name)
static byte [] Deveel.Data.TableStateStore.SerializeResources ( IEnumerable< TableState list)
inlinestaticprivate

Definition at line 64 of file TableStateStore.cs.

64  {
65  using (var stream = new MemoryStream()) {
66  using (var writer = new BinaryWriter(stream, Encoding.Unicode)) {
67  writer.Write(1); // version
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);
73  }
74 
75  writer.Flush();
76 
77  return stream.ToArray();
78  }
79  }
80  }
long Deveel.Data.TableStateStore.WriteListToStore ( IEnumerable< TableState list)
inlineprivate

Definition at line 82 of file TableStateStore.cs.

82  {
83  var bytes = SerializeResources(list);
84 
85  var area = Store.CreateArea(bytes.Length);
86  long listP = area.Id;
87  area.Write(bytes, 0, bytes.Length);
88  area.Flush();
89 
90  return listP;
91  }
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
static byte[] SerializeResources(IEnumerable< TableState > list)
long Id
Returns the unique identifier that represents this area.
Definition: IArea.cs:31

Member Data Documentation

int Deveel.Data.TableStateStore.currentTableId
private

Definition at line 37 of file TableStateStore.cs.

long Deveel.Data.TableStateStore.delAreaPointer
private

Definition at line 29 of file TableStateStore.cs.

List<TableState> Deveel.Data.TableStateStore.deleteList
private

Definition at line 30 of file TableStateStore.cs.

bool Deveel.Data.TableStateStore.delListChange
private

Definition at line 31 of file TableStateStore.cs.

IArea Deveel.Data.TableStateStore.headerArea
private

Definition at line 27 of file TableStateStore.cs.

const int Deveel.Data.TableStateStore.Magic = 0x0BAC8001
private

Definition at line 39 of file TableStateStore.cs.

long Deveel.Data.TableStateStore.visAreaPointer
private

Definition at line 33 of file TableStateStore.cs.

List<TableState> Deveel.Data.TableStateStore.visibleList
private

Definition at line 34 of file TableStateStore.cs.

bool Deveel.Data.TableStateStore.visListChange
private

Definition at line 35 of file TableStateStore.cs.

Property Documentation

IStore Deveel.Data.TableStateStore.Store
getprivate set

Definition at line 48 of file TableStateStore.cs.


The documentation for this class was generated from the following file: