Statistics
| Revision:

root / trunk / libraries / libRaster / tmp / cache2 / RasterCache2.java @ 10740

History | View | Annotate | Download (27.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19

    
20
package org.gvsig.raster.dataaccess.cache2;
21

    
22
import java.io.IOException;
23

    
24
import org.gvsig.raster.dataaccess.buffer.IBand;
25
import org.gvsig.raster.dataaccess.buffer.RasterBand;
26
import org.gvsig.raster.dataaccess.buffer.RasterBuffer;
27
import org.gvsig.raster.dataaccess.cache.Cache;
28
import org.gvsig.raster.dataaccess.cache.CacheDataServer;
29
import org.gvsig.raster.dataaccess.cache.InvalidPageNumberException;
30
import org.gvsig.raster.driver.IBuffer;
31

    
32
/*
33
 * TODO: OPTIMIZACION: Trabajo de optimizaci?n en la velocidad e acceso a cach?.
34
 * TODO: FUNCIONALIDAD: Acabar de implementar los m?todos de acceso a datos, intercambio de bandas, etc...
35
 */
36
/**
37
 * Esta clase representa un buffer de datos cacheado. El estar en cache significa que
38
 * solo una parte de los datos estar?n en memoria y que gestiona, dependiendo de las
39
 * peticiones, que partes hay que cargar a memoria y en que momento. Para esto el buffer
40
 * no se llena de golpe sino que utiliza una clase que sirve los datos desde la fuente. 
41
 * Esta clase servidora de datos debe implementar el interfaz ICacheDatasetSourcepara 
42
 * servir datos de la forma requerida.
43
 *   
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 *
46
 */
47
public class RasterCache2 extends RasterBuffer implements IBuffer {
48

    
49
        private MultiBandCache cache = null;
50
        
51
        /**
52
         * 
53
         * @author Nacho Brodin (nachobrodin@gmail.com)
54
         *
55
         */
56
        public class CacheBand extends RasterBand{
57
                private Cache                                cache = null;
58
            
59
            public CacheBand(int height, int width, boolean malloc){
60
                    super(height, width);
61
                    cache = new Cache(1, dataType, height, width);
62
                    cache.setDataSource(new CacheDataServer());
63
            }
64

    
65
                public Object getLine(int line) {
66
                        try {
67
                                cacheAccess(line, true);
68
                        } catch (InvalidPageNumberException e) {return null;}
69
                        switch(dataType){
70
                        case IBuffer.TYPE_BYTE: return cache.getAccessPage().getLineByte((line & cache.getOffset()))[0];
71
                        case IBuffer.TYPE_SHORT: return cache.getAccessPage().getLineShort((line & cache.getOffset()))[0];
72
                        case IBuffer.TYPE_INT: return cache.getAccessPage().getLineInt((line & cache.getOffset()))[0];
73
                        case IBuffer.TYPE_FLOAT: return cache.getAccessPage().getLineFloat((line & cache.getOffset()))[0];
74
                        case IBuffer.TYPE_DOUBLE: return cache.getAccessPage().getLineDouble((line & cache.getOffset()))[0];
75
                        }
76
                        return null;
77
                }
78
                
79
                public void setLine(int line, Object value){
80
                        try {
81
                                cacheAccess(line, false);
82
                        } catch (InvalidPageNumberException e) {return;}
83
                        switch(dataType){
84
                        case IBuffer.TYPE_BYTE: byte[][] bLine = new byte[1][];
85
                                                                        bLine[0] = (byte[])value;
86
                                                                        cache.getAccessPage().setLineByte(bLine, (line & cache.getOffset())); 
87
                                                                        break;
88
                        case IBuffer.TYPE_SHORT:short[][] sLine = new short[1][];
89
                                                                        sLine[0] = (short[])value;
90
                                                                        cache.getAccessPage().setLineShort(sLine, (line & cache.getOffset())); 
91
                                                                        break;
92
                        case IBuffer.TYPE_INT:         int[][] iLine = new int[1][];
93
                                                                        iLine[0] = (int[])value;
94
                                                                        cache.getAccessPage().setLineInt(iLine, (line & cache.getOffset())); 
95
                                                                        break;
96
                        case IBuffer.TYPE_FLOAT:float[][] fLine = new float[1][];
97
                                                                        fLine[0] = (float[])value;
98
                                                                        cache.getAccessPage().setLineFloat(fLine, (line & cache.getOffset())); 
99
                                                                        break;
100
                        case IBuffer.TYPE_DOUBLE:        double[][] dLine = new double[1][];
101
                                                                                dLine[0] = (double[])value;
102
                                                                                cache.getAccessPage().setLineDouble(dLine, (line & cache.getOffset())); 
103
                                                                                break;
104
                        }
105
                }
106
                
107
                public Object getBuf(){
108
                        return cache;
109
                }
110
    }
111
        
112
    /**
113
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de 
114
     * la cach? con los datos pasados.
115
     * @param dataType Tipo de dato
116
     * @param width Ancho
117
     * @param height Alto
118
     * @param bandNr Banda
119
     * @param orig
120
     */
121
        public RasterCache2(int dataType, int width, int height, int nBand){
122
                cache = new MultiBandCache(dataType, width, height, nBand);
123
            this.dataType = dataType;
124
        this.width = width;
125
        this.height = height;
126
        this.nBands = nBand;
127
        }
128
                        
129
        public void malloc(int dataType, int width, int height, int bandNr) {
130
        }
131

    
132
        /**
133
         * Obtiene la cach? de cada banda
134
         * @return
135
         */
136
        public MultiBandCache getCacheList() {
137
                return cache;
138
        }
139
        //*********************************************************
140
        
141
        public byte[][] getLineByte(int line) {
142
                try {
143
                        cacheAccess(line, true);
144
                } catch (InvalidPageNumberException e) {return null;}
145
                return cache.getAccessPage().getLineByte((line & cache.getOffset()));
146
        }
147

    
148
        public short[][] getLineShort(int line) {
149
                try {
150
                        cacheAccess(line, true);
151
                } catch (InvalidPageNumberException e) {return null;}
152
                return cache.getAccessPage().getLineShort((line & cache.getOffset()));
153
        }
154

    
155
        public int[][] getLineInt(int line) {
156
                try {
157
                        cacheAccess(line, true);
158
                } catch (InvalidPageNumberException e) {return null;}
159
                return cache.getAccessPage().getLineInt((line & cache.getOffset()));
160
        }
161

    
162
        public float[][] getLineFloat(int line) {
163
                try {
164
                        cacheAccess(line, true);
165
                } catch (InvalidPageNumberException e) {return null;}
166
                return cache.getAccessPage().getLineFloat((line & cache.getOffset()));
167
        }
168

    
169
        public double[][] getLineDouble(int line) {
170
                try {
171
                        cacheAccess(line, true);
172
                } catch (InvalidPageNumberException e) {return null;}
173
                return cache.getAccessPage().getLineDouble((line & cache.getOffset()));
174
        }
175

    
176
        //*********************************************************
177
        
178
        public void setLineByte(byte[][] data, int line) {
179
                try {
180
                        cacheAccess(line, false);
181
                } catch (InvalidPageNumberException e) {return;}
182
                cache.getAccessPage().setLineByte(data, (line & cache.getOffset()));
183
        }
184

    
185
        public void setLineShort(short[][] data, int line) {
186
                try {
187
                        cacheAccess(line, false);
188
                } catch (InvalidPageNumberException e) {return;}
189
                cache.getAccessPage().setLineShort(data, (line & cache.getOffset()));
190
        }
191

    
192
        public void setLineInt(int[][] data, int line) {
193
                try {
194
                        cacheAccess(line, false);
195
                } catch (InvalidPageNumberException e) {return;}
196
                cache.getAccessPage().setLineInt(data, (line & cache.getOffset()));
197
        }
198

    
199
        public void setLineFloat(float[][] data, int line) {
200
                try {
201
                        cacheAccess(line, false);
202
                } catch (InvalidPageNumberException e) {return;}
203
                cache.getAccessPage().setLineFloat(data, (line & cache.getOffset()));
204
        }
205

    
206
        public void setLineDouble(double[][] data, int line) {
207
                try {
208
                        cacheAccess(line, false);
209
                } catch (InvalidPageNumberException e) {return;}
210
                cache.getAccessPage().setLineDouble(data, (line & cache.getOffset()));
211
        }
212

    
213
        //*********************************************************
214
        
215
        public byte[] getLineFromBandByte(int line, int band) {
216
                try {
217
                        cacheAccess(line, true);
218
                } catch (InvalidPageNumberException e) {return null;}
219
                return cache.getAccessPage().getLineFromBandByte((line & cache.getOffset()), band);
220
        }
221

    
222
        public short[] getLineFromBandShort(int line, int band) {
223
                try {
224
                        cacheAccess(line, true);
225
                } catch (InvalidPageNumberException e) {return null;}
226
                return cache.getAccessPage().getLineFromBandShort((line & cache.getOffset()), band);
227
        }
228

    
229
        public int[] getLineFromBandInt(int line, int band) {
230
                try {
231
                        cacheAccess(line, true);
232
                } catch (InvalidPageNumberException e) {return null;}
233
                return cache.getAccessPage().getLineFromBandInt((line & cache.getOffset()), band);
234
        }
235

    
236
        public float[] getLineFromBandFloat(int line, int band) {
237
                try {
238
                        cacheAccess(line, true);
239
                } catch (InvalidPageNumberException e) {return null;}
240
                return cache.getAccessPage().getLineFromBandFloat((line & cache.getOffset()), band);
241
        }
242

    
243
        public double[] getLineFromBandDouble(int line, int band) {
244
                try {
245
                        cacheAccess(line, true);
246
                } catch (InvalidPageNumberException e) {return null;}
247
                return cache.getAccessPage().getLineFromBandDouble((line & cache.getOffset()), band);
248
        }
249

    
250
        //*********************************************************
251
        
252
        public void setLineInBandByte(byte[] data, int line, int band) {
253
                try {
254
                        cacheAccess(line, false);
255
                } catch (InvalidPageNumberException e) {return;}
256
                cache.getAccessPage().setLineInBandByte(data, (line & cache.getOffset()), band);
257
        }
258

    
259
        public void setLineInBandShort(short[] data, int line, int band) {
260
                try {
261
                        cacheAccess(line, false);
262
                } catch (InvalidPageNumberException e) {return;}
263
                cache.getAccessPage().setLineInBandShort(data, (line & cache.getOffset()), band);
264
        }
265

    
266
        public void setLineInBandInt(int[] data, int line, int band) {
267
                try {
268
                        cacheAccess(line, false);
269
                } catch (InvalidPageNumberException e) {return;}
270
                cache.getAccessPage().setLineInBandInt(data, (line & cache.getOffset()), band);
271
        }
272

    
273
        public void setLineInBandFloat(float[] data, int line, int band) {
274
                try {
275
                        cacheAccess(line, false);
276
                } catch (InvalidPageNumberException e) {return;}
277
                cache.getAccessPage().setLineInBandFloat(data, (line & cache.getOffset()), band);
278
        }
279

    
280
        public void setLineInBandDouble(double[] data, int line, int band) {
281
                try {
282
                        cacheAccess(line, false);
283
                } catch (InvalidPageNumberException e) {return;}
284
                cache.getAccessPage().setLineInBandDouble(data, (line & cache.getOffset()), band);
285
        }
286

    
287
        //*********************************************************
288
        
289
        public byte getElemByte(int line, int col, int band) {
290
                try {
291
                        cacheAccess(line, true);
292
                } catch (InvalidPageNumberException e) {
293
                        return (byte)getNoDataValue(); //No leemos el dato
294
                }
295
                return cache.getAccessPage().getElemByte((line & cache.getOffset()), col, band);
296
        }
297

    
298
        public short getElemShort(int line, int col, int band) {
299
                try {
300
                        cacheAccess(line, true);
301
                } catch (InvalidPageNumberException e) {
302
                        return (short)getNoDataValue(); //No leemos el dato
303
                }
304
                return cache.getAccessPage().getElemShort((line & cache.getOffset()), col, band);
305
        }
306

    
307
        public int getElemInt(int line, int col, int band) {
308
                try {
309
                        cacheAccess(line, true);
310
                } catch (InvalidPageNumberException e) {
311
                        return (int)getNoDataValue(); //No leemos el dato
312
                }
313
                return cache.getAccessPage().getElemInt((line & cache.getOffset()), col, band);
314
        }
315

    
316
        public float getElemFloat(int line, int col, int band) {
317
                try {
318
                        cacheAccess(line, true);
319
                } catch (InvalidPageNumberException e) {
320
                        return (float)getNoDataValue(); //No leemos el dato
321
                }
322
                return cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, band);
323
        }
324

    
325
        public double getElemDouble(int line, int col, int band) {
326
                try {
327
                        cacheAccess(line, true);
328
                } catch (InvalidPageNumberException e) {
329
                        return (double)getNoDataValue(); //No leemos el dato
330
                }
331
                return cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, band);
332
        }
