DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SingleFileStore.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Diagnostics;
19 using System.IO;
20 
21 using Deveel.Data.Util;
22 
23 namespace Deveel.Data.Store {
24  [DebuggerDisplay("[{Id}] {Name}")]
25  public sealed class SingleFileStore : StoreBase {
27  private Stream dataStream;
28 
29  // TODO: use this counter to prevent disposing if referenced
30  private int lockCount;
31 
32  internal SingleFileStore(SingleFileStoreSystem system, string name, int id)
33  : base(system.IsReadOnly) {
34  this.system = system;
35  Name = name;
36  Id = id;
37  }
38 
39  public string Name { get; private set; }
40 
41  public int Id { get; private set; }
42 
43  public bool IsOpen { get; private set; }
44 
45  private object CheckPointLock {
46  get { return system.CheckPointLock; }
47  }
48 
49  protected override long DataAreaEndOffset {
50  get { return DataLength; }
51  }
52 
53  public long DataLength {
54  get {
55  return dataStream == null ? 0 : dataStream.Length;
56  }
57  }
58 
59  protected override void SetDataAreaSize(long length) {
60  lock (CheckPointLock) {
61  if (dataStream != null)
62  dataStream.SetLength(length);
63  }
64  }
65 
66  internal void Create(int bufferSize) {
67  lock (CheckPointLock) {
68  dataStream = new MemoryStream(bufferSize);
69  IsOpen = true;
70  }
71  }
72 
73  public override void Lock() {
74  lockCount++;
75  }
76 
77  public override void Unlock() {
78  lockCount--;
79  }
80 
81  //public override void CheckPoint() {
82  // throw new NotImplementedException();
83  //}
84 
85  protected override void OpenStore(bool readOnly) {
86  lock (CheckPointLock) {
87  dataStream = new MemoryStream();
88 
89  var inputStream = system.LoadStoreData(Id);
90  if (inputStream != null) {
91  inputStream.Seek(0, SeekOrigin.Begin);
92  inputStream.CopyTo(dataStream);
93  }
94 
95  IsOpen = true;
96  }
97  }
98 
99  protected override void CloseStore() {
100  lock (CheckPointLock) {
101  if (lockCount == 0)
102  IsOpen = false;
103  }
104  }
105 
106  protected override int Read(long offset, byte[] buffer, int index, int length) {
107  lock (CheckPointLock) {
108  if (dataStream == null || !IsOpen)
109  return 0;
110 
111  dataStream.Seek(offset, SeekOrigin.Begin);
112  return dataStream.Read(buffer, index, length);
113  }
114  }
115 
116  protected override void Write(long offset, byte[] buffer, int index, int length) {
117  lock (CheckPointLock) {
118  if (dataStream != null && IsOpen) {
119  dataStream.Seek(offset, SeekOrigin.Begin);
120  dataStream.Write(buffer, index, length);
121  }
122  }
123  }
124 
125  private bool disposed;
126 
127  protected override void Dispose(bool disposing) {
128  if (!disposed) {
129  base.Dispose(disposing);
130 
131  if (disposing) {
132  lock (CheckPointLock) {
133  if (dataStream != null)
134  dataStream.Dispose();
135  }
136  }
137 
138  dataStream = null;
139  system = null;
140  IsOpen = false;
141  disposed = true;
142  }
143  }
144 
145  internal void WriteTo(Stream stream) {
146  lock (CheckPointLock) {
147  if (dataStream != null) {
148  dataStream.Seek(0, SeekOrigin.Begin);
149  dataStream.CopyTo(stream);
150  }
151  }
152  }
153  }
154 }
override void Dispose(bool disposing)
override void Lock()
This method is called before the start of a sequence of Write commands between consistant states of s...
override void SetDataAreaSize(long length)
SingleFileStore(SingleFileStoreSystem system, string name, int id)
override void OpenStore(bool readOnly)
override void Write(long offset, byte[] buffer, int index, int length)
override int Read(long offset, byte[] buffer, int index, int length)
override void Unlock()
This method is called after the end of a sequence of Write commands between consistant states of some...