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

AssociateCallbacks.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2001-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: AssociateCallbacks.java,v 12.1 2005/06/16 20:23:40 bostic Exp $
00008  */
00009 
00010 package com.sleepycat.db.rpcserver;
00011 
00012 import com.sleepycat.db.*;
00013 
00015 class AssociateCallbacks {
00016     /*
00017      * Tcl passes one of these special flags for the callbacks used in the
00018      * test suite.  Note: these must match db_int.in!
00019      */
00020     static final int DB_RPC2ND_REVERSEDATA = 0x00100000;
00021     static final int DB_RPC2ND_NOOP = 0x00200000;
00022     static final int DB_RPC2ND_CONCATKEYDATA = 0x00300000;
00023     static final int DB_RPC2ND_CONCATDATAKEY = 0x00400000;
00024     static final int DB_RPC2ND_REVERSECONCAT = 0x00500000;
00025     static final int DB_RPC2ND_TRUNCDATA = 0x00600000;
00026     static final int DB_RPC2ND_CONSTANT = 0x00700000;
00027     static final int DB_RPC2ND_GETZIP = 0x00800000;
00028     static final int DB_RPC2ND_GETNAME = 0x00900000;
00029 
00030     static final int DB_RPC2ND_MASK = 0x00f00000;
00031 
00032     static SecondaryKeyCreator getCallback(int flags) {
00033         switch(flags & DB_RPC2ND_MASK) {
00034         case 0:
00035             return null;
00036 
00037         case DB_RPC2ND_REVERSEDATA:
00038             return new SecondaryKeyCreator() {
00039                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00040                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00041                     throws DatabaseException {
00042                     byte[] dataBuf = data.getData();
00043                     int dataSize = data.getSize();
00044                     byte[] buf = new byte[dataSize];
00045                     for (int i = 0; i < dataSize; i++)
00046                         buf[dataSize - 1 - i] = dataBuf[i];
00047                     result.setData(buf);
00048                     result.setSize(buf.length);
00049                     return true;
00050                 }
00051             };
00052 
00053         case DB_RPC2ND_NOOP:
00054             return new SecondaryKeyCreator() {
00055                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00056                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00057                     throws DatabaseException {
00058                     result.setData(data.getData());
00059                     result.setSize(data.getSize());
00060                     return true;
00061                 }
00062             };
00063 
00064         case DB_RPC2ND_CONCATKEYDATA:
00065             return new SecondaryKeyCreator() {
00066                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00067                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00068                     throws DatabaseException {
00069                     byte[] buf = new byte[key.getSize() +
00070                                           data.getSize()];
00071                     System.arraycopy(key.getData(), 0,
00072                                      buf, 0,
00073                                      key.getSize());
00074                     System.arraycopy(data.getData(), 0,
00075                                      buf, key.getSize(),
00076                                      data.getSize());
00077                     result.setData(buf);
00078                     result.setSize(buf.length);
00079                     return true;
00080                 }
00081             };
00082 
00083         case DB_RPC2ND_CONCATDATAKEY:
00084             return new SecondaryKeyCreator() {
00085                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00086                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00087                     throws DatabaseException {
00088                     byte[] buf = new byte[key.getSize() +
00089                                           data.getSize()];
00090                     System.arraycopy(data.getData(), 0,
00091                                      buf, 0,
00092                                      data.getSize());
00093                     System.arraycopy(key.getData(), 0,
00094                                      buf, data.getSize(),
00095                                      key.getSize());
00096                     result.setData(buf);
00097                     result.setSize(buf.length);
00098                     return true;
00099                 }
00100             };
00101 
00102         case DB_RPC2ND_REVERSECONCAT:
00103             return new SecondaryKeyCreator() {
00104                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00105                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00106                     throws DatabaseException {
00107                     byte[] keyBuf = key.getData();
00108                     int keySize = key.getSize();
00109                     byte[] dataBuf = data.getData();
00110                     int dataSize = data.getSize();
00111                     byte[] buf = new byte[keySize + dataSize];
00112                     for (int i = 0; i < keySize; i++)
00113                         buf[buf.length - 1 - i] = keyBuf[i];
00114                     for (int i = 0; i < dataSize; i++)
00115                         buf[dataSize - 1 - i] = dataBuf[i];
00116                     result.setData(buf);
00117                     result.setSize(buf.length);
00118                     return true;
00119                 }
00120             };
00121 
00122         case DB_RPC2ND_TRUNCDATA:
00123             return new SecondaryKeyCreator() {
00124                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00125                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00126                     throws DatabaseException {
00127                     result.setData(data.getData());
00128                     result.setOffset(1);
00129                     result.setSize(data.getSize() - 1);
00130                     return true;
00131                 }
00132             };
00133 
00134         case DB_RPC2ND_CONSTANT:
00135             return new SecondaryKeyCreator() {
00136                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00137                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00138                     throws DatabaseException {
00139                     byte[] buf = "constant data".getBytes();
00140                     result.setData(buf);
00141                     result.setSize(buf.length);
00142                     return true;
00143                 }
00144             };
00145 
00146         case DB_RPC2ND_GETZIP:
00147             return new SecondaryKeyCreator() {
00148                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00149                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00150                     throws DatabaseException {
00151                     result.setData(data.getData());
00152                     result.setSize(5);
00153                     return true;
00154                 }
00155             };
00156 
00157         case DB_RPC2ND_GETNAME:
00158             return new SecondaryKeyCreator() {
00159                 public boolean createSecondaryKey(SecondaryDatabase secondary,
00160                                                   DatabaseEntry key, DatabaseEntry data, DatabaseEntry result)
00161                     throws DatabaseException {
00162                     result.setData(data.getData());
00163                     result.setOffset(5);
00164                     result.setSize(data.getSize() - 5);
00165                     return true;
00166                 }
00167             };
00168 
00169         default:
00170             Server.err.println("Warning: Java RPC server doesn't implement callback: " + (flags & DB_RPC2ND_MASK));
00171             return null;
00172         }
00173     }
00174 
00175     // Utility classes should not have a public or default constructor
00176     protected AssociateCallbacks() {
00177     }
00178 }

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