333

    
334
        //*********************************************************
335
        
336
        public void setElem(int line, int col, int band, byte data) {
337
                try {
338
                        cacheAccess(line, false);
339
                } catch (InvalidPageNumberException e) { return;}
340
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
341
        }
342

    
343
        public void setElem(int line, int col, int band, short data) {
344
                try {
345
                        cacheAccess(line, false);
346
                } catch (InvalidPageNumberException e) {return;}
347
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
348
        }
349

    
350
        public void setElem(int line, int col, int band, int data) {
351
                try {
352
                        cacheAccess(line, false);
353
                } catch (InvalidPageNumberException e) {return;}
354
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
355
        }
356

    
357
        public void setElem(int line, int col, int band, float data) {
358
                try {
359
                        cacheAccess(line, false);
360
                } catch (InvalidPageNumberException e) {return;}
361
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
362
        }
363

    
364
        public void setElem(int line, int col, int band, double data) {
365
                try {
366
                        cacheAccess(line, false);
367
                } catch (InvalidPageNumberException e) {return;}
368
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
369
        }
370
        
371
        //*********************************************************
372

    
373
        public void getElemByte(int line, int col, byte[] data) {
374
                try {
375
                        cacheAccess(line, true);
376
                } catch (InvalidPageNumberException e) {
377
                        for (int iBand = 0; iBand < data.length; iBand++)
378
                    data[iBand] = (byte)getNoDataValue();
379
                }
380
                cache.getAccessPage().getElemByte((line & cache.getOffset()), col, data);
381
        }
