Changeset 2221


Ignore:
Timestamp:
08/03/09 11:33:15 (12 years ago)
Author:
mkommend
Message:

performance improvements by reusing the datacontext (ticket #712)

Location:
branches/HeuristicLab.Modeling Database Backend/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.CEDMA.Core/3.3/Results.cs

    r2217 r2221  
    7373    public Results(IModelingDatabase database) {
    7474      this.database = database;
    75       multiDimensionalOrdinalVariables = database.GetAllResultsForInputVariables().Select(x => "VariableImpacts: "+ x.Name).ToArray();
     75      multiDimensionalOrdinalVariables = database.GetAllResultsForInputVariables().Select(x => "VariableImpacts: " + x.Name).ToArray();
    7676    }
    7777
     
    8585
    8686    private IEnumerable<ResultsEntry> SelectRows() {
    87       database.GetAllModelResults();
     87      database.Connect();     
    8888      entries = new List<ResultsEntry>();
    8989      foreach (var model in database.GetAllModels()) {
    9090        ResultsEntry modelEntry = new ResultsEntry();
    91         foreach(var modelResult in database.GetModelResults(model)) {
     91        foreach (var modelResult in database.GetModelResults(model)) {
    9292          modelEntry.Set(modelResult.Result.Name, modelResult.Value);
    9393        }
    9494        modelEntry.Set("PersistedData", database.GetModelData(model));
    9595        modelEntry.Set("TargetVariable", model.TargetVariable.Name);
    96         Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry> inputVariableResultsEntries = 
     96        Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry> inputVariableResultsEntries =
    9797          new Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry>();
    9898
     
    107107        entries.Add(modelEntry);
    108108      }
    109      
     109      database.Disconnect();
    110110      FireChanged();
    111111      cached = true;
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.CEDMA.Core/3.3/TableResultsView.cs

    r2207 r2221  
    6868      if (e.Button == MouseButtons.Left && e.Clicks == 2) {
    6969        DataGridView.HitTestInfo hitInfo = dataGridView.HitTest(e.X, e.Y);
    70         ResultsEntry entry = (ResultsEntry)dataGridView.Rows[hitInfo.RowIndex].Tag;
     70        ResultsEntry entry = (ResultsEntry)dataGridView.Rows[hitInfo.RowIndex].Tag;       
    7171        var model = (IItem)PersistenceManager.RestoreFromGZip((byte[])entry.Get("PersistedData"));
    7272        PluginManager.ControlManager.ShowControl(model.CreateView());
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs

    r2217 r2221  
    1717    public DatabaseService(string connection) {
    1818      this.connection = connection;
    19       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    20         if (!ctx.DatabaseExists())
    21           ctx.CreateDatabase();
    22       }
     19      Connect();
     20      if (!ctx.DatabaseExists())
     21        ctx.CreateDatabase();
    2322    }
    2423
    2524    private void EmptyDatabase() {
    26       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    27         ctx.DeleteDatabase();
    28         ctx.CreateDatabase();
    29       }
     25      ctx.DeleteDatabase();
     26      ctx.CreateDatabase();
     27    }
     28
     29    private ModelingDataContext ctx;
     30    public void Connect() {
     31      if (ctx != null)
     32        return;
     33
     34      ctx = new ModelingDataContext(connection);
     35      DataLoadOptions dlo = new DataLoadOptions();
     36      dlo.LoadWith<ModelResult>(mr => mr.Result);
     37      dlo.LoadWith<InputVariableResult>(ir => ir.Variable);
     38      dlo.LoadWith<InputVariableResult>(ir => ir.Result);
     39      dlo.LoadWith<Model>(m => m.TargetVariable);
     40      ctx.LoadOptions = dlo;
     41    }
     42
     43    public void Disconnect() {
     44      if (ctx == null)
     45        return;
     46      ctx.Dispose();
     47      ctx = null;
    3048    }
    3149
     
    103121
    104122    public Dataset GetDataset() {
    105       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    106         if (ctx.Problems.Count() != 1)
    107           throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database.");
    108 
    109         Problem problem = ctx.Problems.Single();
    110         return problem.Dataset;
    111       }
     123      if (ctx.Problems.Count() != 1)
     124        throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database.");
     125
     126      Problem problem = ctx.Problems.Single();
     127      return problem.Dataset;
     128
    112129    }
    113130
    114131    public Problem GetOrCreateProblem(Dataset dataset) {
    115132      Problem problem;
    116       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    117         if (ctx.Problems.Count() == 0)
    118           problem = PersistProblem(dataset);
    119         else
    120           problem = ctx.Problems.Single();
    121         if (problem.Dataset.ToString() != dataset.ToString())
    122           throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset.");
    123       }
     133      if (ctx.Problems.Count() == 0)
     134        problem = PersistProblem(dataset);
     135      else
     136        problem = ctx.Problems.Single();
     137      if (problem.Dataset.ToString() != dataset.ToString())
     138        throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset.");
    124139      return problem;
    125140    }
     
    165180    public Dictionary<string, Variable> GetAllVariables() {
    166181      Dictionary<string, Variable> dict = new Dictionary<string, Variable>();
    167       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    168         dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; });
    169       }
     182      dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; });
    170183      return dict;
    171184    }
     
    192205
    193206    public IEnumerable<IResult> GetAllResults() {
    194       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    195         return ctx.Results.ToList().Cast<IResult>();
    196       }
     207      return ctx.Results.ToList().Cast<IResult>();
    197208    }
    198209
    199210    public IEnumerable<IResult> GetAllResultsForInputVariables() {
    200       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    201         return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
    202       }
     211      return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
    203212    }
    204213
     
    207216    #region ModelResult
    208217    public IEnumerable<IModelResult> GetModelResults(IModel model) {
    209       ModelingDataContext ctx = new ModelingDataContext(connection);
    210       DataLoadOptions dlo = new DataLoadOptions();
    211       dlo.LoadWith<ModelResult>(mr => mr.Result);
    212       ctx.LoadOptions = dlo;
    213 
    214218      var results = from result in ctx.ModelResults
    215219                    where result.Model == model
     
    217221      return results.ToList().Cast<IModelResult>();
    218222    }
    219 
    220     public void GetAllModelResults() {
    221       ModelingDataContext ctx = new ModelingDataContext(connection);
    222       DataLoadOptions dlo = new DataLoadOptions();
    223       dlo.LoadWith<ModelResult>(mr => mr.Result);
    224       dlo.LoadWith<ModelResult>(mr => mr.Model);
    225       ctx.LoadOptions = dlo;
    226 
    227       var results = from result in ctx.ModelResults
    228                     select result;
    229       results.ToList();     
    230 
    231     }
    232223    #endregion
    233224
    234225    #region InputVariableResults
    235226    public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
    236       ModelingDataContext ctx = new ModelingDataContext(connection);
    237       DataLoadOptions dlo = new DataLoadOptions();
    238       dlo.LoadWith<InputVariableResult>(ir => ir.Variable);
    239       dlo.LoadWith<InputVariableResult>(ir => ir.Result);     
    240       ctx.LoadOptions = dlo;
    241 
    242227      var inputResults = from ir in ctx.InputVariableResults
    243228                         where ir.Model == model
     
    250235    #region Model
    251236    public IEnumerable<IModel> GetAllModels() {
    252       ModelingDataContext ctx = new ModelingDataContext(connection);
    253       DataLoadOptions dlo = new DataLoadOptions();
    254       dlo.LoadWith<Model>(m => m.TargetVariable);
    255       ctx.LoadOptions = dlo;
    256237      return ctx.Models.ToList().Cast<IModel>();
    257238    }
    258239
    259240    public byte[] GetModelData(IModel model) {
    260       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    261         var data = (from md in ctx.ModelData
    262                     where md.Model == model
    263                     select md);
    264         if (data.Count() == 0)
    265           return null;
    266         return data.Single().Data;
    267       }
     241      var data = (from md in ctx.ModelData
     242                  where md.Model == model
     243                  select md);
     244      if (data.Count() == 0)
     245        return null;
     246      return data.Single().Data;
    268247    }
    269248    #endregion
  • branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.Modeling.Database/3.2/IModelingDatabase.cs

    r2217 r2221  
    3535    IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model);
    3636
    37     void GetAllModelResults();
     37    void Connect();
     38    void Disconnect();
    3839  }
    3940}
Note: See TracChangeset for help on using the changeset viewer.