<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    linugb118--java space

    Java

    Object DataBase --DB4O之旅

    Object DataBase --DB4O之旅

    Object DataBase 出現有段時間了,最近對此有點興趣,想探索一下,于是開始了我的db4o discovery 之旅。

    db4o的jar 以及 api 在他document中都有寫到, 其實他的 example 在document中也能找到,但是因為本人
    也是在學習的過程,所以將用例也簡單描述一下:

    1.First Step:

    define an object Class, this is Pilot object;

    public class Pilot { ?
    ? private String name;
    ? private int points;
    ?
    ? public Pilot(String name,int points) {
    ? ? this.name=name;
    ? ? this.points=points;
    ? }
    ? ?
    ? public int getPoints() {
    ? ? return points;
    ? }
    ?
    ? public void addPoints(int points) {
    ? ? this.points+=points;
    ? }
    ?
    ? public String getName() {
    ? ? return name;
    ? }
    ?
    ? public String toString() {
    ? ? return name+"/"+points;
    ? }
    }

    the next target is how to store, retrieve , update, delete the instance of the Pilot Class.

    2.open database:

    [accessDb4o]
    ObjectContainer db=Db4o.openFile(Util.YAPFILENAME);
    try {
    // do something with db4o
    }
    finally {
    db.close();
    }

    db4o 中通過創建一個file 來建立database,這是一個二進制文件。這里就是通過 Db4o.openFile(..)來得到一個 ObjectContainer instance.
    ObjectContainer 就表示database。 實現對數據庫的操作后, 可以通過 close,來release all resource。

    3.store object

    [storeFirstPilot]
    Pilot pilot1=new Pilot("Michael Schumacher",100);
    db.set(pilot1);
    System.out.println("Stored "+pilot1);

    只需要簡單的調用set method 就可以了。

    4.retrieve object

    db4o 提供了三種 quering systems.
    Query by Example (QBE)
    Native Queries (NQ)
    SODA Query API (SODA)

    這里先介紹QBE:
    首先需要構造你需要query 的對象的 prototype

    To retrieve all pilots from our database, we provide an 'empty' prototype:
    [retrieveAllPilots]
    Pilot proto=new Pilot(null,0);
    ObjectSet result=db.get(proto);
    listResult(result);

    public static void listResult(ObjectSet result) {
    System.out.println(result.size());
    while(result.hasNext()) {
    System.out.println(result.next());
    }

    在 jdk 1.5中 可以這樣寫
    List <Pilot> pilots = db.query(Pilot.class);

    如果想查詢 pilot name is Bobbi, 可以構造這樣的prototype:
    Pilot proto=new Pilot("Bobbi",0);

    同樣如果想查詢 Point 是100的 可以這樣 construct prototype:
    Pilot proto=new Pilot(null,100);

    5.update object

    updating objects is just as easy as storing them,In fact, you can use retrieve a object and modify it, set it to database again.

    [updatePilot]
    ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
    Pilot found=(Pilot)result.next();
    found.addPoints(11);
    db.set(found);
    System.out.println("Added 11 points for "+found);
    retrieveAllPilots(db);

    6. Delete objects
    Objects are removed from the database using the delete() method.
    [deleteFirstPilotByName]
    ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
    Pilot found=(Pilot)result.next();
    PDF by iText, generated by Doctor, courtesy of db4objects Inc.
    db.delete(found);
    System.out.println("Deleted "+found);

    -------------------------------------------
    Query:

    對應數據庫來說,Query是很復雜 很重要的。
    剛才我們只是簡單的 做了一個QBE的 introduce。現在我們來進一步學習db4o的Query

    db4o 提供了三種 quering systems.
    Query by Example (QBE) 表示簡單的查詢,只做簡單的CRUD(Create Read Update Delete)
    Native Queries (NQ) 接口層的調用,是db4o query的主要用法。
    SODA Query API (SODA) 內部的接口層調用, 對db4o 更進一步的深入了解,可以做出更復雜的query,可以動態生成query

    其實對應一般的數據庫操作, 第二種 query (NQ) 才是最常用的。
    NQ:
    http://www.cs.utexas.edu/users/wcook/papers/NativeQueries/NativeQueries8-23-05.pdf
    http://www.cs.utexas.edu/users/wcook/papers/SafeQuery/SafeQueryFinal.pdf
    (所謂navtive queries 從字面上的意思,我們的查詢可以通過native language 來query 數據,而不需要象傳統的做法,需要develpment language
    和 query language 兩種語言來實現query。)
    NQ 看上去也 傳統的O/R mapping 看來,他更能進行對象直接的操作,但NQ 也有一個弊端,當我們需要檢查一段很復雜的logic,如果我們是用的傳統
    的JDBC 我們可以找出query string 直接通過db UI Tool 去check logic 的正確性,但是如果是NQ 那就不行了, 我們需要運行這段code, 那當然需
    要初始化涉及到的persistence Object, 這可能也是NQ 的很大的一個drawback。

    SODA Query API:

    The SODA query API is low level quering api, allowing direct access to nodes of query graphs. It can generate drynamic queries.

    [retrieveAllPilots]
    Query query=db.query();
    query.constrain(Pilot.class);
    ObjectSet result=query.execute();
    listResult(result);

    通過query method 來創建Query instance,然后通過 constrain instances 來 execute出 相應的result。
    a query graph made up of query nodes and constraints. A query node is a placeholder for a
    candidate object, a constraint decides whether to add or exclude candidates from the result.

    ////////////////////////////////
    //Three query method example
    ///////////////////////////////

    //QBE
    just do CRUD simple operation, The code show above.

    //NQ
    public static void retrieveComplexNQ(ObjectContainer db) {
    ObjectSet result=db.query(new Predicate() {
    public boolean match(Pilot pilot) {
    return pilot.getPoints()>99
    && pilot.getPoints()<199
    || pilot.getName().equals("Rubens Barrichello");
    }
    });
    listResult(result);
    }

    //SODA
    public static void retrieveComplexSODA(ObjectContainer db) {
    Query query=db.query();
    query.constrain(Pilot.class);
    Query pointQuery=query.descend("points");
    query.descend("name").constrain("Rubens Barrichello")
    .or(pointQuery.constrain(new Integer(99)).greater()
    .and(pointQuery.constrain(new
    Integer(199)).smaller()));
    ObjectSet result=query.execute();
    listResult(result);
    }
    4.Structed Object

    如果一個對象中不僅僅有常見的java類型還包含其他的對象,那么在modify 被包含的對象的時候,就會出現一個問題。如果我更新了這些被包含的對象,那么在update delete 的時候,他們會不會被操作到?db4o中 提供了一個 depth的概念。所有object 的默認的update depth為1,這就意味著該object 中的primitive 和 string members 能被update,其他類型的對象將不被upda
    te。 同樣對應delete 如果想實現 對象中的遞歸刪除, 那同樣需要利用db4o中的 delete depth


    這是update depth:
    [updatePilotSeparateSessionsImprovedPart1]
    Db4o.configure().objectClass("com.db4o.f1.chapter2.Car")
    .cascadeOnUpdate(true);

    這是delete depth:
    [deleteDeepPart1]
    Db4o.configure().objectClass("com.db4o.f1.chapter2.Car")
    .cascadeOnDelete(true);

    Again: Note that all configuration must take place before the ObjectContainer is opened.

    其實這里還沒有結束,對于delete 會出現這么一個問題,當我通過 delet depth 將一個instance 刪除了,他里面包含的某個其他類型的object instance 也被刪除了,但是這個
    對象還在被其他對象引用,那這個問題怎么辦? 現在db4o 還沒有解決方法。我們現在只能
    小心操作delete。



    5. Transactions
    對應數據庫來說,transaction 是必須的,下面我們來看看object dataBase--db4o是怎么
    來處理transcation的。

    當open a container的時候,一個transaction 就隱型地開始了,而當close 這個container,
    那么當前的transaction就隱型地提交了。

    那么如果顯型地commit 和 rollback呢? 下面有這么兩個例子:

    public static void storeCarCommit(ObjectContainer db) {
    Pilot pilot=new Pilot("Rubens Barrichello",99);
    Car car=new Car("BMW");
    car.setPilot(pilot);
    db.set(car);
    db.commit();
    }


    public static void storeCarRollback(ObjectContainer db) {
    Pilot pilot=new Pilot("Michael Schumacher",100);
    Car car=new Car("Ferrari");
    car.setPilot(pilot);
    db.set(car);
    db.rollback();
    }

    與JDBC相比,好像有一個區別就是,db4o不需要來設置commit的模式, setAutoCommit()

    她就是如果db.close(),那么就autoCommit了。

    對于Object Database,因為他們都是用object 來存取,那么當object 被set 相應的值后,但沒有save到database,也就說
    current transaction 被rollback,那么為了保持數據的一致性,需要refresh live object.那么這個怎么來實現呢?

    public static void carSnapshotRollbackRefresh(ObjectContainer db)
    {
    ObjectSet result=db.get(new Car("BMW"));
    Car car=(Car)result.next();
    car.snapshot();
    db.set(car);
    db.rollback();
    PDF by iText, generated by Doctor, courtesy of db4objects Inc.
    db.ext().refresh(car,Integer.MAX_VALUE);
    System.out.println(car);
    }

    6.Embedded server
    對應 Embedded server,open一個port為0的server。

    [accessLocalServer]
    ObjectServer server=Db4o.openServer(Util.YAPFILENAME,0);
    try {
    PDF by iText, generated by Doctor, courtesy of db4objects Inc.
    ObjectContainer client=server.openClient();
    // Do something with this client, or open more clients
    client.close();
    }
    finally {
    server.close();
    }

    7.Networking

    [accessRemoteServer]
    ObjectServer server=Db4o.openServer(Util.YAPFILENAME,PORT);
    server.grantAccess(USER,PASSWORD);
    try {
    ObjectContainer
    client=Db4o.openClient("localhost",PORT,USER,PASSWORD);
    // Do something with this client, or open more clients
    client.close();
    }
    finally {
    server.close();
    }

    posted on 2006-11-29 13:21 linugb118 閱讀(1355) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    My Links

    Blog Stats

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 在线观看视频免费完整版| 人妻在线日韩免费视频| 妞干网免费视频观看| 亚洲一区二区三区免费视频| 老司机永久免费网站在线观看| 亚洲国产美女视频| 无码国产精品一区二区免费I6| 亚洲专区中文字幕| 91精品全国免费观看青青| 国产精品免费观看| 中文字幕在线日亚洲9| 免费涩涩在线视频网| 国产一区二区三区亚洲综合| 国产国产成年年人免费看片| 亚洲精品午夜在线观看| 五月婷婷综合免费| 国产成人精品日本亚洲语音| 亚洲日韩VA无码中文字幕| 亚洲va精品中文字幕| 成人黄18免费视频| 黄色一级毛片免费| 日本高清免费中文字幕不卡| 美女视频黄.免费网址| 亚洲夜夜欢A∨一区二区三区| 美女露100%胸无遮挡免费观看| 国产伦精品一区二区三区免费下载 | 国产精品日本亚洲777| 亚洲精品线路一在线观看| 水蜜桃视频在线观看免费播放高清| 亚洲国产综合91精品麻豆| 拨牐拨牐x8免费| 国产精品无码免费专区午夜| 午夜国产羞羞视频免费网站| 丝袜足液精子免费视频| 亚洲日韩精品无码专区网站| 日韩免费在线视频| 亚洲精品GV天堂无码男同| 好爽…又高潮了免费毛片 | 中文在线日本免费永久18近| 亚洲日韩国产精品无码av| 免费永久在线观看黄网站|