Ignore:
Timestamp:
2013-04-07T17:07:27+02:00 (12 years ago)
Author:
akks
Message:

JOSM/ImageryCache: updated MapDB (no more deadlocks, Java 1.6 compatible), less crashes, multiple-JOSM support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagerycache/src/org/mapdb/BTreeMap.java

    r29363 r29484  
    114114
    115115    /** holds node level locks*/
    116     protected final Locks.RecidLocks nodeLocks = new Locks.LongHashMapRecidLocks();
     116    protected final LongConcurrentHashMap<Thread> nodeLocks = new LongConcurrentHashMap<Thread>();
    117117
    118118    /** maximal node size allowed in this BTree*/
     
    138138    private final Values values = new Values(this);
    139139    protected final Serializer defaultSerializer;
     140    protected final Atomic.Long counter;
    140141
    141142
     
    154155            out.writeBoolean(value.valsOutsideNodes);
    155156            out.writeInt(value.maxNodeSize);
     157            out.writeLong(value.counterRecid);
    156158            defaultSerializer.serialize(out, value.keySerializer);
    157159            defaultSerializer.serialize(out, value.valueSerializer);
     
    168170            ret.valsOutsideNodes = in.readBoolean();
    169171            ret.maxNodeSize = in.readInt();
     172            ret.counterRecid = in.readLong();
    170173            ret.keySerializer = (BTreeKeySerializer) defaultSerializer.deserialize(in, -1);
    171174            ret.valueSerializer = (Serializer) defaultSerializer.deserialize(in, -1);
     
    181184        boolean valsOutsideNodes;
    182185        int maxNodeSize;
     186        long counterRecid;
    183187        BTreeKeySerializer keySerializer;
    184188        Serializer valueSerializer;
    185189        Comparator comparator;
    186 
    187 
    188 
    189190    }
    190191
     
    425426     * @param comparator Comparator to sort keys in this BTree, may be null.
    426427     */
    427     public BTreeMap(Engine engine, int maxNodeSize, boolean hasValues, boolean valsOutsideNodes,
     428    public BTreeMap(Engine engine, int maxNodeSize, boolean hasValues, boolean valsOutsideNodes, boolean keepCounter,
    428429                    Serializer defaultSerializer,
    429430                    BTreeKeySerializer<K> keySerializer, Serializer<V> valueSerializer, Comparator<K> comparator) {
     
    449450        this.valueSerializer = valueSerializer==null ? (Serializer<V>) defaultSerializer : valueSerializer;
    450451
     452
    451453        this.keySet = new KeySet(this, hasValues);
    452454
     
    454456        long rootRecidVal = engine.put(emptyRoot, nodeSerializer);
    455457        rootRecidRef = engine.put(rootRecidVal,Serializer.LONG_SERIALIZER);
     458
     459        long counterRecid = 0;
     460        if(keepCounter){
     461            counterRecid = engine.put(0L, Serializer.LONG_SERIALIZER);
     462            this.counter = new Atomic.Long(engine,counterRecid);
     463            Bind.size(this,counter);
     464        }else{
     465            this.counter = null;
     466        }
    456467
    457468        BTreeRoot r = new BTreeRoot();
     
    463474        r.valueSerializer =  this.valueSerializer;
    464475        r.comparator =  this.comparator;
     476        r.counterRecid = counterRecid;
    465477        this.treeRecid = engine.put(r, new BTreeRootSerializer(this.defaultSerializer));
     478
     479
    466480    }
    467481
     
    491505        this.valsOutsideNodes = r.valsOutsideNodes;
    492506
     507
    493508        this.keySet = new KeySet(this, hasValues);
     509
     510        if(r.counterRecid!=0){
     511            counter = new Atomic.Long(engine,r.counterRecid);
     512            Bind.size(this,counter);
     513        }else{
     514            this.counter = null;
     515        }
    494516    }
    495517
     
    617639            boolean found;
    618640            do{
    619                 nodeLocks.lock(current);
     641                Utils.lock(nodeLocks, current);
    620642                found = true;
    621643                A = engine.get(current, nodeSerializer);
     
    627649                    if(putOnlyIfAbsent){
    628650                        //is not absent, so quit
    629                         nodeLocks.unlock(current);
    630                         nodeLocks.assertNoLocks();
     651                        Utils.unlock(nodeLocks, current);
     652                        Utils.assertNoLocks(nodeLocks);
    631653                        V ret =  valExpand(oldVal);
    632654                        notify(v,ret, value2);
     
    643665                    engine.update(current, A, nodeSerializer);
    644666                    //already in here
    645                     nodeLocks.unlock(current);
    646                     nodeLocks.assertNoLocks();
     667                    Utils.unlock(nodeLocks, current);
     668                    Utils.assertNoLocks(nodeLocks);
    647669                    V ret =  valExpand(oldVal);
    648670                    notify(v,ret, value2);
     
    652674                if(A.highKey() != null && comparator.compare(v, A.highKey())>0){
    653675                    //follow link until necessary
    654                     nodeLocks.unlock(current);
     676                    Utils.unlock(nodeLocks, current);
    655677                    found = false;
    656678                    int pos2 = findChildren(v, A.keys());
     
    686708                }
    687709
    688                 nodeLocks.unlock(current);
    689                 nodeLocks.assertNoLocks();
     710                Utils.unlock(nodeLocks, current);
     711                Utils.assertNoLocks(nodeLocks);
    690712                notify(v,  null, value2);
    691713                return null;
     
    734756
    735757                if(!isRoot){
    736                     nodeLocks.unlock(current);
     758                    Utils.unlock(nodeLocks, current);
    737759                    p = q;
    738760                    v = (K) A.highKey();
     
    757779
    758780                    //TODO update tree levels
    759                     nodeLocks.unlock(current);
    760                     nodeLocks.assertNoLocks();
     781                    Utils.unlock(nodeLocks, current);
     782                    Utils.assertNoLocks(nodeLocks);
    761783                    notify(v, null, value2);
    762784                    return null;
     
    843865        while(true){
    844866
    845             nodeLocks.lock(current);
     867            Utils.lock(nodeLocks, current);
    846868            A = engine.get(current, nodeSerializer);
    847869            int pos = findChildren(key, A.keys());
     
    852874                oldVal = valExpand(oldVal);
    853875                if(value!=null && !value.equals(oldVal)){
    854                     nodeLocks.unlock(current);
     876                    Utils.unlock(nodeLocks, current);
    855877                    return null;
    856878                }
    857879                //check for last node which was already deleted
    858880                if(pos == A.keys().length-1 && value == null){
    859                     nodeLocks.unlock(current);
     881                    Utils.unlock(nodeLocks, current);
    860882                    return null;
    861883                }
     
    874896                A = new LeafNode(keys2, vals2, ((LeafNode)A).next);
    875897                engine.update(current, A, nodeSerializer);
    876                 nodeLocks.unlock(current);
     898                Utils.unlock(nodeLocks, current);
    877899                notify((K)key, (V)oldVal, null);
    878900                return (V) oldVal;
    879901            }else{
    880                 nodeLocks.unlock(current);
     902                Utils.unlock(nodeLocks, current);
    881903                //follow link until necessary
    882904                if(A.highKey() != null && comparator.compare(key, A.highKey())>0){
     
    960982    @Override
    961983    public int size(){
     984        if(counter!=null)
     985            return (int) counter.get(); //TODO larger then MAX_INT
     986
    962987        long size = 0;
    963988        BTreeIterator iter = new BTreeIterator();
     
    9941019        }
    9951020
    996         nodeLocks.lock(current);
     1021        Utils.lock(nodeLocks, current);
    9971022        LeafNode leaf = (LeafNode) engine.get(current, nodeSerializer);
    9981023
     
    10001025        while(pos==leaf.keys.length){
    10011026            //follow leaf link until necessary
    1002             nodeLocks.lock(leaf.next);
    1003             nodeLocks.unlock(current);
     1027            Utils.lock(nodeLocks, leaf.next);
     1028            Utils.unlock(nodeLocks, current);
    10041029            current = leaf.next;
    10051030            leaf = (LeafNode) engine.get(current, nodeSerializer);
     
    10271052            }
    10281053        }
    1029         nodeLocks.unlock(current);
     1054        Utils.unlock(nodeLocks, current);
    10301055        return ret;
    10311056    }
     
    10431068        }
    10441069
    1045         nodeLocks.lock(current);
     1070        Utils.lock(nodeLocks, current);
    10461071        LeafNode leaf = (LeafNode) engine.get(current, nodeSerializer);
    10471072
     
    10491074        while(pos==leaf.keys.length){
    10501075            //follow leaf link until necessary
    1051             nodeLocks.lock(leaf.next);
    1052             nodeLocks.unlock(current);
     1076            Utils.lock(nodeLocks, leaf.next);
     1077            Utils.unlock(nodeLocks, current);
    10531078            current = leaf.next;
    10541079            leaf = (LeafNode) engine.get(current, nodeSerializer);
     
    10731098
    10741099        }
    1075         nodeLocks.unlock(current);
     1100        Utils.unlock(nodeLocks, current);
    10761101        return (V)ret;
    10771102    }
Note: See TracChangeset for help on using the changeset viewer.