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;
15 import gnu.trove.map.hash.TIntIntHashMap;
16 import gnu.trove.map.hash.TIntLongHashMap;
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.nio.channels.FileChannel;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.WriteOnlyGraph;
32 import org.simantics.db.exception.DatabaseException;
34 public class DelayedGraphWriter extends AbstractDelayedGraphWriter {
37 protected ObjectOutputStream s;
38 TIntIntHashMap clusterHints = new TIntIntHashMap();
39 TIntLongHashMap clusterIds = new TIntLongHashMap();
41 public DelayedGraphWriter(ReadGraph graph) {
44 file = File.createTempFile("graph", ".tmp");
45 System.out.println("Temp file: " + file.getAbsolutePath());
46 s = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file), 1024*1024));
47 // s = new ObjectOutputStream(backup);
50 } catch (IOException e) {
51 throw new RuntimeException("Opening output stream to temporary file failed", e);
55 private void writeRef(int ref) throws IOException {
57 timestamps.set(ref-1, time++);
62 public GraphWriter let(Resource p, Resource o) throws DatabaseException {
68 writeRef(getPredicateId(p));
70 } catch (IOException e) {
71 throw new RuntimeException("Writing statement failed.", e);
77 public GraphWriter let(Resource p, Object value, Resource dataType) throws DatabaseException {
79 assert(value != null);
80 assert(dataType != null);
86 writeRef(internalCount);
87 s.writeUnshared(value);
88 writeRef(getId(dataType));
92 writeRef(getPredicateId(p));
93 writeRef(internalCount);
94 } catch (IOException e) {
95 throw new RuntimeException("Writing statement failed.", e);
101 public GraphWriter let(int clusterHint, Resource p, Object value,
102 Resource dataType) throws DatabaseException {
103 let(p, value, dataType);
104 clusterHints.put(internalCount, clusterHint);
109 public GraphWriter createLiteral(Object value, Resource dataType) {
110 assert(value != null);
111 assert(dataType != null);
117 writeRef(internalCount);
118 s.writeUnshared(value);
119 writeRef(getId(dataType));
121 current = internalCount;
122 } catch (IOException e) {
123 throw new RuntimeException("Writing statement failed.", e);
129 public GraphWriter createLiteral(int clusterHint, Object value,
131 createLiteral(value, dataType);
133 clusterHints.put(current, clusterHint);
138 public GraphWriter createInverse(int cluster, Resource r) {
145 } catch (IOException e) {
146 throw new RuntimeException("Writing statement failed.", e);
152 public GraphWriter createInverse(Resource r) {
159 } catch (IOException e) {
160 throw new RuntimeException("Writing statement failed.", e);
165 protected Resource getResource(WriteOnlyGraph wg, Resource[] internals, long[] resourceIds, int id) {
167 Resource ret = internals[id-1];
169 throw new Error("Error");
170 // ret = wg.newResource(types.get(id));
171 // internals[id-1] = ret;
172 // resourceIds[id-1] = ret.getResourceId();
174 if(timestamps.getQuick(id-1)==time)
175 internals[id-1] = null;
180 return externals.get(-1-id);
187 public void commit(IProgressMonitor monitor, WriteOnlyGraph wg) throws DatabaseException {
196 monitor.beginTask("", 100);
198 int lastPercentageDone = 0;
199 long fileLength = file.length();
201 // System.out.println("size of commit file: " + fileLength);
203 Resource[] internals = new Resource[internalCount];
204 resourceIds = new long[internalCount];
207 FileInputStream fis = new FileInputStream(file);
208 FileChannel fc = fis.getChannel();
209 ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(fis, 1024*1024));
210 int resourceCounter = 0;
211 int statementCounter = 0;
212 int valueCounter = 0;
219 int s = is.readInt();
220 int p = is.readInt();
221 int o = is.readInt();
222 Resource rs = getResource(wg, internals, resourceIds, s);
223 Resource rp = getResource(wg, internals, resourceIds, p);
224 Resource ro = getResource(wg, internals, resourceIds, o);
225 Resource rpInv = inverses.get(p);
226 wg.claim(rs, rp, rpInv, ro);
227 statementCounter += 2;
230 int id = is.readInt();
231 Object value = is.readUnshared();
232 int type = is.readInt();
234 Resource r = newResource(wg, internals, id);
235 wg.claim(r, l0.InstanceOf, null, getResource(wg, internals, resourceIds, type));
236 wg.claimValue(r, value);
247 int s = is.readInt();
248 int t = is.readInt();
250 Resource type = getResource(wg, internals, resourceIds, t);
251 wg.claim(newResource(wg, internals, s), l0.InstanceOf, null, type);
258 int s = is.readInt();
259 newResource(wg, internals, s);
263 case 5: { // InverseOf
265 int r1 = is.readInt();
266 int r2 = is.readInt();
268 Resource rr1 = getResource(wg, internals, resourceIds, r1);
269 Resource rr2 = getResource(wg, internals, resourceIds, r2);
270 wg.claim(rr1, l0.InverseOf, l0.InverseOf, rr2);
271 statementCounter += 2;
273 inverses.put(r1, rr2);
274 inverses.put(r2, rr1);
279 // if((counter % 200000) == 0) {
280 // System.out.println("Written " + counter + " statements.");
283 double percentageDone = 100.0 * (double) fc.position() / (double) fileLength;
284 int newPercentageDone = (int) Math.round(percentageDone);
285 if(newPercentageDone > lastPercentageDone) {
286 monitor.setTaskName("Writing database (" + newPercentageDone + "%)");
287 monitor.worked(newPercentageDone - lastPercentageDone);
288 lastPercentageDone = newPercentageDone;
292 System.out.println("clusterIds.size() = " + clusterIds.size());
294 System.out.println("Wrote " + resourceCounter + " resources, " + statementCounter + " statements and " + valueCounter + " values.");
296 } catch (IOException e) {
297 throw new RuntimeException("Commiting delayed graph writings failed.", e);
298 } catch (ClassNotFoundException e) {
299 throw new RuntimeException("Commiting delayed graph writings failed.", e);
306 private Resource newResource(WriteOnlyGraph wg, Resource[] internals, int s) throws DatabaseException {
307 int clusterHint = clusterHints.get(s);
311 r = wg.newResource();
313 long clusterId = clusterIds.get(clusterHint);
316 r = wg.newResource();
317 clusterIds.put(clusterHint, clustering.getCluster(r));
320 r = wg.newResource(clusterId);
323 resourceIds[s-1] = r.getResourceId();
327 // public ValueDescriptor createDescriptor(Object obj) {
329 // if(obj instanceof String[])
330 // return new ValueDescriptor(ValueTrait.StaticValue, (String[])obj);
331 // else if(obj instanceof int[])
332 // return new ValueDescriptor(ValueTrait.StaticValue, (int[])obj);
333 // else if(obj instanceof double[])
334 // return new ValueDescriptor(ValueTrait.StaticValue, (double[])obj);
335 // else if(obj instanceof float[])
336 // return new ValueDescriptor(ValueTrait.StaticValue, (float[])obj);
337 // else if(obj instanceof boolean[])
338 // return new ValueDescriptor(ValueTrait.StaticValue, (boolean[])obj);
339 // else if(obj instanceof long[])
340 // return new ValueDescriptor(ValueTrait.StaticValue, (long[])obj);
341 // else if(obj instanceof byte[])
342 // return new ValueDescriptor(ValueTrait.StaticValue, (byte[])obj);
344 // else if(obj instanceof String)
345 // return new ValueDescriptor(ValueTrait.StaticValue, new String[] {(String)obj});
346 // else if(obj instanceof Double)
347 // return new ValueDescriptor(ValueTrait.StaticValue, new double[] {(Double)obj});
348 // else if(obj instanceof Float)
349 // return new ValueDescriptor(ValueTrait.StaticValue, new float[] {(Float)obj});
350 // else if(obj instanceof Integer)
351 // return new ValueDescriptor(ValueTrait.StaticValue, new int[] {(Integer)obj});
352 // else if(obj instanceof Boolean)
353 // return new ValueDescriptor(ValueTrait.StaticValue, new boolean[] {(Boolean)obj});
354 // else if(obj instanceof Byte)
355 // return new ValueDescriptor(ValueTrait.StaticValue, new byte[] {(Byte)obj});
356 // else if(obj instanceof Long)
357 // return new ValueDescriptor(ValueTrait.StaticValue, new long[] {(Long)obj});
359 // throw new Error("Wrong type!");
364 public GraphWriter create() {
366 current = ++internalCount;
372 } catch (IOException e) {
373 throw new RuntimeException("Writing statement failed.", e);
381 public GraphWriter create(int clusterHint) {
384 clusterHints.put(current, clusterHint);
390 public GraphWriter create(Resource type) {
391 assert(type != null);
393 current = ++internalCount;
399 s.writeInt(getId(type));
400 } catch (IOException e) {
401 throw new RuntimeException("Writing statement failed.", e);
409 public GraphWriter create(int clusterHint, Resource type) {
412 clusterHints.put(current, clusterHint);
418 public Resource get() {
420 return new InternalResource(current);
422 return externals.get(-1-current);
428 public GraphWriter handle(Resource s) {
435 public GraphWriter flush() {
438 } catch(IOException e) {
439 throw new RuntimeException("Writing flush failed.", e);