382

    
383
        public void getElemShort(int line, int col, short[] data) {
384
                try {
385
                        cacheAccess(line, true);
386
                } catch (InvalidPageNumberException e) {
387
                        for (int iBand = 0; iBand < data.length; iBand++)
388
                    data[iBand] = (short)getNoDataValue();
389
                }
390
                cache.getAccessPage().getElemShort((line & cache.getOffset()), col, data);
391
        }
392

    
393
        public void getElemInt(int line, int col, int[] data) {
394
                try {
395
                        cacheAccess(line, true);
396
                } catch (InvalidPageNumberException e) {
397
                        for (int iBand = 0; iBand < data.length; iBand++)
398
                    data[iBand] = (int)getNoDataValue();
399
                }
400
                cache.getAccessPage().getElemInt((line & cache.getOffset()), col, data);
401
        }
402

    
403
        public void getElemFloat(int line, int col, float[] data) {
404
                try {
405
                        cacheAccess(line, true);
406
                } catch (InvalidPageNumberException e) {
407
                        for (int iBand = 0; iBand < data.length; iBand++)
408
                    data[iBand] = (float)getNoDataValue();
409
                }
410
                cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, data);
411
        }
412

    
413
        public void getElemDouble(int line, int col, double[] data) {
414
                try {
415
                        cacheAccess(line, true);
416
                } catch (InvalidPageNumberException e) {
417
                        for (int iBand = 0; iBand < data.length; iBand++)
418
                    data[iBand] = (double)getNoDataValue();
419
                }
420
                cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, data);
