DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Public Attributes | Package Functions | Properties | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Store.SingleFileStoreSystem Class Reference
Inheritance diagram for Deveel.Data.Store.SingleFileStoreSystem:
Deveel.Data.Store.IStoreSystem

Classes

class  StoreInfo
 

Public Member Functions

 SingleFileStoreSystem (IDatabaseContext context, IConfiguration configuration)
 
void Dispose ()
 
bool StoreExists (string name)
 
SingleFileStore CreateStore (string name)
 
SingleFileStore OpenStore (string name)
 
bool CloseStore (SingleFileStore store)
 
bool DeleteStore (SingleFileStore store)
 
void SetCheckPoint ()
 Sets a new check point at the current state of this store system. More...
 
void Lock (string lockName)
 
void Unlock (string lockName)
 
- Public Member Functions inherited from Deveel.Data.Store.IStoreSystem
bool StoreExists (String name)
 Returns true if the store with the given name exists within the system, or false otherwise. More...
 
IStore CreateStore (String name)
 Creates and returns a new persistent Store object given the unique name of the store. More...
 
IStore OpenStore (String name)
 Opens an existing persistent Store object in the system and returns the IStore object that contains its data. More...
 
void Lock (String lockName)
 Attempts to lock this store system exclusively so that no other process may access or change the persistent data in the store. More...
 
void Unlock (String lockName)
 Unlocks the exclusive access to the persistent store objects. More...
 

Public Attributes

const string DefaultFileExtension = "db"
 

Package Functions

Stream LoadStoreData (int id)
 

Properties

string FileName [get, set]
 
string TempFileName [get]
 
string LockFileName [get]
 
bool ReadOnly [get, set]
 
bool IsReadOnly [get, set]
 
IFileSystem FileSystem [get]
 
object CheckPointLock [get]
 

Private Member Functions

 ~SingleFileStoreSystem ()
 
void Configure (IConfiguration config)
 
void Dispose (bool disposing)
 
void OpenOrCreateFile ()
 
void LoadStores (IFile dataFile)
 
void LoadHeaders (BinaryReader reader)
 
IStore IStoreSystem. CreateStore (string name)
 
IStore IStoreSystem. OpenStore (string name)
 
bool IStoreSystem. CloseStore (IStore store)
 Closes a store that has been either created or opened with the CreateStore or OpenStore methods. More...
 
bool IStoreSystem. DeleteStore (IStore store)
 Permanently deletes a store from the system - use with care! More...
 
long GetDataStartOffset ()
 
void WriteStores (Stream stream)
 
long WriteStoreInfo (BinaryWriter writer, long offset, SingleFileStore store)
 
void WriteHeaders (Stream stream, long dataStartOffset)
 
int FindMaxStoreId ()
 
bool TryFindStore (string storeName, out SingleFileStore store)
 

Private Attributes

IDatabaseContext context
 
bool disposed
 
readonly object checkPointLock = new object()
 
IDictionary< int, SingleFileStorestores
 
IDictionary< string, int > nameIdMap
 
IDictionary< int, StoreInfostoreInfo
 
int storeId
 
const int Magic = 0xf09a671
 

Detailed Description

Definition at line 28 of file SingleFileStoreSystem.cs.

Constructor & Destructor Documentation

Deveel.Data.Store.SingleFileStoreSystem.SingleFileStoreSystem ( IDatabaseContext  context,
IConfiguration  configuration 
)
inline

Definition at line 45 of file SingleFileStoreSystem.cs.

45  {
46  if (context == null)
47  throw new ArgumentNullException("context");
48 
49  this.context = context;
50 
51  Configure(configuration);
52 
54  }
Deveel.Data.Store.SingleFileStoreSystem.~SingleFileStoreSystem ( )
inlineprivate

Definition at line 56 of file SingleFileStoreSystem.cs.

56  {
57  Dispose(false);
58  }

Member Function Documentation

