11/**
22 The MIT License (MIT)
33
4- Copyright (c) 2010-2019 head systems, ltd
4+ Copyright (c) 2010-2021 head systems, ltd
55
66 Permission is hereby granted, free of charge, to any person obtaining a copy of
77 this software and associated documentation files (the "Software"), to deal in
@@ -25,47 +25,49 @@ this software and associated documentation files (the "Software"), to deal in
2525package su .interference .core ;
2626
2727import java .util .*;
28- import java .util .concurrent .CopyOnWriteArrayList ;
29- import java .util .concurrent .ConcurrentHashMap ;
3028
3129/**
3230 * @author Yuriy Glotanov
3331 * @since 1.0
3432 */
3533
3634public class ChunkMap {
37- private final ConcurrentHashMap <Integer , Chunk > hmap ;
38- private final ConcurrentHashMap <ValueSet , List <Chunk >> imap ;
35+ private final HashMap <Integer , Chunk > hmap ;
36+ private final HashMap <ValueSet , List <Chunk >> imap ;
3937 private final List <Chunk > list ;
4038 private final Frame frame ;
4139 private volatile boolean sorted ;
40+ private volatile int used ;
4241
4342 public ChunkMap (Frame frame ) {
44- hmap = new ConcurrentHashMap <>();
45- imap = new ConcurrentHashMap <>();
46- list = new CopyOnWriteArrayList <>();
43+ hmap = new HashMap <>();
44+ imap = frame instanceof IndexFrame ? new HashMap <>() : null ;
45+ list = frame instanceof IndexFrame ? new ArrayList <>() : null ;
4746 this .frame = frame ;
4847 }
4948
5049 @ SuppressWarnings ("unchecked" )
51- public synchronized void sort () {
52- Collections .sort (list );
53- sorted = true ;
50+ protected synchronized void sort () {
51+ if (frame instanceof IndexFrame ) {
52+ Collections .sort (list );
53+ sorted = true ;
54+ }
5455 }
5556
56- public synchronized void add (Chunk c ) {
57+ protected synchronized void add (Chunk c ) {
5758 hmap .put (c .getHeader ().getPtr (), c );
5859 if (frame instanceof IndexFrame ) {
5960 if (imap .get (c .getDcs ()) == null ) {
6061 imap .put (c .getDcs (), new ArrayList <>());
6162 }
6263 imap .get (c .getDcs ()).add (c );
64+ list .add (c );
6365 }
64- list .add (c );
6566 sorted = false ;
67+ used = used + c .getBytesAmount ();
6668 }
6769
68- public synchronized void check () {
70+ protected synchronized void check () {
6971 for (Map .Entry <Integer , Chunk > entry : hmap .entrySet ()) {
7072 Chunk c = entry .getValue ();
7173 if (c .getHeader ().getPtr () != entry .getKey ()) {
@@ -74,26 +76,29 @@ public synchronized void check() {
7476 }
7577 }
7678
77- public synchronized List <Chunk > getChunks () {
78- return list ;
79+ protected synchronized Collection <Chunk > getChunks () {
80+ if (frame instanceof IndexFrame ) {
81+ return list ;
82+ }
83+ return hmap .values ();
7984 }
8085
81- public synchronized Chunk getByPtr (int i ) {
86+ protected synchronized Chunk getByPtr (int i ) {
8287 return hmap .get (i );
8388 }
8489
85- public synchronized Chunk get (int i ) {
90+ // index only
91+ protected synchronized Chunk get (int i ) {
8692 return list .get (i );
8793 }
8894
8995 //for unique indexes
90- public synchronized List <Chunk > getByKey (ValueSet key ) {
96+ protected synchronized List <Chunk > getByKey (ValueSet key ) {
9197 return imap .get (key );
9298 }
9399
94- public synchronized void removeByPtr (int i ) {
95- final boolean x = list .remove (hmap .get (i ));
96- final Chunk c = (Chunk )hmap .remove (i );
100+ protected synchronized void removeByPtr (int i ) {
101+ final Chunk c = hmap .remove (i );
97102 if (frame instanceof IndexFrame ) {
98103 int i_ = 0 ;
99104 for (int i__ = 0 ; i__ < imap .get (c .getDcs ()).size (); i__ ++) {
@@ -102,14 +107,17 @@ public synchronized void removeByPtr(int i) {
102107 }
103108 }
104109 imap .get (c .getDcs ()).remove (i_ );
110+ list .remove (hmap .get (i ));
105111 }
106112 sorted = false ;
107- if (!x || c == null ) {
113+ used = used - c .getBytesAmount ();
114+ if (c == null ) {
108115 throw new RuntimeException ("Internal error during remove object from frame" );
109116 }
110117 }
111118
112- public synchronized void remove (int i ) {
119+ // index only
120+ protected synchronized void remove (int i ) {
113121 final Chunk c = list .get (i );
114122 list .remove (i );
115123 hmap .remove (c .getHeader ().getPtr ());
@@ -122,21 +130,29 @@ public synchronized void remove(int i) {
122130 }
123131 imap .get (c .getDcs ()).remove (i_ );
124132 }
133+ used = used - c .getBytesAmount ();
125134 sorted = false ;
126135 }
127136
128- public synchronized int size () {
137+ //index only
138+ protected synchronized int size () {
129139 return list .size ();
130140 }
131141
132- public synchronized void clear () {
142+ protected synchronized void clear () {
133143 hmap .clear ();
134144 list .clear ();
135145 imap .clear ();
146+ used = 0 ;
136147 sorted = false ;
137148 }
138149
139- public synchronized boolean isSorted () {
150+ protected synchronized boolean isSorted () {
140151 return sorted ;
141152 }
153+
154+ protected int getUsed () {
155+ return used ;
156+ }
157+
142158}
0 commit comments