Skip to content

Commit 7d18af7

Browse files
committed
Created more FieldExtractable types for templates
1 parent a0f901f commit 7d18af7

10 files changed

+260
-43
lines changed

mercurydb/src/main/java/org/mercurydb/ClassToTableExtractor.java

+40-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.Set;
1818
import java.util.TreeSet;
1919

20+
import org.mercurydb.queryutils.FieldExtractable;
21+
2022
import com.github.mustachejava.DefaultMustacheFactory;
2123
import com.github.mustachejava.Mustache;
2224
import com.github.mustachejava.MustacheFactory;
@@ -41,6 +43,8 @@ public class ClassToTableExtractor {
4143

4244
public int joinId;
4345

46+
public List<TemplateCounter> templateCounters;
47+
4448
public ClassToTableExtractor(
4549
Class<?> c, String superTable, Collection<String> subClassTables, String tableSuffix, int joinId)
4650
throws IOException {
@@ -56,10 +60,12 @@ public ClassToTableExtractor(
5660
this.subClasses = subClassTables;
5761
populateFieldsList();
5862
populateQueriesList(queries, fields);
63+
templateCounters = new ArrayList<>(fields.size());
64+
for (int i = 1; i <= fields.size(); ++i) {
65+
templateCounters.add(new TemplateCounter(i));
66+
}
5967
}
6068

61-
62-
6369
private void populateFieldsList() {
6470
for (Field f : c.getFields()) {
6571
// Ignore fields belonging to superclasses
@@ -155,6 +161,38 @@ public int compareTo(FieldData o) {
155161
}
156162
}
157163

164+
@SuppressWarnings("unused")
165+
private class TemplateCounter {
166+
public List<Integer> counter;
167+
168+
public TemplateCounter(int size) {
169+
counter = new ArrayList<Integer>(size);
170+
for (int i = 1; i <= size; ++i) {
171+
counter.add(i);
172+
}
173+
}
174+
175+
public String template() {
176+
StringBuilder sb = new StringBuilder();
177+
sb.append("<");
178+
for (Integer i : counter) {
179+
sb.append("F"+i+",");
180+
}
181+
sb.setCharAt(sb.length()-1, '>');
182+
return sb.toString();
183+
}
184+
185+
public String prototype() {
186+
StringBuilder sb = new StringBuilder();
187+
for (Integer i : counter) {
188+
sb.append(FieldExtractable.class.getSimpleName());
189+
sb.append("<"+sourceClass()+", F"+i+"> fe"+i+", F"+i+" val"+i+",");
190+
}
191+
sb.setLength(sb.length()-1);
192+
return sb.toString();
193+
}
194+
}
195+
158196
public String fullSourceClass() {
159197
return c.getName();
160198
}

mercurydb/src/main/java/org/mercurydb/queryutils/FieldExtractable.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
package org.mercurydb.queryutils;
22

3+
import java.util.Map;
4+
import java.util.Set;
5+
36
/**
47
* This class defines two accessors for data
58
* in a JoinStream. Note that JoinStream implements
69
* JoinField. It provides a way for JoinStreams to
710
* ask other JoinStreams how to extract a join key given
811
* an instance of their class owner.
912
*/
10-
public interface FieldExtractable<T> {
13+
public interface FieldExtractable<T, F> {
1114
/**
1215
* Returns the name of the class owner. Note
1316
* that the parameter to extractJoinKey should
1417
* always be of this type. Ex. For a JoinStream
1518
* from ATable, this should return A.class
1619
* @return a Class
1720
*/
18-
public Class<?> getContainerClass();
21+
public Class<T> getContainerClass();
1922

2023
/**
2124
* Extracts a join key value from an instance of the
2225
* type returned by getClassOwner()
2326
* @param o
2427
* @return
2528
*/
26-
public T extractField(Object o);
29+
public F extractField(T o);
2730

2831

2932
/**
3033
* @return true if field is indexed, false otherwise
3134
*/
3235
public boolean isIndexed();
3336

37+
/**
38+
*
39+
* @return map associated with index this field, or null if no index is present
40+
*/
41+
public Map<F, Set<T>> getIndex();
42+
3443
/**
3544
* Returns the id generated by the HgDB bootstrap process. This
3645
* is the JOIN_ID in the tables. JOIN_ID should be packaged into
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.mercurydb.queryutils;
2+
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
public class FieldExtractableJoinInput<T,F> implements FieldExtractable<T,F> {
7+
private FieldExtractable<T,F> _fwd;
8+
private HgStream<T> _stream;
9+
10+
public FieldExtractableJoinInput(FieldExtractable<T,F> fe, HgStream<T> stream) {
11+
this._fwd = fe;
12+
this._stream = stream;
13+
}
14+
15+
@Override
16+
public Class<T> getContainerClass() {
17+
return _fwd.getContainerClass();
18+
}
19+
20+
@Override
21+
public F extractField(T o) {
22+
return _fwd.extractField(o);
23+
}
24+
25+
@Override
26+
public boolean isIndexed() {
27+
return _fwd.isIndexed();
28+
}
29+
30+
@Override
31+
public Map<F, Set<T>> getIndex() {
32+
return _fwd.getIndex();
33+
}
34+
35+
@Override
36+
public int getContainerId() {
37+
return _fwd.getContainerId();
38+
}
39+
40+
// TODO merge this class with HgMonoStream
41+
public HgMonoStream<T> getMonoStream() {
42+
return new HgMonoStream<T>(_stream, this);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.mercurydb.queryutils;
2+
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
public class FieldExtractablePredicate<T,F> implements FieldExtractable<T,F> {
7+
8+
private final FieldExtractable<T,F> _fwd;
9+
10+
public final HgPredicate<F> predicate;
11+
12+
public FieldExtractablePredicate(FieldExtractable<T,F> fe, HgPredicate<F> pred) {
13+
this._fwd = fe;
14+
this.predicate = pred;
15+
}
16+
17+
@Override
18+
public Class<T> getContainerClass() {
19+
return _fwd.getContainerClass();
20+
}
21+
22+
@Override
23+
public F extractField(T o) {
24+
return _fwd.extractField(o);
25+
}
26+
27+
@Override
28+
public boolean isIndexed() {
29+
return _fwd.isIndexed();
30+
}
31+
32+
@Override
33+
public Map<F,Set<T>> getIndex() {
34+
return _fwd.getIndex();
35+
}
36+
37+
@Override
38+
public int getContainerId() {
39+
return _fwd.getContainerId();
40+
}
41+
42+
public boolean test(F value) {
43+
return predicate.predicate(value);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.mercurydb.queryutils;
2+
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
/**
7+
*
8+
* @param <T> Type that the table contains. (e.g. CustomerTable -> Customer)
9+
* @param <F> Field type.
10+
*/
11+
public class FieldExtractableValue<T, F> implements FieldExtractable<T, F> {
12+
13+
private final FieldExtractable<T,F> _fwd;
14+
15+
public final F value;
16+
17+
public FieldExtractableValue(FieldExtractable<T,F> src, F value) {
18+
this._fwd = src;
19+
this.value = value;
20+
}
21+
22+
@Override
23+
public Class<T> getContainerClass() {
24+
return _fwd.getContainerClass();
25+
}
26+
27+
@Override
28+
public F extractField(T o) {
29+
return _fwd.extractField(o);
30+
}
31+
32+
@Override
33+
public boolean isIndexed() {
34+
return _fwd.isIndexed();
35+
}
36+
37+
@Override
38+
public Map<F,Set<T>> getIndex() {
39+
return _fwd.getIndex();
40+
}
41+
42+
@Override
43+
public int getContainerId() {
44+
return _fwd.getContainerId();
45+
}
46+
}

mercurydb/src/main/java/org/mercurydb/queryutils/HgMonoStream.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
import com.google.common.collect.Sets;
66

7-
public class HgMonoStream<C> extends HgStream<C> {
8-
private HgStream<C> stream;
7+
public class HgMonoStream<T> extends HgStream<T> {
8+
private HgStream<T> stream;
99
public final FieldExtractable joinKey;
1010

11-
public HgMonoStream(HgStream<C> stream, FieldExtractable joinKey) {
11+
public HgMonoStream(HgStream<T> stream, FieldExtractable joinKey) {
1212
super(stream.cardinality);
1313
this.joinKey = joinKey;
1414
this.stream = stream;
1515
}
1616

17-
public HgStream<C> getWrappedStream() {
17+
public HgStream<T> getWrappedStream() {
1818
return stream;
1919
}
2020

@@ -32,7 +32,7 @@ public boolean hasNext() {
3232
}
3333

3434
@Override
35-
public C next() {
35+
public T next() {
3636
return stream.next();
3737
}
3838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.mercurydb.queryutils;
2+
3+
public interface HgPredicate<T> {
4+
public boolean predicate(T value);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.mercurydb.queryutils;
2+
3+
public class HgQuery {
4+
5+
public static<T> HgStream<T> query(FieldExtractableValue<T,?>...extractableValues) {
6+
return null;
7+
}
8+
}

0 commit comments

Comments
 (0)