diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/tabular/TabularIndexHost.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/tabular/TabularIndexHost.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/tabular/TabularIndexHost.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/tabular/TabularIndexHost.java new file mode 100644 index 00000000..6f2a1f5f --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/tabular/TabularIndexHost.java | |||
@@ -0,0 +1,145 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Gabor Bergmann, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.tabular; | ||
10 | |||
11 | import tools.refinery.viatra.runtime.api.ViatraQueryEngine; | ||
12 | import tools.refinery.viatra.runtime.api.scope.IEngineContext; | ||
13 | import tools.refinery.viatra.runtime.api.scope.IIndexingErrorListener; | ||
14 | import tools.refinery.viatra.runtime.api.scope.QueryScope; | ||
15 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
16 | import tools.refinery.viatra.runtime.matchers.scopes.IStorageBackend; | ||
17 | import tools.refinery.viatra.runtime.matchers.scopes.TabularRuntimeContext; | ||
18 | import tools.refinery.viatra.runtime.matchers.scopes.tables.IIndexTable; | ||
19 | import tools.refinery.viatra.runtime.matchers.scopes.tables.ITableWriterBinary; | ||
20 | import tools.refinery.viatra.runtime.matchers.scopes.tables.ITableWriterUnary; | ||
21 | |||
22 | /** | ||
23 | * Simple tabular index host. | ||
24 | * | ||
25 | * Unlike traditional Viatra instances initialized on a model, | ||
26 | * this index host can be initialized and then queried, | ||
27 | * while its queriable "contents" (base relations) can be separately written using table writer API. | ||
28 | * | ||
29 | * <p> Deriving classes are responsible for setting up the tables of this index and providing the writer API to clients. | ||
30 | * For the former, use {@link #newUnaryInputTable(IInputKey, boolean)}, | ||
31 | * {@link #newBinaryInputTable(IInputKey, boolean)} to create input tables, | ||
32 | * or {@link #registerNewTable(IIndexTable)} to register manually created derived tables. | ||
33 | * Instantiate such tables with {@link #runtimeContext} as their table context. | ||
34 | * | ||
35 | * | ||
36 | * <p> | ||
37 | * <strong>EXPERIMENTAL</strong>. This class or interface has been added as | ||
38 | * part of a work in progress. There is no guarantee that this API will | ||
39 | * work or that it will remain the same. | ||
40 | * | ||
41 | * @see EcoreIndexHost EcoreIndexHost for EMF-specific example usage. | ||
42 | * | ||
43 | * @author Gabor Bergmann | ||
44 | * @since 2.1 | ||
45 | */ | ||
46 | public abstract class TabularIndexHost { | ||
47 | |||
48 | private final IStorageBackend storage; | ||
49 | protected final TabularRuntimeContext runtimeContext; | ||
50 | protected final TabularIndexScope scope = new TabularIndexScope(); | ||
51 | |||
52 | public TabularIndexHost(IStorageBackend storage, TabularRuntimeContext runtimeContext) { | ||
53 | this.storage = storage; | ||
54 | this.runtimeContext = runtimeContext; | ||
55 | } | ||
56 | |||
57 | |||
58 | public TabularRuntimeContext getRuntimeContext() { | ||
59 | return runtimeContext; | ||
60 | } | ||
61 | |||
62 | public TabularIndexScope getScope() { | ||
63 | return scope; | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * @return true if this index host aims to serve queries that have a scope of the given type | ||
68 | */ | ||
69 | protected abstract boolean isQueryScopeEmulated(Class<? extends QueryScope> queryScopeClass); | ||
70 | |||
71 | |||
72 | |||
73 | /** | ||
74 | * Marks the beginning of an update transaction. | ||
75 | * In transaction mode, index table updates may be temporarily delayed for better performance. | ||
76 | * Stateful query backends will not update their results during the index update transaction. (TODO actually achieve this) | ||
77 | */ | ||
78 | public void startUpdateTransaction() { | ||
79 | storage.startTransaction(); | ||
80 | } | ||
81 | /** | ||
82 | * Marks the end of an update transaction. | ||
83 | * Any updates to index tables that were delayed during the transaction must now be flushed. | ||
84 | * Afterwards, stateful query backends will update their results. (TODO actually achieve this) | ||
85 | */ | ||
86 | public void finishUpdateTransaction() { | ||
87 | storage.finishTransaction(); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * To be called by deriving class. Creates and registers a new unary input table. | ||
92 | */ | ||
93 | protected ITableWriterUnary.Table<Object> newUnaryInputTable(IInputKey key, boolean unique) { | ||
94 | return registerNewTable(storage.createUnaryTable(key, runtimeContext, unique)); | ||
95 | } | ||
96 | /** | ||
97 | * To be called by deriving class. Creates and registers a new binary input table. | ||
98 | */ | ||
99 | protected ITableWriterBinary.Table<Object,Object> newBinaryInputTable(IInputKey key, boolean unique) { | ||
100 | return registerNewTable(storage.createBinaryTable(key, runtimeContext, unique)); | ||
101 | } | ||
102 | /** | ||
103 | * To be called by deriving class. Registers the given freshly created table and also returns it for convenience. | ||
104 | */ | ||
105 | protected <Table extends IIndexTable> Table registerNewTable(Table newTable) { | ||
106 | runtimeContext.registerIndexTable(newTable); | ||
107 | return newTable; | ||
108 | } | ||
109 | |||
110 | |||
111 | /** | ||
112 | * A scope describing queries evaluated against tzhis index host. | ||
113 | * @author Gabor Bergmann | ||
114 | * | ||
115 | */ | ||
116 | public class TabularIndexScope extends QueryScope { | ||
117 | |||
118 | public TabularIndexHost getIndexHost() { | ||
119 | return TabularIndexHost.this; | ||
120 | } | ||
121 | |||
122 | @Override | ||
123 | public int hashCode() { | ||
124 | return getIndexHost().hashCode(); | ||
125 | } | ||
126 | |||
127 | @Override | ||
128 | public boolean equals(Object obj) { | ||
129 | if (obj instanceof TabularIndexScope) | ||
130 | return getIndexHost().equals(((TabularIndexScope) obj).getIndexHost()); | ||
131 | return false; | ||
132 | } | ||
133 | |||
134 | @Override | ||
135 | public boolean isCompatibleWithQueryScope(Class<? extends QueryScope> queryScopeClass) { | ||
136 | return isQueryScopeEmulated(queryScopeClass) || super.isCompatibleWithQueryScope(queryScopeClass); | ||
137 | } | ||
138 | |||
139 | @Override | ||
140 | protected IEngineContext createEngineContext(ViatraQueryEngine engine, IIndexingErrorListener errorListener, org.apache.log4j.Logger logger) { | ||
141 | return new TabularEngineContext(getIndexHost(), engine, errorListener, logger); | ||
142 | } | ||
143 | |||
144 | } | ||
145 | } | ||