18 using System.Collections.Generic;
22 namespace Deveel.Data.Store {
45 public string Name {
get;
private set; }
50 if (fixedAreaBlock == null)
53 return fixedAreaBlock;
60 fixedAreaBlock = null;
67 GC.SuppressFinalize(
this);
72 return FixedAreaBlock;
74 return GetAreaBlock(pointer);
80 var hashPos = (int)(pointer % areaMap.Length);
82 var block = areaMap[hashPos];
85 while (block != null && block.Id != pointer) {
91 throw new IOException(
"Pointer " + pointer +
" is invalid.");
96 block.
Next = areaMap[hashPos];
97 areaMap[hashPos] = block;
106 if (size > Int32.MaxValue)
107 throw new IOException(
"'size' is too large.");
111 long id = uniqueIdKey;
118 int hashPos = (int)(
id % areaMap.Length);
121 element.Next = areaMap[hashPos];
122 areaMap[hashPos] = element;
132 var hashPos = (int)(
id % areaMap.Length);
137 while (block != null && block.
Id !=
id) {
144 throw new IOException(
"Area ID " +
id +
" is invalid.");
148 areaMap[hashPos] = block.
Next;
159 return GetBlock(
id).GetArea(readOnly);
175 public bool ClosedClean {
181 throw new NotImplementedException();
184 #region InMemoryBlock
191 block =
new byte[size];
194 public long Id {
get;
private set; }
199 return new InMemoryArea(Id, readOnly, block, 0, block.Length);
213 public InMemoryArea(
long id,
bool readOnly, byte[] data,
int offset,
int length) {
217 position = startPosition = offset;
218 endPosition = offset + length;
221 IsReadOnly = readOnly;
224 public long Id {
get;
private set; }
226 public bool IsReadOnly {
get;
private set; }
228 public long Position {
229 get {
return position; }
231 var actPosition = startPosition + value;
232 if (actPosition < 0 || actPosition >= endPosition)
233 throw new IOException(
"Moved position out of bounds.");
235 position = actPosition;
239 public int Capacity {
240 get {
return endPosition - startPosition; }
243 public int Length {
get;
private set; }
246 var newPos = position + diff;
247 if (newPos > endPosition)
248 throw new IOException(String.Format(
"Attempt to read out of bounds: from {0} to {1} (position {2} to {3})",
249 startPosition, endPosition, position, newPos));
251 var oldPos = position;
257 const int bufferSize = 2048;
258 byte[] buf =
new byte[bufferSize];
259 int toCopy =
System.Math.Min(size, bufferSize);
262 Read(buf, 0, toCopy);
263 destArea.
Write(buf, 0, toCopy);
265 toCopy =
System.Math.Min(size, bufferSize);
269 public int Read(byte[] buffer,
int offset,
int length) {
271 Array.Copy(data, (
int)CheckPositionBounds(length), buffer, offset, length);
273 Array.Copy(data, CheckPositionBounds(length), buffer, offset, length);
278 public void Write(byte[] buffer,
int offset,
int length) {
280 Array.Copy(buffer, offset, data, (
int) CheckPositionBounds(length), length);
282 Array.Copy(buffer, offset, data, CheckPositionBounds(length), length);
InMemoryBlock fixedAreaBlock
InMemoryBlock GetAreaBlock(long pointer)
void DeleteArea(long id)
Deletes an area that was previously allocated by the CreateArea method by the area id...
readonly int startPosition
void Write(byte[] buffer, int offset, int length)
An implementation of IStore that persists data in the application memory.
IArea CreateArea(long size)
Allocates a block of memory in the store of the specified size and returns an IArea object that can b...
InMemoryStore(string name, int hashSize)
void Dispose(bool disposing)
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...
int Read(byte[] buffer, int offset, int length)
Reads an array of bytes from the underlying IArea and advances the position by length ...
long CheckPositionBounds(int diff)
An interface for access the contents of an area of a store.
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...
InMemoryBlock GetBlock(long pointer)
IEnumerable< long > GetAllAreas()
Returns a complete list of pointers to all areas in the Store as long objects sorted from lowest poin...
InMemoryArea(long id, bool readOnly, byte[] data, int offset, int length)
IArea GetArea(bool readOnly)
void Write(byte[] buffer, int offset, int length)
InMemoryBlock(long id, int size)
A store is a resource where areas can be allocated and freed to store information (a memory allocator...
void CopyTo(IArea destArea, int size)
Copies the given amount of bytes from the current position of the this area to another one...