1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common;
\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
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.WriteOnlyGraph;
\r
23 import org.simantics.db.common.utils.Logger;
\r
26 * A internal temporary store for database indexing-related low-level utilities
\r
29 * This will be moved to more a more logical location at some point.
\r
31 * @author Antti Villberg
\r
32 * @author Tuukka Lehtonen
\r
35 public class Indexing {
\r
37 private static final boolean PROFILE = false;
\r
39 private static Set<UUID> indexPendings = new HashSet<UUID>();
\r
41 private static AtomicBoolean dependenciesIndexingDisabled = new AtomicBoolean();
\r
43 private static int indexPendingCounter = 0;
\r
45 private static Map<Resource, Map<Class<?>, Object>> caches = new HashMap<Resource, Map<Class<?>, Object>>();
\r
47 private static boolean useIndexing = true;
\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
56 public static <T> T createCache(Resource root, T object) {
\r
57 Map<Class<?>,Object> cache = caches.get(root);
\r
59 cache = new HashMap<Class<?>,Object>();
\r
60 caches.put(root, cache);
\r
62 cache.put(object.getClass(), object);
\r
66 public static void clearCaches(Resource root) {
\r
67 caches.remove(root);
\r
70 public static UUID makeIndexPending() {
\r
71 synchronized (indexPendings) {
\r
72 UUID guid = UUID.randomUUID();
\r
73 indexPendings.add(guid);
\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
88 public static int getIndexPendingCounter() {
\r
89 synchronized (indexPendings) {
\r
90 return indexPendingCounter;
\r
94 public static boolean isIndexPending() {
\r
95 synchronized (indexPendings) {
\r
96 return !indexPendings.isEmpty();
\r
100 private static long totalWaitTime = 0;
\r
102 public static void waitIndexPending() {
\r
103 long startTime = PROFILE ? System.nanoTime() : 0;
\r
104 boolean waited = false;
\r
106 synchronized (indexPendings) {
\r
107 while (isIndexPending()) {
\r
110 indexPendings.wait(time++);
\r
111 if (time > 10) time = 10;
\r
112 } catch (InterruptedException e) {
\r
113 Logger.defaultLogError(e);
\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
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
133 public static boolean setDependenciesIndexingDisabled(WriteOnlyGraph graph, boolean disabled) {
\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
140 public static boolean resetDependenciesIndexingDisabled() {
\r
141 return dependenciesIndexingDisabled.compareAndSet(true, false);
\r
144 public static boolean isDependenciesIndexingDisabled() {
\r
147 return dependenciesIndexingDisabled.get();
\r
150 public static void setDefaultDependenciesIndexingEnabled(boolean b) {
\r