Java Code Examples for com.sleepycat.je.Database#put()

The following examples show how to use com.sleepycat.je.Database#put() . 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: BerkeleyDBManager.java    From WebCollector with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void inject(CrawlDatums datums, boolean force) throws Exception {
    Database database = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    for (int i = 0; i < datums.size(); i++) {
        CrawlDatum datum = datums.get(i);
        DatabaseEntry key = BerkeleyDBUtils.strToEntry(datum.key());
        DatabaseEntry value = new DatabaseEntry();
        if (!force) {
            if (database.get(null, key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                continue;
            }
        }
        value = BerkeleyDBUtils.strToEntry(datum.asJsonArray().toString());
        database.put(null, key, value);
    }
    database.close();
}
 
Example 2
Source File: UpgradeFrom7To8.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void storeConfiguredObjectEntry(Database configuredObjectsDb, final Transaction txn, ConfiguredObjectRecord configuredObject)
{
    DatabaseEntry key = new DatabaseEntry();
    UUIDTupleBinding uuidBinding = UUIDTupleBinding.getInstance();
    uuidBinding.objectToEntry(configuredObject.getId(), key);

    DatabaseEntry value = new DatabaseEntry();
    ConfiguredObjectBinding configuredObjectBinding = ConfiguredObjectBinding.getInstance();

    configuredObjectBinding.objectToEntry(configuredObject, value);
    OperationStatus status = configuredObjectsDb.put(txn, key, value);
    if (status != OperationStatus.SUCCESS)
    {
        throw new StoreException("Error writing configured object " + configuredObject + " to database: "
                + status);
    }
}
 
Example 3
Source File: AbstractBDBPreferenceStore.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void updateOrCreateInternal(final Transaction txn,
                                    final Collection<PreferenceRecord> preferenceRecords)
{
    Database preferencesDb = getPreferencesDb();
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
    MapBinding valueBinding = MapBinding.getInstance();
    for (PreferenceRecord record : preferenceRecords)
    {
        keyBinding.objectToEntry(record.getId(), key);
        valueBinding.objectToEntry(record.getAttributes(), value);
        OperationStatus status = preferencesDb.put(txn, key, value);
        if (status != OperationStatus.SUCCESS)
        {
            throw new StoreException(String.format("Error writing preference with id '%s' (status %s)",
                                                   record.getId(),
                                                   status.name()));
        }
    }
}
 
Example 4
Source File: UpgradeFrom5To6.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void put(final Database database, Transaction txn, DatabaseEntry key, DatabaseEntry value)
{
    OperationStatus status = database.put(txn, key, value);
    if (status != OperationStatus.SUCCESS)
    {
        throw new StoreException("Cannot add record into " + database.getDatabaseName() + ":" + status);
    }
}
 
