1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package fi.vtt.simantics.procore.internal;
16 //public abstract class ResourceImpl implements Resource {
17 // public final static ResourceImpl[] NONE = new ResourceImpl[0];
20 //class ResourceProxy {
21 // private final int offset;
22 // private final int size;
23 // private WeakReference<ResourceImpl> weakResource = null;
24 // private ResourceImplModified modifiedResourceImpl = null;
25 // private Value_ value;
26 // ResourceProxy(int offset, int size) {
27 // this.offset = offset;
33 // private ResourceImpl getResourceImplModified(Cluster cluster, long subject) {
35 // if (null == weakResource || null == (r = weakResource.get())) {
36 // if (null == modifiedResourceImpl)
37 // modifiedResourceImpl = new ResourceImplModified(cluster, offset, size, value);
38 // r = new ResourceImpl(subject, modifiedResourceImpl);
39 // weakResource = new WeakReference<ResourceImpl>(r);
40 // } else if (!r.isModified()) {
41 // if (null == modifiedResourceImpl)
42 // modifiedResourceImpl = new ResourceImplModified(cluster, offset, size, value);
43 // r.setImpl(modifiedResourceImpl);
47 // void setValue(Value_ value) {
48 // this.value = value;
50 // ResourceImpl addRelation(Cluster cluster, long subject, long predicate, long predicateCluster, long object, long objectCluster) {
51 // ResourceImpl r = getResourceImplModified(cluster, subject);
52 // return r.addRelation(predicate, predicateCluster, object, objectCluster);
54 // void removeRelation(Cluster cluster, long subject, long predicate, long predicateCluster, long object, long objectCluster) throws StatementNotFoundException {
55 // ResourceImpl r = getResourceImplModified(cluster, subject);
56 // r.removeRelation(predicate, predicateCluster, object, objectCluster);
58 // ResourceImpl getResource(Cluster cluster, long id) {
60 // if (null != weakResource) {
61 // r = weakResource.get();
66 // if (null == modifiedResourceImpl)
67 // modifiedResourceImpl = new ResourceImplModified(cluster, offset, size, value);
68 // r = new ResourceImpl(id, modifiedResourceImpl);
70 // r = new ResourceImpl(id, new ResourceImplFixed(cluster, offset, size, value));
71 // weakResource = new WeakReference<ResourceImpl>(r);
74 // ResourceImpl getResourceImpl(long id, ResourceImplModified impl) {
76 // assert(null == modifiedResourceImpl);
77 // assert(null == weakResource);
78 // modifiedResourceImpl = impl;
79 // r = new ResourceImpl(id, impl);
80 // weakResource = new WeakReference<ResourceImpl>(r);
84 // if (null == weakResource)
86 // weakResource = null;
87 // modifiedResourceImpl = null;
91 //abstract class ResourceImplAbstract {
92 // abstract void init(ResourceImpl self);
93 // abstract void dispose();
94 // abstract Value_ getValue(ResourceImpl self);
95 // abstract long getClusterId();
96 // abstract boolean isModified();
97 // abstract int getNumberOfRelations(ResourceImpl self);
98 // abstract StatementImpl[] getStatements(ResourceImpl self) throws ResourceNotFoundException;
99 // abstract void addRelation(ResourceImpl self, long predicate, long predicateCluster, long object, long objectCluster);
100 // abstract void removeRelation(long predicate, long predicateCluster, long object, long objectCluster) throws StatementNotFoundException;
101 // abstract void addValue(ResourceImpl self, Value_ value); // TODO: Remove return value, inefficient.
104 //abstract class ResourceImplCluster extends ResourceImplAbstract {
105 // private Cluster cluster;
106 // protected Value_ value;
107 // protected Value_ valueOld = null;
108 // protected Cluster getCluster() {
111 // ResourceImplCluster(Cluster cluster) {
112 // this.cluster = cluster;
113 // cluster.incReferenceCount();
115 // void setCluster(Cluster c) {
119 // void init(ResourceImpl self) {
123 // cluster.decReferenceCount();
126 // long getClusterId() {
127 // return cluster.getId();
130 // Value_ getValue(ResourceImpl self) {
133 // protected void setValue(Value_ value) {
134 // this.value = this.valueOld = value;
136 // void moveValue() {
139 // void removeValue() {
142 // void addValue(ResourceImpl self, Value_ value) { // TODO: Remove return value, inefficient.
143 // this.value = value;
147 //class ResourceImplFixed extends ResourceImplCluster {
148 // final int fixedOffset;
149 // final int fixedSize;
150 // public ResourceImplFixed(Cluster cluster, int offset, int size, Value_ value) {
152 // this.fixedOffset = offset;
153 // this.fixedSize = size;
157 // int getNumberOfRelations(ResourceImpl self) {
161 // StatementImpl[] getStatements(ResourceImpl self) throws ResourceNotFoundException {
162 // StatementImpl[] stms = new StatementImpl[fixedSize];
163 // GraphSession gs = this.getCluster().client;
164 // int end = fixedOffset + fixedSize;
165 // for (int i=fixedOffset, j=0; i<end; ++i, ++j) {
166 // ResourceImpl p = gs.getResourceOrThrow(getCluster().raw.predicates[i], getCluster().raw.predicateClusters[i]);
167 // stms[j] = new StatementImplNew(self, p, getCluster().raw.objects[i], getCluster().raw.objectClusters[i], gs);
168 //// ResourceImpl o = gs.getResourceOrThrow(getCluster().raw.objects[i], getCluster().raw.objectClusters[i]);
169 //// stms[j] = new StatementImplOld(self, p, o);
174 // boolean isModified() {
178 // void addRelation(ResourceImpl self, long predicate, long predicateCluster, long object, long objectCluster) {
179 // throw new Error("Not allowed");
182 // void removeRelation(long predicate, long predicateCluster, long object, long objectCluster) {
183 // throw new Error("Not allowed");
187 //class ResourceImplModifiedStatement {
189 // long predicateCluster;
191 // long objectCluster;
192 // ResourceImplModifiedStatement(long predicate, long predicateCluster, long object, long objectCluster) {
193 // this.predicate = predicate;
194 // this.predicateCluster = predicateCluster;
195 // this.object = object;
196 // this.objectCluster = objectCluster;
199 // public int hashCode() {
200 // return (int)(object^(object>>32)^predicate^(predicate>>>32));
203 // public boolean equals(Object obj) {
206 // else if (obj == null)
208 // else if (ResourceImplModifiedStatement.class != obj.getClass())
210 // ResourceImplModifiedStatement d = (ResourceImplModifiedStatement)obj;
211 // return (d.object == object) && (d.predicate == predicate);
215 //class ResourceImplModified extends ResourceImplCluster {
216 // static ResourceImplModifiedStatement findStatement = new ResourceImplModifiedStatement(0, 0, 0, 0);
217 // THashSet<ResourceImplModifiedStatement> statements = new THashSet<ResourceImplModifiedStatement>();
218 // THashSet<ResourceImplModifiedStatement> reclusteringStatements = new THashSet<ResourceImplModifiedStatement>();
219 // public ResourceImplModified(Cluster cluster, int offset, int size, Value_ value) {
222 // statements.ensureCapacity(size);
223 // for (int i=0,j=offset; i<size; ++i,++j) {
224 // assert(Constants.ReservedClusterId != cluster.raw.predicateClusters[j]);
225 // assert(Constants.ReservedClusterId != cluster.raw.objectClusters[j]);
226 // statements.add(new ResourceImplModifiedStatement(cluster.raw.predicates[j], cluster.raw.predicateClusters[j], cluster.raw.objects[j], cluster.raw.objectClusters[j]));
229 // void moveStatements(HashMap<Long, Long> oldResourceIdToNewClusterIdMap) {
230 // for (ResourceImplModifiedStatement s : reclusteringStatements) {
231 // statements.remove(s);
232 // if (Constants.ReservedClusterId == s.predicateCluster)
233 // s.predicateCluster = oldResourceIdToNewClusterIdMap.get(s.predicate);
234 // if (Constants.ReservedClusterId == s.objectCluster)
235 // s.objectCluster = oldResourceIdToNewClusterIdMap.get(s.object);
236 // statements.add(s);
238 // reclusteringStatements.clear();
241 // void removeStatements() {
242 // for (ResourceImplModifiedStatement s : reclusteringStatements) {
243 // statements.remove(s);
245 // reclusteringStatements.clear();
249 // int getNumberOfRelations(ResourceImpl self) {
250 // return statements.size();
253 // boolean isModified() {
254 // return true; // HACK: This is not necessarily true. In creation no modifications given and modifications can cancel each other out.
257 // StatementImpl[] getStatements(ResourceImpl self) throws ResourceNotFoundException {
258 // int size = statements.size();
259 // StatementImpl[] stms = new StatementImpl[size];
260 // GraphSession gs = getCluster().client;
262 // for (ResourceImplModifiedStatement s : statements) {
264 // if (Constants.ReservedClusterId == s.predicateCluster)
265 // p = this.getCluster().getResourceReservedOrThrow(Constants.ReservedClusterId, s.predicate);
267 // p = gs.getResourceOrThrow(s.predicate, s.predicateCluster);
268 // if (Constants.ReservedClusterId == s.objectCluster) {
269 // ResourceImpl o = this.getCluster().getResourceReservedOrThrow(Constants.ReservedClusterId, s.object);
270 // stms[++i] = new StatementImplOld(self, p, o);
272 // stms[++i] = new StatementImplNew(self, p, s.object, s.objectCluster, gs);
276 // private boolean find(long predicate, long predicateCluster, long object, long objectCluster) {
277 // findStatement.predicate = predicate;
278 // findStatement.predicateCluster = predicateCluster;
279 // findStatement.object = object;
280 // findStatement.objectCluster = objectCluster;
281 // return statements.contains(findStatement);
284 // void addRelation(ResourceImpl self, long predicate, long predicateCluster, long object, long objectCluster) {
285 // if (find(predicate, predicateCluster, object, objectCluster))
287 // //System.out.println(self + "." + getNumberOfRelations(self) + ": " + predicate + "." + predicateCluster + " " + object + "." + objectCluster);
288 // ResourceImplModifiedStatement s = new ResourceImplModifiedStatement(predicate, predicateCluster, object, objectCluster);
289 // statements.add(s);
290 // if (Constants.ReservedClusterId == predicateCluster ||
291 // Constants.ReservedClusterId == objectCluster) {
292 // reclusteringStatements.add(s);
293 // getCluster().addModifiedResource(self);
297 // void removeRelation(long predicate, long predicateCluster, long object, long objectCluster) throws StatementNotFoundException {
298 // findStatement.predicate = predicate;
299 // findStatement.predicateCluster = predicateCluster;
300 // findStatement.object = object;
301 // findStatement.objectCluster = objectCluster;
302 // if (!statements.remove(findStatement))
303 // throw new StatementNotFoundException();
304 // reclusteringStatements.remove(findStatement);
308 //class ResourceImplChangeSet extends ResourceImplAbstract {
309 // private SessionImplHistorical session;
310 // private long cluster;
311 // private ResourceImpl original;
312 // public ResourceImplChangeSet(SessionImplHistorical session, long cluster) {
313 // this.session = session;
314 // this.cluster = cluster;
317 // void init(ResourceImpl self) {
318 // Cluster c = session.getCluster(cluster);
320 // this.original = c.getResource(self.id);
326 // long getClusterId() {
330 // Value_ getValue(ResourceImpl self) {
331 // Value_ value = null;
332 // if (null != original)
333 // value = original.getValue();
334 // return session.getValue(self.id, value);
337 // int getNumberOfRelations(ResourceImpl self) {
339 // if (null != original)
340 // n = original.getNumberOfRelations();
341 // n += session.getNumberOfAddedRelations(self.id);
342 // n -= session.getNumberOfRemovedRelations(self.id);
346 // StatementImpl[] getStatements(ResourceImpl self) throws ResourceNotFoundException {
347 // StatementImpl[] stms = StatementImpl.NONE;
348 // if (null != original)
349 // stms = original.getStatements();
350 // return session.getStatements(self.id, stms);
353 // boolean isModified() {
357 // void addValue(ResourceImpl self, Value_ value) {
358 // throw new Error("Not allowed");
361 // void addRelation(ResourceImpl self, long predicate, long predicateCluster, long object, long objectCluster) {
362 // throw new Error("Not allowed");
365 // void removeRelation(long predicate, long predicateCluster, long object, long objectCluster) {
366 // throw new Error("Not allowed");
370 //class ResourceImplClusterNew extends ResourceImplAbstract {
371 // //private Cluster clusterOld;
372 // private final long id;
373 // private org.simantics.db.procore.cluster.Cluster cluster;
374 // protected Cluster getCluster() {
375 // throw new Error("No can do.");
377 // ResourceImplClusterNew(long id, org.simantics.db.procore.cluster.Cluster cluster) {
379 // this.cluster = cluster;
381 // final org.simantics.db.procore.cluster.Cluster getClusterNew() {
384 // void setCluster(Cluster c) {
385 // throw new Error("No can do.");
388 // void init(ResourceImpl self) {
394 // long getClusterId() {
395 // return cluster.getId();
398 // Value_ getValue(ResourceImpl self) {
399 // byte[] value = cluster.getValue(id);
400 // return new Zany(value);
402 // protected void setValue(Value_ value) {
403 // throw new Error("No can do.");
405 // void moveValue() {
406 // throw new Error("No can do.");
408 // void removeValue() {
409 // throw new Error("No can do.");
411 // void addValue(ResourceImpl self, Value_ value) { // TODO: Remove return value, inefficient.
412 // throw new Error("No can do.");
415 // void addRelation(ResourceImpl self, long predicate, long predicateCluster,
416 // long object, long objectCluster) {
417 // throw new Error("No can do.");
420 // int getNumberOfRelations(ResourceImpl self) {
421 // throw new Error("No can do.");
424 // StatementImpl[] getStatements(ResourceImpl self) {
425 // throw new Error("No can do.");
428 // boolean isModified() {
429 // throw new Error("No can do.");
432 // void removeRelation(long predicate, long predicateCluster, long object,
433 // long objectCluster) throws StatementNotFoundException {
434 // throw new Error("No can do.");