bool IStoreSystem. Deveel.Data.Store.SingleFileStoreSystem.CloseStore ( IStore  store)
inlineprivate

Closes a store that has been either created or opened with the CreateStore or OpenStore methods.

Parameters
store
Returns
Returns true if the store was successfully closed.

Implements Deveel.Data.Store.IStoreSystem.

Definition at line 277 of file SingleFileStoreSystem.cs.

277  {
278  return CloseStore((SingleFileStore) store);
279  }
bool IStoreSystem. CloseStore(IStore store)
Closes a store that has been either created or opened with the CreateStore or OpenStore methods...
bool Deveel.Data.Store.SingleFileStoreSystem.CloseStore ( SingleFileStore  store)
inline

Definition at line 281 of file SingleFileStoreSystem.cs.

281  {
282  try {
283  SingleFileStore fileStore;
284  if (!TryFindStore(store.Name, out fileStore))
285  throw new IOException("The store was not found in this database.");
286 
287  fileStore.Close();
288  return true;
289  } catch (IOException) {
290  throw;
291  } catch (Exception ex) {
292  throw new IOException("Unable to close the store.", ex);
293  }
294  }
bool TryFindStore(string storeName, out SingleFileStore store)
void Deveel.Data.Store.SingleFileStoreSystem.Configure ( IConfiguration  config)
inlineprivate

Definition at line 60 of file SingleFileStoreSystem.cs.

60  {
61  var basePath = config.GetString("database.basePath");
62  var fileName = config.GetString("database.fileName");
63  var fullPath = config.GetString("database.fullPath");
64 
65  if (String.IsNullOrEmpty(basePath)) {
66  if (String.IsNullOrEmpty(fullPath)) {
67  FileName = fileName;
68  } else {
69  FileName = fullPath;
70  }
71  } else if (String.IsNullOrEmpty(fileName)) {
72  if (!String.IsNullOrEmpty(basePath)) {
73  fileName = String.Format("{0}.{1}", context.DatabaseName(), DefaultFileExtension);
74  FileName = FileSystem.CombinePath(basePath, fileName);
75  } else if (!String.IsNullOrEmpty(fullPath)) {
76  FileName = fullPath;
77  }
78  } else if (String.IsNullOrEmpty(fullPath)) {
79  FileName = FileSystem.CombinePath(basePath, fileName);
80  }
81 
82  if (String.IsNullOrEmpty(FileName))
83  throw new DatabaseConfigurationException("Could not configure the file name of the database.");
84  }
A long string in the system.
string CombinePath(string path1, string path2)
IStore IStoreSystem. Deveel.Data.Store.SingleFileStoreSystem.CreateStore ( string  name)
inlineprivate

Definition at line 237 of file SingleFileStoreSystem.cs.

237  {
238  return CreateStore(name);
239  }
IStore IStoreSystem. CreateStore(string name)
SingleFileStore Deveel.Data.Store.SingleFileStoreSystem.CreateStore ( string  name)
inline

Definition at line 241 of file SingleFileStoreSystem.cs.

241  {
242  lock (checkPointLock) {
243  SingleFileStore store;
244  if (TryFindStore(name, out store))
245  throw new IOException(string.Format("The store '{0}' already exists in this database.", name));
246 
247  if (nameIdMap == null)
248  nameIdMap = new Dictionary<string, int>();
249 
250  if (stores == null)
251  stores = new Dictionary<int, SingleFileStore>();
252 
253  var id = ++storeId;
254  store = new SingleFileStore(this, name, id);
255  store.Open();
256  stores[id] = store;
257  nameIdMap[name] = id;
258  return store;
259  }
260  }
bool TryFindStore(string storeName, out SingleFileStore store)
IDictionary< int, SingleFileStore > stores
bool IStoreSystem. Deveel.Data.Store.SingleFileStoreSystem.DeleteStore ( IStore  store)
inlineprivate

