1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.layer0.utils.writer;
\r
14 import gnu.trove.list.array.TIntArrayList;
\r
15 import gnu.trove.map.hash.TIntObjectHashMap;
\r
16 import gnu.trove.map.hash.TObjectIntHashMap;
\r
18 import java.util.ArrayList;
\r
20 import org.eclipse.core.runtime.IProgressMonitor;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.Session;
\r
24 import org.simantics.db.WriteOnlyGraph;
\r
25 import org.simantics.db.exception.DatabaseException;
\r
27 public abstract class AbstractDelayedGraphWriter extends GraphWriterPartial {
\r
29 protected int current = 0;
\r
30 int externalCount = 0;
\r
31 protected int internalCount = 0;
\r
32 ArrayList<Resource> externals = new ArrayList<Resource>();
\r
33 protected TObjectIntHashMap<Resource> externalsInv = new TObjectIntHashMap<Resource>();
\r
34 protected TIntObjectHashMap<Resource> inverses = new TIntObjectHashMap<Resource>();
\r
35 TIntArrayList timestamps = new TIntArrayList();
\r
39 public long getResourceId(Session session, Resource r) {
\r
40 if(r instanceof InternalResource) {
\r
41 if(resourceIds == null) {
\r
42 System.out.println("ERROR. Requesting resource id for new resource in writer before ids have been assigned.");
\r
45 return resourceIds[((InternalResource)r).id-1];
\r
47 return r.getResourceId();
\r
51 protected static class InternalResource implements Resource {
\r
55 public InternalResource(int id) {
\r
60 public long getResourceId() {
\r
65 public Resource get() {
\r
70 public boolean isPersistent() {
\r
75 public boolean equals(Object obj) {
\r
76 return this==obj ||
\r
77 (obj instanceof InternalResource &&
\r
78 ((InternalResource)obj).id == id);
\r
82 public boolean equalsResource(Resource other) {
\r
83 return equals(other);
\r
87 public int hashCode() {
\r
92 public int getThreadHash() {
\r
97 public int compareTo(Resource o) {
\r
100 if(o instanceof InternalResource)
\r
101 return id - ((InternalResource)o).id;
\r
108 public AbstractDelayedGraphWriter(ReadGraph graph) {
\r
113 public GraphWriter create() {
\r
114 current = ++internalCount;
\r
120 public Resource get() {
\r
122 return new InternalResource(current);
\r
123 else if(current < 0)
\r
124 return externals.get(-1-current);
\r
129 protected int getId(Resource r) {
\r
130 if(r instanceof InternalResource)
\r
131 return ((InternalResource)r).id;
\r
132 int id = externalsInv.get(r);
\r
134 id = -(++externalCount);
\r
136 externalsInv.put(r, id);
\r
142 public GraphWriter handle(Resource s) {
\r
143 current = getId(s);
\r
147 protected int getPredicateId(Resource p) throws DatabaseException {
\r
148 int pId = getId(p);
\r
149 if(!inverses.contains(pId))
\r
150 if(!(p instanceof InternalResource))
\r
151 inverses.put(pId, graph.getInverse(p));
\r
155 public abstract void commit(IProgressMonitor monitor, WriteOnlyGraph wg) throws DatabaseException;
\r