File: sysblockmap.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (551 lines) | stat: -rw-r--r-- 20,561 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/******************************************************************************
 *
 * Purpose:  Implementation of the SysBlockMap class.
 *
 * This class is used to manage access to the SYS virtual block map segment
 * (named SysBMDir).  This segment is used to keep track of one or more 
 * virtual files stored in SysBData segments.  These virtual files are normally
 * used to hold tiled images for primary bands or overviews.  
 *
 * This class is closely partnered with the SysVirtualFile class, and the
 * primary client is the CTiledChannel class. 
 * 
 ******************************************************************************
 * Copyright (c) 2009
 * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "pcidsk_exception.h"
#include "pcidsk_file.h"
#include "core/sysvirtualfile.h"
#include "segment/sysblockmap.h"
#include "core/cpcidskfile.h"

#include <cassert>
#include <vector>
#include <cstring>
#include <cstdlib>

using namespace PCIDSK;

/************************************************************************/
/*                            SysBlockMap()                             */
/************************************************************************/

SysBlockMap::SysBlockMap( PCIDSKFile *file, int segment,
                              const char *segment_pointer )
        : CPCIDSKSegment( file, segment, segment_pointer )

{
    partial_loaded = false;
    full_loaded = false;
    dirty = false;
    growing_segment = 0;
}

/************************************************************************/
/*                            ~SysBlockMap()                            */
/************************************************************************/

SysBlockMap::~SysBlockMap()

{
    size_t i;
    
    for( i = 0; i < virtual_files.size(); i++ )
    {
        delete virtual_files[i];
        virtual_files[i] = NULL;
    }

    Synchronize();
}

/************************************************************************/
/*                             Initialize()                             */
/*                                                                      */
/*      This method is used after creation of the SysBMDir segment      */
/*      to fill in valid contents.  Prepares bare minimum contents.     */
/************************************************************************/

void SysBlockMap::Initialize()

{
    PCIDSKBuffer init_data(512);

    init_data.Put( "VERSION  1", 0, 10 );
    init_data.Put( 0, 10, 8 );
    init_data.Put( 0, 18, 8 );
    init_data.Put( -1, 26, 8 );
    init_data.Put( "", 34, 512-34 );

    WriteToFile( init_data.buffer, 0, init_data.buffer_size );
#ifdef notdef
    // arbitrarily grow the segment a bit to avoid having to move it too soon.
    WriteToFile( "\0", 8191, 1 );				       
#endif
}

/************************************************************************/
/*                            PartialLoad()                             */
/*                                                                      */
/*      Load the header and some per-layer information.                 */
/************************************************************************/

void SysBlockMap::PartialLoad()

{
    if( partial_loaded )
        return;

//    printf( "<PartialLoad>" );
//    fflush( stdout );

/* -------------------------------------------------------------------- */
/*      Load the 512 byte count section of the blockmap.                */
/* -------------------------------------------------------------------- */
    PCIDSKBuffer count_data;

    count_data.SetSize( 512 );
    ReadFromFile( count_data.buffer, 0, 512 );

    if( strncmp(count_data.buffer,"VERSION",7) != 0 )
        ThrowPCIDSKException( "SysBlockMap::PartialLoad() - block map corrupt." );

    if( count_data.GetInt( 7, 3 ) != 1 )
        ThrowPCIDSKException( "SysBlockMap::PartialLoad() - unsupported version." );

/* -------------------------------------------------------------------- */
/*      Establish our SysVirtualFile array based on the number of       */
/*      images listed in the image list.                                */
/* -------------------------------------------------------------------- */
    int layer_count = count_data.GetInt( 10, 8 );

    virtual_files.resize( layer_count );

    block_count = count_data.GetInt( 18, 8 );
    first_free_block = count_data.GetInt( 26, 8 );

/* -------------------------------------------------------------------- */
/*      Load the layer list definitions.  These are fairly small.       */
/* -------------------------------------------------------------------- */
    layer_data.SetSize( 24 * layer_count );
    ReadFromFile( layer_data.buffer, 
                  512 + 28 * block_count, 
                  layer_data.buffer_size);

    partial_loaded = true;

//    FullLoad();
}