421
        }
422
        
423
        //*********************************************************
424
        
425
        public void setElemByte(int line, int col, byte[] data) {
426
                try {
427
                        cacheAccess(line, false);
428
                } catch (InvalidPageNumberException e) {return;}
429
                cache.getAccessPage().setElemByte((line & cache.getOffset()), col, data);
430
        }
431

    
432
        public void setElemShort(int line, int col, short[] data) {
433
                try {
434
                        cacheAccess(line, false);
435
                } catch (InvalidPageNumberException e) {return;}
436
                cache.getAccessPage().setElemShort((line & cache.getOffset()), col, data);
437
        }
438

    
439
        public void setElemInt(int line, int col, int[] data) {
440
                try {
441
                        cacheAccess(line, false);
442
                } catch (InvalidPageNumberException e) {return;}
443
                cache.getAccessPage().setElemInt((line & cache.getOffset()), col, data);
444
        }
445

    
446
        public void setElemFloat(int line, int col, float[] data) {
447
                try {
448
                        cacheAccess(line, false);
449
                } catch (InvalidPageNumberException e) {return;}
450
                cache.getAccessPage().setElemFloat((line & cache.getOffset()), col, data);
451
        }
452

    
453
        public void setElemDouble(int line, int col, double[] data) {
454
                try {
455
                        cacheAccess(line, false);
456
                } catch (InvalidPageNumberException e) {return;}
457
                cache.getAccessPage().setElemDouble((line & cache.getOffset()), col, data);
458
        }
