Statistics
| Revision:

root / trunk / libraries / libRaster / tmp / cache / RasterCache.java @ 10740

History | View | Annotate | Download (25.3 KB)

1 10740 nacho
/* 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.cache;
21
22
import java.io.IOException;
23
24
import org.gvsig.raster.dataaccess.buffer.IBand;
25
import org.gvsig.raster.dataaccess.buffer.RasterBuffer;
26
import org.gvsig.raster.driver.IBuffer;
27
28
/*
29
 * TODO: OPTIMIZACION: Trabajo de optimizaci?n en la velocidad e acceso a cach?.
30
 * TODO: FUNCIONALIDAD: Acabar de implementar los m?todos de acceso a datos, intercambio de bandas, etc...
31
 */
32
/**
33
 * Esta clase representa un buffer de datos cacheado. El estar en cache significa que
34
 * solo una parte de los datos estar?n en memoria y que gestiona, dependiendo de las
35
 * peticiones, que partes hay que cargar a memoria y en que momento. Para esto el buffer
36
 * no se llena de golpe sino que utiliza una clase que sirve los datos desde la fuente.
37
 * Esta clase servidora de datos debe implementar el interfaz ICacheDatasetSourcepara
38
 * servir datos de la forma requerida.
39
 *
40
 * @author Nacho Brodin (brodin_ign@gva.es)
41
 *
42
 */