Permanently deletes a store from the system - use with care!

Parameters
store

Note that it is quite likely that a store may fail to delete in which case the delete operation should be re-tried after a short timeout.

Returns
Returns true if the store was successfully deleted and the resources associated with it were freed. Returns false if the store could not be deleted.

Implements Deveel.Data.Store.IStoreSystem.

Definition at line 296 of file SingleFileStoreSystem.cs.

296  {
297  return DeleteStore((SingleFileStore) store);
298  }
bool IStoreSystem. DeleteStore(IStore store)
Permanently deletes a store from the system - use with care!
bool Deveel.Data.Store.SingleFileStoreSystem.DeleteStore ( SingleFileStore  store)
inline

Definition at line 300 of file SingleFileStoreSystem.cs.

300  {
301  try {
302  if (stores == null || !stores.ContainsKey(store.Id))
303  return false;
304 
305  return stores.Remove(store.Id);
306  } catch (IOException) {
307  throw;
308  } catch (Exception ex) {
309  throw new IOException("Unable to delete the store.", ex);
310  } finally {
311  if (nameIdMap != null)
312  nameIdMap.Remove(store.Name);
313  }
314  }
IDictionary< int, SingleFileStore > stores
void Deveel.Data.Store.SingleFileStoreSystem.Dispose ( )
inline

Definition at line 98 of file SingleFileStoreSystem.cs.

98  {
99  Dispose(true);
100  GC.SuppressFinalize(this);
101  }
void Deveel.Data.Store.SingleFileStoreSystem.Dispose ( bool  disposing)
inlineprivate

Definition at line 103 of file SingleFileStoreSystem.cs.

103  {
104  if (disposing) {
105  if (stores != null) {
106  foreach (var store in stores.Values) {
107  if (store != null)
108  store.Dispose();
109  }
110 
111  stores.Clear();
112  }
113  }
114 
115  disposed = true;
116  storeInfo = null;
117  stores = null;
118  }
IDictionary< int, StoreInfo > storeInfo
IDictionary< int, SingleFileStore > stores
int Deveel.Data.Store.SingleFileStoreSystem.FindMaxStoreId ( )
inlineprivate

Definition at line 407 of file SingleFileStoreSystem.cs.

407  {
408  return stores == null ? 0 : stores.Max(x => x.Value.Id);
409  }
IDictionary< int, SingleFileStore > stores
long Deveel.Data.Store.SingleFileStoreSystem.GetDataStartOffset ( )
inlineprivate

Definition at line 344 of file SingleFileStoreSystem.cs.

344  {
345  long offset = 24;
346 
347  foreach (var store in stores.Values) {
348  var nameLength = Encoding.Unicode.GetByteCount(store.Name);
349 
350  offset += 4 + nameLength + 4 + 8 + 8;
351  }
352 
353  return offset;
354  }
IDictionary< int, SingleFileStore > stores
void Deveel.Data.Store.SingleFileStoreSystem.LoadHeaders ( BinaryReader  reader)
inlineprivate

Definition at line 169 of file SingleFileStoreSystem.cs.

169  {
170  var magic = reader.ReadInt32();
171 
172  if (magic != Magic)
173  throw new IOException("The magic number in the header is invalid.");
174 
175  var version = reader.ReadInt32();
176  var lastModified = reader.ReadInt64();
177  var storeCount = reader.ReadInt32();
178 
179  // the maximum number of the stores
180  storeId = reader.ReadInt32();
181 
182  storeInfo = new Dictionary<int, StoreInfo>(storeCount);
183 
184  for (int i = 0; i < storeCount; i++) {
185  var strLength = reader.ReadInt32();
186  var nameChars = reader.ReadChars(strLength);
187 
188  var name = new string(nameChars);
189  var id = reader.ReadInt32();
190  var offset = reader.ReadInt64();
191  var size = reader.ReadInt64();
192 
193  storeInfo[id] = new StoreInfo(name, id, offset, size);
194 
195  if (nameIdMap == null)
196  nameIdMap = new Dictionary<string, int>();
197 
198  nameIdMap[name] = id;
199  }
200  }
IDictionary< int, StoreInfo > storeInfo
Stream Deveel.Data.Store.SingleFileStoreSystem.LoadStoreData ( int  id)
inlinepackage

