com.sleepycat.bind.tuple.StringBinding Java Examples

The following examples show how to use com.sleepycat.bind.tuple.StringBinding. 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: UpgradeFrom8To9Test.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private List<String> loadVersions()
{
    final List<String> versions = new ArrayList<>();
    CursorOperation configuredObjectsCursor = new CursorOperation()
    {
        @Override
        public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction,
                                 DatabaseEntry key, DatabaseEntry value)
        {
            String version = StringBinding.entryToString(key);
            versions.add(version);
        }
    };
    new DatabaseTemplate(_environment, PREFERENCES_VERSION_DB_NAME, null).run(configuredObjectsCursor);
    return versions;
}
 
Example #2
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the id of the vector which was assigned the given internal id or null if the internal id does
 * not exist. Accesses the BDB store!
 * 
 * @param iid
 *            The internal id of the vector
 * @return The id mapped to the given internal id or null if the internal id does not exist
 */
public String getId(int iid) {
	if (iid < 0 || iid > loadCounter) {
		System.out.println("Internal id " + iid + " is out of range!");
		return null;
	}
	DatabaseEntry key = new DatabaseEntry();
	IntegerBinding.intToEntry(iid, key);
	DatabaseEntry data = new DatabaseEntry();
	if ((iidToIdDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		return StringBinding.entryToString(data);
	} else {
		System.out.println("Internal id " + iid + " is in range but id was not found..");
		System.out.println("Index is probably corrupted");
		System.exit(0);
		return null;
	}
}
 
Example #3
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 #4
Source File: BDBLinkStore.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private ModelVersion getStoredVersion() throws RuntimeException
{
    try(Cursor cursor = getLinksVersionDb().openCursor(null, null))
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();

        ModelVersion storedVersion = null;
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS)
        {
            String versionString = StringBinding.entryToString(key);
            ModelVersion version = ModelVersion.fromString(versionString);
            if (storedVersion == null || storedVersion.lessThan(version))
            {
                storedVersion = version;
            }
        }
        if (storedVersion == null)
        {
            throw new StoreException("No link version information.");
        }
        return storedVersion;
    }
    catch (RuntimeException e)
    {
        throw getEnvironmentFacade().handleDatabaseException("Cannot visit link version", e);
    }
}
 
Example #5
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 #6
Source File: AbstractBDBPreferenceStore.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
ModelVersion getStoredVersion()
{
    try(Cursor cursor = getPreferencesVersionDb().openCursor(null, null))
    {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();

        ModelVersion storedVersion = null;
        while (cursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS)
        {
            String versionString = StringBinding.entryToString(key);
            ModelVersion version = ModelVersion.fromString(versionString);
            if (storedVersion == null || storedVersion.lessThan(version))
            {
                storedVersion = version;
            }
        }
        if (storedVersion == null)
        {
            throw new StoreException("No preference version information.");
        }
        return storedVersion;
    }
    catch (RuntimeException e)
    {
        throw getEnvironmentFacade().handleDatabaseException("Cannot visit preference version", e);
    }
}
 
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: BDBPreferenceStoreTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void populateTestData(final List<PreferenceRecord> records, final String modelVersion)
{
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    envConfig.setTransactional(false);
    try (Environment environment = new Environment(_storeFile, envConfig))
    {
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        try (Database versionDb = environment.openDatabase(null, "USER_PREFERENCES_VERSION", dbConfig);
             Database preferencesDb = environment.openDatabase(null, "USER_PREFERENCES", dbConfig))
        {
            DatabaseEntry key = new DatabaseEntry();
            DatabaseEntry value = new DatabaseEntry();
            UUIDTupleBinding keyBinding = UUIDTupleBinding.getInstance();
            MapBinding valueBinding = MapBinding.getInstance();
            for (PreferenceRecord record : records)
            {
                keyBinding.objectToEntry(record.getId(), key);
                valueBinding.objectToEntry(record.getAttributes(), value);
                preferencesDb.put(null, key, value);
            }

            ByteBinding.byteToEntry((byte) 0, value);
            StringBinding.stringToEntry(modelVersion, key);
            versionDb.put(null, key, value);
        }
    }
}
 
Example #9
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the internal id assigned to the vector with the given id or -1 if the id is not found. Accesses
 * the BDB store!
 * 
 * @param id
 *            The id of the vector
 * @return The internal id assigned to this vector or -1 if the id is not found.
 */
