]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/util/EventUtils.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / util / EventUtils.java
1 package org.simantics.event.util;
2
3 import java.util.List;
4 import java.util.UUID;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.PossibleTypedParent;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.event.ontology.EventResource;
12 import org.simantics.layer0.Layer0;
13
14 public class EventUtils {
15
16         public static final int MAX_EVENTS = 1000;
17         public static final int SLICE_SIZE = 64;
18         public static final int KEEPS_SLICES = (MAX_EVENTS / SLICE_SIZE) + 2;
19         public static final String MAX_SLICE_NAME = "" + (SLICE_SIZE-1);
20
21         public static Resource createLog(WriteGraph graph) throws DatabaseException {
22                 Layer0 L0 = Layer0.getInstance(graph);
23                 EventResource EVENT = EventResource.getInstance(graph);
24         Resource log = graph.newResource();
25         graph.claim(log, L0.InstanceOf, null, EVENT.EventLog);
26         graph.claimLiteral(log, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);
27         graph.claimLiteral(log, EVENT.HasModificationCounter, 0, Bindings.INTEGER);
28         return log;
29         }
30
31     /**
32      * Bumps the modification counter value of the event log of the specified
33      * events using {@link #bumpModificationCounter(WriteGraph, Resource)}.
34      * <p>
35      * The code assumes that all events are from the same log.
36      * 
37      * @param graph
38      * @param forLogOfEvents
39      * @throws DatabaseException
40      */
41     public static void bumpModificationCounter(WriteGraph graph, List<Resource> forLogOfEvents) throws DatabaseException {
42         EventResource EVENT = EventResource.getInstance(graph);
43         for (Resource event : forLogOfEvents) {
44             Resource log = graph.syncRequest(new PossibleTypedParent(event, EVENT.EventLog));
45             if (log != null) {
46                 bumpModificationCounter(graph, log);
47                 return;
48             }
49         }
50     }
51
52     /**
53      * Bumps the modification counter of the specified event log by 1.
54      * 
55      * @param graph
56      * @param eventLog
57      * @return new modification counter value
58      * @throws DatabaseException
59      */
60     public static int bumpModificationCounter(WriteGraph graph, Resource eventLog) throws DatabaseException {
61         EventResource EVENT = EventResource.getInstance(graph);
62         Resource counter = graph.getPossibleObject(eventLog, EVENT.HasModificationCounter);
63         if (counter != null) {
64             Integer c = graph.getValue(counter, Bindings.INTEGER);
65             c = c == null ? 1 : c+1;
66             graph.claimValue(counter, c, Bindings.INTEGER);
67             return c;
68         }
69         return 0;
70     }
71
72 }