43
public class RasterCache extends RasterBuffer implements IBuffer {
44
45
        private Cache cache = null;
46
47
    /**
48
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de
49
     * la cach? con los datos pasados.
50
     * @param dataType Tipo de dato
51
     * @param width Ancho
52
     * @param height Alto
53
     * @param bandNr Banda
54
     * @param orig
55
     */
56
        public RasterCache(int dataType, int width, int height, int nBand){
57
                cache = new Cache(nBand, dataType, width, height);
58
                cache.setDataSource(new CacheDataServer());
59
60
            this.dataType = dataType;
61
        this.width = width;
62
        this.height = height;
63
        this.nBands = nBand;
64
        }
65
66
        public void malloc(int dataType, int width, int height, int bandNr) {
67
        }
68
69
        /**
70
         * Obtiene la cach?
71
         * @return
72
         */
73
        public Cache getCache() {
74
                return cache;
75
        }
76
        //*********************************************************
77
78
        public byte[][] getLineByte(int line) {
79
                try {
80
                        cacheAccess(line, true);
81
                } catch (InvalidPageNumberException e) {return null;}
82
                return cache.getAccessPage().getLineByte((line & cache.getOffset()));
83
        }
84
85
        public short[][] getLineShort(int line) {
86
                try {
87
                        cacheAccess(line, true);
88
                } catch (InvalidPageNumberException e) {return null;}
89
                return cache.getAccessPage().getLineShort((line & cache.getOffset()));
90
        }
91
92
        public int[][] getLineInt(int line) {
93
                try {
94
                        cacheAccess(line, true);
95
                } catch (InvalidPageNumberException e) {return null;}
96
                return cache.getAccessPage().getLineInt((line & cache.getOffset()));
97
        }
98
99
        public float[][] getLineFloat(int line) {
100
                try {
101
                        cacheAccess(line, true);
102
                } catch (InvalidPageNumberException e) {return null;}
103
                return cache.getAccessPage().getLineFloat((line & cache.getOffset()));
104
        }
105
106
        public double[][] getLineDouble(int line) {
107
                try {
108
                        cacheAccess(line, true);
109
                } catch (InvalidPageNumberException e) {return null;}
110
                return cache.getAccessPage().getLineDouble((line & cache.getOffset()));
111
        }
112
113
        //*********************************************************
114
115
        public void setLineByte(byte[][] data, int line) {
116
                try {
117
                        cacheAccess(line, false);
118
                } catch (InvalidPageNumberException e) {return;}
119
                cache.getAccessPage().setLineByte(data, (line & cache.getOffset()));
120
        }
121
122
        public void setLineShort(short[][] data, int line) {
123
                try {
124
                        cacheAccess(line, false);
125
                } catch (InvalidPageNumberException e) {return;}
126
                cache.getAccessPage().setLineShort(data, (line & cache.getOffset()));
127
        }
128
129
        public void setLineInt(int[][] data, int line) {
130
                try {
131
                        cacheAccess(line, false);
132
                } catch (InvalidPageNumberException e) {return;}
133
                cache.getAccessPage().setLineInt(data, (line & cache.getOffset()));
134
        }
135
136
        public void setLineFloat(float[][] data, int line) {
137
                try {
138
                        cacheAccess(line, false);
139
                } catch (InvalidPageNumberException e) {return;}
140
                cache.getAccessPage().setLineFloat(data, (line & cache.getOffset()));
141
        }
142
143
        public void setLineDouble(double[][] data, int line) {
144
                try {
145
                        cacheAccess(line, false);
146
                } catch (InvalidPageNumberException e) {return;}
147
                cache.getAccessPage().setLineDouble(data, (line & cache.getOffset()));
148
        }
149
150
        //*********************************************************
151
152
        public byte[] getLineFromBandByte(int line, int band) {
153
                try {
154
                        cacheAccess(line, true);
155
                } catch (InvalidPageNumberException e) {return null;}
156
                return cache.getAccessPage().getLineFromBandByte((line & cache.getOffset()), band);
157
        }
158
159
        public short[] getLineFromBandShort(int line, int band) {
160
                try {
161
                        cacheAccess(line, true);
162
                } catch (InvalidPageNumberException e) {return null;}
163
                return cache.getAccessPage().getLineFromBandShort((line & cache.getOffset()), band);
164
        }
165
166
        public int[] getLineFromBandInt(int line, int band) {
167
                try {
168
                        cacheAccess(line, true);
169
                } catch (InvalidPageNumberException e) {return null;}
170
                return cache.getAccessPage().getLineFromBandInt((line & cache.getOffset()), band);
171
        }
172
173
        public float[] getLineFromBandFloat(int line, int band) {
174
                try {
175
                        cacheAccess(line, true);
176
                } catch (InvalidPageNumberException e) {return null;}
177
                return cache.getAccessPage().getLineFromBandFloat((line & cache.getOffset()), band);
178
        }
179
180
        public double[] getLineFromBandDouble(int line, int band) {
181
                try {
182
                        cacheAccess(line, true);
183
                } catch (InvalidPageNumberException e) {return null;}
184
                return cache.getAccessPage().getLineFromBandDouble((line & cache.getOffset()), band);
185
        }
186
187
        //*********************************************************
188
189
        public void setLineInBandByte(byte[] data, int line, int band) {
190
                try {
191
                        cacheAccess(line, false);
192
                } catch (InvalidPageNumberException e) {return;}
193
                cache.getAccessPage().setLineInBandByte(data, (line & cache.getOffset()), band);
194
        }
195
196
        public void setLineInBandShort(short[] data, int line, int band) {
197
                try {
198
                        cacheAccess(line, false);
199
                } catch (InvalidPageNumberException e) {return;}
200
                cache.getAccessPage().setLineInBandShort(data, (line & cache.getOffset()), band);
201
        }
202
203
        public void setLineInBandInt(int[] data, int line, int band) {
204
                try {
205
                        cacheAccess(line, false);
206
                } catch (InvalidPageNumberException e) {return;}
207
                cache.getAccessPage().setLineInBandInt(data, (line & cache.getOffset()), band);
208
        }
209
210
        public void setLineInBandFloat(float[] data, int line, int band) {
211
                try {
212
                        cacheAccess(line, false);
213
                } catch (InvalidPageNumberException e) {return;}
214
                cache.getAccessPage().setLineInBandFloat(data, (line & cache.getOffset()), band);
215
        }
216
217
        public void setLineInBandDouble(double[] data, int line, int band) {
218
                try {
219
                        cacheAccess(line, false);
220
                } catch (InvalidPageNumberException e) {return;}
221
                cache.getAccessPage().setLineInBandDouble(data, (line & cache.getOffset()), band);
222
        }
223
224
        //*********************************************************
225
226
        public byte getElemByte(int line, int col, int band) {
227
                try {
228
                        cacheAccess(line, true);
229
                } catch (InvalidPageNumberException e) {
230
                        return (byte)getNoDataValue(); //No leemos el dato
231
                }
232
                return cache.getAccessPage().getElemByte((line & cache.getOffset()), col, band);
233
        }
234
235
        public short getElemShort(int line, int col, int band) {
236
                try {
237
                        cacheAccess(line, true);
238
                } catch (InvalidPageNumberException e) {
239
                        return (short)getNoDataValue(); //No leemos el dato
240
                }
241
                return cache.getAccessPage().getElemShort((line & cache.getOffset()), col, band);
242
        }
243
244
        public int getElemInt(int line, int col, int band) {
245
                try {
246
                        cacheAccess(line, true);
247
                } catch (InvalidPageNumberException e) {
248
                        return (int)getNoDataValue(); //No leemos el dato
249
                }
250
                return cache.getAccessPage().getElemInt((line & cache.getOffset()), col, band);
251
        }
252
253
        public float getElemFloat(int line, int col, int band) {
254
                try {
255
                        cacheAccess(line, true);
256
                } catch (InvalidPageNumberException e) {
257
                        return (float)getNoDataValue(); //No leemos el dato
258
                }
259
                return cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, band);
260
        }