Definition at line 202 of file SingleFileStoreSystem.cs.

202  {
203  StoreInfo info;
204  if (storeInfo == null ||
205  !storeInfo.TryGetValue(id, out info))
206  return null;
207 
208  var size = info.Size;
209  var offset = info.Offset;
210 
211  using (var dataFile = FileSystem.OpenFile(FileName, true)) {
212  using (var stream = new FileStream(dataFile)) {
213  stream.Seek(offset, SeekOrigin.Begin);
214 
215  var buffer = new byte[size];
216 
217  var outputStream = new MemoryStream();
218 
219  // TODO: support larger portions...
220  stream.Read(buffer, 0, (int) size);
221 
222  outputStream.Write(buffer, 0, buffer.Length);
223 
224  if (outputStream.Length != size)
225  throw new IOException("Corruption when reading the store.");
226 
227  return outputStream;
228  }
229  }
230  }
IDictionary< int, StoreInfo > storeInfo
IFile OpenFile(string path, bool readOnly)
void Deveel.Data.Store.SingleFileStoreSystem.LoadStores ( IFile  dataFile)
inlineprivate

Definition at line 161 of file SingleFileStoreSystem.cs.

161  {
162  using (var fileStream = new FileStream(dataFile)) {
163  using (var reader = new BinaryReader(fileStream, Encoding.Unicode)) {
164  LoadHeaders(reader);
165  }
166  }
167  }
void Deveel.Data.Store.SingleFileStoreSystem.Lock ( string  lockName)
inline

Definition at line 426 of file SingleFileStoreSystem.cs.

426  {
427  SingleFileStore store;
428  if (!TryFindStore(lockName, out store))
429  return;
430 
431  store.Lock();
432  }
bool TryFindStore(string storeName, out SingleFileStore store)
void Deveel.Data.Store.SingleFileStoreSystem.OpenOrCreateFile ( )
inlineprivate

Definition at line 130 of file SingleFileStoreSystem.cs.

130  {
131  bool created = false;
132 
133  IFile dataFile;
134 
136  dataFile = FileSystem.OpenFile(FileName, ReadOnly);
137  } else if (ReadOnly) {
138  throw new InvalidOperationException(
139  string.Format("The file '{0}' does not exist and the store is configured to be read-only.", FileName));
140  } else {
141  dataFile = FileSystem.CreateFile(FileName);
142  created = true;
143  }
144 
145  try {
146  if (!created) {
147  LoadStores(dataFile);
148  } else {
149  using (var stream = new FileStream(dataFile)) {
150  WriteHeaders(stream, 24);
151 
152  stream.Flush();
153  }
154  }
155  } finally {
156  if (dataFile != null)
157  dataFile.Dispose();
158  }
159  }
bool FileExists(string path)
void WriteHeaders(Stream stream, long dataStartOffset)
IFile CreateFile(string path)
IFile OpenFile(string path, bool readOnly)
IStore IStoreSystem. Deveel.Data.Store.SingleFileStoreSystem.OpenStore ( string  name)
inlineprivate

Definition at line 262 of file SingleFileStoreSystem.cs.

262  {
263  return OpenStore(name);
264  }
IStore IStoreSystem. OpenStore(string name)
SingleFileStore Deveel.Data.Store.SingleFileStoreSystem.OpenStore ( string  name)
inline

Definition at line 266 of file SingleFileStoreSystem.cs.

