]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/Indexing.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / Indexing.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.HashSet;\r
16 import java.util.Map;\r
17 import java.util.Set;\r
18 import java.util.UUID;\r
19 import java.util.concurrent.atomic.AtomicBoolean;\r
20 \r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.WriteOnlyGraph;\r
23 import org.simantics.db.common.utils.Logger;\r
24 \r
25 /**\r
26  * A internal temporary store for database indexing-related low-level utilities\r
27  * and static data.\r
28  * \r
29  * This will be moved to more a more logical location at some point.\r
30  * \r
31  * @author Antti Villberg\r
32  * @author Tuukka Lehtonen\r
33  * @since 1.8\r
34  */\r
35 public class Indexing {\r
36 \r
37     private static final boolean PROFILE                      = false;\r
38 \r
39     private static Set<UUID>     indexPendings                = new HashSet<UUID>();\r
40 \r
41     private static AtomicBoolean dependenciesIndexingDisabled = new AtomicBoolean();\r
42 \r
43     private static int indexPendingCounter = 0;\r
44 \r
45     private static Map<Resource, Map<Class<?>, Object>> caches = new HashMap<Resource, Map<Class<?>, Object>>(); \r
46     \r
47     private static boolean useIndexing = true;\r
48     \r
49     @SuppressWarnings("unchecked")\r
50         public static <T> T getCache(Resource root, Class<T> clazz) {\r
51         Map<Class<?>,Object> cache = caches.get(root);\r
52         if(cache == null) return null;\r
53         return (T)cache.get(clazz);\r
54     }\r
55     \r
56     public static <T> T createCache(Resource root, T object) {\r
57         Map<Class<?>,Object> cache = caches.get(root);\r
58         if(cache == null) {\r
59                 cache = new HashMap<Class<?>,Object>();\r
60                 caches.put(root, cache);\r
61         }\r
62         cache.put(object.getClass(), object);\r
63         return object;\r
64     }\r
65     \r
66     public static void clearCaches(Resource root) {\r
67         caches.remove(root);\r
68     }\r
69     \r
70     public static UUID makeIndexPending() {\r
71         synchronized (indexPendings) {\r
72             UUID guid = UUID.randomUUID();\r
73             indexPendings.add(guid);\r
74             return guid;\r
75         }\r
76     }\r
77 \r
78     public static void releaseIndexPending(UUID guid) {\r
79         synchronized (indexPendings) {\r
80             indexPendings.remove(guid);\r
81             if (indexPendings.isEmpty()) {\r
82                 indexPendings.notifyAll();\r
83                 indexPendingCounter++;\r
84             }\r
85         }\r
86     }\r
87 \r
88     public static int getIndexPendingCounter() {\r
89         synchronized (indexPendings) {\r
90             return indexPendingCounter;\r
91         }\r
92     }\r
93     \r
94     public static boolean isIndexPending() {\r
95         synchronized (indexPendings) {\r
96             return !indexPendings.isEmpty();\r
97         }\r
98     }\r
99 \r
100     private static long totalWaitTime = 0;\r
101 \r
102     public static void waitIndexPending() {\r
103         long startTime = PROFILE ? System.nanoTime() : 0;\r
104         boolean waited = false;\r
105         int time = 1;\r
106         synchronized (indexPendings) {\r
107             while (isIndexPending()) {\r
108                 try {\r
109                     waited = true;\r
110                     indexPendings.wait(time++);\r
111                     if (time > 10) time = 10;\r
112                 } catch (InterruptedException e) {\r
113                     Logger.defaultLogError(e);\r
114                 }\r
115             }\r
116         }\r
117         if (PROFILE) {\r
118             if (waited) {\r
119                 long endTime = System.nanoTime();\r
120                 long waitTime = endTime - startTime;\r
121                 totalWaitTime += waitTime;\r
122                 System.out.println("Indexing wait time " + (waitTime*1e-6) + " ms (total " + (totalWaitTime*1e-6) + " ms)");\r
123             }\r
124         }\r
125     }\r
126 \r
127     /**\r
128      * @param graph an active database write handle to prove one is in a write\r
129      *        transaction and wants to disable dependencies indexing for this\r
130      *        transaction only.\r
131      * @return previous value\r
132      */\r
133     public static boolean setDependenciesIndexingDisabled(WriteOnlyGraph graph, boolean disabled) {\r
134         if (graph == null)\r
135             throw new NullPointerException("null write graph");\r
136         // TODO: check that graph is valid once made possible\r
137         return dependenciesIndexingDisabled.getAndSet(disabled);\r
138     }\r
139 \r
140     public static boolean resetDependenciesIndexingDisabled() {\r
141         return dependenciesIndexingDisabled.compareAndSet(true, false);\r
142     }\r
143 \r
144     public static boolean isDependenciesIndexingDisabled() {\r
145         if (!useIndexing)\r
146                 return true;\r
147         return dependenciesIndexingDisabled.get();\r
148     }\r
149     \r
150     public static void setDefaultDependenciesIndexingEnabled(boolean b) {\r
151         useIndexing = b;\r
152     }\r
153 \r
154 }\r