~pbeaman/akiban-persistit/fix-1032701-interrupts-leave-latches

« back to all changes in this revision

Viewing changes to examples/PersistitMapDemo/PersistitMapDemo.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.util.Iterator;
 
17
import java.util.Map;
 
18
 
 
19
import com.persistit.Exchange;
 
20
import com.persistit.Persistit;
 
21
import com.persistit.PersistitMap;
 
22
 
 
23
public class PersistitMapDemo
 
24
{
 
25
    public static void main(String[] args)
 
26
    throws Exception 
 
27
    {
 
28
        // This program uses PersistitMap, an implementation of 
 
29
        // java.util.SortedMap to store a copy of the system properties.
 
30
        // Each time you run this program it will compare the current
 
31
        // system properties to those that were stored previously and will
 
32
        // display any differences.
 
33
        //
 
34
        // PersistitMap works just like any other Map except that its values
 
35
        // are persistent.  (See the API documentation for PersistitMap for
 
36
        // a few other considerations.)
 
37
        //
 
38
        Persistit persistit = new Persistit();
 
39
        try
 
40
        {
 
41
            persistit.initialize();
 
42
            Exchange dbex = new Exchange(persistit, "pmdemo", "properties", true);
 
43
            //
 
44
            // Create a PersistitMap over this exchange.  The map will be
 
45
            // non-empty if this program has already been run previously.
 
46
            //
 
47
            PersistitMap persistitMap = new PersistitMap(dbex);
 
48
            //
 
49
            // All Persistit databases below are invoked through the Map
 
50
            // interface.
 
51
            //
 
52
            if (persistitMap.size() == 0)
 
53
            {
 
54
                System.out.println("This is the first time PersistitMapDemo has run");
 
55
            }
 
56
            else
 
57
            {
 
58
                System.out.println("Comparing property values:");
 
59
                for (Iterator iter = System.getProperties().entrySet().iterator();
 
60
                     iter.hasNext();)
 
61
                {
 
62
                    Map.Entry entry = (Map.Entry)iter.next();
 
63
                    String name = (String)entry.getKey();
 
64
                    String newValue = (String)entry.getValue();
 
65
                    
 
66
                    String oldValue = (String)persistitMap.remove(name);
 
67
                    if (oldValue == null)
 
68
                    {
 
69
                        System.out.println(
 
70
                            "New value for " + name + " is '" + newValue + "'");
 
71
                    }
 
72
                    else if (!newValue.equals(oldValue))
 
73
                    {
 
74
                        System.out.println(
 
75
                            "Value changed for " + name +
 
76
                            " from '" + oldValue + "' to '" + newValue + "'");
 
77
                    }
 
78
                }
 
79
            }
 
80
 
 
81
            for (Iterator iter = persistitMap.entrySet().iterator();
 
82
                 iter.hasNext();)
 
83
            {
 
84
                Map.Entry entry = (Map.Entry)iter.next();
 
85
                String name = (String)entry.getKey();
 
86
                String oldValue = (String)entry.getValue();
 
87
                
 
88
                System.out.println(
 
89
                    "Old value for " + name + "='" + oldValue + "' is gone");
 
90
            }
 
91
 
 
92
            persistitMap.putAll(System.getProperties());
 
93
        }
 
94
        finally
 
95
        {
 
96
            // Always close Persistit. If the application does not do 
 
97
            // this, Persistit's background threads will keep the JVM from
 
98
            // terminating.
 
99
            //
 
100
            persistit.close();
 
101
        }
 
102
    }
 
103
}
 
 
b'\\ No newline at end of file'