266  {
267  lock (checkPointLock) {
268  SingleFileStore store;
269  if (!TryFindStore(name, out store))
270  throw new IOException(string.Format("The store '{0}' does not exist in this database.", name));
271 
272  store.Open();
273  return store;
274  }
275  }
bool TryFindStore(string storeName, out SingleFileStore store)
void Deveel.Data.Store.SingleFileStoreSystem.SetCheckPoint ( )
inline

Sets a new check point at the current state of this store system.

This is intended to help journalling check point and recovery systems. A check point is set whenever data is committed to the database. Some systems can be designed to be able to roll forward or backward to different check points. Each check point represents a stable state in the database life cycle.

A checkpoint based system greatly improves stability because if a crash occurs in an intermediate state the changes can simply be rolled back to the last stable state.

An implementation may choose not to implement check points in which case this would be a no-op.

Implements Deveel.Data.Store.IStoreSystem.

Definition at line 316 of file SingleFileStoreSystem.cs.

316  {
317  lock (checkPointLock) {
318  try {
321 
322  using (var dataFile = FileSystem.CreateFile(TempFileName)) {
323  using (var stream = new FileStream(dataFile)) {
324  var dataStartOffset = GetDataStartOffset();
325  WriteHeaders(stream, dataStartOffset);
326  WriteStores(stream);
327 
328  stream.Flush();
329  }
330  }
331 
334 
336  } catch (IOException) {
337  throw;
338  } catch (Exception ex) {
339  throw new IOException("An error occurred while saving data to database file.", ex);
340  }
341  }
342  }
bool DeleteFile(string path)
bool FileExists(string path)
void WriteHeaders(Stream stream, long dataStartOffset)
IFile CreateFile(string path)
bool RenameFile(string sourcePath, string destPath)
bool Deveel.Data.Store.SingleFileStoreSystem.StoreExists ( string  name)
inline

Definition at line 232 of file SingleFileStoreSystem.cs.

232  {
233  SingleFileStore store;
234  return TryFindStore(name, out store);
235  }
bool TryFindStore(string storeName, out SingleFileStore store)
bool Deveel.Data.Store.SingleFileStoreSystem.TryFindStore ( string  storeName,
out SingleFileStore  store 
)
inlineprivate

Definition at line 411 of file SingleFileStoreSystem.cs.

411  {
412  int id;
413  if (nameIdMap == null || !nameIdMap.TryGetValue(storeName, out id)) {
414  store = null;
415  return false;
416  }
417 
418  if (stores == null || !stores.TryGetValue(id, out store)) {
419  store = null;
420  return false;
421  }
422 
423  return true;
424  }
IDictionary< int, SingleFileStore > stores
void Deveel.Data.Store.SingleFileStoreSystem.Unlock ( string  lockName)
inline

Definition at line 434 of file SingleFileStoreSystem.cs.

434  {
435  SingleFileStore store;
436  if (!TryFindStore(lockName, out store))
437  return;
438 
439  store.Unlock();
440  }
bool TryFindStore(string storeName, out SingleFileStore store)
void Deveel.Data.Store.SingleFileStoreSystem.WriteHeaders ( Stream  stream,
long  dataStartOffset 
)
inlineprivate

Definition at line 384 of file SingleFileStoreSystem.cs.

384  {
385  var writer = new BinaryWriter(stream, Encoding.Unicode);
386  writer.Write(Magic);
387  writer.Write(1);
388  writer.Write(DateTime.UtcNow.Ticks);
389 
390  var storeCount = stores == null ? 0 : stores.Count;
391  var topId = FindMaxStoreId();
392 
393  writer.Write(storeCount);
394  writer.Write(topId);
395 
396  long offset = dataStartOffset;
397 
398  if (stores != null) {
399  storeInfo = new Dictionary<int, StoreInfo>(stores.Count);
400 
401  foreach (var store in stores.Values) {
402  offset = WriteStoreInfo(writer, offset, store);
403  }
404  }
405  }
IDictionary< int, StoreInfo > storeInfo
IDictionary< int, SingleFileStore > stores
long WriteStoreInfo(BinaryWriter writer, long offset, SingleFileStore store)
long Deveel.Data.Store.SingleFileStoreSystem.WriteStoreInfo ( BinaryWriter  writer,
long  offset,
SingleFileStore  store 
)
inlineprivate

