开发者

using BerkeleyDB to replace java.util.List

开发者 https://www.devze.com 2023-04-01 22:37 出处:网络
does someone maybe have sample code how to replace a java List (LinkedList or ArrayList) with something similar in BerkeleyDB? My problem is that I have to replace Lists to scale beyond main memory li

does someone maybe have sample code how to replace a java List (LinkedList or ArrayList) with something similar in BerkeleyDB? My problem is that I have to replace Lists to scale beyond main memory limits. Some simple sample code would be really nice.

I've now used a simple TupleBinding for Integers (keys) and a SerialBinding for the Diff-class (data values).

Now I'm receiving the Error:

14:03:29.287 [pool-5-thread-1] ERROR o.t.g.view.model.TraverseCompareTree - org.treetank.diff.Diff; local class incompatible: stream classdesc serialVersionUID = 8484615870884317488, local class serialVersionUID = -8805161170968505227

java.io.InvalidClassException: org.treetank.diff.Diff; local class incompatible: stream classdesc serialVersionUID = 8484615870884317488, local class serialVersionUID = -8805161170968505227

The listener and TransactionRunner classes which I'm using are:

/** {@inheritDoc} */
@Override
public void diffListener(final EDiff paramDiff, final IStructuralItem paramNewNode,
    final IStructuralItem paramOldNode, final DiffDepth paramDepth) {
    try {
        mRunner.run(new PopulateDatabase(mDiffDatabase, mKey++, new Diff(paramDiff, paramNewNode.getNodeKey(), paramOldNode.getNodeKey(), paramDepth)));
    } catch (final Exception e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}

private static class PopulateDatabase implements TransactionWorker {

    private StoredMap<Integer, Diff> mMap;
    private int mKey;
    private Diff mValue;

    public PopulateDatabase(final DiffDatabase paramDatabase, final int paramKey, final Diff paramValue) {
        Objects.requireNonNull(paramDatabase);
        Objects.requireNonNull(paramValue);
        mMap = paramDatabase.getMap();
        mKey = paramKey;
        mVal开发者_运维问答ue = paramValue;
    }

    @Override
    public void doWork() throws DatabaseException {
        mMap.put(mKey, mValue);
    }
}

I don't know why it doesn't work :-/

Edit: Sorry, I just had to delete the generated environment/database and create a new one.


I'm afraid, it wont be that simple. A first step, you might want to take is to refactor your code to move all accesses to the list into a separate class (call it a DAO, if you like). Then it will be a lot easier to move to a database instead of the list.


Berkeley DB is severe over-kill for this type of task. It's a fair beast to configure and set up, plus I believe the license is now commercial. You'll be much better off using a disk-backed list or map. As an example of the latter take a look at Kyoto Cabinet. It's extremely fast, implements the standard Java Collections interface and is as easy to use as a List or Map. See my other answer for example code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号