summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/examples/java/com/wiredtiger/examples/ex_call_center.java
blob: a3f0f56ded8b2b1bbced831ccdce1ccc97b7578a (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*-
 * Public Domain 2014-2016 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * ex_call_center.java
 *	This is an example application that demonstrates how to map a
 *	moderately complex SQL application into WiredTiger.
 */

package com.wiredtiger.examples;
import com.wiredtiger.db.*;
import java.io.*;
import java.util.*;

/*! [call-center decl] */
/*
 * In SQL, the tables are described as follows:
 *
 * CREATE TABLE Customers(id INTEGER PRIMARY KEY,
 *     name VARCHAR(30), address VARCHAR(50), phone VARCHAR(15))
 * CREATE INDEX CustomersPhone ON Customers(phone)
 *
 * CREATE TABLE Calls(id INTEGER PRIMARY KEY, call_date DATE,
 *     cust_id INTEGER, emp_id INTEGER, call_type VARCHAR(12),
 *     notes VARCHAR(25))
 * CREATE INDEX CallsCustDate ON Calls(cust_id, call_date)
 *
 * In this example, both tables will use record numbers for their IDs, which
 * will be the key.  The C structs for the records are as follows.
 */

/* Customer records. */
class Customer {
    public long id;
    public String name;
    public String address;
    public String phone;
    public Customer(long id, String name, String address, String phone) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }
    public Customer() {}
}

/* Call records. */
class Call {
    public long id;
    public long call_date;
    public long cust_id;
    public long emp_id;
    public String call_type;
    public String notes;
    public Call(long id, long call_date, long cust_id, long emp_id,
                String call_type, String notes) {
        this.id = id;
        this.call_date = call_date;
        this.cust_id = cust_id;
        this.emp_id = emp_id;
        this.call_type = call_type;
        this.notes = notes;
    }
    public Call() {}
}
/*! [call-center decl] */

public class ex_call_center {

    public static String home;

