Changeset 29484 in osm for applications/editors/josm/plugins/imagerycache/src/org/mapdb/BTreeMap.java
- Timestamp:
- 2013-04-07T17:07:27+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagerycache/src/org/mapdb/BTreeMap.java
r29363 r29484 114 114 115 115 /** holds node level locks*/ 116 protected final Lo cks.RecidLocks nodeLocks = new Locks.LongHashMapRecidLocks();116 protected final LongConcurrentHashMap<Thread> nodeLocks = new LongConcurrentHashMap<Thread>(); 117 117 118 118 /** maximal node size allowed in this BTree*/ … … 138 138 private final Values values = new Values(this); 139 139 protected final Serializer defaultSerializer; 140 protected final Atomic.Long counter; 140 141 141 142 … … 154 155 out.writeBoolean(value.valsOutsideNodes); 155 156 out.writeInt(value.maxNodeSize); 157 out.writeLong(value.counterRecid); 156 158 defaultSerializer.serialize(out, value.keySerializer); 157 159 defaultSerializer.serialize(out, value.valueSerializer); … … 168 170 ret.valsOutsideNodes = in.readBoolean(); 169 171 ret.maxNodeSize = in.readInt(); 172 ret.counterRecid = in.readLong(); 170 173 ret.keySerializer = (BTreeKeySerializer) defaultSerializer.deserialize(in, -1); 171 174 ret.valueSerializer = (Serializer) defaultSerializer.deserialize(in, -1); … … 181 184 boolean valsOutsideNodes; 182 185 int maxNodeSize; 186 long counterRecid; 183 187 BTreeKeySerializer keySerializer; 184 188 Serializer valueSerializer; 185 189 Comparator comparator; 186 187 188 189 190 } 190 191 … … 425 426 * @param comparator Comparator to sort keys in this BTree, may be null. 426 427 */ 427 public BTreeMap(Engine engine, int maxNodeSize, boolean hasValues, boolean valsOutsideNodes, 428 public BTreeMap(Engine engine, int maxNodeSize, boolean hasValues, boolean valsOutsideNodes, boolean keepCounter, 428 429 Serializer defaultSerializer, 429 430 BTreeKeySerializer<K> keySerializer, Serializer<V> valueSerializer, Comparator<K> comparator) { … … 449 450 this.valueSerializer = valueSerializer==null ? (Serializer<V>) defaultSerializer : valueSerializer; 450 451 452 451 453 this.keySet = new KeySet(this, hasValues); 452 454 … … 454 456 long rootRecidVal = engine.put(emptyRoot, nodeSerializer); 455 457 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 } 456 467 457 468 BTreeRoot r = new BTreeRoot(); … … 463 474 r.valueSerializer = this.valueSerializer; 464 475 r.comparator = this.comparator; 476 r.counterRecid = counterRecid; 465 477 this.treeRecid = engine.put(r, new BTreeRootSerializer(this.defaultSerializer)); 478 479 466 480 } 467 481 … … 491 505 this.valsOutsideNodes = r.valsOutsideNodes; 492 506 507 493 508 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 } 494 516 } 495 517 … … 617 639 boolean found; 618 640 do{ 619 nodeLocks.lock(current);641 Utils.lock(nodeLocks, current); 620 642 found = true; 621 643 A = engine.get(current, nodeSerializer); … … 627 649 if(putOnlyIfAbsent){ 628 650 //is not absent, so quit 629 nodeLocks.unlock(current);630 nodeLocks.assertNoLocks();651 Utils.unlock(nodeLocks, current); 652 Utils.assertNoLocks(nodeLocks); 631 653 V ret = valExpand(oldVal); 632 654 notify(v,ret, value2); … … 643 665 engine.update(current, A, nodeSerializer); 644 666 //already in here 645 nodeLocks.unlock(current);646 nodeLocks.assertNoLocks();667 Utils.unlock(nodeLocks, current); 668 Utils.assertNoLocks(nodeLocks); 647 669 V ret = valExpand(oldVal); 648 670 notify(v,ret, value2); … … 652 674 if(A.highKey() != null && comparator.compare(v, A.highKey())>0){ 653 675 //follow link until necessary 654 nodeLocks.unlock(current);676 Utils.unlock(nodeLocks, current); 655 677 found = false; 656 678 int pos2 = findChildren(v, A.keys()); … … 686 708 } 687 709 688 nodeLocks.unlock(current);689 nodeLocks.assertNoLocks();710 Utils.unlock(nodeLocks, current); 711 Utils.assertNoLocks(nodeLocks); 690 712 notify(v, null, value2); 691 713 return null; … … 734 756 735 757 if(!isRoot){ 736 nodeLocks.unlock(current);758 Utils.unlock(nodeLocks, current); 737 759 p = q; 738 760 v = (K) A.highKey(); … … 757 779 758 780 //TODO update tree levels 759 nodeLocks.unlock(current);760 nodeLocks.assertNoLocks();781 Utils.unlock(nodeLocks, current); 782 Utils.assertNoLocks(nodeLocks); 761 783 notify(v, null, value2); 762 784 return null; … … 843 865 while(true){ 844 866 845 nodeLocks.lock(current);867 Utils.lock(nodeLocks, current); 846 868 A = engine.get(current, nodeSerializer); 847 869 int pos = findChildren(key, A.keys()); … … 852 874 oldVal = valExpand(oldVal); 853 875 if(value!=null && !value.equals(oldVal)){ 854 nodeLocks.unlock(current);876 Utils.unlock(nodeLocks, current); 855 877 return null; 856 878 } 857 879 //check for last node which was already deleted 858 880 if(pos == A.keys().length-1 && value == null){ 859 nodeLocks.unlock(current);881 Utils.unlock(nodeLocks, current); 860 882 return null; 861 883 } … … 874 896 A = new LeafNode(keys2, vals2, ((LeafNode)A).next); 875 897 engine.update(current, A, nodeSerializer); 876 nodeLocks.unlock(current);898 Utils.unlock(nodeLocks, current); 877 899 notify((K)key, (V)oldVal, null); 878 900 return (V) oldVal; 879 901 }else{ 880 nodeLocks.unlock(current);902 Utils.unlock(nodeLocks, current); 881 903 //follow link until necessary 882 904 if(A.highKey() != null && comparator.compare(key, A.highKey())>0){ … … 960 982 @Override 961 983 public int size(){ 984 if(counter!=null) 985 return (int) counter.get(); //TODO larger then MAX_INT 986 962 987 long size = 0; 963 988 BTreeIterator iter = new BTreeIterator(); … … 994 1019 } 995 1020 996 nodeLocks.lock(current);1021 Utils.lock(nodeLocks, current); 997 1022 LeafNode leaf = (LeafNode) engine.get(current, nodeSerializer); 998 1023 … … 1000 1025 while(pos==leaf.keys.length){ 1001 1026 //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); 1004 1029 current = leaf.next; 1005 1030 leaf = (LeafNode) engine.get(current, nodeSerializer); … … 1027 1052 } 1028 1053 } 1029 nodeLocks.unlock(current);1054 Utils.unlock(nodeLocks, current); 1030 1055 return ret; 1031 1056 } … … 1043 1068 } 1044 1069 1045 nodeLocks.lock(current);1070 Utils.lock(nodeLocks, current); 1046 1071 LeafNode leaf = (LeafNode) engine.get(current, nodeSerializer); 1047 1072 … … 1049 1074 while(pos==leaf.keys.length){ 1050 1075 //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); 1053 1078 current = leaf.next; 1054 1079 leaf = (LeafNode) engine.get(current, nodeSerializer); … … 1073 1098 1074 1099 } 1075 nodeLocks.unlock(current);1100 Utils.unlock(nodeLocks, current); 1076 1101 return (V)ret; 1077 1102 }
Note:
See TracChangeset
for help on using the changeset viewer.