public int getInternalId(String id) {
	DatabaseEntry key = new DatabaseEntry();
	StringBinding.stringToEntry(id, key);
	DatabaseEntry data = new DatabaseEntry();
	// check if the id already exists in id to iid database
	if ((idToIidDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		return IntegerBinding.entryToInt(data);
	} else {
		return -1;
	}
}
 
Example #10
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to create a persistent mapping between the given id and an internal id (equal to
 * the current value of {@link #loadCounter}). Should be called every time that a new vector is indexed.
 * 
 * @param id
 *            The id
 */
protected void createMapping(String id) {
	DatabaseEntry key = new DatabaseEntry();
	DatabaseEntry data = new DatabaseEntry();
	IntegerBinding.intToEntry(loadCounter, key);
	StringBinding.stringToEntry(id, data);
	iidToIdDB.put(null, key, data); // required during name look-up
	idToIidDB.put(null, data, key); // required during indexing
}
 
Example #11
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * This is a utility method that can be used to dump the contents of the iidToIdDB to a txt file.
 * 
 * @param dumpFilename
 *            Full path to the file where the dump will be written.
 * @throws Exception
 */
public void dumpiidToIdDB(String dumpFilename) throws Exception {
	DatabaseEntry foundKey = new DatabaseEntry();
	DatabaseEntry foundData = new DatabaseEntry();

	ForwardCursor cursor = iidToIdDB.openCursor(null, null);
	BufferedWriter out = new BufferedWriter(new FileWriter(new File(dumpFilename)));
	while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
		int iid = IntegerBinding.entryToInt(foundKey);
		String id = StringBinding.entryToString(foundData);
		out.write(iid + " " + id + "\n");
	}
	cursor.close();
	out.close();
}
 
Example #12
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 5 votes vote down vote up
/**
 * This is a utility method that can be used to dump the contents of the idToIidDB to a txt file.
 * 
 * @param dumpFilename
 *            Full path to the file where the dump will be written.
 * @throws Exception
 */
public void dumpidToIidDB(String dumpFilename) throws Exception {
	DatabaseEntry foundKey = new DatabaseEntry();
	DatabaseEntry foundData = new DatabaseEntry();

	ForwardCursor cursor = idToIidDB.openCursor(null, null);
	BufferedWriter out = new BufferedWriter(new FileWriter(new File(dumpFilename)));
	while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
		int iid = IntegerBinding.entryToInt(foundData);
		String id = StringBinding.entryToString(foundKey);
		out.write(id + " " + iid + "\n");
	}
	cursor.close();
	out.close();
}
 
Example #13
Source File: UpgradeFrom8To9.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public void performUpgrade(final Environment environment,
                           final UpgradeInteractionHandler handler,
                           final ConfiguredObject<?> parent)
{
    reportStarting(environment, 8);

    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    final Transaction transaction = environment.beginTransaction(null, null);
    try
    {
        Database userPreferencesDb = environment.openDatabase(transaction, "USER_PREFERENCES", dbConfig);
        userPreferencesDb.close();

        try (Database userPreferencesVersionDb = environment.openDatabase(transaction,
                                                                          "USER_PREFERENCES_VERSION",
                                                                          dbConfig))
        {
            if (userPreferencesVersionDb.count() == 0L)
            {
                DatabaseEntry key = new DatabaseEntry();
                DatabaseEntry value = new DatabaseEntry();
                StringBinding.stringToEntry(DEFAULT_VERSION, key);
                LongBinding.longToEntry(System.currentTimeMillis(), value);
                OperationStatus status = userPreferencesVersionDb.put(transaction, key, value);
                if (status != OperationStatus.SUCCESS)
                {
                    throw new StoreException("Error initialising user preference version: " + status);
                }
            }
        }

        transaction.commit();
        reportFinished(environment, 9);
    }
    catch (RuntimeException e)
    {
        try
        {
            if (transaction.isValid())
            {
                transaction.abort();
            }
        }
        finally
        {
            throw e;
        }
    }
}
 
Example #14
Source File: AbstractSearchStructure.java    From multimedia-indexing with Apache License 2.0 3 votes vote down vote up
/**
 * <b>{@link #getInternalId(String)} can always be called instead of this method at the same cost!</b>
 * <br>
 * Checks if the vector with the given id is already indexed. This method is useful to avoid re-indexing
 * the same vector. Its convention is that if the given name is already in idToIidBDB, then the vector is
 * indexed in all other structures e.g. iidToIdBDB. The rest of the checks are avoided for efficiency.
 * Accesses the BDB store!
 * 
 * @param id
 *            The id the vector
 * @return true if the vector is indexed, false otherwise
 */
public boolean isIndexed(String id) {
	DatabaseEntry key = new DatabaseEntry();
	StringBinding.stringToEntry(id, key);
	DatabaseEntry data = new DatabaseEntry();
	if ((idToIidDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
		return true;
	} else {
		return false;
	}
}