DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
FileStoreData.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.IO;
19 
20 namespace Deveel.Data.Store {
24  public sealed class FileStoreData : IStoreData {
25  private LocalFile file;
26  private readonly object objectLock = new object();
27 
28  public FileStoreData(string filePath) {
29  if (String.IsNullOrEmpty(filePath))
30  throw new ArgumentNullException("filePath");
31 
32  FilePath = filePath;
33  IsOpen = false;
34  }
35 
37  Dispose(false);
38  }
39 
40  public string FilePath { get; private set; }
41 
42  public bool IsOpen { get; private set; }
43 
44  public void Dispose() {
45  Dispose(true);
46  GC.SuppressFinalize(this);
47  }
48 
49  private void Dispose(bool disposing) {
50  if (disposing) {
51  if (IsOpen)
52  Close();
53 
54  if (file != null)
55  file.Dispose();
56  }
57  }
58 
59  public bool Exists {
60  get {
61  lock (objectLock) {
62  return File.Exists(FilePath);
63  }
64  }
65  }
66 
67  public long Length {
68  get { return IsOpen ? file.Length : new FileInfo(FilePath).Length; }
69  }
70 
71  public bool IsReadOnly { get; private set; }
72 
73  public bool Delete() {
74  if (IsOpen)
75  return false;
76 
77  try {
78  File.Delete(FilePath);
79  return !Exists;
80  } catch (Exception) {
81  return false;
82  }
83  }
84 
85  public void Open(bool readOnly) {
86  lock (objectLock) {
87  file = new LocalFile(FilePath, readOnly);
88  IsOpen = true;
89  IsReadOnly = readOnly;
90  }
91  }
92 
93  public void Close() {
94  lock (objectLock) {
95  try {
96  file.Close();
97  } finally {
98  IsOpen = false;
99  }
100  }
101  }
102 
103  public int Read(long position, byte[] buffer, int offset, int length) {
104  // Make sure we don't Read past the end
105  lock (objectLock) {
106  length = System.Math.Max(0, System.Math.Min(length, (int)(Length - position)));
107  int count = 0;
108  if (position < Length) {
109  file.Seek(position, SeekOrigin.Begin);
110  count = file.Read(buffer, offset, length);
111  }
112 
113  return count;
114  }
115  }
116 
117  public void Write(long position, byte[] buffer, int offset, int length) {
118  if (IsReadOnly)
119  throw new IOException();
120 
121  // Make sure we don't Write past the end
122  lock (objectLock) {
123  length = System.Math.Max(0, System.Math.Min(length, (int)(Length - position)));
124  if (position < Length) {
125  file.Seek(position, SeekOrigin.Begin);
126  file.Write(buffer, offset, length);
127  }
128  }
129  }
130 
131  public void Flush() {
132  try {
133  file.Flush(true);
134  } catch (IOException) {
135  // There isn't much we can do about this exception. By itself it
136  // doesn't indicate a terminating error so it's a good idea to ignore
137  // it. Should it be silently ignored?
138  }
139  }
140 
141  public void SetLength(long value) {
142  lock (objectLock) {
143  file.SetLength(value);
144  }
145  }
146  }
147 }
void Write(long position, byte[] buffer, int offset, int length)
Writes a given buffer into the block, starting at the absolute position given.
long Seek(long offset, SeekOrigin origin)
Definition: LocalFile.cs:67
void Flush(bool writeThrough)
Definition: LocalFile.cs:83
bool Delete()
Deletes the data block.
int Read(byte[] buffer, int offset, int length)
Definition: LocalFile.cs:75
void Flush()
Flushes the data written in the temporary store of the block to the underlying medium.
void Close()
Closes the block and make it unavailable.
void Write(byte[] buffer, int offset, int length)
Definition: LocalFile.cs:79
An interface for low level store data access methods.
Definition: IStoreData.cs:23
A data store that is backed by a file located at the path given.
void Open(bool readOnly)
Opens the data block and make it ready to be accessed.
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...
void SetLength(long value)
Sets the length of the data block.
void Dispose(bool disposing)
void SetLength(long value)
Definition: LocalFile.cs:71