    public static int
    callCenterExample()
	throws WiredTigerException
    {
	Connection conn;
	Cursor cursor;
	Session session;
	int count, ret;
        SearchStatus nearstatus;
        List<Customer> custSample = new ArrayList<Customer>();
        List<Call> callSample = new ArrayList<Call>();

        custSample.add(new Customer(0, "Professor Oak",
            "LeafGreen Avenue", "123-456-7890"));
        custSample.add(new Customer(0, "Lorelei",
            "Sevii Islands", "098-765-4321"));
        callSample.add(new Call(0, 32, 1, 2, "billing", "unavailable"));
        callSample.add(new Call(0, 33, 1, 2, "billing", "available"));
        callSample.add(new Call(0, 34, 1, 2, "reminder", "unavailable"));
        callSample.add(new Call(0, 35, 1, 2, "reminder", "available"));

	/*
	 * Create a clean test directory for this run of the test program if the
	 * environment variable isn't already set (as is done by make check).
	 */
	if (System.getenv("WIREDTIGER_HOME") == null) {
            home = "WT_HOME";
            try {
                Process proc = Runtime.getRuntime().exec("/bin/rm -rf WT_HOME");
                BufferedReader br = new BufferedReader(
                    new InputStreamReader(proc.getInputStream()));
                while(br.ready())
                    System.out.println(br.readLine());
                br.close();
                proc.waitFor();
                new File("WT_HOME").mkdir();
            } catch (Exception ex) {
                System.err.println("Exception: " + ex);
                return (1);
            }
	} else
            home = null;

        try {
            conn = wiredtiger.open(home, "create");
            session = conn.open_session(null);
        } catch (WiredTigerException wte) {
            System.err.println("WiredTigerException: " + wte);
            return(1);
        }
	/* Note: further error checking omitted for clarity. */

	/*! [call-center work] */
	/*
	 * Create the customers table, give names and types to the columns.
	 * The columns will be stored in two groups: "main" and "address",
	 * created below.
	 */
	ret = session.create("table:customers",
	    "key_format=r," +
	    "value_format=SSS," +
	    "columns=(id,name,address,phone)," +
	    "colgroups=(main,address)");

	/* Create the main column group with value columns except address. */
	ret = session.create(
	    "colgroup:customers:main", "columns=(name,phone)");

	/* Create the address column group with just the address. */
	ret = session.create(
	    "colgroup:customers:address", "columns=(address)");

	/* Create an index on the customer table by phone number. */
	ret = session.create(
	    "index:customers:phone", "columns=(phone)");

	/* Populate the customers table with some data. */
	cursor = session.open_cursor("table:customers", null, "append");
	for (Customer cust : custSample) {
            cursor.putValueString(cust.name);
            cursor.putValueString(cust.address);
            cursor.putValueString(cust.phone);
            ret = cursor.insert();
	}
	ret = cursor.close();

	/*
	 * Create the calls table, give names and types to the columns.  All the
	 * columns will be stored together, so no column groups are declared.
	 */
	ret = session.create("table:calls",
	    "key_format=r," +
	    "value_format=qrrSS," +
	    "columns=(id,call_date,cust_id,emp_id,call_type,notes)");

	/*
	 * Create an index on the calls table with a composite key of cust_id
	 * and call_date.
	 */
	ret = session.create("index:calls:cust_date",
	    "columns=(cust_id,call_date)");

	/* Populate the calls table with some data. */
	cursor = session.open_cursor("table:calls", null, "append");
	for (Call call : callSample) {
            cursor.putValueLong(call.call_date);
            cursor.putValueRecord(call.cust_id);
            cursor.putValueRecord(call.emp_id);
            cursor.putValueString(call.call_type);
            cursor.putValueString(call.notes);
            ret = cursor.insert();
	}
	ret = cursor.close();

	/*
	 * First query: a call arrives.  In SQL:
	 *
	 * SELECT id, name FROM Customers WHERE phone=?
	 *
	 * Use the cust_phone index, lookup by phone number to fill the
	 * customer record.  The cursor will have a key format of "S" for a
	 * string because the cust_phone index has a single column ("phone"),
	 * which is of type "S".
	 *
	 * Specify the columns we want: the customer ID and the name.  This
	 * means the cursor's value format will be "rS".
	 */
	cursor = session.open_cursor(
	    "index:customers:phone(id,name)", null, null);
	cursor.putKeyString("123-456-7890");
	ret = cursor.search();
	if (ret == 0) {
            Customer cust = new Customer();
            cust.id = cursor.getValueRecord();
            cust.name = cursor.getValueString();
            System.out.println("Read customer record for " + cust.name +
                " (ID " + cust.id + ")");
	}
	ret = cursor.close();

	/*
	 * Next query: get the recent order history.  In SQL:
	 *
	 * SELECT * FROM Calls WHERE cust_id=? ORDER BY call_date DESC LIMIT 3
	 *
	 * Use the call_cust_date index to find the matching calls.  Since it is
	 * is in increasing order by date for a given customer, we want to start
	 * with the last record for the customer and work backwards.
	 *
	 * Specify a subset of columns to be returned.  (Note that if these were
	 * all covered by the index, the primary would not have to be accessed.)
	 * Stop after getting 3 records.
	 */
	cursor = session.open_cursor(
	    "index:calls:cust_date(cust_id,call_type,notes)",
	    null, null);

	/*
	 * The keys in the index are (cust_id,call_date) -- we want the largest
	 * call date for a given cust_id.  Search for (cust_id+1,0), then work
	 * backwards.
	 */
        long custid = 1;
	cursor.putKeyRecord(custid + 1);
	cursor.putKeyLong(0);
	nearstatus = cursor.search_near();

	/*
	 * If the table is empty, search_near will return WT_NOTFOUND, else the
	 * cursor will be positioned on a matching key if one exists, or an
	 * adjacent key if one does not.  If the positioned key is equal to or
	 * larger than the search key, go back one.
	 */
	if (ret == 0 && (nearstatus == SearchStatus.LARGER ||
            nearstatus == SearchStatus.FOUND))
            ret = cursor.prev();
	for (count = 0; ret == 0 && count < 3; ++count) {
            Call call = new Call();
            call.cust_id = cursor.getValueRecord();
            call.call_type = cursor.getValueString();
            call.notes = cursor.getValueString();
            if (call.cust_id != custid)
                break;
            System.out.println("Call record: customer " + call.cust_id +
                               " (" + call.call_type +
                               ": " + call.notes + ")");
            ret = cursor.prev();
	}
	/*! [call-center work] */

	ret = conn.close(null);

	return (ret);
    }

    public static void
    main(String[] argv)
    {
        try {
            System.exit(callCenterExample());
        }
        catch (WiredTigerException wte) {
            System.err.println("Exception: " + wte);
            wte.printStackTrace();
            System.exit(1);
        }
    }
}