261
262
        public double getElemDouble(int line, int col, int band) {
263
                try {
264
                        cacheAccess(line, true);
265
                } catch (InvalidPageNumberException e) {
266
                        return (double)getNoDataValue(); //No leemos el dato
267
                }
268
                return cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, band);
269
        }
270
271
        //*********************************************************
272
273
        public void setElem(int line, int col, int band, byte data) {
274
                try {
275
                        cacheAccess(line, false);
276
                } catch (InvalidPageNumberException e) { return;}
277
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
278
        }
279
280
        public void setElem(int line, int col, int band, short data) {
281
                try {
282
                        cacheAccess(line, false);
283
                } catch (InvalidPageNumberException e) {return;}
284
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
285
        }
286
287
        public void setElem(int line, int col, int band, int data) {
288
                try {
289
                        cacheAccess(line, false);
290
                } catch (InvalidPageNumberException e) {return;}
291
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
292
        }
293
294
        public void setElem(int line, int col, int band, float data) {
295
                try {
296
                        cacheAccess(line, false);
297
                } catch (InvalidPageNumberException e) {return;}
298
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
299
        }
300
301
        public void setElem(int line, int col, int band, double data) {
302
                try {
303
                        cacheAccess(line, false);
304
                } catch (InvalidPageNumberException e) {return;}
305
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
306
        }
307
308
        //*********************************************************
309
310
        public void getElemByte(int line, int col, byte[] data) {
311
                try {
312
                        cacheAccess(line, true);
313
                } catch (InvalidPageNumberException e) {
314
                        for (int iBand = 0; iBand < data.length; iBand++)
315
                    data[iBand] = (byte)getNoDataValue();
316
                }
317
                cache.getAccessPage().getElemByte((line & cache.getOffset()), col, data);
318
        }
319
320
        public void getElemShort(int line, int col, short[] data) {
321
                try {
322
                        cacheAccess(line, true);
323
                } catch (InvalidPageNumberException e) {
324
                        for (int iBand = 0; iBand < data.length; iBand++)
325
                    data[iBand] = (short)getNoDataValue();
326
                }
327
                cache.getAccessPage().getElemShort((line & cache.getOffset()), col, data);
328
        }
329
330
        public void getElemInt(int line, int col, int[] data) {
331
                try {
332
                        cacheAccess(line, true);
333
                } catch (InvalidPageNumberException e) {
334
                        for (int iBand = 0; iBand < data.length; iBand++)
335
                    data[iBand] = (int)getNoDataValue();
336
                }
337
                cache.getAccessPage().getElemInt((line & cache.getOffset()), col, data);
338
        }
339
340
        public void getElemFloat(int line, int col, float[] data) {
341
                try {
342
                        cacheAccess(line, true);
343
                } catch (InvalidPageNumberException e) {
344
                        for (int iBand = 0; iBand < data.length; iBand++)
345
                    data[iBand] = (float)getNoDataValue();
346
                }
347
                cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, data);
348
        }
349
350
        public void getElemDouble(int line, int col, double[] data) {
351
                try {
352
                        cacheAccess(line, true);
353
                } catch (InvalidPageNumberException e) {
354
                        for (int iBand = 0; iBand < data.length; iBand++)
355
                    data[iBand] = (double)getNoDataValue();
356
                }
357
                cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, data);
