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

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

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