DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
InMemoryStorageSystem.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.Collections.Generic;
19 
20 namespace Deveel.Data.Store {
21  public sealed class InMemoryStorageSystem : IStoreSystem {
22  private Dictionary<string, InMemoryStore> nameStoreMap;
23 
24  public const int DefaultStoreSize = 1024;
25 
27  nameStoreMap = new Dictionary<string, InMemoryStore>();
28  }
29 
31  Dispose(false);
32  }
33 
34  public void Dispose() {
35  Dispose(true);
36  GC.SuppressFinalize(this);
37  }
38 
39  private void Dispose(bool disposing) {
40  if (disposing) {
41  if (nameStoreMap != null) {
42  lock (this) {
43  nameStoreMap.Clear();
44  }
45  }
46  }
47 
48  lock (this) {
49  nameStoreMap = null;
50  }
51  }
52 
53  public bool StoreExists(string name) {
54  lock (this) {
55  return nameStoreMap.ContainsKey(name);
56  }
57  }
58 
60  return CreateStore(name, DefaultStoreSize);
61  }
62 
63  public InMemoryStore CreateStore(string name) {
64  return CreateStore(name, DefaultStoreSize);
65  }
66 
67  public InMemoryStore CreateStore(string name, int hashSize) {
68  if (String.IsNullOrEmpty(name))
69  throw new ArgumentNullException("name");
70 
71  lock (this) {
72  if (StoreExists(name))
73  throw new ArgumentException(String.Format("The store {0} already exists.", name));
74 
75  var store = new InMemoryStore(name, hashSize);
76  nameStoreMap[name] = store;
77  return store;
78  }
79  }
80 
81  public InMemoryStore OpenStore(string name) {
82  lock (this) {
83  InMemoryStore store;
84  if (!nameStoreMap.TryGetValue(name, out store))
85  throw new InvalidOperationException(String.Format("Store {0} does not exist.", name));
86 
87  return store;
88  }
89  }
90 
91  IStore IStoreSystem.OpenStore(string name) {
92  return OpenStore(name);
93  }
94 
96  return CloseStore((InMemoryStore) store);
97  }
98 
99  public bool CloseStore(InMemoryStore store) {
100  if (!StoreExists(store.Name))
101  throw new InvalidOperationException(String.Format("Store {0} does not exist", store.Name));
102 
103  return true;
104  }
105 
107  return DeleteStore((InMemoryStore) store);
108  }
109 
110  public bool DeleteStore(InMemoryStore store) {
111  if (store == null)
112  throw new ArgumentNullException("store");
113 
114  lock (this) {
115  return nameStoreMap.Remove(store.Name);
116  }
117  }
118 
119  public void SetCheckPoint() {
120  // Check point logging not necessary with memory store
121  }
122 
123  public void Lock(string lockName) {
124  }
125 
126  public void Unlock(string lockName) {
127  }
128  }
129 }
string Name
Gets the unique name of the store within the application.
IStore CreateStore(String name)
Creates and returns a new persistent Store object given the unique name of the store.
An object that creates and manages the IStore objects that the database engine uses to represent itse...
Definition: IStoreSystem.cs:31
An implementation of IStore that persists data in the application memory.
bool CloseStore(IStore store)
Closes a store that has been either created or opened with the CreateStore or OpenStore methods...
IStore OpenStore(String name)
Opens an existing persistent Store object in the system and returns the IStore object that contains i...
void SetCheckPoint()
Sets a new check point at the current state of this store system.
bool DeleteStore(IStore store)
Permanently deletes a store from the system - use with care!
Dictionary< string, InMemoryStore > nameStoreMap
InMemoryStore CreateStore(string name, int hashSize)
A store is a resource where areas can be allocated and freed to store information (a memory allocator...
Definition: IStore.cs:56