1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.HashMap;
7 import java.util.Stack;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.Session;
13 import org.simantics.db.Statement;
14 import org.simantics.db.VirtualGraph;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.interop.test.GraphChanges;
23 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
26 public abstract class UpdateOperations {
28 private List<UpdateOp> operations = new ArrayList<UpdateOp>();
29 private Map<Resource, UpdateOp> resourceMap = new HashMap<Resource, UpdateOp>();
30 private Map<Statement, UpdateOp> statementMap = new HashMap<Statement, UpdateOp>();
31 private GraphChanges changes;
32 private int chunkSize = -1;
34 public UpdateOperations(GraphChanges changes) {
35 this.changes = changes;
38 public UpdateOp getUpdateOp(Resource r) {
39 return resourceMap.get(r);
42 public UpdateOp getUpdateOp(Statement s) {
43 return statementMap.get(s);
46 public int getChunkSize() {
50 public void setChunkSize(int chunkSize) {
51 this.chunkSize = chunkSize;
55 * Applies all changes.
58 * @throws DatabaseException
60 public void applyAll(WriteGraph graph) throws DatabaseException {
61 List<UpdateOp> list = operations;
66 * Applies selected changes.
68 * @throws DatabaseException
70 public void applySelected(WriteGraph graph) throws DatabaseException {
71 List<UpdateOp> list = new ArrayList<UpdateOp>();
72 for (UpdateOp op : operations) {
80 * Applies all changes with chunked DB writes.
83 * @throws DatabaseException
85 public void applyAll(Session session, VirtualGraph vg) throws DatabaseException {
86 List<UpdateOp> list = operations;
87 apply(session, list, vg);
91 * Applies selected changes with chunked DB writes.
94 * @throws DatabaseException
96 public void applySelected(Session session, VirtualGraph vg) throws DatabaseException {
97 List<UpdateOp> list = new ArrayList<UpdateOp>();
98 for (UpdateOp op : operations) {
102 apply(session, list, vg);
105 protected void apply(WriteGraph graph, List<UpdateOp> list) throws DatabaseException {
106 for (UpdateOp op : list) {
111 protected void apply(Session session, List<UpdateOp> list, VirtualGraph vg) throws DatabaseException {
112 if (getChunkSize() > 0) {
113 for (int s = 0; s < list.size(); ) {
114 int e = s + getChunkSize();
117 List<UpdateOp> subList = list.subList(s, e);
118 session.syncRequest(new WriteRequest(vg) {
121 public void perform(WriteGraph graph) throws DatabaseException {
123 for (UpdateOp op : subList) {
131 session.syncRequest(new WriteRequest(vg) {
134 public void perform(WriteGraph graph) throws DatabaseException {
136 for (UpdateOp op : list) {
144 public List<UpdateOp> getOperations() {
148 public GraphChanges getChanges() {
152 public Map<Resource, UpdateOp> getResourceMap() {
156 public Map<Statement, UpdateOp> getStatementMap() {
160 private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
161 Stack<UpdateOp> stack = new Stack<UpdateOp>();
162 _apply(g, stack, op);
165 private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
168 if (stack.contains(op)) {
173 for (UpdateOp pop : op.getParentOps())
174 if (op.requiresOp(pop)) {
176 _apply(g, stack, pop);
178 for (UpdateOp sop : op.getSubOps())
179 if (op.requiresOp(sop)) {
181 _apply(g, stack, sop);
187 protected List<UpdateOp> getOps() {
188 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
189 list.addAll(operations);
193 protected void addOp(Resource r, UpdateOp op) {
194 resourceMap.put(r, op);
198 protected void addOp(Statement s, UpdateOp op) {
199 statementMap.put(s, op);
203 protected void replaceOp(Resource r, UpdateOp op) {
204 UpdateOp oldOp = resourceMap.remove(r);
206 operations.remove(oldOp);
208 resourceMap.put(r, op);
212 protected UpdateOp getOP(Resource r) {
213 return resourceMap.get(r);
216 protected UpdateOp getOP(Statement r) {
217 return statementMap.get(r);
220 public abstract void populate(ReadGraph g) throws DatabaseException;
223 * Secondary populate method. Override this for chunked DB operations.
225 * @throws DatabaseException
227 public void populate(Session session, IProgressMonitor monitor) throws DatabaseException {
228 session.syncRequest(new ReadRequest() {
231 public void run(ReadGraph graph) throws DatabaseException {
237 protected boolean compares(Resource r1, Resource r2) {
240 if (changes.getComparable().contains(r1, r2))