]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/TGRemover.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / TGRemover.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.procedure.adapter.DirectStatementProcedure;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.util.ModelTransferableGraphSource;
24 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
25 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
26 import org.simantics.db.service.DirectQuerySupport;
27 import org.simantics.db.service.SerialisationSupport;
28 import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceProcedure;
29 import org.simantics.graph.representation.Identity;
30 import org.simantics.graph.representation.Internal;
31 import org.simantics.graph.representation.Root;
32 import org.simantics.layer0.Layer0;
33
34 /**
35  * @author Tuukka Lehtonen
36  */
37 public class TGRemover extends AbstractRemover {
38
39     @SuppressWarnings("unused")
40     private IProgressMonitor    monitor;
41
42     private ArrayList<Resource> roots = new ArrayList<>();
43
44     public TGRemover(Resource resource) {
45         super(resource);
46     }
47
48     public TGRemover(IProgressMonitor monitor, Resource resource) {
49         super(resource);
50         this.monitor = monitor;
51     }
52
53     public List<Resource> getRoots() {
54         return roots;
55     }
56
57     @Override
58     public void remove(final WriteGraph graph) throws DatabaseException {
59
60         Layer0 L0 = Layer0.getInstance(graph);
61
62         TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, resource);
63         conf.values = false;
64         final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
65         final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
66
67         try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf))) {
68
69             long[] rev = source.getResourceArray(graph);
70
71             source.forIdentities(graph, new TransferableGraphSourceProcedure<Identity>() {
72
73                 @Override
74                 public void execute(Identity value) throws Exception {
75                     if (value.definition instanceof Internal) {
76                         long res = rev[value.resource];
77                         Resource r = ss.getResource(res);
78                         Resource name = graph.getPossibleObject(r, L0.HasName);
79                         if (name != null) {
80                             graph.deny(r, L0.HasName, L0.NameOf, name);
81                             graph.denyValue(name);
82                             for (Statement stm : dqs.getDirectPersistentStatements(graph, name)) {
83                                 graph.deny(name, stm.getPredicate(), stm.getObject());
84                             }
85                         }
86                     } else if (value.definition instanceof Root) {
87                         long res = rev[value.resource];
88                         Resource r = ss.getResource(res);
89                         roots.add(r);
90                     }
91                 }
92
93             });
94
95             source.forResourceStatements(graph, new TransferableGraphSourceProcedure<int[]>() {
96
97                 @Override
98                 public void execute(int[] value) throws Exception {
99                     Resource s = ss.getResource(value[0]);
100                     Resource p = ss.getResource(value[1]);
101                     Resource i = null;
102                     if (value[2] != -1)
103                         i = ss.getResource(value[2]);
104                     Resource o = ss.getResource(value[3]);
105                     graph.deny(s, p, i, o);
106                 }
107
108             });
109
110             source.forValueResources(graph, new TransferableGraphSourceProcedure<int[]>() {
111
112                 @Override
113                 public void execute(int[] value) throws Exception {
114                     graph.denyValue(ss.getResource(value[0]));
115                 }
116
117             });
118
119         } catch (Exception e) {
120             throw new DatabaseException(e);
121         }
122
123     }
124
125 }