Example 5
Source File: BerkeleyDBManager.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void merge() throws Exception {
    LOG.info("start merge");
    Database crawldbDatabase = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    /*合并fetch库*/
    LOG.info("merge fetch database");
    Database fetchDatabase = env.openDatabase(null, "fetch", BerkeleyDBUtils.defaultDBConfig);
    Cursor fetchCursor = fetchDatabase.openCursor(null, null);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    while (fetchCursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        crawldbDatabase.put(null, key, value);
    }
    fetchCursor.close();
    fetchDatabase.close();
    /*合并link库*/
    LOG.info("merge link database");
    Database linkDatabase = env.openDatabase(null, "link", BerkeleyDBUtils.defaultDBConfig);
    Cursor linkCursor = linkDatabase.openCursor(null, null);
    while (linkCursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
        if (!(crawldbDatabase.get(null, key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS)) {
            crawldbDatabase.put(null, key, value);
        }
    }
    linkCursor.close();
    linkDatabase.close();
    LOG.info("end merge");
    crawldbDatabase.close();

    env.removeDatabase(null, "fetch");
    LOG.debug("remove fetch database");
    env.removeDatabase(null, "link");
    LOG.debug("remove link database");

}
 
Example 6
Source File: BerkeleyDBManager.java    From WebCollector with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void inject(CrawlDatum datum, boolean force) throws Exception {
    Database database = env.openDatabase(null, "crawldb", BerkeleyDBUtils.defaultDBConfig);
    DatabaseEntry key = BerkeleyDBUtils.strToEntry(datum.key());
    DatabaseEntry value = new DatabaseEntry();
    if (!force) {
        if (database.get(null, key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
            database.close();
            return;
        }
    }
    value = BerkeleyDBUtils.strToEntry(datum.asJsonArray().toString());
    database.put(null, key, value);
    database.close();
}
 
Example 7
Source File: ReplicatedEnvironmentFacadeTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void putRecord(final ReplicatedEnvironmentFacade master, final Database db, final int keyValue,
                       final String dataValue)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();

    TransactionConfig transactionConfig = new TransactionConfig();
    transactionConfig.setDurability(master.getRealMessageStoreDurability());
    Transaction txn = master.beginTransaction(transactionConfig);
    IntegerBinding.intToEntry(keyValue, key);
    StringBinding.stringToEntry(dataValue, data);

    db.put(txn, key, data);
    txn.commit();
}
 
Example 8
Source File: AbstractBDBPreferenceStore.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private Database updateVersion(Transaction txn, final String currentVersion)
{
    final Database preferencesVersionDb =
            getEnvironmentFacade().openDatabase(PREFERENCES_VERSION_DB_NAME, DEFAULT_DATABASE_CONFIG);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    StringBinding.stringToEntry(currentVersion, key);
    LongBinding.longToEntry(System.currentTimeMillis(), value);
    preferencesVersionDb.put(txn, key, value);
    return preferencesVersionDb;
}
 
Example 9
Source File: BDBLinkStore.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void updateVersion(final Transaction txn, final String currentVersion)
{
    Database linksVersionDb = getEnvironmentFacade().openDatabase(LINKS_VERSION_DB_NAME, DEFAULT_DATABASE_CONFIG);
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    StringBinding.stringToEntry(currentVersion, key);
    LongBinding.longToEntry(System.currentTimeMillis(), value);
    linksVersionDb.put(txn, key, value);
}
 
Example 10
Source File: UpgradeFrom6To7.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Override
public void performUpgrade(Environment environment, UpgradeInteractionHandler handler, ConfiguredObject<?> parent)
{
    reportStarting(environment, 6);
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    Database versionDb = environment.openDatabase(null, "CONFIG_VERSION", dbConfig);

    if(versionDb.count() == 0L)
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        IntegerBinding.intToEntry(DEFAULT_CONFIG_VERSION, value);
        ByteBinding.byteToEntry((byte) 0, key);
        OperationStatus status = versionDb.put(null, key, value);
        if (status != OperationStatus.SUCCESS)
        {
            throw new StoreException("Error initialising config version: " + status);
        }
    }

    versionDb.close();

    reportFinished(environment, 7);
}
 
Example 11
Source File: UpgradeFrom7To8.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
void storeVirtualHostHierarchyRecord(Database hierarchyDb, Transaction txn, UUID id, UUID virtualHostId)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    UUIDTupleBinding.getInstance().objectToEntry(virtualHostId, value);
    TupleOutput tupleOutput = new TupleOutput();
    tupleOutput.writeLong(id.getMostSignificantBits());
    tupleOutput.writeLong(id.getLeastSignificantBits());
    tupleOutput.writeString("VirtualHost");
    TupleBinding.outputToEntry(tupleOutput, key);
    hierarchyDb.put(txn, key, value);
}
 
Example 12
Source File: Upgrader.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
void performUpgradeFromVersion(int sourceVersion, Database versionDb)
        throws StoreException
{
    while(sourceVersion != BDBConfigurationStore.VERSION)
    {
        upgrade(sourceVersion, ++sourceVersion);
        DatabaseEntry key = new DatabaseEntry();
        IntegerBinding.intToEntry(sourceVersion, key);
        DatabaseEntry value = new DatabaseEntry();
        LongBinding.longToEntry(System.currentTimeMillis(), value);
        versionDb.put(null, key, value);
    }
}
 