358
        }
359
360
        //*********************************************************
361
362
        public void setElemByte(int line, int col, byte[] data) {
363
                try {
364
                        cacheAccess(line, false);
365
                } catch (InvalidPageNumberException e) {return;}
366
                cache.getAccessPage().setElemByte((line & cache.getOffset()), col, data);
367
        }
368
369
        public void setElemShort(int line, int col, short[] data) {
370
                try {
371
                        cacheAccess(line, false);
372
                } catch (InvalidPageNumberException e) {return;}
373
                cache.getAccessPage().setElemShort((line & cache.getOffset()), col, data);
374
        }
375
376
        public void setElemInt(int line, int col, int[] data) {
377
                try {
378
                        cacheAccess(line, false);
379
                } catch (InvalidPageNumberException e) {return;}
380
                cache.getAccessPage().setElemInt((line & cache.getOffset()), col, data);
381
        }
382
383
        public void setElemFloat(int line, int col, float[] data) {
384
                try {
385
                        cacheAccess(line, false);
386
                } catch (InvalidPageNumberException e) {return;}
387
                cache.getAccessPage().setElemFloat((line & cache.getOffset()), col, data);
388
        }
389
390
        public void setElemDouble(int line, int col, double[] data) {
391
                try {
392
                        cacheAccess(line, false);
393
                } catch (InvalidPageNumberException e) {return;}
394
                cache.getAccessPage().setElemDouble((line & cache.getOffset()), col, data);
395
        }
396
397
        //TODO: FUNCIONALIDAD: Terminar m?todos de RasterCach?
398
399
        //***********************************************
400
    //Obtiene una banda entera
401
402
    public IBand getBand(int band){
403
            return null;
404
    }
405
406
    /*
407
     *  (non-Javadoc)
408
     * @see org.gvsig.fmap.driver.IBuffer#getBandBuffer(int)
409
     */
410
    public IBuffer getBandBuffer(int iBand){
411
            return null;
412
    }
413
414
    /*
415
     *  (non-Javadoc)
416
     * @see org.gvsig.fmap.driver.IBuffer#replicateBand(int, int)
417
     */
418
        public void replicateBand(int orig, int dest) {
419
        }
420
421
        /*
422
     *  (non-Javadoc)
423
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
424
     */
425
    public void switchBands(int[] bandPosition){
426
427
    }
428
429
        //*********************************************************
430
431
        public RasterBuffer adjustRasterNearestNeighbourInterpolation(int w, int h, int[] bands) {
432
                return null;
433
        }
434
435
        public RasterBuffer adjustRasterBilinearInterpolation(int w, int h, int[] bands) {
436
                return null;
437
        }
438
439
        public RasterBuffer adjustRasterInverseDistanceInterpolation(int w, int h, int[] bands) {
440
                return null;
441
        }
442
443
        public RasterBuffer adjustRasterBicubicSplineInterpolation(int w, int h, int[] bands) {
444
                return null;
445
        }
446
447
        public RasterBuffer adjustRasterBSplineInterpolation(int w, int h, int[] bands) {
448
                return null;
449
        }
450
451
        //*********************************************************
452
453
        public void assign(int band, byte value) {
454
                for(int line = 0; line < height; line ++){
455
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
456
                    for(int col = 0; col < width; col ++){
457
                            try {
458
                                    if(beginLine){
459
                                            cacheAccess(line, false);
460
                                            beginLine = false;
461
                                    }
462
                            } catch (InvalidPageNumberException e) {return;}
463
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);
464
                    }
465
                }
466
        }
467
468
        public void assign(int band, short value) {
469
                for(int line = 0; line < height; line ++){
470
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
471
                    for(int col = 0; col < width; col ++){
472
                            try {
473
                                    if(beginLine){
474
                                            cacheAccess(line, false);
475
                                            beginLine = false;
476
                                    }
477
                            } catch (InvalidPageNumberException e) {return;}
478
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);
479
                    }
480
                }
481
        }
482
483
        public void assign(int band, int value) {
484
                for(int line = 0; line < height; line ++){
485
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
486
                    for(int col = 0; col < width; col ++){
487
                            try {
488
                                    if(beginLine){
489
                                            cacheAccess(line, false);
490
                                            beginLine = false;
491
                                    }
492
                            } catch (InvalidPageNumberException e) {return;}
493
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);
494
                    }
495
                }
