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

Public Member Functions

 ScatteringFileStoreData (IFileSystem fileSystem, string basePath, string fileName, string fileExtention, int maxFileSlice)
 
void Dispose ()
 
bool Delete ()
 Deletes the data block. More...
 
void Open (bool readOnly)
 Opens the data block and make it ready to be accessed. More...
 
void Close ()
 Closes the block and make it unavailable. More...
 
int Read (long position, byte[] buffer, int offset, int length)
 Reads a given amount of data from the block, starting at the absolute position given and copying into the provided buffer. More...
 
void Write (long position, byte[] buffer, int offset, int length)
 Writes a given buffer into the block, starting at the absolute position given. More...
 
void Flush ()
 Flushes the data written in the temporary store of the block to the underlying medium. More...
 
void SetLength (long value)
 Sets the length of the data block. More...
 

Properties

string BasePath [get, private set]
 
string FileName [get, private set]
 
string FileExtention [get, private set]
 
int MaxFileSlice [get, private set]
 
IFileSystem FileSystem [get, private set]
 
int FileCount [get]
 
bool IsOpen [get, private set]
 
bool Exists [get]
 
long Length [get]
 
bool IsReadOnly [get, private set]
 
- Properties inherited from Deveel.Data.Store.IStoreData
bool Exists [get]
 Gets a value indicating whether the data block object exists. More...
 
long Length [get]
 Gets the current length of the data block. More...
 
bool IsReadOnly [get]
 Gets a value indicating whether the data block is in read-only mode. More...
 

Private Member Functions

 ~ScatteringFileStoreData ()
 
string SliceFileName (int i)
 
long DiscoverSize ()
 
void Dispose (bool disposing)
 

Private Attributes

readonly object objectLock = new object()
 
List< FileStoreDatafileSlices
 
long trueFileLength
 

Detailed Description

Definition at line 23 of file ScatteringFileStoreData.cs.

Constructor & Destructor Documentation

Deveel.Data.Store.ScatteringFileStoreData.ScatteringFileStoreData ( IFileSystem  fileSystem,
string  basePath,
string  fileName,
string  fileExtention,
int  maxFileSlice 
)
inline

Definition at line 28 of file ScatteringFileStoreData.cs.

28  {
29  if (fileSystem == null)
30  throw new ArgumentNullException("fileSystem");
31 
32  FileSystem = fileSystem;
33  MaxFileSlice = maxFileSlice;
34  FileExtention = fileExtention;
35  FileName = fileName;
36  BasePath = basePath;
37  fileSlices = new List<FileStoreData>();
38  }
Deveel.Data.Store.ScatteringFileStoreData.~ScatteringFileStoreData ( )
inlineprivate

Definition at line 40 of file ScatteringFileStoreData.cs.

Member Function Documentation

void Deveel.Data.Store.ScatteringFileStoreData.Close ( )
inline

Closes the block and make it unavailable.

When IDisposable.Dispose is invoked this method is also called to prevent any operation before disposal.

Implements Deveel.Data.Store.IStoreData.

Definition at line 198 of file ScatteringFileStoreData.cs.

198  {
199  lock (objectLock) {
200  foreach (var slice in fileSlices) {
201  slice.Close();
202  }
203  }
204  }
bool Deveel.Data.Store.ScatteringFileStoreData.Delete ( )
inline

Deletes the data block.

Returns
Returns true if the block was successfully deleted, or false otherwise.

Implements Deveel.Data.Store.IStoreData.

Definition at line 135 of file ScatteringFileStoreData.cs.

135  {
136  // The number of files
137  int countFiles = FileCount;
138  // Delete each file from back to front
139  for (int i = countFiles - 1; i >= 0; --i) {
140  string f = SliceFileName(i);
141  bool deleteSuccess = new FileStoreData(f).Delete();
142  if (!deleteSuccess)
143  return false;
144  }
145  return true;
146  }
long Deveel.Data.Store.ScatteringFileStoreData.DiscoverSize ( )
inlineprivate

Definition at line 84 of file ScatteringFileStoreData.cs.

84  {
85  long runningTotal = 0;
86 
87  lock (objectLock) {
88  // Does the file exist?
89  int i = 0;
90  string f = SliceFileName(i);
91  while (FileSystem.FileExists(f)) {
92  var fileLength = FileSystem.GetFileSize(f);
93 
94  runningTotal += fileLength;
95 
96  ++i;
97  f = SliceFileName(i);
98  }
99  }
100 
101  return runningTotal;
102  }
bool FileExists(string path)
long GetFileSize(string path)
void Deveel.Data.Store.ScatteringFileStoreData.Dispose ( bool  disposing)
inlineprivate

Definition at line 104 of file ScatteringFileStoreData.cs.

104  {
105  if (disposing) {
106  foreach (var slice in fileSlices) {
107  if (slice != null)
108  slice.Dispose();
109  }
110 
111  fileSlices.Clear();
112  fileSlices = null;
113  }
114  }
void Deveel.Data.Store.ScatteringFileStoreData.Dispose ( )
inline