/************************************************************************/
/*                              FullLoad()                              */
/*                                                                      */
/*      Load the blockmap data (can be large) into blockmap_data.       */
/************************************************************************/

void SysBlockMap::FullLoad()

{
    PartialLoad();

    if( full_loaded )
        return;

//    printf( "<FullLoad>" );
//    fflush( stdout );

    // TODO: this should likely be protected by a mutex. 

/* -------------------------------------------------------------------- */
/*      Load the segment contents into a buffer.                        */
/* -------------------------------------------------------------------- */
    blockmap_data.SetSize( block_count * 28 );
    ReadFromFile( blockmap_data.buffer, 512, blockmap_data.buffer_size );

    full_loaded = true;
}

/************************************************************************/
/*                            Synchronize()                             */
/************************************************************************/

void SysBlockMap::Synchronize()

{
    if( !full_loaded || !dirty )
        return;

    PCIDSKBuffer init_data(512);

    init_data.Put( "VERSION  1", 0, 10 );
    init_data.Put( (int) virtual_files.size(), 10, 8 );
    init_data.Put( block_count, 18, 8 );
    init_data.Put( first_free_block, 26, 8 );
    init_data.Put( "", 34, 512-34 );

    WriteToFile( init_data.buffer, 0, init_data.buffer_size );

    WriteToFile( blockmap_data.buffer, 512, blockmap_data.buffer_size );
    WriteToFile( layer_data.buffer, 512 + blockmap_data.buffer_size, 
                 layer_data.buffer_size );

    dirty = false;
}

/************************************************************************/
/*                           AllocateBlocks()                           */
/*                                                                      */
/*      Allocate a bunch of new blocks and attach to the free list.     */
/************************************************************************/

void SysBlockMap::AllocateBlocks()