459

    
460
        //TODO: FUNCIONALIDAD: Terminar m?todos de RasterCach?
461
                
462
        //***********************************************
463
    //Obtiene una banda entera
464
    
465
    public IBand getBand(int band){
466
            return null;
467
    }
468

    
469
    /*
470
     *  (non-Javadoc)
471
     * @see org.gvsig.fmap.driver.IBuffer#getBandBuffer(int)
472
     */
473
    public IBuffer getBandBuffer(int iBand){
474
            return null;
475
    }
476
    
477
    /*
478
     *  (non-Javadoc)
479
     * @see org.gvsig.fmap.driver.IBuffer#replicateBand(int, int)
480
     */
481
        public void replicateBand(int orig, int dest) {
482
        }
483
                
484
        /*
485
     *  (non-Javadoc)
486
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
487
     */
488
    public void switchBands(int[] bandPosition){
489
            
490
    }
491

    
492
        //*********************************************************
493
        
494
        public RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands) {
495
                return null;
496
        }
497

    
498
        public RasterBuffer adjustRasterBilinearInterpolation(int w, int h, int[] bands) {
499
                return null;
500
        }
501

    
502
        public RasterBuffer adjustRasterInverseDistanceInterpolation(int w, int h, int[] bands) {
503
                return null;
504
        }
505

    
506
        public RasterBuffer adjustRasterBicubicSplineInterpolation(int w, int h, int[] bands) {
507
                return null;
508
        }
509

    
510
        public RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands) {
511
                return null;
512
        }
513

    
514
        //*********************************************************
515
        
516
        public void assign(int band, byte value) {
517
                for(int line = 0; line < height; line ++){
518
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
519
                    for(int col = 0; col < width; col ++){
520
                            try {
521
                                    if(beginLine){
522
                                            cacheAccess(line, false);
523
                                            beginLine = false;
524
                                    }
525
                            } catch (InvalidPageNumberException e) {return;}
526
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
527
                    }
528
                }
529
        }
530

    
531
        public void assign(int band, short value) {
532
                for(int line = 0; line < height; line ++){
533
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
534
                    for(int col = 0; col < width; col ++){
535
                            try {
536
                                    if(beginLine){
537
                                            cacheAccess(line, false);
538
                                            beginLine = false;
539
                                    }
540
                            } catch (InvalidPageNumberException e) {return;}
541
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
542
                    }
543
                }                
544
        }
545

    
546
        public void assign(int band, int value) {
547
                for(int line = 0; line < height; line ++){
548
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
549
                    for(int col = 0; col < width; col ++){
550
                            try {
551
                                    if(beginLine){
552
                                            cacheAccess(line, false);
553
                                            beginLine = false;
554
                                    }
555
                            } catch (InvalidPageNumberException e) {return;}
556
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
557
                    }
558
                }        
559
        }
