KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > mono > SessionMonoImpl


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.se.impl.mono;
25
26 import org.objectweb.jalisto.se.JalistoFactory;
27 import org.objectweb.jalisto.se.api.ClassDescription;
28 import org.objectweb.jalisto.se.api.JalistoProperties;
29 import org.objectweb.jalisto.se.api.internal.DataWrapper;
30 import org.objectweb.jalisto.se.exception.IdentityException;
31 import org.objectweb.jalisto.se.impl.LogicalOid;
32 import org.objectweb.jalisto.se.impl.server.PhysicalOid;
33 import org.objectweb.jalisto.se.impl.server.SessionImpl;
34 import org.objectweb.jalisto.se.impl.trace.Trace;
35
36 public class SessionMonoImpl extends SessionImpl {
37     public SessionMonoImpl(JalistoProperties properties) {
38         super(properties);
39         trace.println(Trace.DEBUG, "CREATE A MONO SESSION, id = {0}", sessionId);
40     }
41
42     // return oid
43
public Object JavaDoc createObject(Object JavaDoc oid, Object JavaDoc[] objectToCreate) {
44         trace.println(Trace.SESSION, "{0} : createObject({1}, {2})", sessionId, oid, objectToCreate);
45         checkValidity("createObject", true);
46
47         DataWrapper datas = JalistoFactory.getInternalFactory().getNewDataWrapper(props, objectToCreate);
48         LogicalOid floid = getFloidFromOid(oid);
49         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
50         cache.put(floid, datas);
51         if (queryManager != null) {
52             ClassDescription meta = repository.getPersistentClassMetaDescription(fpoid.getClid());
53             queryManager.getIndexManager().createObject(meta, floid, objectToCreate);
54         }
55         fileAccess.insertDatas(sessionId, fpoid, datas);
56         oidTable.markAsCreated(sessionId, floid);
57         return floid;
58     }
59
60     public void deleteObjectByOid(Object JavaDoc oid) {
61         trace.println(Trace.SESSION, "{0} : deleteObjectByOid({1})", sessionId, oid);
62         checkValidity("deleteObjectByOid", true);
63
64         LogicalOid floid = getFloidFromOid(oid);
65         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
66         Object JavaDoc[] objectToRemove = null;
67         if (cache.containsKey(floid)) {
68             objectToRemove = ((DataWrapper) cache.remove(floid)).getDatas();
69         }
70         if (fpoid != null) {
71             fileAccess.deleteDatas(sessionId, fpoid);
72             if (queryManager != null) {
73                 ClassDescription meta = repository.getPersistentClassMetaDescription(fpoid.getClid());
74                 if (objectToRemove == null) {
75                     objectToRemove = fileAccess.readDatas(sessionId, fpoid).getDatas();
76                 }
77                 queryManager.getIndexManager().removeObject(meta, floid, objectToRemove);
78             }
79             oidTable.removeFloid(sessionId, floid);
80         }
81     }
82
83     // make new oid and put empty datas in base
84
public Object JavaDoc makeNewFileOid(Class JavaDoc objectClass) {
85         return makeNewFileOid(objectClass.getName());
86     }
87
88     public Object JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
89         trace.println(Trace.SESSION, "{0} : makeNewFileOid({1})", sessionId, objectClassName);
90         checkValidity("makeNewFileOid", true);
91         Object JavaDoc theClid = repository.getClidFromClassName(objectClassName);
92         LogicalOid floid = oidTable.allocateNewFloid(((Short JavaDoc) theClid).shortValue());
93         PhysicalOid fpoid = fileAccess.allocateNewFpoid(sessionId, theClid);
94         oidTable.insertFpoid(sessionId, floid, fpoid);
95         return floid.getClone();
96     }
97
98     public Object JavaDoc makeNewFileOid(LogicalOid floid) {
99         trace.println(Trace.SESSION, "{0} : makeNewFileOid({1})", sessionId, floid);
100         checkValidity("makeNewFileOid", true);
101         LogicalOid clone = floid.getClone();
102         Object JavaDoc theClid = new Short JavaDoc(clone.getClid());
103         PhysicalOid fpoid = fileAccess.allocateNewFpoid(sessionId, theClid);
104         oidTable.insertFpoid(sessionId, clone, fpoid);
105         return floid;
106     }
107
108     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid, boolean withCache) {
109         trace.println(Trace.SESSION, "{0} : readObjectByOid({1})", sessionId, oid);
110         checkValidity("readObjectByOid", true);
111
112         LogicalOid floid = getFloidFromOid(oid);
113         if (withCache && cache.containsKey(floid)) {
114             return ((DataWrapper) cache.get(floid)).getDatas();
115         } else {
116             PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
117             if (fpoid != null) {
118                 DataWrapper datas = fileAccess.readDatas(sessionId, fpoid);
119                 if (datas != null) {
120                     if (withCache) {
121                         cache.put(floid, datas);
122                     }
123                     return datas.getDatas();
124                 }
125             }
126             throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore");
127         }
128     }
129
130     public Object JavaDoc updateObjectByOid(Object JavaDoc oid, Object JavaDoc[] objectToUpdate) {
131         trace.println(Trace.SESSION, "{0} : readObjectByOid({1}, {2})", sessionId, oid, objectToUpdate);
132         checkValidity("updateObjectByOid", true);
133         checkSerialisable(objectToUpdate);
134
135         LogicalOid floid = getFloidFromOid(oid);
136         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
137         DataWrapper datas = JalistoFactory.getInternalFactory().getNewDataWrapper(props, objectToUpdate);
138         if (fpoid != null) {
139             PhysicalOid updatedFpoid = fileAccess.updateDatas(sessionId, fpoid, datas);
140             cache.put(floid, datas);
141             if (!updatedFpoid.equals(fpoid)) {
142                 oidTable.updatePoid(sessionId, floid, updatedFpoid);
143             }
144             if (queryManager != null) {
145                 ClassDescription meta = repository.getPersistentClassMetaDescription(fpoid.getClid());
146                 Object JavaDoc[] oldValue = fileAccess.readDatas(sessionId, fpoid).getDatas();
147                 queryManager.getIndexManager().updateObject(meta, floid, oldValue, objectToUpdate);
148             }
149             return floid.getClone();
150         }
151         throw new IdentityException("the given oid " + oid + " doesn't exist in this Jalisto datastore");
152     }
153 }
154
Popular Tags