java BerkeleyDB remove couple of records

667 views Asked by At

How can I remove couple of records in one transaction?

Configs:

EnvironmentConfig myEnvConfig = new EnvironmentConfig();
StoreConfig storeConfig = new StoreConfig();
myEnvConfig.setReadOnly(readOnly);
storeConfig.setReadOnly(readOnly);

// If the environment is opened for write, then we want to be
// able to create the environment and entity store if
// they do not exist.
myEnvConfig.setAllowCreate(!readOnly);
storeConfig.setAllowCreate(!readOnly);

// Allow transactions if we are writing to the store.
myEnvConfig.setTransactional(!readOnly);
storeConfig.setTransactional(!readOnly);

// Open the environment and entity store

bklEnv = new Environment(envHome, myEnvConfig);
//bklEnv.openDatabase(null, envHome.getAbsolutePath(), myEnvConfig);
bklstore = new EntityStore(bklEnv, entryStore, storeConfig);

Clear old data in cyclically. Here we are clearing data by getting firstKey() from index:

public void clearOldDBData(Integer maxCount) throws DatabaseException {
    TransactionConfig config = new TransactionConfijg();
    config.setReadUncommitted(true);
    Transaction txn = berkeleyDbEnv.getBklEnv().beginTransaction(null, config);
    txn.setTxnTimeout(1000);

    Long keyV = null;
    try{
        PrimaryIndex<Long,MemoryBTB> memoryBTBIndex =
                berkeleyDbEnv.getBklstore().getPrimaryIndex(Long.class, MemoryBTB.class);

        if(!memoryBTBIndex.sortedMap().isEmpty()  && memoryBTBIndex.sortedMap().keySet().size() > maxCount){
            for(int i = 0; i < memoryBTBIndex.sortedMap().keySet().size() - maxCount; i++){
                log.trace(BERKELEYDB_CLEAR_DATA);
                System.out.println("**************************************************");
                PrimaryIndex<Long,MemoryBTB> memoryBTBIndexInternal =
                        berkeleyDbEnv.getBklstore().getPrimaryIndex(Long.class, MemoryBTB.class);
                memoryBTBIndexInternal.delete(txn, memoryBTBIndexInternal.sortedMap().firstKey());
            }
        }
        txn.commit();
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");
    }catch(DatabaseException dbe){
        // one more time deleting
        try {
            Thread.sleep(100);
            dataAccessor.getMemoryBTB().delete(txn, keyV);
            txn.commit();
        }catch(DatabaseException dbeInternal){
            log.trace(String.format(TXN_ABORT, dbeInternal.getMessage()));
            txn.abort();
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw dbe;
        }
    }
}

Stacktrace:

[12/12 10:35:20] - TRACE - BerkeleyRepository - Berkeley DB clear data
**************************************************
[12/12 10:35:20] - TRACE - BerkeleyRepository - Berkeley DB clear data
**************************************************
[12/12 10:35:21] - TRACE - MemService - Berkeley DB JSON produce error: (JE 3.3.75) Lock expired. Locker 7752330 -1_Thread-295_ThreadLocker: waited for lock on database=persist#MemoryEntityStore#com.company.memcheck.persists.MemoryBTB LockAddr:1554328 node=333 type=READ grant=WAIT_NEW timeoutMillis=500 startTime=1386862520718 endTime=1386862521218
Owners: [<LockInfo locker="31510392 17395_Thread-295_Txn" type="WRITE"/>]
Waiters: []

com.sleepycat.util.RuntimeExceptionWrapper: (JE 3.3.75) Lock expired. Locker 7752330 -1_Thread-295_ThreadLocker: waited for lock on database=persist#MemoryEntityStore#com.com pany.memcheck.persists.MemoryBTB LockAddr:1554328 node=333 type=READ grant=WAIT_NEW timeoutMillis=500 startTime=1386862520718 endTime=1386862521218
Owners: [<LockInfo locker="31510392 17395_Thread-295_Txn" type="WRITE"/>]
Waiters: []
    at com.sleepycat.collections.StoredContainer.convertException(StoredContainer.java:466)
    at com.sleepycat.collections.StoredSortedMap.getFirstOrLastKey(StoredSortedMap.java:216)
    at com.sleepycat.collections.StoredSortedMap.firstKey(StoredSortedMap.java:185)
    at com.company.memcheck.repository.BerkeleyRepositoryImpl.clearOldDBData(BerkeleyRepositoryImpl.java:142)
    at com.company.memcheck.service.MemServiceImpl.removeOldData(MemServiceImpl.java:305)
    at com.company.memcheck.service.MemServiceImpl.access$3(MemServiceImpl.java:299)
    at com.company.memcheck.service.MemServiceImpl$2.run(MemServiceImpl.java:129)
    at java.lang.Thread.run(Thread.java:662)

So as we can see only one entry in a loop where "*******" indicating that it was successful but the others failed. Do I use cursor for this one?

0

There are 0 answers