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 org.simantics.layer0.utils.writer;
14 import gnu.trove.list.array.TIntArrayList;
15 import gnu.trove.map.hash.TIntObjectHashMap;
16 import gnu.trove.map.hash.TObjectIntHashMap;
18 import java.util.ArrayList;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.WriteOnlyGraph;
25 import org.simantics.db.exception.DatabaseException;
27 public abstract class AbstractDelayedGraphWriter extends GraphWriterPartial {
29 protected int current = 0;
30 int externalCount = 0;
31 protected int internalCount = 0;
32 ArrayList<Resource> externals = new ArrayList<Resource>();
33 protected TObjectIntHashMap<Resource> externalsInv = new TObjectIntHashMap<Resource>();
34 protected TIntObjectHashMap<Resource> inverses = new TIntObjectHashMap<Resource>();
35 TIntArrayList timestamps = new TIntArrayList();
39 public long getResourceId(Session session, Resource r) {
40 if(r instanceof InternalResource) {
41 if(resourceIds == null) {
42 System.out.println("ERROR. Requesting resource id for new resource in writer before ids have been assigned.");
45 return resourceIds[((InternalResource)r).id-1];
47 return r.getResourceId();
51 protected static class InternalResource implements Resource {
55 public InternalResource(int id) {
60 public long getResourceId() {
65 public Resource get() {
70 public boolean isPersistent() {
75 public boolean equals(Object obj) {
77 (obj instanceof InternalResource &&
78 ((InternalResource)obj).id == id);
82 public boolean equalsResource(Resource other) {
87 public int hashCode() {
92 public int getThreadHash() {
97 public int compareTo(Resource o) {
100 if(o instanceof InternalResource)
101 return id - ((InternalResource)o).id;
108 public AbstractDelayedGraphWriter(ReadGraph graph) {
113 public GraphWriter create() {
114 current = ++internalCount;
120 public Resource get() {
122 return new InternalResource(current);
124 return externals.get(-1-current);
129 protected int getId(Resource r) {
130 if(r instanceof InternalResource)
131 return ((InternalResource)r).id;
132 int id = externalsInv.get(r);
134 id = -(++externalCount);
136 externalsInv.put(r, id);
142 public GraphWriter handle(Resource s) {
147 protected int getPredicateId(Resource p) throws DatabaseException {
149 if(!inverses.contains(pId))
150 if(!(p instanceof InternalResource))
151 inverses.put(pId, graph.getInverse(p));
155 public abstract void commit(IProgressMonitor monitor, WriteOnlyGraph wg) throws DatabaseException;