2626
2727import de .bluecolored .bluemap .core .debug .DebugDump ;
2828import de .bluecolored .bluemap .core .map .BmMap ;
29+ import de .bluecolored .bluemap .core .storage .FileStorage ;
30+ import de .bluecolored .bluemap .core .storage .Storage ;
2931import de .bluecolored .bluemap .core .util .FileUtils ;
3032
3133import java .io .IOException ;
3234import java .nio .file .Files ;
3335import java .nio .file .Path ;
3436import java .util .LinkedList ;
37+ import java .util .Objects ;
3538import java .util .stream .Collectors ;
3639
37- public class MapPurgeTask implements RenderTask {
40+ public abstract class MapPurgeTask implements RenderTask {
3841
39- @ DebugDump private final BmMap map ;
40- @ DebugDump private final Path directory ;
41- @ DebugDump private final int subFilesCount ;
42- private final LinkedList <Path > subFiles ;
43-
44- @ DebugDump private volatile boolean hasMoreWork ;
45- @ DebugDump private volatile boolean cancelled ;
46-
47- public MapPurgeTask (Path mapDirectory ) throws IOException {
48- this (null , mapDirectory );
42+ public static MapPurgeTask create (BmMap map ) throws IOException {
43+ Storage storage = map .getStorage ();
44+ if (storage instanceof FileStorage ) {
45+ return new MapFilePurgeTask (map , (FileStorage ) storage );
46+ } else {
47+ return new MapStoragePurgeTask (map );
48+ }
4949 }
5050
51- public MapPurgeTask ( BmMap map ) throws IOException {
52- this ( map , map . getFileRoot () );
51+ public static MapPurgeTask create ( Path mapDirectory ) throws IOException {
52+ return new MapFilePurgeTask ( mapDirectory );
5353 }
5454
55- private MapPurgeTask (BmMap map , Path directory ) throws IOException {
56- this .map = map ;
57- this .directory = directory ;
58- this .subFiles = Files .walk (directory , 3 )
59- .collect (Collectors .toCollection (LinkedList ::new ));
60- this .subFilesCount = subFiles .size ();
61- this .hasMoreWork = true ;
62- this .cancelled = false ;
63- }
55+ @ DebugDump
56+ private static class MapFilePurgeTask extends MapPurgeTask {
6457
65- @ Override
66- public void doWork () throws Exception {
67- synchronized (this ) {
68- if (!this .hasMoreWork ) return ;
69- this .hasMoreWork = false ;
58+ private final BmMap map ;
59+ private final Path directory ;
60+ private final int subFilesCount ;
61+ private final LinkedList <Path > subFiles ;
62+
63+ private volatile boolean hasMoreWork ;
64+ private volatile boolean cancelled ;
65+
66+ public MapFilePurgeTask (Path mapDirectory ) throws IOException {
67+ this (null , mapDirectory );
68+ }
69+
70+ public MapFilePurgeTask (BmMap map , FileStorage fileStorage ) throws IOException {
71+ this (map , fileStorage .getFilePath (map .getId ()));
7072 }
7173
72- try {
73- // delete subFiles first to be able to track the progress and cancel
74- while (!subFiles .isEmpty ()) {
75- Path subFile = subFiles .getLast ();
76- FileUtils .delete (subFile .toFile ());
77- subFiles .removeLast ();
78- if (this .cancelled ) return ;
74+ private MapFilePurgeTask (BmMap map , Path directory ) throws IOException {
75+ this .map = map ;
76+ this .directory = directory ;
77+ this .subFiles = Files .walk (directory , 3 )
78+ .collect (Collectors .toCollection (LinkedList ::new ));
79+ this .subFilesCount = subFiles .size ();
80+ this .hasMoreWork = true ;
81+ this .cancelled = false ;
82+ }
83+
84+ @ Override
85+ public void doWork () throws Exception {
86+ synchronized (this ) {
87+ if (!this .hasMoreWork ) return ;
88+ this .hasMoreWork = false ;
7989 }
8090
81- // make sure everything is deleted
82- FileUtils .delete (directory .toFile ());
83- } finally {
84- // reset map render state
85- if (this .map != null ) {
86- this .map .getRenderState ().reset ();
91+ try {
92+ // delete subFiles first to be able to track the progress and cancel
93+ while (!subFiles .isEmpty ()) {
94+ Path subFile = subFiles .getLast ();
95+ FileUtils .delete (subFile .toFile ());
96+ subFiles .removeLast ();
97+ if (this .cancelled ) return ;
98+ }
99+
100+ // make sure everything is deleted
101+ FileUtils .delete (directory .toFile ());
102+ } finally {
103+ // reset map render state
104+ if (this .map != null ) {
105+ this .map .getRenderState ().reset ();
106+ }
87107 }
88108 }
89- }
90109
91- @ Override
92- public boolean hasMoreWork () {
93- return this .hasMoreWork ;
94- }
110+ @ Override
111+ public boolean hasMoreWork () {
112+ return this .hasMoreWork ;
113+ }
95114
96- @ Override
97- public double estimateProgress () {
98- return 1d - (subFiles .size () / (double ) subFilesCount );
99- }
115+ @ Override
116+ @ DebugDump
117+ public double estimateProgress () {
118+ return 1d - (subFiles .size () / (double ) subFilesCount );
119+ }
100120
101- @ Override
102- public void cancel () {
103- this .cancelled = true ;
104- }
121+ @ Override
122+ public void cancel () {
123+ this .cancelled = true ;
124+ }
105125
106- @ Override
107- public boolean contains (RenderTask task ) {
108- if (task == this ) return true ;
109- if (task instanceof MapPurgeTask ) {
110- return ((MapPurgeTask ) task ).directory .toAbsolutePath ().normalize ().startsWith (this .directory .toAbsolutePath ().normalize ());
126+ @ Override
127+ public boolean contains (RenderTask task ) {
128+ if (task == this ) return true ;
129+ if (task instanceof MapFilePurgeTask ) {
130+ return ((MapFilePurgeTask ) task ).directory .toAbsolutePath ().normalize ()
131+ .startsWith (this .directory .toAbsolutePath ().normalize ());
132+ }
133+
134+ return false ;
135+ }
136+
137+ @ Override
138+ public String getDescription () {
139+ return "Purge Map " + directory .getFileName ();
111140 }
112141
113- return false ;
114142 }
115143
116- @ Override
117- public String getDescription () {
118- return "Purge Map " + directory .getFileName ();
144+ @ DebugDump
145+ private static class MapStoragePurgeTask extends MapPurgeTask {
146+
147+ private final BmMap map ;
148+
149+ private volatile boolean hasMoreWork ;
150+
151+ public MapStoragePurgeTask (BmMap map ) {
152+ this .map = Objects .requireNonNull (map );
153+ this .hasMoreWork = true ;
154+ }
155+
156+ @ Override
157+ public void doWork () throws Exception {
158+ synchronized (this ) {
159+ if (!this .hasMoreWork ) return ;
160+ this .hasMoreWork = false ;
161+ }
162+
163+ try {
164+ map .getStorage ().purgeMap (map .getId ());
165+ } finally {
166+ // reset map render state
167+ map .getRenderState ().reset ();
168+ }
169+ }
170+
171+ @ Override
172+ public boolean hasMoreWork () {
173+ return this .hasMoreWork ;
174+ }
175+
176+ @ Override
177+ @ DebugDump
178+ public double estimateProgress () {
179+ return 0d ;
180+ }
181+
182+ @ Override
183+ public void cancel () {
184+ this .hasMoreWork = false ;
185+ }
186+
187+ @ Override
188+ public boolean contains (RenderTask task ) {
189+ if (task == this ) return true ;
190+ if (task instanceof MapStoragePurgeTask ) {
191+ return map .equals (((MapStoragePurgeTask ) task ).map );
192+ }
193+
194+ return false ;
195+ }
196+
197+ @ Override
198+ public String getDescription () {
199+ return "Purge Map " + map .getId ();
200+ }
201+
119202 }
120203
121- }
204+ }
0 commit comments