head	1.2;
access;
symbols
	RPM_4_2_1:1.1.1.5
	RPM_4_2:1.1.1.5
	RPM_4_1_1:1.1.1.5
	RPM_4_1:1.1.1.4
	RPM_4_0_5:1.1.1.3
	RPM_4_0_4:1.1.1.2
	RPM_4_0_3:1.1.1.1
	RPM:1.1.1;
locks; strict;
comment	@# @;


1.2
date	2008.01.02.09.57.16;	author rse;	state dead;
branches;
next	1.1;
commitid	z4cpSiAhOCXk5PLs;

1.1
date	2001.07.23.20.45.39;	author rse;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2001.07.23.20.45.39;	author rse;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	2002.01.08.01.12.40;	author rse;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	2003.01.18.13.49.06;	author rse;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	2001.10.15.03.47.26;	author rse;	state Exp;
branches;
next	1.1.1.5;

1.1.1.5
date	2003.01.18.14.05.02;	author rse;	state Exp;
branches;
next	;


desc
@@


1.2
log
@remove the ancient RPM 4.2.1 source tree copy
@
text
@/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997-2001
 *	Sleepycat Software.  All rights reserved.
 *
 * $Id: BulkAccessExample.java,v 1.1 2001/07/23 20:45:39 rse Exp $
 */

package com.sleepycat.examples;

import com.sleepycat.db.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;

class BulkAccessExample
{
    private static final String FileName = "access.db";

    public BulkAccessExample()
    {
    }

    public static void main(String argv[])
    {
        try
        {
            BulkAccessExample app = new BulkAccessExample();
            app.run();
        }
        catch (DbException dbe)
        {
            System.err.println("BulkAccessExample: " + dbe.toString());
            System.exit(1);
        }
        catch (FileNotFoundException fnfe)
        {
            System.err.println("BulkAccessExample: " + fnfe.toString());
            System.exit(1);
        }
        System.exit(0);
    }

    // Prompts for a line, and keeps prompting until a non blank
    // line is returned.  Returns null on error.
    //
    static public String askForLine(InputStreamReader reader,
                                    PrintStream out, String prompt)
    {
        String result = "";
        while (result != null && result.length() == 0) {
            out.print(prompt);
            out.flush();
            result = getLine(reader);
        }
        return result;
    }

    // Not terribly efficient, but does the job.
    // Works for reading a line from stdin or a file.
    // Returns null on EOF.  If EOF appears in the middle
    // of a line, returns that line, then null on next call.
    //
    static public String getLine(InputStreamReader reader)
    {
        StringBuffer b = new StringBuffer();
        int c;
        try {
            while ((c = reader.read()) != -1 && c != '\n') {
                if (c != '\r')
                    b.append((char)c);
            }
        }
        catch (IOException ioe) {
            c = -1;
        }

        if (c == -1 && b.length() == 0)
            return null;
        else
            return b.toString();
    }

