/** * Inserts an entity and returns null, or updates it if the primary key already exists and returns * the existing entity. * * <p>If a {@link PrimaryKey#sequence} is used and the primary key field of the given entity is * null or zero, this method will assign the next value from the sequence to the primary key field * of the given entity. * * @param txn the transaction used to protect this operation, null to use auto-commit, or null if * the store is non-transactional. * @param entity the entity to be inserted or updated. * @return the existing entity that was updated, or null if the entity was inserted. * @throws DatabaseException the base class for all BDB exceptions. */ public E put(Transaction txn, E entity) throws DatabaseException { DatabaseEntry keyEntry = new DatabaseEntry(); DatabaseEntry dataEntry = new DatabaseEntry(); assignKey(entity, keyEntry); boolean autoCommit = false; Environment env = db.getEnvironment(); if (transactional && txn == null && DbCompat.getThreadTransaction(env) == null) { txn = env.beginTransaction(null, getAutoCommitTransactionConfig()); autoCommit = true; } CursorConfig cursorConfig = null; if (concurrentDB) { cursorConfig = new CursorConfig(); DbCompat.setWriteCursor(cursorConfig, true); } boolean failed = true; Cursor cursor = db.openCursor(txn, cursorConfig); LockMode lockMode = locking ? LockMode.RMW : null; try { while (true) { OperationStatus status = cursor.getSearchKey(keyEntry, dataEntry, lockMode); if (status == OperationStatus.SUCCESS) { E existing = entityBinding.entryToObject(keyEntry, dataEntry); entityBinding.objectToData(entity, dataEntry); cursor.put(keyEntry, dataEntry); failed = false; return existing; } else { entityBinding.objectToData(entity, dataEntry); status = cursor.putNoOverwrite(keyEntry, dataEntry); if (status != OperationStatus.KEYEXIST) { failed = false; return null; } } } } finally { cursor.close(); if (autoCommit) { if (failed) { txn.abort(); } else { txn.commit(); } } } }
public static Database buildtertiary(Database std) { try { // configure new database instance io.deleteFile("tertiarydb"); DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setType(DatabaseType.HASH); dbConfig.setAllowCreate(true); dbConfig.setUnsortedDuplicates(true); Database tdb = new Database("tertiarydb", null, dbConfig); // configure cursors and entries Cursor stdCurs = std.openCursor(null, null); Cursor tCurs = tdb.openCursor(null, null); DatabaseEntry stdKey = new DatabaseEntry(); DatabaseEntry stdData = new DatabaseEntry(); DatabaseEntry tKey = new DatabaseEntry(); DatabaseEntry tData = new DatabaseEntry(); // extract from linearly constructed database to populate <song>,<users> table, making // use of songs grouped by id String currUsers = ""; String prevID = "default"; boolean firstIter = true; while (stdCurs.getNext(stdKey, stdData, LockMode.DEFAULT) == OperationStatus.SUCCESS) { // get rating data from current row String[] currIDUser = (new String(stdKey.getData())).split(","); String currID = currIDUser[0].trim(); String currUser = currIDUser[1].trim(); String currRating = (new String(stdData.getData())); stdKey.setData(null); stdData.setData(null); if (currID.equals(prevID) || firstIter) { // concatenate new username with current string currUsers += "(" + currUser + "," + currRating + ")"; } else if (!firstIter) { // insert completed <usernames> into table under key <song id> tKey.setData(prevID.getBytes()); tData.setData(currUsers.substring(0, currUsers.length()).getBytes()); tCurs.put(tKey, tData); tKey.setData(null); tData.setData(null); // DEBUG: // System.out.println(prevID+","+currUsers.substring(0, currUsers.length()-1)); // start the new <usernames> for the next song (in currID) currUsers = "(" + currUser + "," + currRating + ")"; } prevID = currID; firstIter = false; } // repeat iteration for last song tKey.setData(prevID.getBytes()); tData.setData(currUsers.substring(0, currUsers.length()).getBytes()); tCurs.put(tKey, tData); tKey.setData(null); tData.setData(null); // DEBUG: // io.debugread(tdb); tCurs.close(); return tdb; } catch (Exception e) { System.out.println(" error creating <song>,<users> tertiary table\n"); System.out.println(e.getMessage()); } return null; // should never happen }