2
* Copyright (C) 2011 Akiban Technologies Inc.
3
* This program is free software: you can redistribute it and/or modify
4
* it under the terms of the GNU Affero General Public License, version 3,
5
* as published by the Free Software Foundation.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU Affero General Public License for more details.
12
* You should have received a copy of the GNU Affero General Public License
13
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
import java.io.BufferedReader;
17
import java.io.InputStreamReader;
19
import com.persistit.Exchange;
20
import com.persistit.Key;
21
import com.persistit.Persistit;
25
* A very simple class to demonstrate basic operations of Persistit™
26
* This class performs the following operations:
28
* <li> Initializes {@link com.persistit.Persistit} using a Properties file.
30
* <li> Creates a {@link com.persistit.Exchange} with which to store and fetch
31
* data to and from a {@link com.persistit.Tree}.
33
* <li> Reads some names from the console and stores records in the <tt>Tree</tt>
34
* indexed by those names.
36
* <li> Dispays the names in alphabetical order.
38
* <li> Closes Persistic to ensure all records have been written to disk.
44
public class SimpleDemo
46
public static void main(String[] args)
50
// Initializes buffer pool, loads Volume(s)
51
// and sets up PrewriteJournal.
53
System.out.println("Initializing Persistit");
54
Persistit persistit = new Persistit();
55
persistit.initialize();
57
// A Exchange provides all the methods you use to
58
// manipulate data. This constructs a Exchange in
59
// volume "SimpleDemo" and either opens or creates
60
// within that volume a Tree called "my_first_tree".
62
Exchange exchange = new Exchange(persistit, "SimpleDemo", "my_first_tree", true);
64
if (args.length > 0 && "-d".equals(args[0]))
67
// Remove all elements.
73
System.out.println("Please enter some names -");
75
BufferedReader reader =
77
new InputStreamReader(System.in));
83
System.out.print("Name: ");
84
String name = reader.readLine();
85
if (name == null || name.length() == 0) break;
87
// Set up the value to insert into the Tree.
89
exchange.getValue().put("Name # " + (++count));
91
// Set up the key and store the record. The to()
92
// method replaces the final segment of the key with
93
// the supplied value. If the key is empty then
94
// it appends the segment.
96
// Note that all key manipulation methods return the
97
// Exchange so that you can chain calls like this:
99
exchange.getKey().to(name);
103
System.out.println();
104
System.out.println("Here are the names you entered: ");
106
// Set Exchange to an imaginary key that's to the left of all real
107
// keys. This allows the next() operation to find the first key.
109
exchange.getKey().to(Key.BEFORE);
111
// The next() method advances to the next ascending key
112
// segment (in this case, the next name). Returns false
113
// if there is none. This method also atomically retrieves the
114
// value associated with the key.
116
while (exchange.next())
119
// Recover the name from the Key.
121
String name = exchange.getKey().decodeString();
123
// Fetch the associated Value and decode it into
126
String value = exchange.getValue().getString();
128
System.out.println(name + " --> " + value);
132
// Flushes everything nicely to disk and releases all resources.