Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

AccessExample.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: AccessExample.java,v 12.2 2005/06/16 20:22:47 bostic Exp $
00008  */
00009 
00010 
00011 package db;
00012 
00013 import com.sleepycat.db.*;
00014 import java.io.File;
00015 import java.io.FileNotFoundException;
00016 import java.io.InputStreamReader;
00017 import java.io.IOException;
00018 import java.io.PrintStream;
00019 
00020 class AccessExample {
00021     private static final int EXIT_SUCCESS = 0;
00022     private static final int EXIT_FAILURE = 1;
00023 
00024     public AccessExample() {
00025     }
00026 
00027     public static void usage() {
00028         System.out.println("usage: java " +
00029                "db.AccessExample [-r] [database]\n");
00030         System.exit(EXIT_FAILURE);
00031     }
00032 
00033     public static void main(String[] argv) {
00034         boolean removeExistingDatabase = false;
00035         String databaseName = "access.db";
00036 
00037         for (int i = 0; i < argv.length; i++) {
00038             if (argv[i].equals("-r"))
00039                 removeExistingDatabase = true;
00040             else if (argv[i].equals("-?"))
00041                 usage();
00042             else if (argv[i].startsWith("-"))
00043                 usage();
00044             else {
00045                 if ((argv.length - i) != 1)
00046                     usage();
00047                 databaseName = argv[i];
00048                 break;
00049             }
00050         }
00051 
00052         try {
00053             AccessExample app = new AccessExample();
00054             app.run(removeExistingDatabase, databaseName);
00055         } catch (DatabaseException dbe) {
00056             System.err.println("AccessExample: " + dbe.toString());
00057             System.exit(EXIT_FAILURE);
00058         } catch (FileNotFoundException fnfe) {
00059             System.err.println("AccessExample: " + fnfe.toString());
00060             System.exit(EXIT_FAILURE);
00061         }
00062         System.exit(EXIT_SUCCESS);
00063     }
00064 
00065     // Prompts for a line, and keeps prompting until a non blank
00066     // line is returned.  Returns null on erroror.
00067     //
00068     public static String askForLine(InputStreamReader reader,
00069                                     PrintStream out, String prompt) {
00070         String result = "";
00071         while (result != null && result.length() == 0) {
00072             out.print(prompt);
00073             out.flush();
00074             result = getLine(reader);
00075         }
00076         return result;
00077     }
00078 
00079     // Not terroribly efficient, but does the job.
00080     // Works for reading a line from stdin or a file.
00081     // Returns null on EOF.  If EOF appears in the middle
00082     // of a line, returns that line, then null on next call.
00083     //
00084     public static String getLine(InputStreamReader reader) {
00085         StringBuffer b = new StringBuffer();
00086         int c;
00087         try {
00088             while ((c = reader.read()) != -1 && c != '\n') {
00089                 if (c != '\r')
00090                     b.append((char)c);
00091             }
00092         } catch (IOException ioe) {
00093             c = -1;
00094         }
00095 
00096         if (c == -1 && b.length() == 0)
00097             return null;
00098         else
00099             return b.toString();
00100     }
00101 
00102     public void run(boolean removeExistingDatabase, String databaseName)
00103         throws DatabaseException, FileNotFoundException {
00104 
00105         // Remove the previous database.
00106         if (removeExistingDatabase)
00107             new File(databaseName).delete();
00108 
00109         // Create the database object.
00110         // There is no environment for this simple example.
00111         DatabaseConfig dbConfig = new DatabaseConfig();
00112         dbConfig.setErrorStream(System.err);
00113         dbConfig.setErrorPrefix("AccessExample");
00114         dbConfig.setType(DatabaseType.BTREE);
00115         dbConfig.setAllowCreate(true);
00116         Database table = new Database(databaseName, null, dbConfig);
00117 
00118         //
00119         // Insert records into the database, where the key is the user
00120         // input and the data is the user input in reverse order.
00121         //
00122         InputStreamReader reader = new InputStreamReader(System.in);
00123 
00124         for (;;) {
00125             String line = askForLine(reader, System.out, "input> ");
00126             if (line == null)
00127                 break;
00128 
00129             String reversed = (new StringBuffer(line)).reverse().toString();
00130 
00131             // See definition of StringDbt below
00132             //
00133             StringEntry key = new StringEntry(line);
00134             StringEntry data = new StringEntry(reversed);
00135 
00136             try {
00137                 if (table.putNoOverwrite(null, key, data) == OperationStatus.KEYEXIST)
00138                     System.out.println("Key " + line + " already exists.");
00139             } catch (DatabaseException dbe) {
00140                 System.out.println(dbe.toString());
00141             }
00142         }
00143 
00144         // Acquire an iterator for the table.
00145         Cursor cursor;
00146         cursor = table.openCursor(null, null);
00147 
00148         // Walk through the table, printing the key/data pairs.
00149         // See class StringDbt defined below.
00150         //
00151         StringEntry key = new StringEntry();
00152         StringEntry data = new StringEntry();
00153         while (cursor.getNext(key, data, null) == OperationStatus.SUCCESS)
00154             System.out.println(key.getString() + " : " + data.getString());
00155         cursor.close();
00156         table.close();
00157     }
00158 
00159     // Here's an example of how you can extend DatabaseEntry in a
00160     // straightforward way to allow easy storage/retrieval of strings,
00161     // or whatever kind of data you wish.  We've declared it as a static
00162     // inner class, but it need not be.
00163     //
00164     static /*inner*/
00165     class StringEntry extends DatabaseEntry {
00166         StringEntry() {
00167         }
00168 
00169         StringEntry(String value) {
00170             setString(value);
00171         }
00172 
00173         void setString(String value) {
00174             byte[] data = value.getBytes();
00175             setData(data);
00176             setSize(data.length);
00177         }
00178 
00179         String getString() {
00180             return new String(getData(), getOffset(), getSize());
00181         }
00182     }
00183 }

Generated on Sun Dec 25 12:14:26 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2