{
    FullLoad();

/* -------------------------------------------------------------------- */
/*      Find a segment we can extend.  We consider any SYS segments     */
/*      with a name of SysBData.                                        */
/* -------------------------------------------------------------------- */
    PCIDSKSegment *seg;

    if( growing_segment > 0 )
    {
        seg = file->GetSegment( growing_segment );
        if( !seg->IsAtEOF() )
            growing_segment = 0;
    }

    if( growing_segment == 0 )
    {
        PCIDSKSegment *seg;
        int  previous = 0;

        while( (seg=file->GetSegment( SEG_SYS, "SysBData", previous )) != NULL )
        {
            previous = seg->GetSegmentNumber();
            
            if( seg->IsAtEOF() )
            {
                growing_segment = previous;
                break;
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      If we didn't find one, then create a new segment.               */
/* -------------------------------------------------------------------- */
    if( growing_segment == 0 )
    {
        growing_segment = 
            file->CreateSegment( "SysBData", 
                                 "System Block Data for Tiles and Overviews "
                                 "- Do not modify",
                                 SEG_SYS, 0L );
    }

/* -------------------------------------------------------------------- */
/*      Allocate another set of space.                                  */
/* -------------------------------------------------------------------- */
    uint64 new_big_blocks = 16;
    uint64 new_bytes = new_big_blocks * SysVirtualFile::block_size;
    seg = file->GetSegment( growing_segment );
    int block_index_in_segment = (int) 
        (seg->GetContentSize() / SysVirtualFile::block_size);

    seg->WriteToFile( "\0", seg->GetContentSize() + new_bytes - 1, 1 );
    
/* -------------------------------------------------------------------- */
/*      Resize the memory image of the blockmap.                        */
/* -------------------------------------------------------------------- */
    if( 28 * (block_count + new_big_blocks) 
        > (unsigned int) blockmap_data.buffer_size )
        blockmap_data.SetSize( (int) (28 * (block_count + new_big_blocks)) );

/* -------------------------------------------------------------------- */
/*      Fill in info on the new blocks.                                 */
/* -------------------------------------------------------------------- */
    uint64 block_index;

    for( block_index = block_count; 
         block_index < block_count + new_big_blocks;
         block_index++ )
    {
        int bi_offset = (int) (block_index * 28);

        blockmap_data.Put( growing_segment, bi_offset, 4 );
        blockmap_data.Put( block_index_in_segment++, bi_offset+4, 8 );
        blockmap_data.Put( -1, bi_offset+12, 8 );

        if( block_index == block_count + new_big_blocks - 1 )
            blockmap_data.Put( -1, bi_offset+20, 8 );
        else
            blockmap_data.Put( block_index+1, bi_offset+20, 8 );
    }

    first_free_block = block_count;

    block_count += (int) new_big_blocks;

    dirty = true;
}

/************************************************************************/
/*                          GrowVirtualFile()                           */
/*                                                                      */
/*      Get one more block for this virtual file.                       */
/************************************************************************/

int SysBlockMap::GrowVirtualFile( int image, int &last_block, 
                                  int &block_segment_ret )

{
    FullLoad();

/* -------------------------------------------------------------------- */
/*      Do we need to create new free blocks?                           */
/* -------------------------------------------------------------------- */
    if( first_free_block == -1 )
        AllocateBlocks();

/* -------------------------------------------------------------------- */
/*      Return the first free block, and update the next pointer of     */
/*      the previous block.                                             */
/* -------------------------------------------------------------------- */
    int alloc_block = first_free_block;

    // update first free block to point to the next free block.
    first_free_block = blockmap_data.GetInt( alloc_block*28+20, 8);

    // mark block as owned by this layer/image. 
    blockmap_data.Put( image, alloc_block*28 + 12, 8 );

    // clear next free block on allocated block - it is the last in the chain
    blockmap_data.Put( -1, alloc_block*28 + 20, 8 );

    // point the previous "last block" for this virtual file to this new block
    if( last_block != -1 )
        blockmap_data.Put( alloc_block, last_block*28 + 20, 8 );
    else
        layer_data.Put( alloc_block, image*24 + 4, 8 );

    dirty = true;

    block_segment_ret = blockmap_data.GetInt( alloc_block*28, 4 );
    last_block = alloc_block;

    return blockmap_data.GetInt( alloc_block*28+4, 8 );
}

/************************************************************************/
/*                         SetVirtualFileSize()                         */
/************************************************************************/

void SysBlockMap::SetVirtualFileSize( int image_index, uint64 file_length )

{
    FullLoad();

    layer_data.Put( file_length, 24*image_index + 12, 12 );
    dirty = true;
}

/************************************************************************/
/*                           GetVirtualFile()                           */
/************************************************************************/

SysVirtualFile *SysBlockMap::GetVirtualFile( int image )

{
    PartialLoad();

    if( image < 0 || image >= (int) virtual_files.size() )
        ThrowPCIDSKException( "GetImageSysFile(%d): invalid image index",
                              image );

    if( virtual_files[image] != NULL )
        return virtual_files[image];

    uint64  vfile_length = layer_data.GetUInt64( 24*image + 12, 12 );
    int  start_block = layer_data.GetInt( 24*image + 4, 8 );

    virtual_files[image] = 
        new SysVirtualFile( dynamic_cast<CPCIDSKFile *>(file), 
                            start_block, vfile_length,
                            this, image );

    return virtual_files[image];
}

/************************************************************************/
/*                         CreateVirtualFile()                          */
/************************************************************************/

int SysBlockMap::CreateVirtualFile()

{
    FullLoad();

/* -------------------------------------------------------------------- */
/*      Is there an existing dead layer we can reuse?                   */
/* -------------------------------------------------------------------- */
    unsigned int layer_index;

    for( layer_index = 0; layer_index < virtual_files.size(); layer_index++ )
    {
        if( layer_data.GetInt( 24*layer_index + 0, 4 ) == 1 /* dead */ )
        {
            break;
        }
    }

/* -------------------------------------------------------------------- */
/*      If not, extend the layer table.                                 */
/* -------------------------------------------------------------------- */
    if( layer_index == virtual_files.size() )
    {
        layer_index = virtual_files.size();
        layer_data.SetSize( (layer_index+1) * 24 );
        virtual_files.push_back( NULL );
    }

/* -------------------------------------------------------------------- */
/*      Set all the entries for this layer.                             */
/* -------------------------------------------------------------------- */
    dirty = true;

    layer_data.Put( 2, 24*layer_index + 0, 4 );
    layer_data.Put( -1, 24*layer_index + 4, 8 );
    layer_data.Put( 0, 24*layer_index + 12, 12 );

    return layer_index;
}

/************************************************************************/
/*                       CreateVirtualImageFile()                       */
/************************************************************************/

int SysBlockMap::CreateVirtualImageFile( int width, int height, 
                                         int block_width, int block_height,
                                         eChanType chan_type,
                                         std::string compression )

{
    if( compression == "" )
        compression = "NONE";

/* -------------------------------------------------------------------- */
/*      Create the underlying virtual file.                             */
/* -------------------------------------------------------------------- */
    int img_index = CreateVirtualFile();
    SysVirtualFile *vfile = GetVirtualFile( img_index );

/* -------------------------------------------------------------------- */
/*      Set up the image header.                                        */
/* -------------------------------------------------------------------- */
    PCIDSKBuffer theader(128);

    theader.Put( "", 0, 128 );

    theader.Put( width, 0, 8 );
    theader.Put( height, 8, 8 );
    theader.Put( block_width, 16, 8 );
    theader.Put( block_height, 24, 8 );
    theader.Put( DataTypeName(chan_type).c_str(), 32, 4 );
    theader.Put( compression.c_str(), 54, 8 );

    vfile->WriteToFile( theader.buffer, 0, 128 );

/* -------------------------------------------------------------------- */
/*      Setup the tile map - initially with no tiles referenced.        */
/* -------------------------------------------------------------------- */
    int tiles_per_row = (width + block_width - 1) / block_width;
    int tiles_per_col = (height + block_height - 1) / block_height;
    int tile_count = tiles_per_row * tiles_per_col;
    int i;

    PCIDSKBuffer tmap( tile_count * 20 );

    for( i = 0; i < tile_count; i++ )
    {
        tmap.Put( -1, i*12, 12 );
        tmap.Put( 0, tile_count*12 + i*8, 8 );
    }

    vfile->WriteToFile( tmap.buffer, 128, tile_count*20 );

    return img_index;
}

/************************************************************************/
/*                        GetNextBlockMapEntry()                        */
/*                                                                      */
/*      SysVirtualFile's call this method to find the next block in     */
/*      the blockmap which belongs to them.  This allows them to        */
/*      fill their blockmap "as needed" without necessarily forcing     */
/*      a full load of the blockmap.                                    */
/************************************************************************/

int SysBlockMap::GetNextBlockMapEntry( int bm_index,
                                       uint16 &segment,
                                       int &block_in_segment )

{
    if( !partial_loaded )
        PartialLoad();

/* -------------------------------------------------------------------- */
/*      If the full blockmap is already loaded, just fetch it from      */
/*      there to avoid extra IO or confusion between what is disk       */
/*      and what is in memory.                                          */
/*                                                                      */
/*      Otherwise we read from disk and hope the io level buffering     */
/*      is pretty good.                                                 */
/* -------------------------------------------------------------------- */
    char bm_entry[29];

    if( full_loaded )
    {
        memcpy( bm_entry, blockmap_data.buffer + bm_index * 28, 28 );
    }
    else
    {
        ReadFromFile( bm_entry, bm_index * 28 + 512, 28 );
    }
    
/* -------------------------------------------------------------------- */
/*      Parse the values as efficiently as we can.                      */
/* -------------------------------------------------------------------- */
    bm_entry[28] = '\0';

    int next_block = atoi( bm_entry+20 );

    bm_entry[12] = '\0';
    block_in_segment = atoi(bm_entry+4);

    bm_entry[4] = '\0';
    segment = atoi(bm_entry);
    
    return next_block;
}