Definition at line 116 of file ScatteringFileStoreData.cs.

116  {
117  Dispose(true);
118  GC.SuppressFinalize(this);
119  }
void Deveel.Data.Store.ScatteringFileStoreData.Flush ( )
inline

Flushes the data written in the temporary store of the block to the underlying medium.

Implements Deveel.Data.Store.IStoreData.

Definition at line 267 of file ScatteringFileStoreData.cs.

267  {
268  lock (objectLock) {
269  foreach (var slice in fileSlices) {
270  slice.Flush();
271  }
272  }
273  }
void Deveel.Data.Store.ScatteringFileStoreData.Open ( bool  readOnly)
inline

Opens the data block and make it ready to be accessed.

Parameters
readOnlyIndicates if the block must be open in read-only mode.

Implements Deveel.Data.Store.IStoreData.

Definition at line 148 of file ScatteringFileStoreData.cs.

148  {
149  lock (objectLock) {
150  // Does the file exist?
151  string f = SliceFileName(0);
152  bool openExisting = FileSystem.FileExists(f);
153 
154  // If the file already exceeds the threshold and there isn't a secondary
155  // file then we need to convert the file.
156  if (openExisting && f.Length > MaxFileSlice) {
157  string f2 = SliceFileName(1);
158  if (FileSystem.FileExists(f2))
159  throw new IOException("File length exceeds maximum slice size setting.");
160 
161  // We need to scatter the file.
162  if (readOnly)
163  throw new IOException("Unable to convert to a scattered store because Read-only.");
164  }
165 
166  // Setup the first file slice
167  var slice = new FileStoreData(f);
168  slice.Open(readOnly);
169 
170  fileSlices.Add(slice);
171  long runningLength = slice.Length;
172 
173  // If we are opening a store that exists already, there may be other
174  // slices we need to setup.
175  if (openExisting) {
176  int i = 1;
177  string slicePart = SliceFileName(i);
178  while (FileSystem.FileExists(slicePart)) {
179  // Create the new slice information for this part of the file.
180  slice = new FileStoreData(slicePart);
181  slice.Open(readOnly);
182 
183  fileSlices.Add(slice);
184  runningLength += slice.Length;
185 
186  ++i;
187  slicePart = SliceFileName(i);
188  }
189  }
190 
191  trueFileLength = runningLength;
192  IsOpen = true;
193  IsReadOnly = readOnly;
194  }
195 
196  }
bool FileExists(string path)
int Deveel.Data.Store.ScatteringFileStoreData.Read ( long  position,
byte[]  buffer,
int  offset,
int  length 
)
inline

Reads a given amount of data from the block, starting at the absolute position given and copying into the provided buffer.

Parameters
positionThe absolute position within the data block from where to start reading.
bufferThe destination buffer where the data read will be filled in.
offsetThe starting offset within the buffer where to start copying the data read.
lengthThe desired number of bytes to read from the data block.
Returns
Returns the actual number of bytes read from the data block or 0 if the end of the block was reached.

Implements Deveel.Data.Store.IStoreData.

Definition at line 206 of file ScatteringFileStoreData.cs.

206  {
207  // Reads the array (potentially across multiple slices).
208  int count = 0;
209  while (length > 0) {
210  int fileI = (int)(position / MaxFileSlice);
211  long fileP = (position % MaxFileSlice);
212  int fileLen = (int)System.Math.Min((long)length, MaxFileSlice - fileP);
213 
214  FileStoreData slice;
215  lock (objectLock) {
216  // Return if out of bounds.
217  if (fileI < 0 || fileI >= fileSlices.Count) {
218  // Error if not open
219  if (!IsOpen)
220  throw new IOException("Store not open.");
221 
222  return 0;
223  }
224  slice = fileSlices[fileI];
225  }
226 
227  int readCount =slice.Read(fileP, buffer, offset, fileLen);
228  if (readCount == 0)
229  break;
230 
231  position += readCount;
232  offset += readCount;
233  length -= readCount;
234  count += readCount;
235  }
236 
237  return count;
238  }
void Deveel.Data.Store.ScatteringFileStoreData.SetLength ( long  value)
inline

Sets the length of the data block.

Parameters
valueThe new size to set for the data block.

If the value is less than Length this method will shrink the data block and trim the exceeding contents. If the value is more than Length then it is responsibility of the implementation of this contract to increase the contents.

Implements Deveel.Data.Store.IStoreData.

Definition at line 275 of file ScatteringFileStoreData.cs.