560

    
561
        public void assign(int band, float value) {
562
                for(int line = 0; line < height; line ++){
563
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
564
                    for(int col = 0; col < width; col ++){
565
                            try {
566
                                    if(beginLine){
567
                                            cacheAccess(line, false);
568
                                            beginLine = false;
569
                                    }
570
                            } catch (InvalidPageNumberException e) {return;}
571
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
572
                    }
573
                }        
574
        }
575

    
576
        public void assign(int band, double value) {
577
                for(int line = 0; line < height; line ++){
578
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
579
                    for(int col = 0; col < width; col ++){
580
                            try {
581
                                    if(beginLine){
582
                                            cacheAccess(line, false);
583
                                            beginLine = false;
584
                                    }
585
                            } catch (InvalidPageNumberException e) {return;}
586
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
587
                    }
588
                }
589
        }
590
    
591
    /*
592
     *  (non-Javadoc)
593
     * @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
594
     */
595
    public IBuffer cloneBuffer(){
596
            return null;
597
    }
598
    
599
    /*
600
     * (non-Javadoc)
601
     * @see org.gvsig.fmap.driver.IBuffer#interchangeBands(int, int)
602
     */
603
        public void interchangeBands(int band1, int band2) {
604
        }
605
        
606
    /*
607
     * (non-Javadoc)
608
     * @see org.gvsig.fmap.driver.IBuffer#interchangeBands(int[])
609
     */
610
    public void interchangeBands(int[] bands){
611
            
612
    }
613
    
614
    /*
615
     *  (non-Javadoc)
616
     * @see org.gvsig.fmap.driver.IBuffer#mallocOneBand(int, int, int, int)
617
     */
618
    public void mallocOneBand(int dataType, int width, int height, int band) {
619
                        
620
        }
621

    
622
    /*
623
     *  (non-Javadoc)
624
     * @see org.gvsig.fmap.driver.IBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
625
     */
626
        public void copyBand(int nBand, IBand band) {
627
                                
628
        }
629

    
630
        /*
631
         *  (non-Javadoc)
632
         * @see org.gvsig.fmap.driver.IBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
633
         */
634
        public void assignBand(int nBand, IBand band) {
635
                                
636
        }
637

    
638
        /*
639
         *  (non-Javadoc)
640
         * @see org.gvsig.fmap.driver.IBuffer#createBand(byte)
641
         */
642
        public IBand createBand(byte defaultValue) {
643
                
644
                return null;
645
        }
646

    
647
        /*
648
         *  (non-Javadoc)
649
         * @see org.gvsig.fmap.driver.IBuffer#assignBandToNotValid(int)
650
         */
651
        public void assignBandToNotValid(int iBand) {
652
                                
653
        }
654
        
655
        //***************************************************************
656
        //Algoritmo de reemplazo
657
        
658
        /**
659
         * Funci?n que controla para cada l?nea si la p?gina a la que accede est? cacheada o no
660
         * . Es la encargada de realizar los reemplazos o cargar la p?gina en el buffer de "p?gina
661
         * actualmente accedida" (accessPage de la clase Cache)
662
         * @param line L?nea del raster a la que se est? accediendo
663
         * @param readOnly ser? true si el acceso que se est? realizando es de lectura y false si se
664
         * est? escribiendo alg?n dato
665
         */
666
        private void cacheAccess(int line, boolean readOnly)throws InvalidPageNumberException{
667
                int pag = line >> cache.getBitsPag();                        
668
                if(cache.isInCache(pag)){
669
                        if(cache.getNumberInAccessPage() != pag)
670
                                loadPage(pag, readOnly);
671
                }else{
672
                        replacePage(pag + 1, readOnly, false);
673
                        replacePage(pag, readOnly, true);
674
                }
675
        }
676
        