496
        }
497
498
        public void assign(int band, float value) {
499
                for(int line = 0; line < height; line ++){
500
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
501
                    for(int col = 0; col < width; col ++){
502
                            try {
503
                                    if(beginLine){
504
                                            cacheAccess(line, false);
505
                                            beginLine = false;
506
                                    }
507
                            } catch (InvalidPageNumberException e) {return;}
508
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);
509
                    }
510
                }
511
        }
512
513
        public void assign(int band, double value) {
514
                for(int line = 0; line < height; line ++){
515
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
516
                    for(int col = 0; col < width; col ++){
517
                            try {
518
                                    if(beginLine){
519
                                            cacheAccess(line, false);
520
                                            beginLine = false;
521
                                    }
522
                            } catch (InvalidPageNumberException e) {return;}
523
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);
524
                    }
525
                }
526
        }
527
528
    /*
529
     *  (non-Javadoc)
530
     * @see org.gvsig.fmap.driver.IBuffer#cloneBuffer()
531
     */
532
    public IBuffer cloneBuffer(){
533
            return null;
534
    }
535
536
    /*
537
     * (non-Javadoc)
538
     * @see org.gvsig.fmap.driver.IBuffer#interchangeBands(int, int)
539
     */
540
        public void interchangeBands(int band1, int band2) {
541
        }
542
543
    /*
544
     * (non-Javadoc)
545
     * @see org.gvsig.fmap.driver.IBuffer#interchangeBands(int[])
546
     */
547
    public void interchangeBands(int[] bands){
548
549
    }
550
551
    /*
552
     *  (non-Javadoc)
553
     * @see org.gvsig.fmap.driver.IBuffer#mallocOneBand(int, int, int, int)
554
     */
555
    public void mallocOneBand(int dataType, int width, int height, int band) {
556
557
        }
558
559
    /*
560
     *  (non-Javadoc)
561
     * @see org.gvsig.fmap.driver.IBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
562
     */
563
        public void copyBand(int nBand, IBand band) {
564
565
        }
566
567
        /*
568
         *  (non-Javadoc)
569
         * @see org.gvsig.fmap.driver.IBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
570
         */
571
        public void assignBand(int nBand, IBand band) {
572
573
        }
574
575
        /*
576
         *  (non-Javadoc)
577
         * @see org.gvsig.fmap.driver.IBuffer#createBand(byte)
578
         */
579
        public IBand createBand(byte defaultValue) {
580
581
                return null;
582
        }
583
584
        /*
585
         *  (non-Javadoc)
586
         * @see org.gvsig.fmap.driver.IBuffer#assignBandToNotValid(int)
587
         */
588
        public void assignBandToNotValid(int iBand) {
589
590
        }
591
592
        //***************************************************************
593
        //Algoritmo de reemplazo
594
595
        /**
596
         * Funci?n que controla para cada l?nea si la p?gina a la que accede est? cacheada o no
597
         * . Es la encargada de realizar los reemplazos o cargar la p?gina en el buffer de "p?gina
598
         * actualmente accedida" (accessPage de la clase Cache)
599
         * @param line L?nea del raster a la que se est? accediendo
600
         * @param readOnly ser? true si el acceso que se est? realizando es de lectura y false si se
601
         * est? escribiendo alg?n dato
602
         */
603
        private void cacheAccess(int line, boolean readOnly)throws InvalidPageNumberException{
604
                int pag = line >> cache.getBitsPag();
605
                if(cache.isInCache(pag)){
606
                        if(cache.getNumberInAccessPage() != pag)
607
                                loadPage(pag, readOnly);
608
                }else{
609
                        replacePage(pag + 1, readOnly, false);
610
                        replacePage(pag, readOnly, true);
611
                }
612
        }
