1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common;
14 import java.util.HashMap;
15 import java.util.HashSet;
18 import java.util.UUID;
19 import java.util.concurrent.atomic.AtomicBoolean;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteOnlyGraph;
23 import org.simantics.db.common.utils.Logger;
26 * A internal temporary store for database indexing-related low-level utilities
29 * This will be moved to more a more logical location at some point.
31 * @author Antti Villberg
32 * @author Tuukka Lehtonen
35 public class Indexing {
37 private static final boolean PROFILE = false;
39 private static Set<UUID> indexPendings = new HashSet<UUID>();
41 private static AtomicBoolean dependenciesIndexingDisabled = new AtomicBoolean();
43 private static int indexPendingCounter = 0;
45 private static Map<Resource, Map<Class<?>, Object>> caches = new HashMap<Resource, Map<Class<?>, Object>>();
47 private static boolean useIndexing = true;
49 @SuppressWarnings("unchecked")
50 public static <T> T getCache(Resource root, Class<T> clazz) {
51 Map<Class<?>,Object> cache = caches.get(root);
52 if(cache == null) return null;
53 return (T)cache.get(clazz);
56 public static <T> T createCache(Resource root, T object) {
57 Map<Class<?>,Object> cache = caches.get(root);
59 cache = new HashMap<Class<?>,Object>();
60 caches.put(root, cache);
62 cache.put(object.getClass(), object);
66 public static void clearCaches(Resource root) {
70 public static UUID makeIndexPending() {
71 synchronized (indexPendings) {
72 UUID guid = UUID.randomUUID();
73 indexPendings.add(guid);
78 public static void releaseIndexPending(UUID guid) {
79 synchronized (indexPendings) {
80 indexPendings.remove(guid);
81 if (indexPendings.isEmpty()) {
82 indexPendings.notifyAll();
83 indexPendingCounter++;
88 public static int getIndexPendingCounter() {
89 synchronized (indexPendings) {
90 return indexPendingCounter;
94 public static boolean isIndexPending() {
95 synchronized (indexPendings) {
96 return !indexPendings.isEmpty();
100 private static long totalWaitTime = 0;
102 public static void waitIndexPending() {
103 long startTime = PROFILE ? System.nanoTime() : 0;
104 boolean waited = false;
106 synchronized (indexPendings) {
107 while (isIndexPending()) {
110 indexPendings.wait(time++);
111 if (time > 10) time = 10;
112 } catch (InterruptedException e) {
113 Logger.defaultLogError(e);
119 long endTime = System.nanoTime();
120 long waitTime = endTime - startTime;
121 totalWaitTime += waitTime;
122 System.out.println("Indexing wait time " + (waitTime*1e-6) + " ms (total " + (totalWaitTime*1e-6) + " ms)");
128 * @param graph an active database write handle to prove one is in a write
129 * transaction and wants to disable dependencies indexing for this
131 * @return previous value
133 public static boolean setDependenciesIndexingDisabled(WriteOnlyGraph graph, boolean disabled) {
135 throw new NullPointerException("null write graph");
136 // TODO: check that graph is valid once made possible
137 return dependenciesIndexingDisabled.getAndSet(disabled);
140 public static boolean resetDependenciesIndexingDisabled() {
141 return dependenciesIndexingDisabled.compareAndSet(true, false);
144 public static boolean isDependenciesIndexingDisabled() {
147 return dependenciesIndexingDisabled.get();
150 public static void setDefaultDependenciesIndexingEnabled(boolean b) {