com.sleepycat.je.DatabaseException Java Examples

The following examples show how to use com.sleepycat.je.DatabaseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BJETxLock.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public synchronized boolean tryLock() {
			try {
				if (readCount.get() == 0) {
//					lock = getEnv().getLock(getLockerId(), true, objectId, LockRequestMode.READ);
				}
				
				if (lock != null) {
					readCount.set(readCount.get() + 1);
					return true;
				}
				else {
					return false;
				}
			}
			catch (LockNotAvailableException le) {
				return false;
			}
			catch (DatabaseException ex) {
				throw new HGException(ex);
			}
		}
 
Example #2
Source File: StandardEnvironmentFacade.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Override
public void commit(com.sleepycat.je.Transaction tx, boolean syncCommit)
{
    try
    {
        tx.commitNoSync();
    }
    catch (DatabaseException de)
    {
        LOGGER.error("Got DatabaseException on commit, closing environment", de);

        closeEnvironmentSafely();

        throw handleDatabaseException("Got DatabaseException on commit", de);
    }
    _committer.commit(tx, syncCommit);
}
 
Example #3
Source File: BJETxCursor.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public void close() {
	if (!open)
		return;
	try {
		cursor.close();
	}
	catch (DatabaseException ex) {
		throw new HGException(ex);
	}
	finally {
		open = false;
		cursor = null;
		if (tx != null) {
			tx.removeCursor(this);
		}
	}
}
 
Example #4
Source File: WebGraph.java    From codes-scratch-crawler with Apache License 2.0 6 votes vote down vote up
/**
 * 加入节点之间的对应关系,如果节点不存在,就创建,如果已经存在对应关 系,就更新权重
 * 
 */
public void addLink(String fromLink, String toLink) throws DatabaseException {
  Link outLinks = new Link();
  outLinks.fromURL = fromLink;
  outLinks.toURL = new HashSet<String>();
  outLinks.toURL.add(toLink);
  boolean inserted = outLinkIndex.putNoOverwrite(outLinks);
  if (!inserted) {
    outLinks = outLinkIndex.get(fromLink);
    outLinks.toURL.add(toLink);
    // System.out.println("outLinks : "+ outLinks.fromURL + "
    // outLinks.toURL:"+outLinks.toURL.size());
    // System.out.println(fromLink+" : "+ toLink);
    outLinkIndex.put(outLinks);
  }
}
 
Example #5
Source File: WebGraph.java    From codes-scratch-crawler with Apache License 2.0 6 votes vote down vote up
/**
 * 构造Web图,从文件内读入。每一行为一个对应关系,例如 http://url1.com -> http://url2.com 1.0 表示对于链接
 * http://url1.com所表示的网页上面,有一个超链接http://url2.com 并且他们之间的权重为1.0
 */
public void load(File file) throws IOException, FileNotFoundException, DatabaseException {
  @SuppressWarnings("resource")
  BufferedReader reader = new BufferedReader(new FileReader(file));
  String line;
  while ((line = reader.readLine()) != null) {
    int index1 = line.indexOf("->");
    if (index1 == -1) {
      continue;
    } else {
      String url1 = line.substring(0, index1).trim();
      String url2 = line.substring(index1 + 2).trim();
      // Double strength = new Double(1.0);
      index1 = url2.indexOf(" ");
      if (index1 != -1)
        try {
          // strength = new
          // Double(url2.substring(index1+1).trim());
          url2 = url2.substring(0, index1).trim();
        } catch (Exception e) {
        }
      addLink(url1, url2);
    }
  }
}
 
Example #6
Source File: WebGraph.java    From codes-scratch-crawler with Apache License 2.0 6 votes vote down vote up
/**
 * 构造函数
 */
public WebGraph(String dbDir) throws DatabaseException {
  File envDir = new File(dbDir);
  EnvironmentConfig envConfig = new EnvironmentConfig();
  envConfig.setTransactional(false);
  envConfig.setAllowCreate(true);
  Environment env = new Environment(envDir, envConfig);

  StoreConfig storeConfig = new StoreConfig();
  storeConfig.setAllowCreate(true);
  storeConfig.setTransactional(false);

  store = new EntityStore(env, "classDb", storeConfig);
  outLinkIndex = store.getPrimaryIndex(String.class, Link.class);
  inLinkIndex = store.getSecondaryIndex(outLinkIndex, String.class, "toURL");
}
 