613
614
        /**
615
         * Carga la p?gina desde cach? al buffer actualmente en uso. Esta operaci?n no lleva
616
         * cambio de datos sino que solo es un cambio de punteros. La secuencia de operaciones es:
617
         * <UL>
618
         * <LI>Cargar la p?gina como accedida</LI>
619
         * <LI>Asignar n?mero a la p?gina accedida</LI>
620
         * <LI>Actualizar la antig?edad de accesos.Se incrementar? uno en todas las p?ginas del
621
         * conjunto y se pondr? a 0 la p?gina accedida.</LI>
622
         * <LI>Poner a 0 el valor de lastAccess de la p?gina cargada. Esto hace que tenga m?nima
623
         * prioridad en la politica de reemplazamiento.</LI>
624
         * <LI>Poner a false el flag de p?gina modificada si el par?metro readOnly es false.</LI>
625
         * </UL>
626
         * @param nPag N?mero de p?gina del raster a la que se intenta acceder.
627
         * @param readOnly ser? true si el acceso que se est? realizando es de lectura y false si se
628
         * est? escribiendo alg?n dato.
629
         */
630
        private void loadPage(int nPag, boolean readOnly)throws InvalidPageNumberException{
631
                PageBuffer buf = cache.getPageBufferFromNumberRasterPage(nPag);
632
                int[] cacheGroupPage = cache.getNumberGroupFromNumberRasterPage(nPag);
633
                if(buf != null && cacheGroupPage != null){ //Comprueba que el n?mero de p?gina sea correcto para la cach?.
634
                        cache.setAccessPage(buf, nPag);
635
                        cache.updateLastAccess(cacheGroupPage[0]);
636
                        cache.setZeroInLastAccess(cacheGroupPage[0], cacheGroupPage[1]);
637
                        if(!readOnly)
638
                                cache.setModify(cacheGroupPage[0], cacheGroupPage[1]);
639
                }else
640
                        throw new InvalidPageNumberException("");
641
                //System.out.println("LOAD: "+nPag);
642
        }
643
644
        /**
645
         * <P>
646
         * Cuando se accede a una p?gina que no est? cacheada necesitamos cargarla en cach? antes de
647
         * acceder a ella. Si hay un hueco libre en su conjunto se meter? en este pero sino habr?
648
         * que reemplazar una ocupada. Para esto habr? que localizar el conjunto donde va
649
         * destinada y luego la posici?n del conjunto donde se cargar? localizando cual es el
650
         * elemento del conjunto que hace m?s tiempo que se accedi?.
651
         * </P>
652
         * <P>
653
         * Si el elemento es insertado en un hueco la secuencia es la siguiente:
654
         * </P>
655
         * <UL>
656
         * <LI>Obtenemos la siguiente p?gina vacia.</LI>
657
         * <LI>La cargamos de datos.</LI>
658
         * <LI>Se asigna como p?gina accedida</LI>
659
         * <LI>Ponemos true en su posici?n en el vector cacheada</LI>
660
         * <LI>Asignamos el n?mero de p?gina de raster que hemos cargado en esa posici?n de la cach?.</LI>
661
         * <LI>Incrementamos en 1 todos los valores de ?ltimo acceso de las p?ginas del grupo.</LI>
662
         * <LI>Ponemos a 0 su ?ltimo acceso</LI>
663
         * <LI>Si el acceso es para escritura ponemos el flag de p?gina modificada a true.</LI>
664
         * </UL>
665
         * <P>
666
         * Si se reemplaza una p?gina la secuencia es la siguiente:
667
         * </P>
668
         * <UL>
669
         *  <LI>Incrementamos en 1 todos los valores de ?ltimo acceso de las p?ginas del grupo y
670
         *  obtenemos la posici?n de la p?gina de reemplazo.</LI>
671
         * <LI>Ponemos a false la p?gina que va a sacarse de cach?</LI>
672
         * <LI>Si ha sido modificada la p?gina que va a sacarse de cach? se vuelca a disco</LI>
673
         * <LI>Ponemos el flag de modificada para la p?gina sacada a disco a false.</LI>
674
         * <LI>Cargamos la p?gina de cach? de datos.</LI>
675
         * <LI>Asignamos el n?mero de p?gina de raster que hemos cargado en esa posici?n de la cach?.</LI>
676
         * <LI>Se asigna como p?gina accedida</LI>
677
         * <LI>Ponemos true en su posici?n en el vector cacheada</LI>
678
         * <LI>Ponemos a 0 su ?ltimo acceso</LI>
679
         * <LI>Si el acceso es para escritura ponemos el flag de p?gina modificada a true.</LI>
680
         * </UL>
681
         * @param nPag N?mero de p?gina que est? siendo accedida
682
         * @param readOnly ser? true si el acceso que se est? realizando es de lectura y false si se
683
         * est? escribiendo alg?n dato.
684
         * @param n ser? true si el acceso se est? haciendo a la p?gina n y false si se est? haciendo a
685
         * la n+1.
686
         */
