Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/24/09 15:15:16 (15 years ago)
Author:
mkommend
Message:

changed modeling database backend to use a shared read connection => notification by exception if another program is connected to the database (ticket #759)

Location:
trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs

    r2525 r2530  
    8787      dlo.LoadWith<Model>(m => m.Algorithm);
    8888      ctx.LoadOptions = dlo;
    89 
    90       if (!ctx.DatabaseExists())
     89      //ctx.Log = System.Console.Out;
     90
     91      if (!ctx.DatabaseExists() && !this.ReadOnly)
    9192        ctx.CreateDatabase();
    9293    }
     
    100101    }
    101102
     103    public void Commit() {
     104      if (ctx != null)
     105        ctx.SubmitChanges();
     106    }
     107
     108    private void CheckConnection() {
     109      CheckConnection(false);
     110    }
     111
     112    private void CheckConnection(bool writeEnabled) {
     113      if (ctx == null)
     114        throw new InvalidOperationException("Could not perform operation, when not connected to the database.");
     115      if (writeEnabled && this.ReadOnly)
     116        throw new InvalidOperationException("Could not perform update operation, when database is in readonly mode.");
     117    }
     118
    102119    public IEnumerable<IModel> GetAllModels() {
     120      this.CheckConnection();
    103121      return ctx.Models.ToList().Cast<IModel>();
    104122    }
    105123
    106124    public IEnumerable<int> GetAllModelIds() {
     125      this.CheckConnection();
    107126      return from m in ctx.Models
    108127             select m.Id;
     
    110129
    111130    public IEnumerable<IVariable> GetAllVariables() {
     131      this.CheckConnection();
    112132      return ctx.Variables.ToList().Cast<IVariable>();
    113133    }
    114134
    115135    public IEnumerable<IResult> GetAllResults() {
     136      this.CheckConnection();
    116137      return ctx.Results.ToList().Cast<IResult>();
    117138    }
    118139
    119140    public IEnumerable<IResult> GetAllResultsForInputVariables() {
     141      this.CheckConnection();
    120142      return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
    121143    }
    122144
    123145    public IEnumerable<IMetaData> GetAllMetaData() {
     146      this.CheckConnection();
    124147      return ctx.MetaData.ToList().Cast<IMetaData>();
    125148    }
    126149
    127150    public IEnumerable<IAlgorithm> GetAllAlgorithms() {
     151      this.CheckConnection();
    128152      return ctx.Algorithms.ToList().Cast<IAlgorithm>();
    129153    }
     
    153177
    154178    public IModel GetModel(int id) {
     179      this.CheckConnection();
    155180      var model = ctx.Models.Where(m => m.Id == id);
    156181      if (model.Count() == 1)
     
    160185
    161186    public void PersistModel(IModel model) {
     187      this.CheckConnection(true);
    162188      Model m = (Model)model;
    163189      //check if model has to be updated or inserted
     
    169195      } else
    170196        ctx.Models.InsertOnSubmit(m);
    171       ctx.SubmitChanges();
    172197    }
    173198
    174199    public void DeleteModel(IModel model) {
     200      this.CheckConnection(true);
    175201      Model m = (Model)model;
    176202      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
     
    187213
    188214    public Dataset GetDataset() {
     215      this.CheckConnection();
    189216      if (ctx.Problems.Count() > 1)
    190217        throw new InvalidOperationException("Could not get dataset. More than one problems are persisted in the database.");
     
    195222
    196223    public void PersistProblem(Dataset dataset) {
     224      this.CheckConnection(true);
    197225      Problem problem;
    198226      if (ctx.Problems.Count() != 0)
     
    203231        ctx.Variables.InsertOnSubmit(new Variable(variable));
    204232      }
    205       ctx.SubmitChanges();
    206233    }
    207234
    208235    public IVariable GetVariable(string variableName) {
     236      this.CheckConnection();
    209237      var variables = ctx.Variables.Where(v => v.Name == variableName);
    210238      if (variables.Count() != 1)
     
    214242
    215243    public IPredictor GetModelPredictor(IModel model) {
     244      this.CheckConnection();
    216245      var data = (from md in ctx.ModelData
    217246                  where md.Model == model
     
    223252
    224253    public void PersistPredictor(IModel model, IPredictor predictor) {
     254      this.CheckConnection(true);
    225255      Model m = (Model)model;
    226256      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
     
    232262      foreach (string variableName in predictor.GetInputVariables())
    233263        ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
    234 
    235       ctx.SubmitChanges();
    236264    }
    237265
    238266    public IInputVariable GetInputVariable(IModel model, string inputVariableName) {
     267      this.CheckConnection();
    239268      var inputVariables = ctx.InputVariables.Where(i => i.Model == model && i.Variable.Name == inputVariableName);
    240269      if (inputVariables.Count() == 1)
     
    248277
    249278    public IAlgorithm GetOrPersistAlgorithm(string algorithmName) {
     279      this.CheckConnection();
    250280      Algorithm algorithm;
    251281      var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
    252282      if (algorithms.Count() == 0) {
    253283        algorithm = new Algorithm(algorithmName, "");
     284        this.CheckConnection(true);
    254285        ctx.Algorithms.InsertOnSubmit(algorithm);
    255286        ctx.SubmitChanges();
     
    262293
    263294    public IResult GetOrPersistResult(string resultName) {
     295      this.CheckConnection();
    264296      Result result;
    265297      var results = ctx.Results.Where(r => r.Name == resultName);
    266298      if (results.Count() == 0) {
     299        this.CheckConnection(true);
    267300        result = new Result(resultName);
    268301        ctx.Results.InsertOnSubmit(result);
     
    276309
    277310    public IMetaData GetOrPersistMetaData(string metaDataName) {
     311      this.CheckConnection();
    278312      MetaData metadata;
    279313      var md = ctx.MetaData.Where(r => r.Name == metaDataName);
    280314      if (md.Count() == 0) {
     315        this.CheckConnection(true);
    281316        metadata = new MetaData(metaDataName);
    282317        ctx.MetaData.InsertOnSubmit(metadata);
     
    290325
    291326    public IEnumerable<IModelResult> GetModelResults(IModel model) {
     327      this.CheckConnection();
    292328      return ctx.ModelResults.Where(mr => mr.Model == model).Cast<IModelResult>();
    293329    }
    294330    public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
     331      this.CheckConnection();
    295332      return ctx.InputVariableResults.Where(ivr => ivr.Model == model).Cast<IInputVariableResult>();
    296333    }
    297334    public IEnumerable<IModelMetaData> GetModelMetaData(IModel model) {
     335      this.CheckConnection();
    298336      return ctx.ModelMetaData.Where(md => md.Model == model).Cast<IModelMetaData>();
    299337    }
     
    306344
    307345    public void PersistModelResults(IModel model, IEnumerable<IModelResult> modelResults) {
     346      this.CheckConnection(true);
    308347      ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
    309348      ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
    310       ctx.SubmitChanges();
    311349    }
    312350
     
    323361
    324362    public void PersistInputVariableResults(IModel model, IEnumerable<IInputVariableResult> inputVariableResults) {
     363      this.CheckConnection(true);
    325364      ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
    326365      ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
    327       ctx.SubmitChanges();
    328366    }
    329367
     
    335373
    336374    public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) {
     375      this.CheckConnection(true);
    337376      ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
    338377      ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
    339       ctx.SubmitChanges();
    340     }
    341 
    342     public IModel Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
    343       if (ctx.Problems.Count() == 0)
    344         PersistProblem(algorithm.Dataset);
    345       return Persist(algorithm.Model, algorithm.Name, algorithm.Description);
    346     }
    347 
    348     public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
     378    }
     379
     380    public void Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
     381      this.CheckConnection(true);
    349382      Algorithm algorithm = (Algorithm)GetOrPersistAlgorithm(algorithmName);
    350       Variable targetVariable = (Variable) GetVariable(model.TargetVariable);
     383      Variable targetVariable = (Variable)GetVariable(model.TargetVariable);
    351384      Model m = (Model)CreateModel(null, model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd,
    352385        model.ValidationSamplesStart, model.ValidationSamplesEnd, model.TestSamplesStart, model.TestSamplesEnd);
    353386      ctx.Models.InsertOnSubmit(m);
    354387      ctx.SubmitChanges();
    355       ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));
     388      ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));     
    356389
    357390      foreach (string variableName in model.Predictor.GetInputVariables())
     
    374407        }
    375408      }
    376       ctx.SubmitChanges();
    377 
    378       //if connected to database return inserted model
    379       if (this.ctx != null)
    380         return this.ctx.Models.Where(x => x.Id == m.Id).Single();
    381       return null;
     409     ctx.SubmitChanges();
    382410    }
    383411  }
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/PredictorPersister.cs

    r2525 r2530  
    3838      db.Connect();
    3939      Dataset temp = db.GetDataset();
    40       if(temp == null)     
    41         db.PersistProblem(ds);     
     40      if (temp == null) {
     41        db.PersistProblem(ds);
     42        db.Commit();
     43      }
    4244
    4345      IAnalyzerModel model = new AnalyzerModel();                 
    4446      DefaultModelAnalyzerOperators.PopulateAnalyzerModel(scope, model, modelType);
    4547      db.Persist(model, algorithm, algorithm);
     48      db.Commit();
    4649      db.Disconnect();
    4750     
Note: See TracChangeset for help on using the changeset viewer.