Package com.sleepycat.je

Examples of com.sleepycat.je.Cursor


  public List<WebURL> get(int max) throws DatabaseException {
    synchronized (mutex) {
      int matches = 0;
      List<WebURL> results = new ArrayList<WebURL>(max);

      Cursor cursor = null;
      OperationStatus result = null;
      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry value = new DatabaseEntry();
      Transaction txn;
      if (resumable) {
        txn = env.beginTransaction(null, null);
      } else {
        txn = null;
      }
      try {
        cursor = urlsDB.openCursor(txn, null);
        result = cursor.getFirst(key, value, null);

        while (matches < max && result == OperationStatus.SUCCESS) {
          if (value.getData().length > 0) {
            WebURL curi = (WebURL) webURLBinding.entryToObject(value);
            results.add(curi);
            matches++;
          }
          result = cursor.getNext(key, value, null);
        }
      } catch (DatabaseException e) {
        if (txn != null) {
          txn.abort();
          txn = null;
        }
        throw e;
      } finally {
        if (cursor != null) {
          cursor.close();
        }
        if (txn != null) {
          txn.commit();
        }
      }
View Full Code Here


  public void delete(int count) throws DatabaseException {
    synchronized (mutex) {
      int matches = 0;

      Cursor cursor = null;
      OperationStatus result = null;
      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry value = new DatabaseEntry();
      Transaction txn;
      if (resumable) {
        txn = env.beginTransaction(null, null);
      } else {
        txn = null;
      }
      try {
        cursor = urlsDB.openCursor(txn, null);
        result = cursor.getFirst(key, value, null);

        while (matches < count && result == OperationStatus.SUCCESS) {
          cursor.delete();
          matches++;
          result = cursor.getNext(key, value, null);
        }
      } catch (DatabaseException e) {
        if (txn != null) {
          txn.abort();
          txn = null;
        }
        throw e;
      } finally {
        if (cursor != null) {
          cursor.close();
        }
        if (txn != null) {
          txn.commit();
        }
      }
View Full Code Here

  public boolean removeURL(WebURL webUrl) {
    synchronized (mutex) {
      try {
        DatabaseEntry key = new DatabaseEntry(Util.int2ByteArray(webUrl.getDocid()));       
        Cursor cursor = null;
        OperationStatus result = null;
        DatabaseEntry value = new DatabaseEntry();
        Transaction txn = env.beginTransaction(null, null);
        try {
          cursor = urlsDB.openCursor(txn, null);
          result = cursor.getSearchKey(key, value, null);
         
          if (result == OperationStatus.SUCCESS) {
            result = cursor.delete();
            if (result == OperationStatus.SUCCESS) {
              return true;
            }
          }
        } catch (DatabaseException e) {
          if (txn != null) {
            txn.abort();
            txn = null;
          }
          throw e;
        } finally {
          if (cursor != null) {
            cursor.close();
          }
          if (txn != null) {
            txn.commit();
          }
        }
View Full Code Here

  * @return  String Numeric value of next key
  */

public synchronized String getNextKey() throws DatabaseException
{
  String lastKey = "0"; Cursor cursor = null;
  cursor = getCurrentDB().openCursor(null, null);
  DatabaseEntry key = new DatabaseEntry(); DatabaseEntry data = new DatabaseEntry();
  if (cursor.getLast(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
   lastKey = new String(key.getData())
  cursor.close();

  int i = new Integer(lastKey).intValue() + 1;
  String keyString = "" + i; 
  String padKeyString = StringTools.fillin( keyString, 10, false, '0', 10 - keyString.length());
  return padKeyString;
View Full Code Here

{ return fetchArray(key, rangeFlag, 10); }

public DatabaseEntry[]  fetchArray (String key, boolean rangeFlag, int RANGE)
{
  ArrayList<DatabaseEntry> al = new ArrayList<DatabaseEntry>();
  Cursor cursor = null;
  DatabaseEntry[] dbData = null;
  try
  {
   DatabaseEntry dbKey = new DatabaseEntry( key.getBytes("UTF-8") );
   DatabaseEntry dbe = new DatabaseEntry();
   cursor = getCurrentDB().openCursor(null, null);
   OperationStatus ostat = (rangeFlag) ? cursor.getSearchKeyRange(dbKey, dbe, LockMode.DEFAULT):
                            cursor.getSearchKey(dbKey, dbe, LockMode.DEFAULT);

   int numRecords = 0;
   if (cursor.count() >= 1)
   { while ( (ostat == OperationStatus.SUCCESS) && (numRecords < RANGE) )
   { al.add(dbe); dbe = new DatabaseEntry();
   ostat = (rangeFlag) ? cursor.getNext(dbKey, dbe, LockMode.DEFAULT):
    cursor.getNextDup(dbKey, dbe, LockMode.DEFAULT);
   numRecords++;
   }
   }
   else { al.add(dbe);
  }
  catch (RuntimeException re)
  { logger.error("Runtime error: Could not fetch documents with key " + key + " " + re.getMessage())}
  catch (UnsupportedEncodingException ue)
  { logger.error("Unsupported encoding: Could not fetch documents with key " + key + " " + ue.getMessage())}
  catch (DatabaseException de)
  { logger.error("Database error: Could not fetch documents with key " + key + " " + de.getMessage())}
  finally
  { try {if (cursor != null) cursor.close(); } catch (DatabaseException dbe) { } }

  if (al.size() >= 1)
  { dbData = new DatabaseEntry[al.size()];
  for (int i = 0; i < al.size(); i++) dbData[i] = (DatabaseEntry) al.get(i);
  }
View Full Code Here

  * @return boolean
  */
public synchronized boolean exists(String key) throws DatabaseException
{

  Cursor cursor = null;
  boolean retval = false;
  try
  { cursor = getCurrentDB().openCursor(null, null);
    DatabaseEntry dkey = new DatabaseEntry(key.getBytes("UTF-8") );
    DatabaseEntry data = new DatabaseEntry();
    if (
    cursor.getSearchKey(dkey, data, LockMode.DEFAULT) ==
      OperationStatus.SUCCESS ) retval = true;
  }
  catch (UnsupportedEncodingException e)
  { logger.error("Invalid encoding " + e.getMessage() )}
  finally { if (cursor != null) cursor.close(); }
  return (retval);
}
View Full Code Here

  * @param size The size of the pre-loaded database, no. of records
  * @return HashMap<String, String> Hashmap containing keys and row contents
  */
public HashMap<String, String> preLoadDB(String dbName, TupleBinding tpb, int size)
{
  boolean readOnly = true; boolean dupFlag = false; Cursor cursor = null;
  HashMap<String, String> hash = new HashMap<String, String> (size, (float) 0.95);
  try
  { openDB(dbName, readOnly, dupFlag);
    cursor = getCurrentDB().openCursor(null, null);
    DatabaseEntry key = new DatabaseEntry(); DatabaseEntry data = new DatabaseEntry();
    while (cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
     { String skey = new String(key.getData(), "UTF-8");
       String sdata = tpb.entryToObject(data).toString();
       hash.put( skey, sdata );
     }
  }
  catch (DatabaseException de) { logger.error("Database error " + de.getMessage() ); }
  catch (UnsupportedEncodingException ue) { logger.error("Encoding error " + ue.getMessage() ); }
  finally {
   try { if (cursor != null) cursor.close(); closeDB(dbName); }
   catch (DatabaseException de) { logger.error("Ignore error"); } }
  return hash;  
}
View Full Code Here

  */
public boolean dumpDB(String dbName, TupleBinding tpb, String filename)
  boolean retval = true;
  boolean readOnly = true; boolean dupFlag = false;
  Cursor cursor = null; PrintWriter pw = null;
  try
  {
   pw = new PrintWriter( new BufferedOutputStream( new FileOutputStream(filename) ) );
   openDB(dbName, readOnly, dupFlag);
   cursor = getCurrentDB().openCursor(null, null);
   DatabaseEntry key = new DatabaseEntry(); DatabaseEntry data = new DatabaseEntry();
   while (cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
   { String sdata = tpb.entryToObject(data).toString();
   pw.println(sdata);
   }
  }
  catch (DatabaseException de) { logger.error("Database error " + de.getMessage() ); retval = false; }
  catch (FileNotFoundException fe) { logger.error("Could not find file " + dbName + " " + fe.getMessage()); }
  finally {
   try
   { if (pw != null) pw.close();
   if (cursor != null) cursor.close();
   closeDB(dbName);
   }
   catch (DatabaseException de) { logger.error("Ignore error"); }
  }
  return (retval);
View Full Code Here

  //*-- scan the database by file name and check if the file name exists, otherwise
  //*-- delete the entry and keep track of the deletions
  DbTools dbt = Constants.getDbt();
  dbt.openDB(Constants.EXT_FILES_DB, false, false);
  Cursor cursor = null; int count = 0; int errors = 0;
  ArrayList<String> delFiles = new ArrayList<String>();
  IndexableDoc idoc = new IndexableDoc();
  try
  {
   cursor = dbt.getCurrentDB().openCursor(null, null);
   DatabaseEntry key = new DatabaseEntry();
   DatabaseEntry data = new DatabaseEntry();
   LOOP: while (cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
   {
    //*-- check if the file name exists in the filesystem
    if (!running) break LOOP;
    String filename = new String( key.getData(), "UTF-8");
    File file = new File(filename);
    if (!file.exists())
    { out.append("ERROR: File " + filename + " does not exist" + Constants.NEWLINE);
    idoc = (IndexableDoc) idoc.getBdbBinding().entryToObject(data);

    //*-- remove the entry from the database
    if (!dbt.delete(filename))
     logger.error("Could not delete " + filename + " from the database");
    else
    { logger.info("Deleted " + filename + " from the database"); delFiles.add(filename); }
    errors++;
    } //*-- end of outer if
    ++count;
   } //*-- end of while

   //*-- clean up the Lucene index
   FSDirectory fsd = FSDirectory.getDirectory(new File(Constants.getINDEXDIR()), false);
   IndexReader ir = IndexReader.open(fsd);
   for (int i = 0; i < delFiles.size(); i++)
    ir.deleteDocuments(new Term("key", (String) delFiles.get(i)) );
   ir.close();
  }
  catch (DatabaseException dbe)
  { logger.error("Could not open cursor to browse File index" + dbe.getMessage() ); }
  catch (UnsupportedEncodingException ue)
  { logger.error("Could not decode key" + ue.getMessage() ); }
  catch (IOException ie)
  { logger.error("Could not read index directory" + ie.getMessage() ); }
  finally
  { try
  { if (cursor != null) cursor.close();
  dbt.closeDB();
  }
  catch (DatabaseException de) { logger.error("Ignore error"); }
  }

View Full Code Here

  docTypes[j++] = key; }

  //*-- scan the database by file name and keep track of the types of files in the index
  DbTools dbt = Constants.getDbt();  
  dbt.openDB(Constants.EXT_FILES_DB, true, false);
  Cursor cursor = null; IndexableDoc idoc = new IndexableDoc();
  try
  {
   cursor = dbt.getCurrentDB().openCursor(null, null);
   DatabaseEntry key = new DatabaseEntry();
   DatabaseEntry data = new DatabaseEntry(); int i = 0;
   LOOP: while (cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
   {
    if (!running) break LOOP;
    idoc = (IndexableDoc) idoc.getBdbBinding().entryToObject(data);
    String docType = idoc.getFileType();
    if (docType == null) docType = "unknown";
    String num =  stats.get(docType);
    if ( num == null) { docType = "unknown"; num =  stats.get(docType); }
    int val =  Integer.parseInt(num) + 1;
   
    i++; if ( (i % 1000) == 0) System.out.println("finished " + i + " documents");
   
    stats.put(docType, String.valueOf(val) );
   } //*-- end of while

  } //*-- end of try
  catch (DatabaseException dbe)
  { logger.error("Could not open cursor to browse File index" + dbe.getMessage() ); }
  finally
  { try { if (cursor != null) cursor.close();  dbt.closeDB(); }
  catch (DatabaseException de) { logger.error("Ignore error"); }
  }

  StringBuffer out = new StringBuffer(); int numFiles = 0;
  int io;
View Full Code Here

TOP

Related Classes of com.sleepycat.je.Cursor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.