687
        private void replacePage(int nPag, boolean readOnly, boolean n){
688
                int group = nPag % cache.getNGroups();
689
                PageBuffer pb = null;
690
691
                if(nPag >= cache.getNTotalPags())
692
                        return;
693
694
                //Insertamos en un hueco
695
696
                if(insertInAHole(group, readOnly, nPag, n))
697
                        return;
698
699
                //Reemplazamos una existente
700
701
                int posInGroupPageToReplace = cache.updateLastAccess(group); //Se actualizan los indices y se obtiene la p?gina a reemplazar
702
703
                cache.setZeroInLastAccess(group, posInGroupPageToReplace);
704
                int rasterPageToReplace = cache.getRasterPageNumberInPosition(group, posInGroupPageToReplace);
705
                pb = cache.getPageBuffer(group, posInGroupPageToReplace);
706
707
                //Sacamos la antigua
708
                cache.setPageAsNotLoadInCache(rasterPageToReplace);
709
                if(cache.isModified(group, posInGroupPageToReplace)){
710
                        try {
711
                                cache.getDataSource().savePage(rasterPageToReplace, pb);         //Volcamos disco
712
                                cache.unsetModify(group, posInGroupPageToReplace);
713
                        } catch (IOException e) {
714
                                System.err.println("No se ha podido salvar la p?gina de cach?.");
715
                                e.printStackTrace();
716
                        }
717
                }
718
719
                //Insertamos la nueva
720
                cache.getDataSource().loadPage(nPag, pb);        //Cargamos de nuevo el buffer
721
                cache.setRasterPageNumberInPosition(group, posInGroupPageToReplace, nPag);
722
                cache.setPageAsLoadInCache(nPag);                //Pone true en su posici?n en el vector cacheada
723
724
                if(n){
725
                        if(!readOnly)
726
                                cache.setModify(group, posInGroupPageToReplace);
727
                        cache.setAccessPage(pb, nPag);          //Asigna como accedida
728
                }
729
                //System.out.println("REPLACE: "+nPag);
730
        }
731
732
        /**
733
         * Comprueba si hay alg?n hueco en el que insertar y si es as? se inserta en el y devuelve
734
         * true.
735
         * @param group conjunto donde hay que buscar para la inserci?n
736
         * @param readOnly si es true la operaci?n es de solo consulta y si es false la operaci?n
737
         * est? modificando alg?n valor de la p?gina a insertar
738
         * @param nPag N?mero de p?gina a cargar
739
         * @return true si se ha insertado en un hueco y false si no se ha insertado
740
         */
741
        private boolean insertInAHole(int group, boolean readOnly, int nPag, boolean n){
742
                PageBuffer pb = null;
743
                for(int i = 0; i < cache.getPagsPerGroup(); i++){
744
                        if(cache.getLastAccess()[group][i] == -1){
745
746
                                //Pone true en su posici?n en el vector cacheada
747
                                pb = cache.getPageBuffer(group, i);
748
                                cache.getDataSource().loadPage(nPag, pb);                                        //Sube la p?gina a cach?
749
                                cache.setRasterPageNumberInPosition(group, i, nPag);//Asigna el n?mero de p?g a una posici?n de cach?
750
                                cache.setPageAsLoadInCache(nPag);
751
                                cache.updateLastAccess(group);
752
                                cache.setZeroInLastAccess(group, i);
753
                                if(n){ //La n+1 no se carga como accedida ni se pone a 0 su contador de ?ltima accedida
754
                                        if(!readOnly)
755
                                                cache.setModify(group, i);
756
                                        cache.setAccessPage(pb, nPag);  //Asigna como accedida
757
                                }
758
                                //System.out.println("INSERT: "+nPag);
759
                                return true;
760
                        }
761
                }
762
                return false;
763
        }
764
765
}