Revision 36207 branches/v2_0_0_prep/libraries/libFMap_dal/src/org/gvsig/fmap/dal/feature/impl/DefaultFeatureIndex.java

View differences:

DefaultFeatureIndex.java
31 31
import java.util.ArrayList;
32 32
import java.util.List;
33 33

  
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

  
34 37
import org.gvsig.fmap.dal.DataTypes;
35 38
import org.gvsig.fmap.dal.exception.DataException;
36 39
import org.gvsig.fmap.dal.exception.InitializeException;
37 40
import org.gvsig.fmap.dal.feature.Feature;
38 41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39 42
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
40 44
import org.gvsig.fmap.dal.feature.FeatureType;
41 45
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
42 46
import org.gvsig.fmap.dal.feature.exception.InvalidFeatureIndexException;
......
45 49
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
46 50
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
47 51
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
52
import org.gvsig.tools.ToolsLocator;
48 53
import org.gvsig.tools.dispose.DisposableIterator;
49 54
import org.gvsig.tools.dispose.DisposeUtils;
55
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
56
import org.gvsig.tools.task.SimpleTaskStatus;
57
import org.gvsig.tools.task.TaskStatusManager;
50 58

  
51 59
/**
52 60
 * Default feature index provider services.
......
54 62
 * @author jyarza
55 63
 * 
56 64
 */
57
public class DefaultFeatureIndex implements FeatureIndexProviderServices {
65
public class DefaultFeatureIndex extends BaseWeakReferencingObservable
66
    implements FeatureIndexProviderServices {
58 67

  
68
    private static final Logger LOG = LoggerFactory
69
        .getLogger(DefaultFeatureIndex.class);
70

  
59 71
    private final FeatureStoreProviderServices featureStore;
60 72
    private final FeatureType featureType;
61 73
    private final String attributeName;
......
132 144
        return dataType;
133 145
    }
134 146

  
135
    /**
136
     * Fills this index with all the data in its FeatureStore
137
     */
138
    public final void fill() throws FeatureIndexException {
147
    public synchronized final void fill() throws FeatureIndexException {
148
        fill(false);
149
    }
150

  
151
    public void fill(boolean background) throws FeatureIndexException {
139 152
        if (!isValid()) {
140 153
            throw new InvalidFeatureIndexException();
141 154
        }
155
        if (background) {
156
            filling = true;
157

  
158
            Runnable task = new Runnable() {
159

  
160
                public void run() {
161
                    try {
162
                        clearAndFill();
163
                    } catch (DataException e) {
164
                        LOG.error("Error filling index: " + this, e);
165
                    }
166
                }
167
            };
168

  
169
            Thread thread = new Thread(task, "Fill index data");
170
            thread.start();
171
        } else {
172
            try {
173
                clearAndFill();
174
            } catch (DataException e) {
175
                throw new FeatureIndexException(e);
176
            }
177
        }
178
    }
179

  
180
    /**
181
     * @throws DataException
182
     */
183
    private void clearAndFill() throws DataException {
142 184
        FeatureSet set = null;
143 185
        try {
186
            filling = true;
144 187
            set =
145 188
                getFeatureStoreProviderServices().getFeatureStore()
146 189
                    .getFeatureSet();
147
            filling = true;
148 190
            clear();
149 191
            doInsert(set);
192
            filling = false;
193
            notify(this, new DefaultFeatureStoreNotification(
194
                getFeatureStoreProviderServices().getFeatureStore(),
195
                FeatureStoreNotification.INDEX_FILLED_SUCCESSFULLY, this));
150 196
        } catch (DataException e) {
197
            filling = false;
198
            setValid(false);
199
            notify(this, new DefaultFeatureStoreNotification(
200
                getFeatureStoreProviderServices().getFeatureStore(),
201
                FeatureStoreNotification.INDEX_FILLING_ERROR, e));
151 202
            throw new FeatureIndexException(e);
152 203
        } finally {
153
            filling = false;
154 204
            DisposeUtils.dispose(set);
155 205
        }
156 206
    }
157 207

  
158
    public final void insert(FeatureSet data) throws DataException {
208
    public synchronized final void insert(FeatureSet data) throws DataException {
159 209
        if (!isValid()) {
160 210
            throw new InvalidFeatureIndexException();
161 211
        }
......
169 219

  
170 220
    private void doInsert(FeatureSet data) throws DataException {
171 221
        DisposableIterator it = null;
172

  
222
        TaskStatusManager statusManager = ToolsLocator.getTaskStatusManager();
223
        SimpleTaskStatus status =
224
            statusManager.createDefaultSimpleTaskStatus("Filling index");
225
        status.setCancellable(false);
226
        // status.setRangeOfValues(0, data.getSize());
227
        status.add();
228
        long counter = 0;
173 229
        try {
174 230
            it = data.fastIterator();
175 231
            while (it.hasNext()) {
176 232
                Feature feat = (Feature) it.next();
177 233
                doInsert(feat);
234
                status.setCurValue(counter++);
178 235
            }
236
        } catch (RuntimeException e) {
237
            status.abort();
238
            throw e;
179 239
        } finally {
180 240
            DisposeUtils.dispose(it);
241
            status.remove();
181 242
        }
182 243
    }
183 244

  
184
    public void insert(Feature feat) {
245
    public synchronized final void insert(Feature feat) {
185 246
        if (!isValid()) {
186 247
            throw new RuntimeException(new InvalidFeatureIndexException());
187 248
        }
......
247 308
        indexProvider.initialize();
248 309
    }
249 310

  
250
    public void delete(Feature feat) {
311
    public synchronized final void delete(Feature feat) {
251 312
        if (!isValid()) {
252 313
            throw new RuntimeException(new InvalidFeatureIndexException());
253 314
        }
......
264 325
            (FeatureReferenceProviderServices) feat.getReference());
265 326
    }
266 327

  
267
    public void delete(FeatureSet data) throws FeatureIndexException {
328
    public synchronized final void delete(FeatureSet data)
329
        throws FeatureIndexException {
268 330
        if (!isValid()) {
269 331
            throw new InvalidFeatureIndexException();
270 332
        }

Also available in: Unified diff