Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB/3.3/QueryService.cs @ 4298

Last change on this file since 4298 was 4279, checked in by swagner, 14 years ago

Integrated OKB services (#1166)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using System.Data;
25using System.Linq;
26using System.ServiceModel;
27using HeuristicLab.Services.OKB.AttributeSelection;
28using HeuristicLab.Services.OKB.DataAccess;
29using log4net;
30
31namespace HeuristicLab.Services.OKB {
32
33  /// <summary>
34  /// Implementation of the <see cref="IQueryService"/>.
35  /// </summary>
36  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
37  public class QueryService : IQueryService, IDisposable {
38
39    private Guid sessionID;
40    private static ILog logger = LogManager.GetLogger(typeof(QueryService));
41
42    private void Log(string message, params object[] args) {
43      using (log4net.ThreadContext.Stacks["NDC"].Push(sessionID.ToString())) {
44        logger.Info(String.Format(message, args));
45      }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of the <see cref="QueryService"/> class.
50    /// </summary>
51    public QueryService() {
52      sessionID = Guid.NewGuid();
53      Log("Instantiating new service");
54    }
55
56    private OKBDataContext GetDataContext() {
57      return new OKBDataContext();
58    }
59
60    /// <summary>
61    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
62    /// </summary>
63    public void Dispose() {
64      Terminate();
65    }
66
67    private DataSetBuilder dataSetBuilder;
68    private OKBDataContext okb;
69    private IEnumerator<DataRow> dataEnumerator;
70
71    /// <summary>
72    /// Gets a list of all possible attribute selectors.
73    /// </summary>
74    /// <returns>
75    /// An array of <see cref="Server.AttributeSelector"/>s
76    /// </returns>
77    public AttributeSelector[] GetAllAttributeSelectors() {
78      Log("retrieving all attribute specifiers");
79      try {
80        OKBDataContext okb = GetDataContext();
81        return RunAttributeSelector.GetAllAttributeSelectors(okb).Select(s => new AttributeSelector(s)).ToArray();
82      }
83      catch (Exception x) {
84        Log("exception caught: " + x.ToString());
85        throw;
86      }
87    }
88
89    /// <summary>
90    /// Prepares the a query using the specified selection criteria.
91    /// </summary>
92    /// <param name="selectors">The list of <see cref="Server.AttributeSelector"/>s.</param>
93    /// <returns>
94    /// An empty <see cref="DataTable"/> containing only the column headers.
95    /// </returns>
96    public DataTable PrepareQuery(AttributeSelector[] selectors) {
97      okb = GetDataContext();
98      Log("validating new query");
99      if (selectors.Length == 0)
100        throw new FaultException("No columns selected");
101      IEnumerable<RunAttributeSelector> checkedSelectors =
102        selectors.Select(s => new RunAttributeSelector(new OKBDataContext(okb.Connection.ConnectionString), s)).ToList();
103      Log("preparing query:\n  {0}", string.Join("\n  ", checkedSelectors.Select(s => s.ToString()).ToArray()));
104      dataSetBuilder = new DataSetBuilder(okb, checkedSelectors);
105      dataEnumerator = dataSetBuilder.GetEnumerator();
106      return dataSetBuilder.Runs;
107    }
108
109    /// <summary>
110    /// Gets the number of matching rows for the prepared query
111    /// (see <see cref="IQueryService.PrepareQuery"/>).
112    /// </summary>
113    /// <returns>The number of matching rows.</returns>
114    public int GetCount() {
115      return dataSetBuilder.GetCount();
116    }
117
118    /// <summary>
119    /// Gets the next rows.
120    /// </summary>
121    /// <param name="maxNRows">The max N rows.</param>
122    /// <returns></returns>
123    public DataTable GetNextRows(int maxNRows) {
124      DataTable runs = dataSetBuilder.Runs.Clone();
125      for (int i = 0; i < maxNRows; i++) {
126        if (!dataEnumerator.MoveNext())
127          break;
128        DataRow row = runs.NewRow();
129        row.ItemArray = (object[])dataEnumerator.Current.ItemArray.Clone();
130        runs.Rows.Add(row);
131      }
132      return runs;
133    }
134
135    /// <summary>
136    /// Terminates the session and closes the connection.
137    /// </summary>
138    public void Terminate() {
139      Log("Terminating session");
140      dataSetBuilder = null;
141      if (dataEnumerator != null) {
142        Log("disposing data enumerate");
143        dataEnumerator.Dispose();
144        dataEnumerator = null;
145      }
146      if (okb != null) {
147        Log("disposing data context");
148        okb.Dispose();
149        okb = null;
150      }
151    }
152
153  }
154}
Note: See TracBrowser for help on using the repository browser.