-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathVersionedBean.java
More file actions
100 lines (84 loc) · 2.47 KB
/
VersionedBean.java
File metadata and controls
100 lines (84 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package hgtest;
import org.hypergraphdb.HGEnvironment;
import org.hypergraphdb.HGHandle;
import org.hypergraphdb.HGQuery;
import org.hypergraphdb.HGSearchResult;
import org.hypergraphdb.HGSortIndex;
import org.hypergraphdb.HyperGraph;
import org.hypergraphdb.HGQuery.hg;
import org.hypergraphdb.indexing.ByPartIndexer;
import org.hypergraphdb.indexing.CompositeIndexer;
import org.hypergraphdb.indexing.HGKeyIndexer;
import org.hypergraphdb.util.HGUtils;
public class VersionedBean
{
private String id;
private int ver;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public int getVer()
{
return ver;
}
public void setVer(int ver)
{
this.ver = ver;
}
public String toString()
{
return "[" + id + "," + ver + "]";
}
private static void add(HyperGraph graph, String id, int ver)
{
VersionedBean x = new VersionedBean();
x.setId(id);
x.setVer(ver);
graph.add(x);
}
@SuppressWarnings("unchecked")
public static void main(String [] argv)
{
String location = "c:/temp/hgtest";
HGUtils.dropHyperGraphInstance(location);
HyperGraph graph = HGEnvironment.get(location);
HGHandle type = graph.getTypeSystem().getTypeHandle(VersionedBean.class);
CompositeIndexer indexer = new CompositeIndexer(type, new HGKeyIndexer[]{ new ByPartIndexer(type, "id"),
new ByPartIndexer(type, "ver")});
graph.getIndexManager().register(indexer);
graph.getIndexManager().register(new ByPartIndexer(type, "ver"));
add(graph, "x", 1);
add(graph, "x", 10);
add(graph, "x", 5);
add(graph, "t", 10);
add(graph, "t", 2);
add(graph, "t", 9);
graph.close();
graph = HGEnvironment.get(location);
HGQuery query = HGQuery.make(graph, hg.and(hg.type(VersionedBean.class), hg.eq("ver", 9)));
/*HGSearchResult<HGHandle> L = */query.execute();
HGSortIndex idx = (HGSortIndex)graph.getIndexManager().getIndex(indexer);
HGSearchResult<HGHandle> scan = idx.scanKeys();
while (scan.hasNext())
{
System.out.println((Object)graph.get((HGHandle)idx.findFirst(scan.next())));
}
VersionedBean bean = new VersionedBean();
bean.setId("x");
bean.setVer(2);
HGSearchResult<HGHandle> rs = idx.findGT(indexer.getKey(graph, bean));
if (!rs.hasNext())
System.out.println("Oops, not found");
else
{
bean = graph.get(rs.next());
System.out.println("Found " + bean);
}
rs.close();
}
}