677
        /**
678
         * Carga la p?gina desde cach? al buffer actualmente en uso. Esta operaci?n no lleva
679
         * cambio de datos sino que solo es un cambio de punteros. La secuencia de operaciones es:
680
         * <UL>
681
         * <LI>Cargar la p?gina como accedida</LI>
682
         * <LI>Asignar n?mero a la p?gina accedida</LI>
683
         * <LI>Actualizar la antig?edad de accesos.Se incrementar? uno en todas las p?ginas del 
684
         * conjunto y se pondr? a 0 la p?gina accedida.</LI>
685
         * <LI>Poner a 0 el valor de lastAccess de la p?gina cargada. Esto hace que tenga m?nima 
686
         * prioridad en la politica de reemplazamiento.</LI>
687
         * <LI>Poner a false el flag de p?gina modificada si el par?metro readOnly es false.</LI>
688
         * </UL>
689
         * @param nPag N?mero de p?gina del raster a la que se intenta acceder.
690
         * @param readOnly ser? true si el acceso que se est? realizando es de lectura y false si se
691
         * est? escribiendo alg?n dato. 
692
         */
693
        private void loadPage(int nPag, boolean readOnly)throws InvalidPageNumberException{
694
                MultiBandPageBuffer buf = cache.getPageBufferFromNumberRasterPage(nPag);
695
                int[] cacheGroupPage = cache.getNumberGroupFromNumberRasterPage(nPag);
696
                if(buf != null && cacheGroupPage != null){ //Comprueba que el n?mero de p?gina sea correcto para la cach?.
697
                        cache.setAccessPage(buf, nPag);
698
                        cache.updateLastAccess(cacheGroupPage[0]);
699
                        cache.setZeroInLastAccess(cacheGroupPage[0], cacheGroupPage[1]);
700
                        if(!readOnly)
701
                                cache.setModify(cacheGroupPage[0], cacheGroupPage[1]);
702
                }else
703
                        throw new InvalidPageNumberException("");
704
                //System.out.println("LOAD: "+nPag);
705
        }
706
        
707
        /**
708
         * <P>
709
         * Cuando se accede a una p?gina que no est? cacheada necesitamos cargarla en cach? antes de 
710
         * acceder a ella. Si hay un hueco libre en su conjunto se meter? en este pero sino habr? 
711
         * que reemplazar una ocupada. Para esto habr? que localizar el conjunto donde va 
712
         * destinada y luego la posici?n del conjunto donde se cargar? localizando cual es el 
713
         * elemento del conjunto que hace m?s tiempo que se accedi?.
714
         * </P>
715
         * <P>
716
         * Si el elemento es insertado en un hueco la secuencia es la siguiente:
717
         * </P>
718
         * <UL>
719
         * <LI>Obtenemos la siguiente p?gina vacia.</LI>
720
         * <LI>La cargamos de datos.</LI>
721
         * <LI>Se asigna como p?gina accedida</LI>
722
         * <LI>Ponemos true en su posici?n en el vector cacheada</LI>
723
         * <LI>Asignamos el n?mero de p?gina de raster que hemos cargado en esa posici?n de la cach?.</LI>
724
         * <LI>Incrementamos en 1 todos los valores de ?ltimo acceso de las p?ginas del grupo.</LI>
725
         * <LI>Ponemos a 0 su ?ltimo acceso</LI>
726
         * <LI>Si el acceso es para escritura ponemos el flag de p?gina modificada a true.</LI>
727
         * </UL>
728
         * <P>
729
         * Si se reemplaza una p?gina la secuencia es la siguiente:
730
         * </P>
731
         * <UL>
732
         *  <LI>Incrementamos en 1 todos los valores de ?ltimo acceso de las p?ginas del grupo y 
733
         *  obtenemos la posici?n de la p?gina de reemplazo.</LI>
734
         * <LI>Ponemos a false la p?gina que va a sacarse de cach?</LI>
735
         * <LI>Si ha sido modificada la p?gina que va a sacarse de cach? se vuelca a disco</LI>
736
         * <LI>Ponemos el flag de modificada para la p?gina sacada a disco a false.</LI>
737
         * <LI>Cargamos la p?gina de cach? de datos.</LI>
738
         * <LI>Asignamos el n?mero de p?gina de raster que hemos cargado en esa posici?n de la cach?.</LI>
739
         * <LI>Se asigna como p?gina accedida</LI>
740
         * <LI>Ponemos true en su posici?n en el vector cacheada</LI>
741
         * <LI>Ponemos a 0 su ?ltimo acceso</LI>
742
         * <LI>Si el acceso es para escritura ponemos el flag de p?gina modificada a true.</LI>
743
         * </UL>
744
         * @param nPag N?mero de p?gina que est? siendo accedida
745
         * @param readOnly ser? true si el acceso que se est? realizando es de lectura y false si se
746
         * est? escribiendo alg?n dato. 
747
         * @param n ser? true si el acceso se est? haciendo a la p?gina n y false si se est? haciendo a
748
         * la n+1. 
749
         */
