boolean insert(Ice.Identity ident, ObjectRecord objectRecord, TransactionI transaction) { com.sleepycat.db.Transaction tx = null; if (transaction != null) { tx = transaction.dbTxn(); if (tx == null) { throw new DatabaseException(_evictor.errorPrefix() + "invalid transaction"); } } com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); com.sleepycat.db.DatabaseEntry dbValue = marshalValue(objectRecord, _communicator, _encoding, _keepStats); if (_sampleServant != null && !objectRecord.servant.ice_id().equals(_sampleServant.ice_id())) { String msg = _evictor.errorPrefix() + "Attempting to save a '" + objectRecord.servant.ice_id() + "' servant in a database of '" + _sampleServant.ice_id() + "' servants"; throw new DatabaseException(msg); } for (; ; ) { try { return _db.putNoOverwrite(tx, dbKey, dbValue) == com.sleepycat.db.OperationStatus.SUCCESS; } catch (com.sleepycat.db.DeadlockException dx) { if (_evictor.deadlockWarning()) { _communicator .getLogger() .warning( "Deadlock in Freeze.ObjectStore.update while updating Db \"" + _evictor.filename() + "/" + _dbName + "\""); } if (tx != null) { throw new DeadlockException( _evictor.errorPrefix() + "Db.putNoOverwrite: " + dx.getMessage(), transaction, dx); } // // Otherwise retry // } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException( _evictor.errorPrefix() + "Db.putNoOverwrite: " + dx.getMessage(), dx); } } }
void close() { if (_db != null) { try { _db.close(); } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException( _store.evictor().errorPrefix() + "Db.close: " + dx.getMessage(), dx); } _db = null; } }
boolean dbHasObject(Ice.Identity ident, TransactionI transaction) { com.sleepycat.db.Transaction tx = null; if (transaction != null) { tx = transaction.dbTxn(); if (tx == null) { throw new DatabaseException(_evictor.errorPrefix() + "inactive transaction"); } } com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); // // Keep 0 length since we're not interested in the data // com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); dbValue.setPartial(true); for (; ; ) { try { com.sleepycat.db.OperationStatus err = _db.get(tx, dbKey, dbValue, null); if (err == com.sleepycat.db.OperationStatus.SUCCESS) { return true; } else if (err == com.sleepycat.db.OperationStatus.NOTFOUND) { return false; } else { throw new DatabaseException(); } } catch (com.sleepycat.db.DeadlockException dx) { if (_evictor.deadlockWarning()) { _communicator .getLogger() .warning( "Deadlock in Freeze.ObjectStore.dhHasObject while reading " + "Db \"" + _evictor.filename() + "/" + _dbName + "\""); } if (tx != null) { throw new DeadlockException( _evictor.errorPrefix() + "Db.get: " + dx.getMessage(), transaction, dx); } // // Otherwise try again // } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx); } } }
void close() { try { _db.close(); for (Index index : _indices) { index.close(); } _indices.clear(); } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException(_evictor.errorPrefix() + "Db.close: " + dx.getMessage(), dx); } _db = null; }
// // Load a servant from the database; will end up in the cache associated with // this ObjectStore. This load is not transactional. // @Override public Object load(Object identObj) { Ice.Identity ident = (Ice.Identity) identObj; com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); for (; ; ) { try { com.sleepycat.db.OperationStatus rs = _db.get(null, dbKey, dbValue, null); if (rs == com.sleepycat.db.OperationStatus.NOTFOUND) { return null; } else if (rs != com.sleepycat.db.OperationStatus.SUCCESS) { assert false; throw new DatabaseException(); } break; } catch (com.sleepycat.db.DeadlockException dx) { if (_evictor.deadlockWarning()) { _communicator .getLogger() .warning( "Deadlock in Freeze.ObjectStore.load while reading Db \"" + _evictor.filename() + "/" + _dbName + "\"; retrying..."); } // // Ignored, try again // } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx); } } ObjectRecord rec = unmarshalValue(dbValue, _communicator, _encoding, _keepStats); _evictor.initialize(ident, _facet, rec.servant); Object result = _evictor.createEvictorElement(ident, rec, this); return result; }
// // Load a servant from the database using the given transaction; this servant // is NOT cached in the ObjectStore associated cache // ObjectRecord load(Ice.Identity ident, TransactionI transaction) { if (transaction == null) { throw new DatabaseException(_evictor.errorPrefix() + "no active transaction"); } com.sleepycat.db.Transaction tx = transaction.dbTxn(); if (tx == null) { throw new DatabaseException(_evictor.errorPrefix() + "inactive transaction"); } com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); com.sleepycat.db.DatabaseEntry dbValue = new com.sleepycat.db.DatabaseEntry(); try { com.sleepycat.db.OperationStatus rs = _db.get(tx, dbKey, dbValue, null); if (rs == com.sleepycat.db.OperationStatus.NOTFOUND) { return null; } else if (rs != com.sleepycat.db.OperationStatus.SUCCESS) { assert false; throw new DatabaseException(); } } catch (com.sleepycat.db.DeadlockException dx) { if (_evictor.deadlockWarning()) { _communicator .getLogger() .warning( "Deadlock in Freeze.ObjectStore.load while reading Db \"" + _evictor.filename() + "/" + _dbName + "\""); } throw new DeadlockException( _evictor.errorPrefix() + "Db.get: " + dx.getMessage(), transaction, dx); } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException(_evictor.errorPrefix() + "Db.get: " + dx.getMessage(), dx); } ObjectRecord rec = unmarshalValue(dbValue, _communicator, _encoding, _keepStats); _evictor.initialize(ident, _facet, rec.servant); return rec; }
public void populateTable(Database dbp, int start_id, int balance, int nrecs, String msg) { Defrec drec = new Defrec(); DatabaseEntry kdbt = new DatabaseEntry(drec.data); kdbt.setSize(4); // sizeof(int) DatabaseEntry ddbt = new DatabaseEntry(drec.data); ddbt.setSize(drec.data.length); // uses whole array try { for (int i = 0; i < nrecs; i++) { kdbt.setRecordNumber(start_id + (int) i); drec.set_balance(balance); dbp.putNoOverwrite(null, kdbt, ddbt); } } catch (DatabaseException dbe) { System.err.println("Failure initializing " + msg + " file: " + dbe.toString()); System.exit(1); } }
boolean remove(Ice.Identity ident, TransactionI transaction) { com.sleepycat.db.Transaction tx = null; if (transaction != null) { tx = transaction.dbTxn(); if (tx == null) { throw new DatabaseException(_evictor.errorPrefix() + "invalid transaction"); } } com.sleepycat.db.DatabaseEntry dbKey = marshalKey(ident, _communicator, _encoding); for (; ; ) { try { return _db.delete(tx, dbKey) == com.sleepycat.db.OperationStatus.SUCCESS; } catch (com.sleepycat.db.DeadlockException dx) { if (_evictor.deadlockWarning()) { _communicator .getLogger() .warning( "Deadlock in Freeze.ObjectStore.remove while updating Db \"" + _evictor.filename() + "/" + _dbName + "\""); } if (tx != null) { throw new DeadlockException( _evictor.errorPrefix() + "Db.delete: " + dx.getMessage(), transaction, dx); } // // Otherwise retry // } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException(_evictor.errorPrefix() + "Db.delete: " + dx.getMessage(), dx); } } }
/** * When Y is opened and X has a key with relatedEntity=Y.class, X should be opened automatically. * If X is not opened, foreign key constraints will not be enforced. [#15358] */ public void testAutoOpenRelatedEntity() throws DatabaseException { PrimaryIndex<Integer, RelatedY> priY; PrimaryIndex<Integer, RelatedX> priX; /* Opening X should create (and open) Y and enforce constraints. */ open(); priX = store.getPrimaryIndex(Integer.class, RelatedX.class); PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedY.class.getName(), null); if (isTransactional) { /* Constraint enforcement requires transactions. */ try { priX.put(new RelatedX()); fail(); } catch (DatabaseException e) { assertTrue( "" + e.getMessage(), (e.getMessage().indexOf("foreign key not allowed: it is not present") >= 0) || (e.getMessage().indexOf("DB_FOREIGN_CONFLICT") >= 0)); } } priY = store.getPrimaryIndex(Integer.class, RelatedY.class); priY.put(new RelatedY()); priX.put(new RelatedX()); close(); /* Delete should cascade even when X is not opened explicitly. */ open(); priY = store.getPrimaryIndex(Integer.class, RelatedY.class); assertEquals(1, priY.count()); priY.delete(88); assertEquals(0, priY.count()); priX = store.getPrimaryIndex(Integer.class, RelatedX.class); assertEquals(0, priX.count()); /* Failed prior to [#15358] fix. */ close(); }
ObjectStore( String facet, String facetType, boolean createDb, EvictorI evictor, java.util.List<Index> indices, boolean populateEmptyIndices) { _cache = new Cache(this); _facet = facet; _evictor = evictor; _indices = indices; _communicator = evictor.communicator(); _encoding = evictor.encoding(); _keepStats = false; if (facet.equals("")) { _dbName = EvictorI.defaultDb; } else { _dbName = facet; } if (facetType != null) { // // Create a sample servant with this type // Ice.ValueFactory factory = _communicator.getValueFactoryManager().find(facetType); if (factory == null) { throw new DatabaseException( _evictor.errorPrefix() + "No value factory registered for type-id '" + facetType + "'"); } _sampleServant = factory.create(facetType); } Connection connection = Util.createConnection(_communicator, evictor.dbEnv().getEnvName()); try { Catalog catalog = new Catalog(connection, Util.catalogName(), true); CatalogData catalogData = catalog.get(evictor.filename()); if (catalogData != null) { if (catalogData.evictor) { _keepStats = catalogData.value.isEmpty(); } else { DatabaseException ex = new DatabaseException(); ex.message = _evictor.errorPrefix() + evictor.filename() + " is not an evictor database"; throw ex; } } com.sleepycat.db.Environment dbEnv = evictor.dbEnv().getEnv(); // // TODO: FREEZE_DB_MODE // com.sleepycat.db.DatabaseConfig config = new com.sleepycat.db.DatabaseConfig(); config.setType(com.sleepycat.db.DatabaseType.BTREE); config.setAllowCreate(createDb); Ice.Properties properties = _evictor.communicator().getProperties(); String propPrefix = "Freeze.Evictor." + _evictor.filename() + "."; int btreeMinKey = properties.getPropertyAsInt(propPrefix + _dbName + ".BtreeMinKey"); if (btreeMinKey > 2) { if (_evictor.trace() >= 1) { _evictor .communicator() .getLogger() .trace( "Freeze.Evictor", "Setting \"" + _evictor.filename() + "." + _dbName + "\"'s btree minkey to " + btreeMinKey); } config.setBtreeMinKey(btreeMinKey); } boolean checksum = properties.getPropertyAsInt(propPrefix + "Checksum") > 0; if (checksum) { if (_evictor.trace() >= 1) { _evictor .communicator() .getLogger() .trace("Freeze.Evictor", "Turning checksum on for \"" + _evictor.filename() + "\""); } config.setChecksum(true); } int pageSize = properties.getPropertyAsInt(propPrefix + "PageSize"); if (pageSize > 0) { if (_evictor.trace() >= 1) { _evictor .communicator() .getLogger() .trace( "Freeze.Evictor", "Setting \"" + _evictor.filename() + "\"'s pagesize to " + pageSize); } config.setPageSize(pageSize); } try { Transaction tx = connection.beginTransaction(); com.sleepycat.db.Transaction txn = Util.getTxn(tx); _db = dbEnv.openDatabase(txn, evictor.filename(), _dbName, config); for (Index index : _indices) { index.associate(this, txn, createDb, populateEmptyIndices); } if (catalogData == null) { catalogData = new CatalogData(true, "::Ice::Identity", "Object"); catalog.put(evictor.filename(), catalogData); } tx.commit(); } catch (java.io.FileNotFoundException dx) { throw new NotFoundException(_evictor.errorPrefix() + "Db.open: " + dx.getMessage(), dx); } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException(_evictor.errorPrefix() + "Db.open: " + dx.getMessage(), dx); } finally { Transaction tx = connection.currentTransaction(); if (tx != null) { try { tx.rollback(); } catch (DatabaseException de) { } } } } finally { connection.close(); } }
protected Ice.Identity[] untypedFindFirst(byte[] k, int firstN) { EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); deactivateController.lock(); try { com.sleepycat.db.DatabaseEntry key = new com.sleepycat.db.DatabaseEntry(k); // // When we have a custom-comparison function, Berkeley DB returns // the key on-disk (when it finds one). We disable this behavior: // (ref Oracle SR 5925672.992) // // In DB > 5.1.x we can not set DB_DBT_PARTIAL in the key Dbt when calling // getSearchKey. // if (com.sleepycat.db.Environment.getVersionMajor() < 5 || (com.sleepycat.db.Environment.getVersionMajor() == 5 && com.sleepycat.db.Environment.getVersionMinor() <= 1)) { key.setPartial(true); } com.sleepycat.db.DatabaseEntry pkey = new com.sleepycat.db.DatabaseEntry(); com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry(); // // dlen is 0, so we should not retrieve any value // value.setPartial(true); Ice.Communicator communicator = _store.communicator(); Ice.EncodingVersion encoding = _store.encoding(); TransactionI transaction = _store.evictor().beforeQuery(); com.sleepycat.db.Transaction tx = transaction == null ? null : transaction.dbTxn(); java.util.List<Ice.Identity> identities; for (; ; ) { com.sleepycat.db.SecondaryCursor dbc = null; identities = new java.util.ArrayList<Ice.Identity>(); try { // // Move to the first record // dbc = _db.openSecondaryCursor(tx, null); boolean first = true; boolean found; do { com.sleepycat.db.OperationStatus status; if (first) { status = dbc.getSearchKey(key, pkey, value, null); } else { status = dbc.getNextDup(key, pkey, value, null); } found = status == com.sleepycat.db.OperationStatus.SUCCESS; if (found) { Ice.Identity ident = ObjectStore.unmarshalKey(pkey, communicator, encoding); identities.add(ident); first = false; } } while ((firstN <= 0 || identities.size() < firstN) && found); break; // for(;;) } catch (com.sleepycat.db.DeadlockException dx) { if (_store.evictor().deadlockWarning()) { communicator .getLogger() .warning( "Deadlock in Freeze.Index.untypedFindFirst while " + "iterating over Db \"" + _store.evictor().filename() + "/" + _dbName + "\""); } if (tx != null) { throw new DeadlockException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx); } // // Otherwise retry // } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), dx); } finally { if (dbc != null) { try { dbc.close(); } catch (com.sleepycat.db.DeadlockException dx) { if (tx != null) { throw new DeadlockException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx); } } catch (com.sleepycat.db.DatabaseException dx) { // // Ignored // } } } } if (identities.size() != 0) { Ice.Identity[] result = new Ice.Identity[identities.size()]; return identities.toArray(result); } else { return new Ice.Identity[0]; } } finally { deactivateController.unlock(); } }
protected int untypedCount(byte[] k) { EvictorI.DeactivateController deactivateController = _store.evictor().deactivateController(); deactivateController.lock(); try { com.sleepycat.db.DatabaseEntry key = new com.sleepycat.db.DatabaseEntry(k); // // When we have a custom-comparison function, Berkeley DB returns // the key on-disk (when it finds one). We disable this behavior: // (ref Oracle SR 5925672.992) // // In DB > 5.1.x we can not set DB_DBT_PARTIAL in the key Dbt when calling // getSearchKey. // if (com.sleepycat.db.Environment.getVersionMajor() < 5 || (com.sleepycat.db.Environment.getVersionMajor() == 5 && com.sleepycat.db.Environment.getVersionMinor() <= 1)) { key.setPartial(true); } com.sleepycat.db.DatabaseEntry value = new com.sleepycat.db.DatabaseEntry(); // // dlen is 0, so we should not retrieve any value // value.setPartial(true); TransactionI transaction = _store.evictor().beforeQuery(); com.sleepycat.db.Transaction tx = transaction == null ? null : transaction.dbTxn(); for (; ; ) { com.sleepycat.db.Cursor dbc = null; try { dbc = _db.openCursor(tx, null); if (dbc.getSearchKey(key, value, null) == com.sleepycat.db.OperationStatus.SUCCESS) { return dbc.count(); } else { return 0; } } catch (com.sleepycat.db.DeadlockException dx) { if (_store.evictor().deadlockWarning()) { _store .communicator() .getLogger() .warning( "Deadlock in Freeze.Index.untypedCount while " + "iterating over Db \"" + _store.evictor().filename() + "/" + _dbName + "\""); } if (tx != null) { throw new DeadlockException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx); } // // Otherwise retry // } catch (com.sleepycat.db.DatabaseException dx) { throw new DatabaseException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), dx); } finally { if (dbc != null) { try { dbc.close(); } catch (com.sleepycat.db.DeadlockException dx) { if (tx != null) { throw new DeadlockException( _store.evictor().errorPrefix() + "Db.cursor: " + dx.getMessage(), transaction, dx); } } catch (com.sleepycat.db.DatabaseException dx) { // // Ignored // } } } } } finally { deactivateController.unlock(); } }
private void doSecondaryBulkLoad(boolean closeAndOpenNormally) throws DatabaseException { PrimaryIndex<Integer, RelatedX> priX; PrimaryIndex<Integer, RelatedY> priY; SecondaryIndex<Integer, Integer, RelatedX> secX; /* Open priX with SecondaryBulkLoad=true. */ StoreConfig config = new StoreConfig(); config.setAllowCreate(true); config.setSecondaryBulkLoad(true); open(config); /* Getting priX should not create the secondary index. */ priX = store.getPrimaryIndex(Integer.class, RelatedX.class); PersistTestUtils.assertDbExists(false, env, STORE_NAME, RelatedX.class.getName(), "key2"); /* We can put records that violate the secondary key constraint. */ priX.put(new RelatedX()); if (closeAndOpenNormally) { /* Open normally and attempt to populate the secondary. */ close(); open(); if (isTransactional && DbCompat.POPULATE_ENFORCES_CONSTRAINTS) { /* Constraint enforcement requires transactions. */ try { /* Before adding the foreign key, constraint is violated. */ priX = store.getPrimaryIndex(Integer.class, RelatedX.class); fail(); } catch (DatabaseException e) { assertTrue(e.toString(), e.toString().contains("foreign key not allowed")); } } /* Open priX with SecondaryBulkLoad=true. */ close(); open(config); /* Add the foreign key to avoid the constraint error. */ priY = store.getPrimaryIndex(Integer.class, RelatedY.class); priY.put(new RelatedY()); /* Open normally and the secondary will be populated. */ close(); open(); priX = store.getPrimaryIndex(Integer.class, RelatedX.class); PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedX.class.getName(), "key2"); secX = store.getSecondaryIndex(priX, Integer.class, "key2"); } else { /* Get secondary index explicitly and it will be populated. */ if (isTransactional && DbCompat.POPULATE_ENFORCES_CONSTRAINTS) { /* Constraint enforcement requires transactions. */ try { /* Before adding the foreign key, constraint is violated. */ secX = store.getSecondaryIndex(priX, Integer.class, "key2"); fail(); } catch (DatabaseException e) { assertTrue(e.toString(), e.toString().contains("foreign key not allowed")); } } /* Add the foreign key. */ priY = store.getPrimaryIndex(Integer.class, RelatedY.class); priY.put(new RelatedY()); secX = store.getSecondaryIndex(priX, Integer.class, "key2"); PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedX.class.getName(), "key2"); } RelatedX x = secX.get(88); assertNotNull(x); close(); }
public void run() { String homedirName = baseDirName + threadNumber; TestUtils.removeDir(homedirName); try { homedir = new File(homedirName); homedir.mkdir(); } catch (Exception e) { TestUtils.DEBUGOUT( 2, "Warning: initialization had a problem creating a clean directory.\n" + e); } try { homedir = new File(homedirName); } catch (NullPointerException npe) { // can't really happen :) } TestUtils.DEBUGOUT(1, "Creating worker: " + threadNumber); envConfig = new EnvironmentConfig(); envConfig.setErrorStream(TestUtils.getErrorStream()); envConfig.setErrorPrefix("RepmgrElectionTest test(" + threadNumber + ")"); envConfig.setAllowCreate(true); envConfig.setRunRecovery(true); envConfig.setThreaded(true); envConfig.setInitializeLocking(true); envConfig.setInitializeLogging(true); envConfig.setInitializeCache(true); envConfig.setTransactional(true); envConfig.setTxnNoSync(true); envConfig.setInitializeReplication(true); envConfig.setVerboseReplication(false); ReplicationManagerSiteConfig localConfig = new ReplicationManagerSiteConfig(address, basePort + threadNumber); localConfig.setLocalSite(true); envConfig.addReplicationManagerSite(localConfig); envConfig.setReplicationPriority(priorities[threadNumber]); envConfig.setEventHandler(this); envConfig.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL); if (masterThreadIndex >= 0) { // If we already have the master, then set it as the bootstrap helper, // otherwise, set local site as new master. ReplicationManagerSiteConfig remoteConfig = new ReplicationManagerSiteConfig(address, basePort + masterThreadIndex); remoteConfig.setBootstrapHelper(true); envConfig.addReplicationManagerSite(remoteConfig); } try { dbenv = new Environment(homedir, envConfig); } catch (FileNotFoundException e) { fail("Unexpected FNFE in standard environment creation." + e); } catch (DatabaseException dbe) { fail("Unexpected database exception came from environment create." + dbe); } try { // If we do not have master, then set local site as new master. if (masterThreadIndex == -1) dbenv.replicationManagerStart(NUM_WORKER_THREADS, ReplicationManagerStartPolicy.REP_MASTER); else dbenv.replicationManagerStart(NUM_WORKER_THREADS, ReplicationManagerStartPolicy.REP_CLIENT); } catch (DatabaseException dbe) { fail("Unexpected database exception came from replicationManagerStart." + dbe); } TestUtils.DEBUGOUT(1, "Started replication site: " + threadNumber); lastSiteStarted = true; try { java.lang.Thread.sleep(1000 * (1 + threadNumber)); } catch (InterruptedException ie) { } if (masterThreadIndex != -1) { // Wait for "Start-up done" for each client, then add next client. ReplicationStats rs = null; int i = 0; do { try { java.lang.Thread.sleep(2000); } catch (InterruptedException e) { } try { rs = dbenv.getReplicationStats(StatsConfig.DEFAULT); } catch (DatabaseException dbe) { dbe.printStackTrace(); fail("Unexpected database exception came from getReplicationStats." + dbe); } } while (!rs.getStartupComplete() && i++ < maxLoopWait); assertTrue(rs.getStartupComplete()); } }