275  {
276  lock (objectLock) {
277  // The size we need to grow the data area
278  long totalSizeToGrow = value - trueFileLength;
279  // Assert that we aren't shrinking the data area size.
280  if (totalSizeToGrow < 0) {
281  throw new IOException("Unable to make the data area size " +
282  "smaller for this type of store.");
283  }
284 
285  while (totalSizeToGrow > 0) {
286  // Grow the last slice by this size
287  int last = fileSlices.Count - 1;
288  var slice = fileSlices[last];
289  long oldSliceLength = slice.Length;
290  long toGrow = System.Math.Min(totalSizeToGrow, (MaxFileSlice - oldSliceLength));
291 
292  // Flush the buffer and set the length of the file
293  slice.SetLength(oldSliceLength + toGrow);
294  slice.Flush();
295 
296  totalSizeToGrow -= toGrow;
297  // Create a new empty slice if we need to extend the data area
298  if (totalSizeToGrow > 0) {
299  string sliceFile = SliceFileName(last + 1);
300 
301  slice = new FileStoreData(sliceFile);
302  slice.Open(false);
303 
304  fileSlices.Add(slice);
305  }
306  }
307  trueFileLength = value;
308  }
309  }
string Deveel.Data.Store.ScatteringFileStoreData.SliceFileName ( int  i)
inlineprivate

Definition at line 68 of file ScatteringFileStoreData.cs.

68  {
69  if (i == 0)
70  return FileSystem.CombinePath(BasePath, String.Format("{0}.{1}", FileName, FileExtention));
71 
72  var fn = new StringBuilder();
73  fn.Append(FileName);
74  fn.Append(".");
75  if (i < 10) {
76  fn.Append("00");
77  } else if (i < 100) {
78  fn.Append("0");
79  }
80  fn.Append(i);
81  return Path.Combine(BasePath, fn.ToString());
82  }
A long string in the system.
string CombinePath(string path1, string path2)
void Deveel.Data.Store.ScatteringFileStoreData.Write ( long  position,
byte[]  buffer,
int  offset,
int  length 
)
inline

Writes a given buffer into the block, starting at the absolute position given.

Parameters
positionThe absolute position within the data block from where to start writing.
bufferThe data to write into the block.
offsetThe starting offset within the buffer where to start writing data from.
lengthThe number of bytes to write into the data block.

Implements Deveel.Data.Store.IStoreData.

Definition at line 240 of file ScatteringFileStoreData.cs.

240  {
241  // Writes the array (potentially across multiple slices).
242  while (length > 0) {
243  var fileI = (int)(position / MaxFileSlice);
244  var fileP = (position % MaxFileSlice);
245  var fileLen = (int)System.Math.Min((long)length, MaxFileSlice - fileP);
246 
247  FileStoreData slice;
248  lock (objectLock) {
249  // Return if out of bounds.
250  if (fileI < 0 || fileI >= fileSlices.Count) {
251  if (!IsOpen)
252  throw new IOException("Store not open.");
253 
254  return;
255  }
256  slice = fileSlices[fileI];
257  }
258 
259  slice.Write(fileP, buffer, offset, fileLen);
260 
261  position += fileLen;
262  offset += fileLen;
263  length -= fileLen;
264  }
265  }

Member Data Documentation

List<FileStoreData> Deveel.Data.Store.ScatteringFileStoreData.fileSlices
private

Definition at line 25 of file ScatteringFileStoreData.cs.

readonly object Deveel.Data.Store.ScatteringFileStoreData.objectLock = new object()
private

Definition at line 24 of file ScatteringFileStoreData.cs.

long Deveel.Data.Store.ScatteringFileStoreData.trueFileLength
private

Definition at line 26 of file ScatteringFileStoreData.cs.

Property Documentation

string Deveel.Data.Store.ScatteringFileStoreData.BasePath
getprivate set

Definition at line 44 of file ScatteringFileStoreData.cs.

bool Deveel.Data.Store.ScatteringFileStoreData.Exists
get

Definition at line 121 of file ScatteringFileStoreData.cs.

int Deveel.Data.Store.ScatteringFileStoreData.FileCount
get

Definition at line 54 of file ScatteringFileStoreData.cs.

string Deveel.Data.Store.ScatteringFileStoreData.FileExtention
getprivate set

Definition at line 48 of file ScatteringFileStoreData.cs.

string Deveel.Data.Store.ScatteringFileStoreData.FileName
getprivate set

Definition at line 46 of file ScatteringFileStoreData.cs.

IFileSystem Deveel.Data.Store.ScatteringFileStoreData.FileSystem
getprivate set

Definition at line 52 of file ScatteringFileStoreData.cs.

bool Deveel.Data.Store.ScatteringFileStoreData.IsOpen
getprivate set

Definition at line 66 of file ScatteringFileStoreData.cs.

bool Deveel.Data.Store.ScatteringFileStoreData.IsReadOnly
getprivate set

Definition at line 133 of file ScatteringFileStoreData.cs.

long Deveel.Data.Store.ScatteringFileStoreData.Length
get

Definition at line 125 of file ScatteringFileStoreData.cs.

int Deveel.Data.Store.ScatteringFileStoreData.MaxFileSlice
getprivate set

Definition at line 50 of file ScatteringFileStoreData.cs.


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