750
        private void replacePage(int nPag, boolean readOnly, boolean n){
751
                int group = nPag % cache.getNGroups();
752
                MultiBandPageBuffer pb = null;
753

    
754
                if(nPag >= cache.getNTotalPags())
755
                        return;
756
                
757
                //Insertamos en un hueco
758
                
759
                if(insertInAHole(group, readOnly, nPag, n))
760
                        return;
761
                                
762
                //Reemplazamos una existente
763
                
764
                int posInGroupPageToReplace = cache.updateLastAccess(group); //Se actualizan los indices y se obtiene la p?gina a reemplazar
765
                
766
                cache.setZeroInLastAccess(group, posInGroupPageToReplace);
767
                int rasterPageToReplace = cache.getRasterPageNumberInPosition(group, posInGroupPageToReplace);
768
                pb = cache.getPageBuffer(group, posInGroupPageToReplace);
769
                
770
                //Sacamos la antigua
771
                cache.setPageAsNotLoadInCache(rasterPageToReplace);
772
                if(cache.isModified(group, posInGroupPageToReplace)){
773
                        try {
774
                                cache.savePage(rasterPageToReplace, pb);         //Volcamos disco 
775
                                cache.unsetModify(group, posInGroupPageToReplace);
776
                        } catch (IOException e) {
777
                                System.err.println("No se ha podido salvar la p?gina de cach?.");
778
                                e.printStackTrace();
779
                        }
780
                }
781
                
782
                //Insertamos la nueva
783
                cache.loadPage(nPag, pb);        //Cargamos de nuevo el buffer
784
                cache.setRasterPageNumberInPosition(group, posInGroupPageToReplace, nPag);
785
                cache.setPageAsLoadInCache(nPag);                //Pone true en su posici?n en el vector cacheada
786
                
787
                if(n){
788
                        if(!readOnly)
789
                                cache.setModify(group, posInGroupPageToReplace);
790
                        cache.setAccessPage(pb, nPag);          //Asigna como accedida
791
                }
792
                //System.out.println("REPLACE: "+nPag);
793
        }
794
        
795
        /**
796
         * Comprueba si hay alg?n hueco en el que insertar y si es as? se inserta en el y devuelve
797
         * true. 
798
         * @param group conjunto donde hay que buscar para la inserci?n
799
         * @param readOnly si es true la operaci?n es de solo consulta y si es false la operaci?n 
800
         * est? modificando alg?n valor de la p?gina a insertar
801
         * @param nPag N?mero de p?gina a cargar
802
         * @return true si se ha insertado en un hueco y false si no se ha insertado
803
         */
804
        private boolean insertInAHole(int group, boolean readOnly, int nPag, boolean n){
805
                MultiBandPageBuffer pb = null;
806
                for(int i = 0; i < cache.getPagsPerGroup(); i++){
807
                        if(cache.getLastAccess()[group][i] == -1){
808

    
809
                                //Pone true en su posici?n en el vector cacheada
810
                                pb = cache.getPageBuffer(group, i);
811
                                cache.loadPage(nPag, pb);                                        //Sube la p?gina a cach?
812
                                cache.setRasterPageNumberInPosition(group, i, nPag);//Asigna el n?mero de p?g a una posici?n de cach?
813
                                cache.setPageAsLoadInCache(nPag);
814
                                cache.updateLastAccess(group);
815
                                cache.setZeroInLastAccess(group, i);
816
                                if(n){ //La n+1 no se carga como accedida ni se pone a 0 su contador de ?ltima accedida        
817
                                        if(!readOnly)
818
                                                cache.setModify(group, i);
819
                                        cache.setAccessPage(pb, nPag);  //Asigna como accedida
820
                                }                                
821
                                //System.out.println("INSERT: "+nPag);
822
                                return true;
823
                        }
824
                }
825
                return false;
826
        }
827

    
828
}