1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Data;
|
---|
27 | using System.Data.SQLite;
|
---|
28 | using System.Data.Common;
|
---|
29 | using System.Threading;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Grid {
|
---|
32 | class Database {
|
---|
33 | private string connectionString;
|
---|
34 | private ReaderWriterLockSlim rwLock;
|
---|
35 | public Database(string connectionString) {
|
---|
36 | this.connectionString = connectionString;
|
---|
37 | rwLock = new ReaderWriterLockSlim();
|
---|
38 | }
|
---|
39 |
|
---|
40 | #region create empty database
|
---|
41 | public void CreateNew() {
|
---|
42 | rwLock.EnterWriteLock();
|
---|
43 | try {
|
---|
44 | using(DbConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
45 | cnn.Open();
|
---|
46 | using(DbTransaction t = cnn.BeginTransaction()) {
|
---|
47 | using(DbCommand cmd = cnn.CreateCommand()) {
|
---|
48 | cmd.CommandText = "CREATE TABLE Job (ID integer primary key autoincrement, Guid Guid, Status text, CreationTime DateTime, StartTime DateTime, RawData blob)";
|
---|
49 | cmd.Transaction = t;
|
---|
50 | cmd.ExecuteNonQuery();
|
---|
51 | }
|
---|
52 | t.Commit();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | } finally {
|
---|
56 | rwLock.ExitWriteLock();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | internal void InsertJob(Guid guid, JobState jobState, byte[] rawData) {
|
---|
62 | rwLock.EnterWriteLock();
|
---|
63 | try {
|
---|
64 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
65 | cnn.Open();
|
---|
66 | using(DbTransaction t = cnn.BeginTransaction()) {
|
---|
67 | using(DbCommand c = cnn.CreateCommand()) {
|
---|
68 | c.Transaction = t;
|
---|
69 | c.CommandText = "Insert into Job (Guid, Status, CreationTime, StartTime, RawData) values (@Guid, @Status, @CreationTime, @StartTime, @RawData)";
|
---|
70 | DbParameter guidParam = c.CreateParameter();
|
---|
71 | guidParam.ParameterName = "@Guid";
|
---|
72 | guidParam.Value = guid;
|
---|
73 | c.Parameters.Add(guidParam);
|
---|
74 | DbParameter statusParam = c.CreateParameter();
|
---|
75 | statusParam.ParameterName = "@Status";
|
---|
76 | statusParam.Value = jobState.ToString();
|
---|
77 | c.Parameters.Add(statusParam);
|
---|
78 | DbParameter creationParam = c.CreateParameter();
|
---|
79 | creationParam.ParameterName = "@CreationTime";
|
---|
80 | DateTime now = DateTime.Now;
|
---|
81 | creationParam.Value = now;
|
---|
82 | c.Parameters.Add(creationParam);
|
---|
83 | DbParameter startParam = c.CreateParameter();
|
---|
84 | startParam.ParameterName = "@StartTime";
|
---|
85 | if(jobState == JobState.Busy) startParam.Value = now;
|
---|
86 | else startParam.Value = null;
|
---|
87 | c.Parameters.Add(startParam);
|
---|
88 | DbParameter rawDataParam = c.CreateParameter();
|
---|
89 | rawDataParam.ParameterName = "@RawData";
|
---|
90 | rawDataParam.Value = rawData;
|
---|
91 | c.Parameters.Add(rawDataParam);
|
---|
92 | c.ExecuteNonQuery();
|
---|
93 | }
|
---|
94 | t.Commit();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | } finally {
|
---|
98 | rwLock.ExitWriteLock();
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | internal List<JobEntry> GetWaitingJobs() {
|
---|
104 | rwLock.EnterReadLock();
|
---|
105 | List<JobEntry> jobs = new List<JobEntry>();
|
---|
106 | try {
|
---|
107 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
108 | cnn.Open();
|
---|
109 | SQLiteCommand c = cnn.CreateCommand();
|
---|
110 | c.CommandText = "Select Guid, CreationTime, StartTime, Rawdata from Job where Status=@Status";
|
---|
111 | DbParameter statusParameter = c.CreateParameter();
|
---|
112 | statusParameter.ParameterName = "@Status";
|
---|
113 | statusParameter.Value = JobState.Waiting.ToString();
|
---|
114 | c.Parameters.Add(statusParameter);
|
---|
115 | SQLiteDataReader r = c.ExecuteReader();
|
---|
116 | while(r.Read()) {
|
---|
117 | JobEntry job = new JobEntry();
|
---|
118 | job.Status = JobState.Waiting;
|
---|
119 | job.Guid = r.GetGuid(0);
|
---|
120 | job.CreationTime = r.GetDateTime(1);
|
---|
121 | job.StartTime = r.IsDBNull(2)?null:new Nullable<DateTime>(r.GetDateTime(2));
|
---|
122 | job.RawData = (byte[])r.GetValue(3);
|
---|
123 | jobs.Add(job);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | } finally {
|
---|
127 | rwLock.ExitReadLock();
|
---|
128 | }
|
---|
129 | return jobs;
|
---|
130 | }
|
---|
131 |
|
---|
132 | internal void SetJobResult(Guid guid, byte[] result) {
|
---|
133 | rwLock.EnterWriteLock();
|
---|
134 | try {
|
---|
135 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
136 | cnn.Open();
|
---|
137 | using(SQLiteTransaction t = cnn.BeginTransaction()) {
|
---|
138 | using(SQLiteCommand c = cnn.CreateCommand()) {
|
---|
139 | c.Transaction = t;
|
---|
140 | c.CommandText = "Update Job set Status=@Status, RawData=@RawData where Guid=@Guid";
|
---|
141 | DbParameter rawDataParam = c.CreateParameter();
|
---|
142 | DbParameter statusParam = c.CreateParameter();
|
---|
143 | DbParameter guidParam = c.CreateParameter();
|
---|
144 | rawDataParam.ParameterName = "@RawData";
|
---|
145 | rawDataParam.Value = result;
|
---|
146 | guidParam.ParameterName = "@Guid";
|
---|
147 | guidParam.Value = guid;
|
---|
148 | statusParam.ParameterName = "@Status";
|
---|
149 | statusParam.Value = JobState.Finished;
|
---|
150 | c.Parameters.Add(rawDataParam);
|
---|
151 | c.Parameters.Add(statusParam);
|
---|
152 | c.Parameters.Add(guidParam);
|
---|
153 | c.ExecuteNonQuery();
|
---|
154 | }
|
---|
155 | t.Commit();
|
---|
156 | }
|
---|
157 | }
|
---|
158 | } finally {
|
---|
159 | rwLock.ExitWriteLock();
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | internal void UpdateJobState(Guid guid, JobState jobState) {
|
---|
164 | rwLock.EnterWriteLock();
|
---|
165 | try {
|
---|
166 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
167 | cnn.Open();
|
---|
168 | using(SQLiteTransaction t = cnn.BeginTransaction()) {
|
---|
169 | using(SQLiteCommand c = cnn.CreateCommand()) {
|
---|
170 | c.Transaction = t;
|
---|
171 | c.CommandText = "Update Job set Status=@Status, StartTime=@StartTime where Guid=@Guid";
|
---|
172 | DbParameter statusParam = c.CreateParameter();
|
---|
173 | DbParameter startTimeParam = c.CreateParameter();
|
---|
174 | DbParameter guidParam = c.CreateParameter();
|
---|
175 | startTimeParam.ParameterName = "@StartTime";
|
---|
176 | if(jobState == JobState.Busy)
|
---|
177 | startTimeParam.Value = DateTime.Now;
|
---|
178 | else
|
---|
179 | startTimeParam.Value = null;
|
---|
180 | guidParam.ParameterName = "@Guid";
|
---|
181 | guidParam.Value = guid;
|
---|
182 | statusParam.ParameterName = "@Status";
|
---|
183 | statusParam.Value = jobState;
|
---|
184 | c.Parameters.Add(startTimeParam);
|
---|
185 | c.Parameters.Add(statusParam);
|
---|
186 | c.Parameters.Add(guidParam);
|
---|
187 | c.ExecuteNonQuery();
|
---|
188 | }
|
---|
189 | t.Commit();
|
---|
190 | }
|
---|
191 | }
|
---|
192 | } finally {
|
---|
193 | rwLock.ExitWriteLock();
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | internal JobEntry GetJob(Guid guid) {
|
---|
199 | rwLock.EnterReadLock();
|
---|
200 | try {
|
---|
201 | using(SQLiteConnection cnn = new SQLiteConnection(connectionString)) {
|
---|
202 | cnn.Open();
|
---|
203 | SQLiteCommand c = cnn.CreateCommand();
|
---|
204 | c.CommandText = "Select Status, CreationTime, StartTime, Rawdata from Job where Guid=@Guid";
|
---|
205 | DbParameter guidParameter = c.CreateParameter();
|
---|
206 | guidParameter.ParameterName = "@Guid";
|
---|
207 | guidParameter.Value = guid;
|
---|
208 | c.Parameters.Add(guidParameter);
|
---|
209 | SQLiteDataReader r = c.ExecuteReader();
|
---|
210 | while(r.Read()) {
|
---|
211 | JobEntry job = new JobEntry();
|
---|
212 | job.Guid = guid;
|
---|
213 | job.Status = (JobState)Enum.Parse(typeof(JobState), r.GetString(0));
|
---|
214 | job.CreationTime = r.GetDateTime(1);
|
---|
215 | job.StartTime = r.IsDBNull(2)?null:new Nullable<DateTime>(r.GetDateTime(2));
|
---|
216 | job.RawData = (byte[])r.GetValue(3);
|
---|
217 | return job;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | } finally {
|
---|
221 | rwLock.ExitReadLock();
|
---|
222 | }
|
---|
223 | return null;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /// <summary>
|
---|
227 | /// Does nothing right now (= running jobs that disappear are never restarted).
|
---|
228 | /// </summary>
|
---|
229 | internal void RestartExpiredActiveJobs() {
|
---|
230 | }
|
---|
231 |
|
---|
232 | /// <summary>
|
---|
233 | /// Does nothing right now (= results are never deleted).
|
---|
234 | /// </summary>
|
---|
235 | internal void DeleteExpiredResults() {
|
---|
236 | }
|
---|
237 | }
|
---|
238 | } |
---|