    public void run()
         throws DbException, FileNotFoundException
    {
        // Remove the previous database.
        new File(FileName).delete();

        // Create the database object.
        // There is no environment for this simple example.
        Db table = new Db(null, 0);
        table.set_error_stream(System.err);
        table.set_errpfx("BulkAccessExample");
        table.open(FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);

        //
        // Insert records into the database, where the key is the user
        // input and the data is the user input in reverse order.
        //
        InputStreamReader reader = new InputStreamReader(System.in);

        for (;;) {
            String line = askForLine(reader, System.out, "input> ");
            if (line == null)
                break;

            String reversed = (new StringBuffer(line)).reverse().toString();

            // See definition of StringDbt below
            //
            StringDbt key = new StringDbt(line);
            StringDbt data = new StringDbt(reversed);

            try
            {
                int err;
                if ((err = table.put(null, 
		    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
                        System.out.println("Key " + line + " already exists.");
                }
            }
            catch (DbException dbe)
            {
                System.out.println(dbe.toString());
            }
            System.out.println("");
        }

        // Acquire a cursor for the table and two Dbts.
	Dbc dbc = table.cursor(null, 0);
        Dbt foo = new Dbt();
	foo.set_flags(Db.DB_DBT_MALLOC);

        Dbt bulk_data = new Dbt();

	// Set Db.DB_DBT_USERMEM on the data Dbt;  Db.DB_MULTIPLE_KEY requires
	// it.  Then allocate a byte array of a reasonable size;  we'll 
	// go through the database in chunks this big.
	bulk_data.set_flags(Db.DB_DBT_USERMEM);
	bulk_data.set_data(new byte[1000000]);
	bulk_data.set_ulen(1000000);

	
        // Walk through the table, printing the key/data pairs.
        //
        while (dbc.get(foo, bulk_data, Db.DB_NEXT | Db.DB_MULTIPLE_KEY) == 0)
        {
        
	    DbMultipleKeyDataIterator iterator;
            iterator = new DbMultipleKeyDataIterator(bulk_data);

	    StringDbt key, data;
	    key = new StringDbt();
	    data = new StringDbt();

	    while (iterator.next(key, data)) {
		System.out.println(key.getString() + " : " + data.getString());
	    }
        }
	dbc.close();
        table.close(0);
    }

    // Here's an example of how you can extend a Dbt in a straightforward
    // way to allow easy storage/retrieval of strings, or whatever
    // kind of data you wish.  We've declared it as a static inner
    // class, but it need not be.
    //
    static /*inner*/
    class StringDbt extends Dbt
    {
        StringDbt()
        {
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
        }

        StringDbt(String value)
        {
            setString(value);
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
        }

        void setString(String value)
        {
            set_data(value.getBytes());
	    set_offset(0);
            set_size(value.length());
        }

        String getString()
        {
            return new String(get_data(), get_offset(), get_size());
        }
    }
}
@


1.1
log
@Initial revision
@
text
@d7 1
a7 1
 * $Id: BulkAccessExample.java,v 1.2 2001/05/12 21:43:27 dda Exp $
@


1.1.1.1
log
@Import: RPM 4.0.3
@
text
@@


1.1.1.2
log
@Import: RPM 4.0.4
@
text
@d7 1
a7 1
 * Id: BulkAccessExample.java,v 1.3 2001/10/05 02:36:07 bostic Exp 
d121 1
a121 1
                if ((err = table.put(null,
d141 1
a141 1
	// it.  Then allocate a byte array of a reasonable size;  we'll
d147 1
a147 1

d152 1
a152 1

@


1.1.1.3
log
@Import: RPM 4.0.5
@
text
@d4 1
a4 1
 * Copyright (c) 1997-2002
d7 1
a7 1
 * Id: BulkAccessExample.java,v 1.6 2002/02/05 22:27:13 mjc Exp 
d98 1
a98 1
        table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
d122 1
a122 1
                    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
d134 1
a134 1
        Dbc dbc = table.cursor(null, 0);
d136 1
a136 1
        foo.set_flags(Db.DB_DBT_MALLOC);
d140 6
a145 6
        // Set Db.DB_DBT_USERMEM on the data Dbt;  Db.DB_MULTIPLE_KEY requires
        // it.  Then allocate a byte array of a reasonable size;  we'll
        // go through the database in chunks this big.
        bulk_data.set_flags(Db.DB_DBT_USERMEM);
        bulk_data.set_data(new byte[1000000]);
        bulk_data.set_ulen(1000000);
d152 2
a153 1
            DbMultipleKeyDataIterator iterator;
d156 7
a162 7
            StringDbt key, data;
            key = new StringDbt();
            data = new StringDbt();

            while (iterator.next(key, data)) {
                System.out.println(key.getString() + " : " + data.getString());
            }
d164 1
a164 1
        dbc.close();
d189 3
a191 3
            byte[] data = value.getBytes();
            set_data(data);
            set_size(data.length);
@


1.1.1.4
log
@Import: RPM 4.1
@
text
@d4 1
a4 1
 * Copyright (c) 1997-2001
d7 1
a7 1
 * Id: BulkAccessExample.java,v 1.3 2001/10/05 02:36:07 bostic Exp 
d98 1
a98 1
        table.open(FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
d122 1
a122 1
		    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
d134 1
a134 1
	Dbc dbc = table.cursor(null, 0);
d136 1
a136 1
	foo.set_flags(Db.DB_DBT_MALLOC);
d140 6
a145 6
	// Set Db.DB_DBT_USERMEM on the data Dbt;  Db.DB_MULTIPLE_KEY requires
	// it.  Then allocate a byte array of a reasonable size;  we'll
	// go through the database in chunks this big.
	bulk_data.set_flags(Db.DB_DBT_USERMEM);
	bulk_data.set_data(new byte[1000000]);
	bulk_data.set_ulen(1000000);
d152 2
d155 3
a157 2
	    DbMultipleKeyDataIterator iterator;
            iterator = new DbMultipleKeyDataIterator(bulk_data);
d159 3
a161 7
	    StringDbt key, data;
	    key = new StringDbt();
	    data = new StringDbt();

	    while (iterator.next(key, data)) {
		System.out.println(key.getString() + " : " + data.getString());
	    }
d163 1
a163 1
	dbc.close();
d188 3
a190 3
            set_data(value.getBytes());
	    set_offset(0);
            set_size(value.length());
@


1.1.1.5
log
@Import: RPM 4.1.1
@
text
@d4 1
a4 1
 * Copyright (c) 1997-2002
d7 1
a7 1
 * Id: BulkAccessExample.java,v 1.6 2002/02/05 22:27:13 mjc Exp 
d98 1
a98 1
        table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
d122 1
a122 1
                    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
d134 1
a134 1
        Dbc dbc = table.cursor(null, 0);
d136 1
a136 1
        foo.set_flags(Db.DB_DBT_MALLOC);
d140 6
a145 6
        // Set Db.DB_DBT_USERMEM on the data Dbt;  Db.DB_MULTIPLE_KEY requires
        // it.  Then allocate a byte array of a reasonable size;  we'll
        // go through the database in chunks this big.
        bulk_data.set_flags(Db.DB_DBT_USERMEM);
        bulk_data.set_data(new byte[1000000]);
        bulk_data.set_ulen(1000000);
d152 2
a153 1
            DbMultipleKeyDataIterator iterator;
d156 7
a162 7
            StringDbt key, data;
            key = new StringDbt();
            data = new StringDbt();

            while (iterator.next(key, data)) {
                System.out.println(key.getString() + " : " + data.getString());
            }
d164 1
a164 1
        dbc.close();
d189 3
a191 3
            byte[] data = value.getBytes();
            set_data(data);
            set_size(data.length);
@