Example #7
Source File: BerkeleyDB.java    From SPADE with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method is invoked by the kernel to shut down the storage.
 *
 * @return True if the storage was shut down successfully.
 */
@Override
public boolean shutdown()
{
    try
    {
        if (myDatabase != null)
            myDatabase.close();
        if (vertexDatabase != null)
            vertexDatabase.close();
        if(edgeDatabase != null)
            edgeDatabase.close();
        if (myDbEnvironment != null)
            myDbEnvironment.close();
        return true;
    }
    catch(DatabaseException ex)
    {
        Logger.getLogger(BerkeleyDB.class.getName()).log(Level.WARNING, null, ex);
    }

    return false;
}
 
Example #8
Source File: StandardEnvironmentFacade.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void closeSequences()
{
    RuntimeException firstThrownException = null;
    for (DatabaseEntry  sequenceKey : _cachedSequences.keySet())
    {
        try
        {
            closeSequence(sequenceKey);
        }
        catch(DatabaseException de)
        {
            if (firstThrownException == null)
            {
                firstThrownException = de;
            }
        }
    }
    if (firstThrownException != null)
    {
        throw firstThrownException;
    }
}
 
Example #9
Source File: CompressedStorage.java    From SPADE with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method is invoked by the kernel to shut down the storage.
 *
 * @return True if the storage was shut down successfully.
 */
public boolean shutdown()
{
	System.out.println("Average time to put 10000 edges in the annotations storage : " + annotationsTime);
	System.out.println("Average time to put 10000 edges in the scaffold strage: " + scaffoldTime);
	try
	{
		if (scaffoldDatabase != null)
			scaffoldDatabase.close();
		if (annotationsDatabase != null)
			annotationsDatabase.close();
		if (DatabaseEnvironment1 != null)
			DatabaseEnvironment1.close();
		if (DatabaseEnvironment2 != null)
			DatabaseEnvironment2.close();
		compresser.end();
		benchmarks.close();
		return true;
	}
	catch(DatabaseException ex)
	{
		logger.log(Level.SEVERE, "Compressed Storage Shutdown not successful!", ex);
	}

	return false;
}
 
Example #10
Source File: StandardEnvironmentFacade.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void closeDatabases()
{
    RuntimeException firstThrownException = null;
    for (String databaseName : _cachedDatabases.keySet())
    {
        try
        {
            closeDatabase(databaseName);
        }
        catch(DatabaseException e)
        {
            if (firstThrownException == null)
            {
                firstThrownException = e;
            }
        }
    }
    if (firstThrownException != null)
    {
        throw firstThrownException;
    }
}
 
Example #11
Source File: WebGraph.java    From codes-scratch-crawler with Apache License 2.0 6 votes vote down vote up
public String[] inLinks(String URL) throws DatabaseException {
  EntityIndex<String, Link> subIndex = inLinkIndex.subIndex(URL);
  // System.out.println(subIndex.count());
  String[] linkList = new String[(int) subIndex.count()];
  int i = 0;
  EntityCursor<Link> cursor = subIndex.entities();
  try {
    for (Link entity : cursor) {
      linkList[i++] = entity.fromURL;
      // System.out.println(entity.fromURL);
    }
  } finally {
    cursor.close();
  }
  return linkList;
}
 
Example #12
Source File: TransactionBJEImpl.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public void commit() throws HGTransactionException
	{
//		if (t != null && traceme)
//			System.err.println("Committing tx " + t.getId());
		try
		{
			Set<BJETxCursor> S = new HashSet<BJETxCursor>(bdbCursors);
			for (BJETxCursor c : S)
				c.close();
			if (t != null)
				t.commit();
		}
		catch (DatabaseException ex)
		{
			throw new HGTransactionException("Failed to commit transaction",
					ex);
		}
	}
 