Example 13
Source File: BDBLinkStore.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void save(Database database, Transaction txn, final LinkDefinition<Source, Target> link)
{
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();

    LinkKey linkKey = new LinkKey(link);
    LinkKeyEntryBinding.getInstance().objectToEntry(linkKey, key);
    LinkValueEntryBinding.getInstance().objectToEntry(new LinkValue(link), value);

    OperationStatus status = database.put(txn, key, value); // TODO: create transaction
    if (status != OperationStatus.SUCCESS)
    {
        throw new StoreException(String.format("Cannot save link %s", linkKey));
    }
}
 
Example 14
Source File: BerkeleyDBUtils.java    From WebCollector with GNU General Public License v3.0 4 votes vote down vote up
public static void put(Database database,String key,String value) throws Exception{
    database.put(null, strToEntry(key), strToEntry(value));
}
 
Example 15
Source File: Upgrader.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public void upgradeIfNecessary()
{
    boolean isEmpty = _environment.getDatabaseNames().isEmpty();
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    Database versionDb = null;
    try
    {
        versionDb = _environment.openDatabase(null, VERSION_DB_NAME, dbConfig);

        if(versionDb.count() == 0L)
        {

            int sourceVersion = isEmpty ? BDBConfigurationStore.VERSION: identifyOldStoreVersion();
            DatabaseEntry key = new DatabaseEntry();
            IntegerBinding.intToEntry(sourceVersion, key);
            DatabaseEntry value = new DatabaseEntry();
            LongBinding.longToEntry(System.currentTimeMillis(), value);

            versionDb.put(null, key, value);
        }

        int version = getSourceVersion(versionDb);

        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("Source message store version is " + version);
        }

        if(version > BDBConfigurationStore.VERSION)
        {
            throw new StoreException("Database version " + version
                                        + " is higher than the most recent known version: "
                                        + BDBConfigurationStore.VERSION);
        }
        performUpgradeFromVersion(version, versionDb);
    }
    finally
    {
        if (versionDb != null)
        {
            versionDb.close();
        }
    }
}
 
Example 16
Source File: DatabasePinger.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public void pingDb(EnvironmentFacade facade)
{
    try
    {
        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("Beginning ping transaction");
        }

        final Database db = facade.openDatabase(PING_DATABASE_NAME,
                                                DATABASE_CONFIG);

        DatabaseEntry key = new DatabaseEntry();
        IntegerBinding.intToEntry(ID, key);

        DatabaseEntry value = new DatabaseEntry();
        LongBinding.longToEntry(System.currentTimeMillis(), value);
        Transaction txn = null;
        try
        {
            txn = facade.beginTransaction(_pingTransactionConfig);
            db.put(txn, key, value);
            txn.commit();

            txn = null;
        }
        finally
        {
            if (LOGGER.isDebugEnabled())
            {
                LOGGER.debug("Ping transaction completed");
            }

            if (txn != null)
            {
                txn.abort();
            }
        }
    }
    catch (RuntimeException de)
    {
        RuntimeException handledException = facade.handleDatabaseException("DatabaseException from DatabasePinger ", de);
        LOGGER.debug("Non fatal exception on invoking DatabasePinger. Ignoring...", handledException);
    }
}
 
Example 17
Source File: UpgradeFrom4To5.java    From qpid-broker-j with Apache License 2.0 3 votes vote down vote up
private void addBindingToDatabase(final BindingTuple bindingTuple, Database targetDatabase, Transaction transaction,
        AMQShortString queueName, AMQShortString exchangeName, AMQShortString routingKey, FieldTable arguments)
{

    DatabaseEntry newKey = new DatabaseEntry();

    bindingTuple.objectToEntry(new BindingRecord(exchangeName, queueName, routingKey, arguments), newKey);

    DatabaseEntry newValue = new DatabaseEntry();
    ByteBinding.byteToEntry((byte) 0, newValue);

    targetDatabase.put(transaction, newKey, newValue);
}