Surance Center

db4o Tutorial 中文翻译(七)

5. 集合和数组


我们将要通过增加Sensor类到Car类,来慢慢转移到实时数据处理

Sensor类

一个Car在需要记录他的比赛情况的时候,可以产生他目前的Sensor Readout :

更新的car类

我们现在只要加静态的数据,下一个章节就要加入更灵活的数据了。


    5.1. 存储数据


    现在你应该很熟悉这个步骤了吧!
    // storeFirstCar
    Car car1 = new Car("Ferrari");
    Pilot pilot1 
    = new Pilot("Michael Schumacher"100);
    car1.Pilot 
    = pilot1;
    db.Set(car1);

    第二个Car我们要加入2个snapshots:
    // storeSecondCar
    Pilot pilot2 = new Pilot("Rubens Barrichello"99);
    Car car2 
    = new Car("BMW");
    car2.Pilot 
    = pilot2;
    car2.Snapshot();
    car2.Snapshot();
    db.Set(car2);

    5.2. 检索



      5.2.1. QBE


      首先,要确定snapshots是否被真的加入了.
      // retrieveAllSensorReadout
      IObjectSet result = db.Get(typeof(SensorReadout));
      ListResult(result);
      注意:原型中给定元素的实际位置是错的。
      为了根据一个car的sensor readouts来检索car,我们要记录查看历史
      // retrieveCarQBE
      SensorReadout protoReadout = new SensorReadout(new double[] 0.60.2 }, DateTime.MinValue, null);
      IList protoHistory 
      = new ArrayList();
      protoHistory.Add(protoReadout);
      Car protoCar 
      = new Car(null, protoHistory);
      IObjectSet result 
      = db.Get(protoCar);
      ListResult(result);
      我们也可以检索集合本身,因为他们也是对象
      // retrieveCollections
      IObjectSet result = db.Get(new ArrayList());
      ListResult(result);


      这对array不起作用:
      // retrieveArrays
      IObjectSet result = db.Get(new double[] 0.60.4 });
      ListResult(result);

      5.2.2. Native Queries


      如果你想用 Native Queries 来通过匹配来查找SensorReadouts, 和检索单个值(非集合、数组),是一样的:
      public class RetrieveSensorReadoutPredicate : Predicate
      {
          
      public bool Match(SensorReadout candidate)
          
      {
              
      return Array.IndexOf(candidate.Values, 0.3> -1
                  
      && Array.IndexOf(candidate.Values, 0.1> -1;
          }

      }

      // retrieveSensorReadoutNative
      IObjectSet results = db.Query(new RetrieveSensorReadoutPredicate());
      ListResult(results);

      这里是根据匹配的readout 来查找car:
      public class RetrieveCarPredicate : Predicate
      {
          
      public bool Match(Car car)
          
      {
              
      foreach (SensorReadout sensor in car.History)
              
      {
                  
      if (Array.IndexOf(sensor.Values, 0.3> -1
                      
      && Array.IndexOf(sensor.Values, 0.1> -1)
                  
      {
                      
      return true
                  }

              }

              
      return false;
          }

      }

      // retrieveCarNative
      IObjectSet results = db.Query(new RetrieveCarPredicate());
      ListResult(results);

      5.2.3. 检索 API


      对arrays and collections操作类似上面的例子. 首先,我们只用匹配值检索 SensorReadouts :
      // retrieveSensorReadoutQuery
      IQuery query = db.Query();
      query.Constrain(typeof(SensorReadout));
      IQuery valueQuery = query.Descend("_values");
      valueQuery.Constrain(0.3);
      valueQuery.Constrain(0.1);
      IObjectSet results = query.Execute();
      ListResult(results);


      然后,我们根据匹配的 Readout 检索car:
      // retrieveCarQuery
      IQuery query = db.Query();
      query.Constrain(
      typeof(Car));
      IQuery historyQuery 
      = query.Descend("_history");
      historyQuery.Constrain(
      typeof(SensorReadout));
      IQuery valueQuery 
      = historyQuery.Descend("_values");
      valueQuery.Constrain(
      0.3);
      valueQuery.Constrain(
      0.1);
      IObjectSet results 
      = query.Execute();
      ListResult(results);

      5.3. 更新和删除


      应该很熟悉了吧,我们只需要设置car的更新深度:
      // updateCarPart1
      Db4oFactory.Configure().ObjectClass(typeof(Car)).CascadeOnUpdate(true);

      // updateCarPart2
      IObjectSet result = db.Get(new Car("BMW"null));
      Car car 
      = (Car)result.Next();
      car.Snapshot();
      db.Set(car);
      RetrieveAllSensorReadouts(db);

      当删除arrays and collections时候没有什么不同的。
      从一个集合中删除对象也是一样的。
      // updateCollection
      IQuery query = db.Query();
      query.Constrain(
      typeof(Car));
      IObjectSet result 
      = query.Descend("_history").Execute();
      IList history 
      = (IList)result.Next();
      history.RemoveAt(
      0);
      db.Set(history);
      Car proto 
      = new Car(nullnull);
      result 
      = db.Get(proto);
      foreach (Car car in result)
      {    
          
      foreach (object readout in car.History)
          
      {
              Console.WriteLine(readout);
          }

      }



      (通过这个例子,我们可以看到db4o可以很容易进入对象内部,这在以前是很少见的。请牢记以上并且小心应用。.)

      在进入下一章之前,删除所有的car:
      // deleteAllPart1
      Db4oFactory.Configure().ObjectClass(typeof(Car)).CascadeOnDelete(true);

      // deleteAllPart2
      IObjectSet result = db.Get(new Car(nullnull));
      foreach (object car in result)
      {
          db.Delete(car);
      }

      IObjectSet readouts 
      = db.Get(new SensorReadout(null, DateTime.MinValue, null));
      foreach (object readout in readouts)
      {
          db.Delete(readout);
      }

      5.4. 总结


      Ok, collections也是对象。但是我们怎么定义它的类型呢?有没有必要定义呢? db4o如何处理继承?下一章节将讲到。


      5.5. 全部代码

       

      锘縰sing System;
      using System.Collections;
      using System.IO;
      using Db4objects.Db4o;
      using Db4objects.Db4o.Query;
      namespace Db4objects.Db4o.Tutorial.F1.Chapter3
      {    
          
      public class CollectionsExample : Util
          
      {
              
      public static void Main(string[] args)
              
      {
                  File.Delete(Util.YapFileName);            
                  IObjectContainer db 
      = Db4oFactory.OpenFile(Util.YapFileName);
                  
      try
                  
      {
                      StoreFirstCar(db);
                      StoreSecondCar(db);
                      RetrieveAllSensorReadouts(db);
                      RetrieveSensorReadoutQBE(db);
                      RetrieveCarQBE(db);
                      RetrieveCollections(db);
                      RetrieveArrays(db);
                      RetrieveSensorReadoutQuery(db);
                      RetrieveCarQuery(db);
                      db.Close();
                      UpdateCarPart1();
                      db 
      = Db4oFactory.OpenFile(Util.YapFileName);
                      UpdateCarPart2(db);
                      UpdateCollection(db);
                      db.Close();
                      DeleteAllPart1();
                      db
      =Db4oFactory.OpenFile(Util.YapFileName);
                      DeleteAllPart2(db);
                      RetrieveAllSensorReadouts(db);
                  }

                  
      finally
                  
      {
                      db.Close();
                  }

              }

              
              
      public static void StoreFirstCar(IObjectContainer db)
              
      {
                  Car car1 
      = new Car("Ferrari");
                  Pilot pilot1 
      = new Pilot("Michael Schumacher"100);
                  car1.Pilot 
      = pilot1;
                  db.Set(car1);
              }

              
              
      public static void StoreSecondCar(IObjectContainer db)
              
      {
                  Pilot pilot2 
      = new Pilot("Rubens Barrichello"99);
                  Car car2 
      = new Car("BMW");
                  car2.Pilot 
      = pilot2;
                  car2.Snapshot();
                  car2.Snapshot();
                  db.Set(car2);       
              }

              
              
      public static void RetrieveAllSensorReadouts(IObjectContainer db)
              
      {
                  IObjectSet result 
      = db.Get(typeof(SensorReadout));
                  ListResult(result);
              }

              
              
      public static void RetrieveSensorReadoutQBE(IObjectContainer db)
              
      {
                  SensorReadout proto 
      = new SensorReadout(new double[] 0.30.1 }, DateTime.MinValue, null);
                  IObjectSet result 
      = db.Get(proto);
                  ListResult(result);
              }

              
              
      public static void RetrieveCarQBE(IObjectContainer db)
              
      {
                  SensorReadout protoReadout 
      = new SensorReadout(new double[] 0.60.2 }, DateTime.MinValue, null);
                  IList protoHistory 
      = new ArrayList();
                  protoHistory.Add(protoReadout);
                  Car protoCar 
      = new Car(null, protoHistory);
                  IObjectSet result 
      = db.Get(protoCar);
                  ListResult(result);
              }

              
              
      public static void RetrieveCollections(IObjectContainer db)
              
      {
                  IObjectSet result 
      = db.Get(new ArrayList());
                  ListResult(result);
              }

              
              
      public static void RetrieveArrays(IObjectContainer db)
              
      {
                  IObjectSet result 
      = db.Get(new double[] 0.60.4 });
                  ListResult(result);
              }

              
              
      public static void RetrieveSensorReadoutQuery(IObjectContainer db)
              
      {
                  IQuery query 
      = db.Query();
                  query.Constrain(
      typeof(SensorReadout));
                  IQuery valueQuery 
      = query.Descend("_values");
                  valueQuery.Constrain(
      0.3);
                  valueQuery.Constrain(
      0.1);
                  IObjectSet results 
      = query.Execute();
                  ListResult(results);
              }

              
              
      public static void RetrieveCarQuery(IObjectContainer db)
              
      {
                  IQuery query 
      = db.Query();
                  query.Constrain(
      typeof(Car));
                  IQuery historyQuery 
      = query.Descend("_history");
                  historyQuery.Constrain(
      typeof(SensorReadout));
                  IQuery valueQuery 
      = historyQuery.Descend("_values");
                  valueQuery.Constrain(
      0.3);
                  valueQuery.Constrain(
      0.1);
                  IObjectSet results 
      = query.Execute();
                  ListResult(results);
              }

              
      public class RetrieveSensorReadoutPredicate : Predicate
              
      {
                  
      public bool Match(SensorReadout candidate)
                  
      {
                      
      return Array.IndexOf(candidate.Values, 0.3> -1
                          
      && Array.IndexOf(candidate.Values, 0.1> -1;
                  }

              }

              
              
      public static void RetrieveSensorReadoutNative(IObjectContainer db) 
              
      {
                  IObjectSet results 
      = db.Query(new RetrieveSensorReadoutPredicate());
                  ListResult(results);
              }

              
      public class RetrieveCarPredicate : Predicate
              
      {
                  
      public bool Match(Car car)
                  
      {
                      
      foreach (SensorReadout sensor in car.History)
                      
      {
                          
      if (Array.IndexOf(sensor.Values, 0.3> -1
                              
      && Array.IndexOf(sensor.Values, 0.1> -1)
                          
      {
                              
      return true
                          }

                      }

                      
      return false;
                  }

              }

              
      public static void RetrieveCarNative(IObjectContainer db)
              
      {
                  IObjectSet results 
      = db.Query(new RetrieveCarPredicate());
                  ListResult(results);
              }

              
      public static void UpdateCarPart1()
              
      {
                  Db4oFactory.Configure().ObjectClass(
      typeof(Car)).CascadeOnUpdate(true);
              }

              
              
      public static void UpdateCarPart2(IObjectContainer db)
              
      {
                  IObjectSet result 
      = db.Get(new Car("BMW"null));
                  Car car 
      = (Car)result.Next();
                  car.Snapshot();
                  db.Set(car);
                  RetrieveAllSensorReadouts(db);
              }

              
              
      public static void UpdateCollection(IObjectContainer db)
              
      {
                  IQuery query 
      = db.Query();
                  query.Constrain(
      typeof(Car));
                  IObjectSet result 
      = query.Descend("_history").Execute();
                  IList history 
      = (IList)result.Next();
                  history.RemoveAt(
      0);
                  db.Set(history);
                  Car proto 
      = new Car(nullnull);
                  result 
      = db.Get(proto);
                  
      foreach (Car car in result)
                  
      {    
                      
      foreach (object readout in car.History)
                      
      {
                          Console.WriteLine(readout);
                      }

                  }

              }

              
              
      public static void DeleteAllPart1()
              
      {
                  Db4oFactory.Configure().ObjectClass(
      typeof(Car)).CascadeOnDelete(true);
              }

              
      public static void DeleteAllPart2(IObjectContainer db)
              
      {
                  IObjectSet result 
      = db.Get(new Car(nullnull));
                  
      foreach (object car in result)
                  
      {
                      db.Delete(car);
                  }

                  IObjectSet readouts 
      = db.Get(new SensorReadout(null, DateTime.MinValue, null));
                  
      foreach (object readout in readouts)
                  
      {
                      db.Delete(readout);
                  }

              }

          }

      }




posted @ 2007-05-06 12:19  xxp  阅读(981)  评论(0编辑  收藏  举报
Surance Center