Personal tools

Webarc:Berkeley DB Wrapper for Carryover DB

From Adapt

Jump to: navigation, search

What It Does

Two wrapper classes to be used by C/C++ codes via JNI.

How To Build

In Eclipse, export 'mwbdbwrap' as a JAR.

  1. Right-click on 'mwbdbwrap' in Package Explorer, select 'export'.
  2. Select mwbdbwrap/src (should have been already selected).
  3. Put <your directory>/mwbdbwrap.jar in Export destination.
  4. Select 'Export generated class files and resources'
  5. Select 'Add directory entries' in options
  6. Click 'Finish'

Usage

Example C++ Code


//
// BDBTaggedDocumentIterator
//
// 22 September 2009 -- scsong
//

#ifndef INDRI_TRECDOCUMENTITERATOR_BDB_HPP
#define INDRI_TRECDOCUMENTITERATOR_BDB_HPP

#include <string>
#include <fstream>
#include <jni.h>

class BDBTaggedDocumentIterator : public DocumentIterator {
private:
   UnparsedDocument _document;
   FILE *_in;
   JavaVM* _jvm;
   JNIEnv* _jniEnv;
   jobject _bdb;
   jclass _clsRevisionDatabase;
   jclass _clsRevisionData;
   jmethodID _mid_RevisionDatabase_getNext;
   jmethodID _mid_RevisionDatabase_construct;
   jmethodID _mid_RevisionDatabase_close;
   jfieldID _fid_RevisionData_date;
   jfieldID _fid_RevisionData_fileName;
   jfieldID _fid_RevisionData_offset;
   
   void _create_vm();

   class RevisionData {
   private:
      JNIEnv* _rdenv;
   public:
      long date;
      const char* filename;
      long offset;
      RevisionData(JNIEnv *env, jobject obj, jfieldID date, jfieldID fname, jfieldID offset);
      ~RevisionData();
   };
   void _openDB(const char* dbName);
   void _closeDB();
   RevisionData* _getNextDocument();
   UnparsedDocument* _nextDocument();

public:
   BDBTaggedDocumentIterator();
   ~BDBTaggedDocumentIterator();
   void setTags( const char* startDoc, const char* endDoc, const char* endMetadata );
   void open( const std::string& filename );
   void close();

   UnparsedDocument* nextDocument();

};

void indri::parse::BDBTaggedDocumentIterator::_create_vm() {
   JavaVMInitArgs vm_args;
   JavaVMOption options[2];
   
   /* For LINUX/UNIX */
   string opt1("-Djava.class.path=./lib/mwbdbwrap.jar:./lib/je-3.3.87.jar");

   /* For Windows */
   //string opt1("-Djava.class.path=.\\lib\\mwbdbwrap.jar;.\\lib\\je-3.3.87.jar");

   string opt2("-verbose:class");
   options[0].optionString = (char*)(opt1.c_str());
   options[1].optionString = (char*)(opt2.c_str());
   vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
   vm_args.nOptions = 1; // Change this to 2 for verbosity
   vm_args.options = options;
   vm_args.ignoreUnrecognized = 0;
   int ret = JNI_CreateJavaVM(&_jvm, (void**)&_jniEnv, &vm_args);
   if(ret < 0)
      printf("\nUnable to Launch JVM\n");

   _clsRevisionDatabase = jniEnv->FindClass("edu/umd/umiacs/mw/bdb/RevisionDatabase");
   if (_jniEnv->ExceptionCheck()) {
      _jniEnv->ExceptionDescribe();
   }
   _mid_RevisionDatabase_construct = _jniEnv->GetMethodID(_clsRevisionDatabase, "<init>", "(Ljava/lang/String;)V");
   _mid_RevisionDatabase_close = _jniEnv->GetMethodID(_clsRevisionDatabase, "close", "()V");
   _mid_RevisionDatabase_getNext = _jniEnv->GetMethodID(_clsRevisionDatabase, "getNext", "()Ledu/umd/umiacs/mw/bdb/RevisionData;");

   _clsRevisionData = _jniEnv->FindClass("edu/umd/umiacs/mw/bdb/RevisionData");
   _fid_RevisionData_date = _jniEnv->GetFieldID(_clsRevisionData, "date", "J");
   _fid_RevisionData_fileName = _jniEnv->GetFieldID(_clsRevisionData, "fileName", "Ljava/lang/String;");
   _fid_RevisionData_offset= _jniEnv->GetFieldID(_clsRevisionData, "offset", "J");
}

indri::parse::BDBTaggedDocumentIterator::RevisionData::RevisionData(JNIEnv *env, jobject obj, jfieldID fidDate, jfieldID fidFname, jfieldID fidOffset) {
   _rdenv = env;
   date = env->GetLongField(obj, fidDate);
   jstring jsfilename =  (jstring)env->GetObjectField(obj, fidFname);
   filename = env->GetStringUTFChars(jsfilename, 0);
   offset = env->GetLongField(obj, fidOffset);
}

indri::parse::BDBTaggedDocumentIterator::RevisionData::~RevisionData() {
   //   _rdenv->ReleaseStringUTFChars(jsfilename, filename);

}

void indri::parse::BDBTaggedDocumentIterator::_openDB(const char* dbName) {
   jstring jsdbName = _jniEnv->NewStringUTF(dbName);
   _bdb = _jniEnv->NewObject(_clsRevisionDatabase, _mid_RevisionDatabase_construct, jsdbName);
}

void indri::parse::BDBTaggedDocumentIterator::_closeDB() {
   _jniEnv->CallObjectMethod(_bdb, _mid_RevisionDatabase_close);
}


indri::parse::BDBTaggedDocumentIterator::RevisionData* indri::parse::BDBTaggedDocumentIterator::_getNextDocument() {
   RevisionData *revData = NULL;
   jobject joRevData = _jniEnv->CallObjectMethod(_bdb, _mid_RevisionDatabase_getNext);
   //if (_jniEnv->ExceptionCheck()) {
   //   _jniEnv->ExceptionDescribe();
   //}

   if (joRevData != NULL) {
      revData = new RevisionData(_jniEnv, joRevData, _fid_RevisionData_date, _fid_RevisionData_fileName, _fid_RevisionData_offset);
   }
   return revData;
}


Source Codes

svn co http://narasvn.umiacs.umd.edu/repository/src/webarc/mwbdbwrap