forked from fauna/faunadb-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpellExample.java
278 lines (233 loc) · 9.99 KB
/
SpellExample.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.fauna.learn;
import com.faunadb.client.FaunaClient;
import com.faunadb.client.query.Expr;
import com.faunadb.client.types.Value;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import static com.faunadb.client.query.Language.*;
/**
* Run the example in maven using:
*
* mvn compile exec:java -Dexec.mainClass="com.fauna.learn.SpellExample"
*/
public class SpellExample {
public static void main(String[] args) throws Exception {
/*
* Create an admin connection to FaunaDB.
*
* If you are using the FaunaDB-Cloud version:
* - remove the 'withEndpoint' line below
* - substitute "secret" for your authentication key's secret
*/
FaunaClient adminClient = FaunaClient.builder()
.withEndpoint("http://127.0.0.1:8443")
.withSecret("secret")
.build();
System.out.println("Succesfully connected to FaunaDB as Admin\n");
/*
* Create a database
*/
String DB_NAME = "demo";
Value dbResults = adminClient.query(
Do(
If(
Exists(Database(DB_NAME)),
Delete(Database(DB_NAME)),
Value(true)
),
CreateDatabase(Obj("name", Value(DB_NAME)))
)
).get();
System.out.println("Successfully created database: " + dbResults.at("name").to(String.class).get() + "\n" + dbResults + "\n");
/*
* Create a client connection to the demo database
*/
Value keyResults = adminClient.query(
CreateKey(Obj("database", Database(Value(DB_NAME)), "role", Value("server")))
).get();
String key = keyResults.at("secret").to(String.class).get();
//Create the client query in a try-with-resources to ensure it gets closed
//Because a Fauna Client implements AutoCloseable it will be closed when the try block finishes
try (FaunaClient client = adminClient.newSessionClient(key)) {
System.out.println("Connected to Fauna database " + DB_NAME + " with server role\n");
runSpellExamples(DB_NAME, client);
}
/*
* Delete the database and close the admin connection
*/
deleteDB(adminClient, DB_NAME);
adminClient.close();
System.out.println("Disconnected from FaunaDB as Admin!");
}
private static void runSpellExamples(String DB_NAME, FaunaClient client) throws Exception {
/*
* Create the spell class and index
*/
String SPELLS_CLASS = "spells";
String INDEX_NAME = "spells_index";
Value classResults = client.query(
CreateCollection(
Obj("name", Value(SPELLS_CLASS))
)
).get();
System.out.println("Create Class for " + DB_NAME + ":\n " + classResults + "\n");
Value indexResults = client.query(
CreateIndex(
Obj("name", Value(INDEX_NAME), "source", Collection(Value(SPELLS_CLASS)))
)
).get();
System.out.println("Create Index for " + DB_NAME + ":\n " + indexResults + "\n");
/*
* Add some entries to the spells class
*/
Value addFireResults = client.query(
Create(
Collection(Value(SPELLS_CLASS)),
Obj("data",
Obj("name", Value("Fire Beak"), "element", Value("water"), "cost", Value(15))
)
)
).get();
System.out.println("Added spell to class " + SPELLS_CLASS + ":\n " + addFireResults + "\n");
Value addDragonResults = client.query(
Create(
Collection(Value(SPELLS_CLASS)),
Obj("data",
Obj("name", Value("Water Dragon's Claw"), "element", Value("water"), "cost", Value(25))
)
)
).get();
System.out.println("Added spell to class " + SPELLS_CLASS + ":\n " + addDragonResults + "\n");
Value addHippoResults = client.query(
Create(
Collection(Value(SPELLS_CLASS)),
Obj("data",
Obj("name", Value("Hippo's Wallow"), "element", Value("water"), "cost", Value(35))
)
)
).get();
System.out.println("Added spell to class " + SPELLS_CLASS + ":\n " + addHippoResults + "\n");
//The results at 'ref' are a resource pointer to the class that was just created.
Value hippoRef = addHippoResults.at("ref");
System.out.println("hippoRef = " + hippoRef);
/*
* Read the hippo back that we just created
*/
Value readHippoResults = client.query(
Select(Value("data"), Get(hippoRef))
).get();
System.out.println("Hippo Spell:\n " + readHippoResults + "\n");
//convert the hippo results into primitive elements
String name = readHippoResults.at("name").to(String.class).get();
Integer cost = readHippoResults.at("cost").to(Integer.class).get();
String element = readHippoResults.at("element").to(String.class).get();
System.out.println(String.format(
"Spell Details: Name=%s, Cost=%d, Element=%s", name, cost, element));
//This would return an empty option if the field is not found or the conversion fails
Optional<String> optSpellElement = readHippoResults.at("element").to(String.class).getOptional();
if (optSpellElement.isPresent()) {
String element2 = optSpellElement.get();
System.out.println("optional spell element = " + element2);
} else {
System.out.println("Something went wrong reading the spell");
}
/*
* Query for all the spells in the index
*/
Value queryIndexResults = client.query(
SelectAll(Path("data", "id"),
Paginate(
Match(Index(Value(INDEX_NAME)))
))
).get();
Collection<String> spellsRefIds = queryIndexResults.asCollectionOf(String.class).get();
System.out.println("spellsRefIds = " + spellsRefIds + "\n");
/*
* Store a Spell java class
*/
Spell newSpell = new Spell("Water Dragon's Claw", "water", 25);
Value storeSpellResult = client.query(
Create(
Collection(Value(SPELLS_CLASS)),
Obj("data", Value(newSpell))
)
).get();
System.out.println("Stored spell:\n " + storeSpellResult + "\n");
/*
* Read the spell we just created
*/
Value dragonRef = storeSpellResult.at("ref");
Value getDragonResult = client.query(
Select(Value("data"),
Get(dragonRef)
)
).get();
Spell spell = getDragonResult.to(Spell.class).get();
System.out.println("dragon spell: " + spell + "\n");
/*
* Store a list of Spells
*/
Spell spellOne = new Spell("Chill Touch", "ice", 18);
Spell spellTwo = new Spell("Dancing Lights", "fire", 45);
Spell spellThree = new Spell("Fire Bolt", "fire", 32);
List<Spell> spellList = Arrays.asList(spellOne, spellTwo, spellThree);
//Lambda Variable for each spell
String NXT_SPELL = "NXT_SPELL";
//Encode the list of spells into an expression
Expr encodedSpellsList = Value(spellList);
//This query can be approximately read as for each spell in the list of spells evaluate the lambda function.
//That lambda function creates a temporary variable with each spell in the list and passes it to the create function.
//The create function then stores that spell in the database
Value spellsListSave = client.query(
Foreach(
encodedSpellsList,
Lambda(Value(NXT_SPELL),
Create(
Collection(Value(SPELLS_CLASS)),
Obj("data", Var(NXT_SPELL))
)
)
)
).get();
System.out.println("Created list of spells from java list: \n" + spellsListSave + "\n");
Collection<Spell> spellCollection = spellsListSave.asCollectionOf(Spell.class).get();
System.out.println("save " + spellCollection.size() + " spells:");
spellCollection.forEach(nextSpell -> System.out.println(" " + nextSpell));
System.out.println("\n");
/*
* Read all Spells for the Spells Index
*/
//Lambda Variable for each spell ref
String REF_SPELL_ID = "NXT_SPELL";
//Select causes the return data to be stored in the data field that is expected when the data is covered to a collection
//The Map is equivalent to a functional map which maps over the set of all values returned by the paginate.
//Then for each value in the list it runs the lambda function which gets and returns the value.
Value findAllSpells = client.query(
SelectAll(Path("data", "data"),
Map(
Paginate(
Match(Index(Value(INDEX_NAME)))
),
Lambda(Value(REF_SPELL_ID), Get(Var(REF_SPELL_ID))))
)
).get();
Collection<Spell> allSpellsCollection = findAllSpells.asCollectionOf(Spell.class).get();
System.out.println("read " + allSpellsCollection.size() + " spells:");
allSpellsCollection.forEach(nextSpell -> System.out.println(" " + nextSpell));
}
private static void deleteDB(FaunaClient adminClient, String dbName) throws Exception {
/*
* Delete the Database created
*/
Value result = adminClient.query(
If(
Exists(Database(dbName)),
Delete(Database(dbName)),
Value(true)
)
).get();
System.out.println("Deleted database: " + dbName + ":\n " + result + "\n");
}
}