~akiban-technologies/akiban-persistit/3.1.7-update

« back to all changes in this revision

Viewing changes to examples/SimpleDemo/SimpleDemo.java

  • Committer: Padraig O'Sullivan
  • Date: 2011-02-01 16:48:36 UTC
  • mfrom: (125.1.12 learning)
  • Revision ID: osullivan.padraig@gmail.com-20110201164836-mtdbk9d0rgcxpwye
Merge Padraig.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
 *
 
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.
 
11
 *
 
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/>.
 
14
 */
 
15
 
 
16
import java.io.BufferedReader;
 
17
import java.io.InputStreamReader;
 
18
 
 
19
import com.persistit.Exchange;
 
20
import com.persistit.Key;
 
21
import com.persistit.Persistit;
 
22
 
 
23
/**
 
24
 * <p>
 
25
 * A very simple class to demonstrate basic operations of Persistit&trade;
 
26
 * This class performs the following operations:
 
27
 * <ol>
 
28
 * <li> Initializes {@link com.persistit.Persistit} using a Properties file.
 
29
 * </li>
 
30
 * <li> Creates a {@link com.persistit.Exchange} with which to store and fetch
 
31
 *      data to and from a {@link com.persistit.Tree}.
 
32
 * </li>
 
33
 * <li> Reads some names from the console and stores records in the <tt>Tree</tt>
 
34
 *      indexed by those names.
 
35
 * </li>
 
36
 * <li> Dispays the names in alphabetical order.
 
37
 * </li>
 
38
 * <li> Closes Persistic to ensure all records have been written to disk.
 
39
 * </ol>
 
40
 * 
 
41
 *
 
42
 * @version 1.0
 
43
 */
 
44
public class SimpleDemo
 
45
{
 
46
    public static void main(String[] args)
 
47
    throws Exception
 
48
    {
 
49
        //
 
50
        // Initializes buffer pool, loads Volume(s)
 
51
        // and sets up PrewriteJournal.
 
52
        //
 
53
        System.out.println("Initializing Persistit");
 
54
        Persistit persistit = new Persistit();
 
55
        persistit.initialize();
 
56
        //
 
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".
 
61
        //
 
62
        Exchange exchange = new Exchange(persistit, "SimpleDemo", "my_first_tree", true);
 
63
        
 
64
        if (args.length > 0 && "-d".equals(args[0]))
 
65
        {
 
66
            //
 
67
            // Remove all elements.
 
68
            //
 
69
            exchange.removeAll();
 
70
        }
 
71
        
 
72
        System.out.println();
 
73
        System.out.println("Please enter some names -");
 
74
        System.out.println();
 
75
        BufferedReader reader =
 
76
            new BufferedReader(
 
77
                new InputStreamReader(System.in));
 
78
                 
 
79
        boolean done = false;
 
80
        int count = 0;
 
81
        while (true)
 
82
        {
 
83
            System.out.print("Name: ");
 
84
            String name = reader.readLine();
 
85
            if (name == null || name.length() == 0) break;
 
86
            //
 
87
            // Set up the value to insert into the Tree.
 
88
            //
 
89
            exchange.getValue().put("Name # " + (++count));
 
90
            //
 
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.
 
95
            //
 
96
            // Note that all key manipulation methods return the
 
97
            // Exchange so that you can chain calls like this:
 
98
            //
 
99
            exchange.getKey().to(name);
 
100
            exchange.store();
 
101
        }
 
102
        
 
103
        System.out.println();
 
104
        System.out.println("Here are the names you entered: ");
 
105
        //
 
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.
 
108
        //
 
109
        exchange.getKey().to(Key.BEFORE);
 
110
        //
 
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.
 
115
        //
 
116
        while (exchange.next())
 
117
        {
 
118
            //
 
119
            // Recover the name from the Key.
 
120
            //
 
121
            String name = exchange.getKey().decodeString();
 
122
            //
 
123
            // Fetch the associated Value and decode it into
 
124
            // a String.
 
125
            //
 
126
            String value = exchange.getValue().getString();
 
127
            
 
128
            System.out.println(name + "  -->  " + value);
 
129
        }
 
130
        
 
131
        //
 
132
        // Flushes everything nicely to disk and releases all resources.
 
133
        //
 
134
        persistit.close();
 
135
    }
 
136
}