Definition at line 364 of file SingleFileStoreSystem.cs.

364  {
365  var nameLength = store.Name.Length;
366  var name = store.Name;
367  var id = store.Id;
368  var size = store.DataLength;
369 
370  writer.Write(nameLength);
371  writer.Write(name.ToCharArray());
372 
373  writer.Write(id);
374  writer.Write(offset);
375  writer.Write(size);
376 
377  storeInfo[store.Id] = new StoreInfo(name, id, offset, size);
378 
379  offset += store.DataLength;
380 
381  return offset;
382  }
IDictionary< int, StoreInfo > storeInfo
void Deveel.Data.Store.SingleFileStoreSystem.WriteStores ( Stream  stream)
inlineprivate

Definition at line 356 of file SingleFileStoreSystem.cs.

356  {
357  if (stores != null) {
358  foreach (var store in stores.Values) {
359  store.WriteTo(stream);
360  }
361  }
362  }
IDictionary< int, SingleFileStore > stores

Member Data Documentation

readonly object Deveel.Data.Store.SingleFileStoreSystem.checkPointLock = new object()
private

Definition at line 33 of file SingleFileStoreSystem.cs.

IDatabaseContext Deveel.Data.Store.SingleFileStoreSystem.context
private

Definition at line 29 of file SingleFileStoreSystem.cs.

const string Deveel.Data.Store.SingleFileStoreSystem.DefaultFileExtension = "db"

Definition at line 41 of file SingleFileStoreSystem.cs.

bool Deveel.Data.Store.SingleFileStoreSystem.disposed
private

Definition at line 31 of file SingleFileStoreSystem.cs.

const int Deveel.Data.Store.SingleFileStoreSystem.Magic = 0xf09a671
private

Definition at line 43 of file SingleFileStoreSystem.cs.

IDictionary<string, int> Deveel.Data.Store.SingleFileStoreSystem.nameIdMap
private

Definition at line 36 of file SingleFileStoreSystem.cs.

int Deveel.Data.Store.SingleFileStoreSystem.storeId
private

Definition at line 39 of file SingleFileStoreSystem.cs.

IDictionary<int, StoreInfo> Deveel.Data.Store.SingleFileStoreSystem.storeInfo
private

Definition at line 37 of file SingleFileStoreSystem.cs.

IDictionary<int, SingleFileStore> Deveel.Data.Store.SingleFileStoreSystem.stores
private

Definition at line 35 of file SingleFileStoreSystem.cs.

Property Documentation

object Deveel.Data.Store.SingleFileStoreSystem.CheckPointLock
get

Definition at line 126 of file SingleFileStoreSystem.cs.

string Deveel.Data.Store.SingleFileStoreSystem.FileName
getset

Definition at line 86 of file SingleFileStoreSystem.cs.

IFileSystem Deveel.Data.Store.SingleFileStoreSystem.FileSystem
getprivate

Definition at line 122 of file SingleFileStoreSystem.cs.

bool Deveel.Data.Store.SingleFileStoreSystem.IsReadOnly
getset

Definition at line 120 of file SingleFileStoreSystem.cs.

string Deveel.Data.Store.SingleFileStoreSystem.LockFileName
getprivate

Definition at line 92 of file SingleFileStoreSystem.cs.

bool Deveel.Data.Store.SingleFileStoreSystem.ReadOnly
getset

Definition at line 96 of file SingleFileStoreSystem.cs.

string Deveel.Data.Store.SingleFileStoreSystem.TempFileName
getprivate

Definition at line 88 of file SingleFileStoreSystem.cs.


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