DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DatabaseSystem.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 using System.Net;
20 
22 using Deveel.Data.Diagnostics;
23 using Deveel.Data.Services;
24 
25 namespace Deveel.Data {
26  public sealed class DatabaseSystem : ISystem {
27  private IDictionary<string, object> metadata;
28 
29  private IDictionary<string, IDatabase> databases;
30 
31  internal DatabaseSystem(ISystemContext context, IEnumerable<ModuleInfo> modules) {
32  Context = context;
33  Modules = modules;
34  CreateMetadata();
35  }
36 
38  Dispose(false);
39  }
40 
41  private void CreateMetadata() {
42  metadata = new Dictionary<string, object>();
43 
44 #if !PCL
45  metadata["[env]:os.platform"] = Environment.OSVersion.Platform;
46  metadata["[env]:os.version"] = Environment.OSVersion.VersionString;
47  metadata["[env]:os.sp"] = Environment.OSVersion.ServicePack;
48  metadata["[env]:runtime.version"] = Environment.Version.ToString();
49  metadata["[env]:machineName"] = Environment.MachineName;
50  metadata["[env]:hostName"] = Dns.GetHostName();
51 
52 #endif
53  metadata["[env]processors"] = Environment.ProcessorCount;
54 
55  foreach (var config in Configuration) {
56  metadata[String.Format("[config]:{0}", config.Key)] = config.Value;
57  }
58 
59  foreach (var module in Modules) {
60  metadata[String.Format("[module]:{0}", module.ModuleName)] = module.Version;
61  }
62  }
63 
64  public IDatabase GetDatabase(string databaseName) {
65  lock (this) {
66  if (databases == null)
67  return null;
68 
69  IDatabase database;
70  if (!databases.TryGetValue(databaseName, out database))
71  return null;
72 
73  return database;
74  }
75  }
76 
77  public IEnumerable<ModuleInfo> Modules { get; private set; }
78 
80  get { return null; }
81  }
82 
83  IEnumerable<KeyValuePair<string, object>> IEventSource.Metadata {
84  get { return metadata; }
85  }
86 
87  public void Dispose() {
88  Dispose(true);
89  GC.SuppressFinalize(this);
90  }
91 
92  private void Dispose(bool disposing) {
93  if (disposing) {
94  lock (this) {
95  if (databases != null) {
96  foreach (var database in databases.Values) {
97  if (database != null)
98  database.Dispose();
99  }
100 
101  databases.Clear();
102  }
103 
104  if (Context != null)
105  Context.Dispose();
106  }
107  }
108 
109  databases = null;
110  Context = null;
111  }
112 
114  get { return Context; }
115  }
116 
117  public ISystemContext Context { get; private set; }
118 
120  get { return Context.Configuration; }
121  }
122 
123  public IEnumerable<string> GetDatabases() {
124  lock (this) {
125  if (databases == null)
126  return new string[0];
127 
128  return databases.Keys;
129  }
130  }
131 
132  public IDatabase CreateDatabase(IConfiguration configuration, string adminUser, string adminPassword) {
133  lock (this) {
134  if (configuration == null)
135  throw new ArgumentNullException("configuration");
136 
137  var databaseName = configuration.GetString("database.name");
138  if (String.IsNullOrEmpty(databaseName))
139  throw new ArgumentException("The configuration must specify a database name.");
140 
141  if (DatabaseExists(databaseName))
142  throw new InvalidOperationException(String.Format("Database '{0}' already exists in the system.", databaseName));
143 
144  var dbContext = Context.CreateDatabaseContext(configuration);
145  var database = new Database(this, dbContext);
146 
147  if (database.Exists)
148  throw new InvalidOperationException(String.Format("The database '{0}' was already created.", databaseName));
149 
150  database.Create(adminUser, adminPassword);
151  database.Open();
152 
153  if (databases == null)
154  databases = new Dictionary<string, IDatabase>();
155 
156  databases[databaseName] = database;
157 
158  return database;
159  }
160  }
161 
162  public IDatabase OpenDatabase(IConfiguration configuration) {
163  lock (this) {
164  if (configuration == null)
165  throw new ArgumentNullException("configuration");
166 
167  var databaseName = configuration.GetString("database.name");
168  if (String.IsNullOrEmpty(databaseName))
169  throw new ArgumentException("The configuration must specify a database name.");
170 
171  IDatabase database;
172  if (databases == null ||
173  !databases.TryGetValue(databaseName, out database))
174  throw new InvalidOperationException(String.Format("Database '{0}' does not exist in the system.", databaseName));
175 
176  if (!database.IsOpen)
177  database.Open();
178 
179  return database;
180  }
181  }
182 
183  public bool DatabaseExists(string databaseName) {
184  lock (this) {
185  if (String.IsNullOrEmpty(databaseName))
186  throw new ArgumentNullException("databaseName");
187 
188  if (databases == null)
189  return false;
190 
191  return databases.ContainsKey(databaseName);
192  }
193  }
194 
195  public bool DeleteDatabase(string databaseName) {
196  lock (this) {
197  if (String.IsNullOrEmpty(databaseName))
198  throw new ArgumentNullException("databaseName");
199 
200  if (databases == null)
201  return false;
202 
203  IDatabase database;
204  if (!databases.TryGetValue(databaseName, out database))
205  return false;
206 
207  if (!database.Exists)
208  return false;
209 
210  // TODO: Implement the delete function in the IDatabase
211 
212  return databases.Remove(databaseName);
213  }
214  }
215 
216  internal void RemoveDatabase(Database database) {
217  lock (this) {
218 
219  databases.Remove(database.Name);
220  }
221  }
222  }
223 }
IDatabase OpenDatabase(IConfiguration configuration)
The execution context of a database system, that is defining the configurations and the components us...
IEnumerable< string > GetDatabases()
virtual void Dispose(bool disposing)
Definition: Context.cs:63
bool DeleteDatabase(string databaseName)
DatabaseSystem(ISystemContext context, IEnumerable< ModuleInfo > modules)
The default implementation of a database in a system.
Definition: Database.cs:38
The representation of a single database in the system.
Definition: IDatabase.cs:40
IEventSource ParentSource
Gets an optional parent source.
Definition: IEventSource.cs:49
IEnumerable< KeyValuePair< string, object > > Metadata
Gets the list of metadata associated to the source.
Definition: IEventSource.cs:57
IDictionary< string, object > metadata
void RemoveDatabase(Database database)
Defines the contract for the configuration node of a component within the system or of the system its...
IDictionary< string, IDatabase > databases
void Open()
Opens the database making it ready to be accessed.
void Dispose(bool disposing)
IDatabase CreateDatabase(IConfiguration configuration, string adminUser, string adminPassword)
string Name
Gets the database name, as configured in the parent context.
Definition: Database.cs:77
IDatabase GetDatabase(string databaseName)
Represents the origin of system events, providing a mechanism to fill the metadata before dispatching...
Definition: IEventSource.cs:40
bool DatabaseExists(string databaseName)