KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > samples > basic2 > JalistoBookDemo


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.samples.basic2;
25
26 import org.objectweb.jalisto.se.impl.meta.type.MetaTypeFactory;
27 import org.objectweb.jalisto.se.impl.*;
28 import org.objectweb.jalisto.se.api.Session;
29 import org.objectweb.jalisto.se.api.Transaction;
30 import org.objectweb.jalisto.se.api.ClassDescription;
31 import org.objectweb.jalisto.samples.basic2.data.Book;
32
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 public class JalistoBookDemo {
37
38     public static void main(String JavaDoc[] args) {
39         String JavaDoc jalistoPropertiesFilename = "jalisto.properties";
40         int nbrBook = 5000;
41
42         try {
43             jalistoPropertiesFilename = args[0];
44             nbrBook = Integer.parseInt(args[1]);
45         } catch (Exception JavaDoc e) {
46         }
47
48         JalistoBookDemo demo = new JalistoBookDemo();
49         demo.initialise(jalistoPropertiesFilename);
50         demo.run(nbrBook);
51     }
52
53     public void initialise(String JavaDoc jalistoPropertiesFilename) {
54         // get the Jalisto working session
55
session = null;
56         // get the transaction associated with this session
57
tx = session.currentTransaction();
58
59         // define structure of persistent data
60
ClassDescription meta = JalistoFactory.createClassDescription(Book.class.getName());
61         meta.addField(JalistoFactory.createFieldDescription("title", MetaTypeFactory.StringType));
62         meta.addField(JalistoFactory.createFieldDescription("pages", MetaTypeFactory.IntegerType));
63         meta.addField(JalistoFactory.createFieldDescription("comments", MetaTypeFactory.StringType));
64         // define this structure in jalisto
65
session.defineClass(meta);
66     }
67
68     public void run(int nbrBook) {
69         long startTime;
70         long execTime;
71         Iterator JavaDoc iterator;
72
73         System.out.println("JalistoBookDemo is running :");
74
75         System.out.println("Create " + nbrBook + " books...");
76         startTime = System.currentTimeMillis();
77
78         tx.begin();
79         for (int i = 0; i < nbrBook; i++) {
80             Book book = Book.newBook();
81             Object JavaDoc[] o = book.toArray();
82             Object JavaDoc oid = session.createObject(o, Book.class);
83         }
84         tx.commit();
85
86         execTime = System.currentTimeMillis() - startTime;
87         System.out.println("ok ! It takes " + execTime + " ms.\n");
88
89
90         System.out.println("Get oids of all books...");
91         startTime = System.currentTimeMillis();
92
93         tx.begin();
94         Collection JavaDoc oids = session.getExtent(Book.class).readFully().collection();
95         tx.commit();
96
97         execTime = System.currentTimeMillis() - startTime;
98         System.out.println("ok ! It takes " + execTime + " ms.\n");
99
100
101         System.out.println("Add a comment to all books...");
102         startTime = System.currentTimeMillis();
103
104         tx.begin();
105         iterator = oids.iterator();
106         int i = 0;
107         while (iterator.hasNext()) {
108             Object JavaDoc oid = iterator.next();
109             Object JavaDoc[] o = session.readObjectByOid(oid);
110             Book book = Book.toBook(o);
111             book.setComments("comments number " + i);
112             session.updateObjectByOid(oid, book.toArray());
113             i++;
114         }
115         tx.commit();
116
117         execTime = System.currentTimeMillis() - startTime;
118         System.out.println("ok ! It takes " + execTime + " ms.\n");
119
120
121         System.out.println("Delete all books...");
122         startTime = System.currentTimeMillis();
123
124         tx.begin();
125         iterator = oids.iterator();
126         while (iterator.hasNext()) {
127             session.deleteObjectByOid(iterator.next());
128         }
129         tx.commit();
130
131         execTime = System.currentTimeMillis() - startTime;
132         System.out.println("ok ! It takes " + execTime + " ms.\n");
133
134         session.reorganize();
135     }
136
137     private Session session;
138     private Transaction tx;
139 }
140
Popular Tags