Example #13
Source File: DefaultBiIndexImpl.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public long countKeys(ValueType value) {
	DatabaseEntry keyEntry = new DatabaseEntry(valueConverter.toByteArray(value));
	DatabaseEntry valueEntry = new DatabaseEntry();
	SecondaryCursor cursor = null;
	
	try {
		cursor = secondaryDb.openCursor(txn().getBJETransaction(), cursorConfig);
		OperationStatus status = cursor.getSearchKey(keyEntry, valueEntry, dummy, LockMode.DEFAULT);
		if (status == OperationStatus.SUCCESS)
			return cursor.count();
		else
			return 0;
	}
	catch (DatabaseException ex) {
		throw new HGException(ex);
	}
	finally {
		if (cursor != null) {
			try {
				cursor.close();
			}
			catch (Throwable t) {
			}
		}
	}
}
 
Example #14
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public boolean containsLink(HGPersistentHandle handle)
{
	DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
	DatabaseEntry value = new DatabaseEntry();
	value.setPartial(0, 0, true);
	try
	{
		if (data_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
		{
			// System.out.println(value.toString());
			return true;
		}
	}
	catch (DatabaseException ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle + ": " + ex.toString(), ex);
	}

	return false;
}
 
Example #15
Source File: BJEStorageImplementation.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public boolean containsData(HGPersistentHandle handle)
{
	DatabaseEntry key = new DatabaseEntry(handle.toByteArray());
	DatabaseEntry value = new DatabaseEntry();
	value.setPartial(0, 0, true);
	try
	{
		if (primitive_db.get(txn().getBJETransaction(), key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)
		{
			return true;
		}
	}
	catch (DatabaseException ex)
	{
		throw new HGException("Failed to retrieve link with handle " + handle + ": " + ex.toString(), ex);
	}
	return false;
}
 
Example #16
Source File: BJETxLock.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public void unlock() {
			try {
				int newcnt = writeCount.get() - 1;
				
				if (newcnt < 0) {
					throw new IllegalStateException("Lock already released.");
				}
				else if (newcnt == 0) {
//					getEnv().putLock(lock);
					lock = null;
				}
				writeCount.set(newcnt);
			}
			catch (DatabaseException ex) {
				throw new HGException(ex);
			}
		}
 
Example #17
Source File: BJETxLock.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public synchronized void unlock() {
			try {
				if (lock != null) {
					int newcnt = readCount.get() - 1;
					if (newcnt < 0)
						throw new IllegalStateException("Lock already released.");
					else if (newcnt == 0) {
//						getEnv().putLock(lock);
						lock = null;
					}
					readCount.set(newcnt);
				}
			}
			catch (DatabaseException ex) {
				throw new HGException(ex);
			}
		}
 
Example #18
Source File: BJETxLock.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public boolean tryLock() {
			try {
				if (writeCount.get() == 0) {
//					lock = getEnv().getLock(getLockerId(), true, objectId, LockRequestMode.WRITE);
				}
				
				if (lock != null) {
					writeCount.set(writeCount.get() + 1);
					return true;
				}
				else
					return false;
			}
			catch (LockNotAvailableException le) {
				return false;
			}
			catch (DatabaseException ex) {
				throw new HGException(ex);
			}
		}
 
Example #19
Source File: WiktionaryArticleParser.java    From dkpro-jwktl with Apache License 2.0 6 votes vote down vote up
protected void saveParsedWiktionaryPage() {
		if (!isAllowed(page)) {
			logger.finer("Ignoring page " + page.getTitle());
			return;
		}
		
		if (wiktionaryDB == null)
			return;
		
		try {
//			long time = System.nanoTime();
			wiktionaryDB.savePage(page);
//			time = System.nanoTime() - time;
//			System.out.println("saveWiktionaryPage " + (time / 1000) + "ms");
			
			if (dumpInfo.getProcessedPages() % 25000 == 0)
				wiktionaryDB.commit();
		} catch (DatabaseException e) {
			throw new WiktionaryException("Unable to save page " + page.getTitle(), e);
		}
	}
 
Example #20
Source File: BerkeleyDBWiktionaryEdition.java    From dkpro-jwktl with Apache License 2.0 6 votes vote down vote up
public WiktionaryIterator<IWiktionaryPage> getAllPages(
		final IWiktionaryPageFilter filter, boolean sortByTitle, 
		boolean normalize) {
	ensureOpen();
	try {
		EntityCursor<WiktionaryPage> cursor;
		if (sortByTitle)
			cursor = (normalize 
					? pageByNormalizedTitle.entities() 
					: pageByTitle.entities());
		else
			cursor = pageById.entities();
		
		return new BerkeleyDBWiktionaryIterator<IWiktionaryPage, WiktionaryPage>(
				this, cursor){
			
			@Override
			protected IWiktionaryPage loadEntity(final WiktionaryPage entity) {
				return loadPage(entity, filter);
			}
			
		};
	} catch (DatabaseException e) {
		throw new WiktionaryException(e);
	}
}
 
Example #21
Source File: BerkeleyDBWiktionaryEdition.java    From dkpro-jwktl with Apache License 2.0 6 votes vote down vote up
/** Hotspot for closing the connection. 
 *  @throws WiktionaryException if the connection could not be closed. */
protected void doClose() {
	if (store == null) 
		return; // DB already closed.
			
	try {
		openCursors.forEach(EntityCursor::close);
		openCursors.clear();

		store.close();
		env.close();
		
		env = null;
		store = null;
	} catch (DatabaseException e) {
		throw new WiktionaryException("Unable to close database", e);
	}
}
 
Example #22
Source File: BerkeleyDBWiktionaryIterator.java    From dkpro-jwktl with Apache License 2.0 6 votes vote down vote up
protected OutputType fetchNext() {
	try {
		if (closed)
			return null;

		do {
			InputType next = cursor.next();
			if (next == null)
				return null;

			OutputType result = loadEntity(next);
			if (result != null)
				return result;
		} while (true);

		//return loadEntity(cursor.next());
	} catch (DatabaseException e) {
		throw new WiktionaryException(e);
	}
}
 
Example #23
Source File: TempTableDisk.java    From Rel with Apache License 2.0 6 votes vote down vote up
public boolean hasNext() {
	if (current != null)
		return true;
	try {
		if (cursor == null)
			cursor = storage.openCursor(null, null);
	    DatabaseEntry foundKey = new DatabaseEntry();
	    DatabaseEntry foundData = new DatabaseEntry();	
		if (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
			current = (ValueTuple)database.getTupleBinding().entryToObject(foundKey); 
			return true;
		}
	} catch (DatabaseException exp) {
		throw new ExceptionFatal("RS0321: Unable to get next tuple: " + exp.getMessage());					
	}
	return false;
}
 
Example #24
Source File: TempIndexDisk.java    From Rel with Apache License 2.0 6 votes vote down vote up
public void put(ValueTuple keyTuple, ValueTuple valueTuple) throws DatabaseException {
	DatabaseEntry theKey = new DatabaseEntry();
	database.getTupleBinding().objectToEntry(keyTuple, theKey);

	DatabaseEntry theID = new DatabaseEntry();
	
	OperationStatus status = keys.get(null, theKey, theID, LockMode.DEFAULT);
	if (status == OperationStatus.NOTFOUND) {
		LongBinding.longToEntry(id++, theID);
		keys.put(null, theKey, theID);
	} else if (status != OperationStatus.SUCCESS)
		throw new ExceptionFatal("RS0318: Insertion failure in put().");
	
	DatabaseEntry theData = new DatabaseEntry();
	database.getTupleBinding().objectToEntry(valueTuple, theData);
	
	values.put(null, theID, theData);
}
 
Example #25
Source File: BerkeleyKeyValueStore.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<byte[]> getDuplicates(String tableName, byte[] keyBytes, DatabaseSession databaseSession) throws BimserverDatabaseException {
	DatabaseEntry key = new DatabaseEntry(keyBytes);
	DatabaseEntry value = new DatabaseEntry();
	try {
		TableWrapper tableWrapper = getTableWrapper(tableName);
		Cursor cursor = tableWrapper.getDatabase().openCursor(getTransaction(databaseSession, tableWrapper), getCursorConfig(tableWrapper));
		try {
			OperationStatus operationStatus = cursor.getSearchKey(key, value, LockMode.DEFAULT);
			List<byte[]> result = new ArrayList<byte[]>();
			while (operationStatus == OperationStatus.SUCCESS) {
				result.add(value.getData());
				operationStatus = cursor.getNextDup(key, value, LockMode.DEFAULT);
			}
			return result;
		} finally {
			cursor.close();
		}
	} catch (DatabaseException e) {
		LOGGER.error("", e);
	}
	return null;
}
 
Example #26
Source File: BerkeleyKeyValueStore.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public RecordIterator getRecordIterator(String tableName, DatabaseSession databaseSession) throws BimserverDatabaseException {
	Cursor cursor = null;
	try {
		TableWrapper tableWrapper = getTableWrapper(tableName);
		cursor = tableWrapper.getDatabase().openCursor(getTransaction(databaseSession, tableWrapper), getCursorConfig(tableWrapper));
		BerkeleyRecordIterator berkeleyRecordIterator = new BerkeleyRecordIterator(cursor, this, cursorCounter.incrementAndGet());
		if (MONITOR_CURSOR_STACK_TRACES) {
			openCursors.put(berkeleyRecordIterator.getCursorId(), new Exception().getStackTrace());
		}
		return berkeleyRecordIterator;
	} catch (DatabaseException e) {
		LOGGER.error("", e);
	}
	return null;
}
 
Example #27
Source File: DatabaseGetter.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
private <U> Stream<U> getItems(Supplier<U> valueMaker) {
  CursorIterator<U> data = new CursorIterator<>(
    initializer,
    iterator,
    valueMaker,
    ExceptionUtils.getStackTrace(new Throwable())
  );

  return stream(data).onClose(() -> {
    try {
      if (data.cursor != null) {
        data.cursor.close();
        cursors.remove(data.cursor);
      }
    } catch (DatabaseException e) {
      LOG.error("Could not close cursor", e);
    }
  });
}
 
Example #28
Source File: DatabaseGetter.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean hasNext() {
  if (shouldMove) {
    try {
      if (cursor == null) {
        cursor = database.openCursor(null, null);
        cursors.put(cursor, stackTrace);
        status = initialLookup.apply(cursor);
      } else {
        status = iteration.apply(cursor);
      }
    } catch (DatabaseException | IllegalStateException e) {
      LOG.error("Database exception!", e);
      status = OperationStatus.NOTFOUND;
    }
    shouldMove = false;
  }
  return status == OperationStatus.SUCCESS;
}
 
Example #29
Source File: BdbNonPersistentEnvironmentCreator.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public <KeyT, ValueT> BdbWrapper<KeyT, ValueT> getDatabase(String userId, String dataSetId, String databaseName,
                                                           boolean allowDuplicates, EntryBinding<KeyT> keyBinder,
                                                           EntryBinding<ValueT> valueBinder,
                                                           IsCleanHandler<KeyT, ValueT> isCleanHandler)
  throws BdbDbCreationException {
  try {
    DatabaseConfig config = new DatabaseConfig();
    config.setAllowCreate(true);
    config.setDeferredWrite(true);
    config.setSortedDuplicates(allowDuplicates);

    String environmentKey = environmentKey(userId, dataSetId);
    File envHome = new File(dbHome, environmentKey);
    envHome.mkdirs();
    Environment dataSetEnvironment = new Environment(envHome, configuration);
    Database database = dataSetEnvironment.openDatabase(null, databaseName, config);
    databases.put(environmentKey + "_" + databaseName, database);
    environmentMap.put(environmentKey, dataSetEnvironment);
    return new BdbWrapper<>(dataSetEnvironment, database, config, keyBinder, valueBinder, isCleanHandler);
  } catch (DatabaseException e) {
    throw new BdbDbCreationException(e);
  }
}
 
Example #30
Source File: BdbNonPersistentEnvironmentCreator.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public void close() throws DatabaseException, IOException {
  for (Database database : databases.values()) {
    database.close();
  }
  boolean wasDeleted = false;
  int tries = 0;
  while (!wasDeleted) {
    try {
      FileUtils.cleanDirectory(dbHome);
      wasDeleted = true;
    } catch (IOException e) {
      tries++;
      if (tries >= 10) {
        wasDeleted = true;
      } else {
        try {
          Thread.sleep(1);
        } catch (InterruptedException e1) {
          LOG.error("Trying to clean up and delete directory, but it failed the first time around and then the " +
            "thread was interrupted");
          wasDeleted = true;
        }
      }
    }
  }
}