<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 閱讀(1351) 評論(0)  編輯  收藏


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


    網站導航:
     

    My Links

    Blog Stats

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精品免费大片一区二区| 亚洲日韩精品无码专区网站| AAA日本高清在线播放免费观看| 亚洲真人无码永久在线观看| 久久精品国产亚洲| 亚洲日韩精品无码专区网站| 在线看片人成视频免费无遮挡| 免费视频成人片在线观看| 一级日本高清视频免费观看| 亚洲国产成人久久综合| 亚洲av产在线精品亚洲第一站| 亚洲国产成人久久精品影视| a级亚洲片精品久久久久久久| 国产一级高清视频免费看| 国内一级一级毛片a免费| 成人免费午夜无码视频| 在线免费观看国产| 久久精品国产这里是免费| 99久久婷婷免费国产综合精品| 曰韩无码AV片免费播放不卡| 美女黄频a美女大全免费皮| 亚洲AV成人无码网天堂| 亚洲国产无线乱码在线观看 | 亚洲制服丝袜一区二区三区| 内射少妇36P亚洲区| 亚洲Av无码专区国产乱码DVD| 亚洲综合伊人久久综合| 狠狠综合久久综合88亚洲| 亚洲日韩国产精品乱| 国产亚洲AV夜间福利香蕉149| 亚洲情侣偷拍精品| 久久精品亚洲福利| 国产亚洲精久久久久久无码77777| 三上悠亚亚洲一区高清| 亚洲永久精品ww47| 国产亚洲欧洲精品| 亚洲天天做日日做天天看| 4480yy私人影院亚洲| 亚洲va在线va天堂va手机| 亚洲xxxx视频| 美女无遮挡免费视频网站|