include/Chunk.h
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 15:14:12 2009 -0700 (2009-09-20)
changeset 0 31a43d94cc26
permissions -rw-r--r--
First official checkin.
     1 /*
     2  *  Chunk.h
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 8/27/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #ifndef _MOOSEYARD_CHUNK_
    11 #define _MOOSEYARD_CHUNK_
    12 #include "File.h"
    13 
    14 namespace Mooseyard {
    15     
    16     class KeyValueChunk;
    17     
    18 #pragma pack(2)
    19     
    20     /** An item stored in a file. It's prefixed with its length, so that the entire file
    21         can be scanned for chunks easily. */
    22     class Chunk {
    23     public:
    24         uint32_t size() const                       {return _size;}
    25         uint16_t type() const;
    26         
    27         /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */
    28         const KeyValueChunk* asKeyValue() const;
    29         
    30         /** Write a Chunk to the file, in pieces a la writev. */
    31         static size_t writeMultiple (File *file, uint16_t type,
    32                                      Blob blobs[], int count)  throw(File::Error);
    33         
    34         static size_t writePadding (File *file);
    35 
    36         static const uint16_t kChunkTypePadding = 0;
    37 
    38         const void* end() const                     {return offsetBy(this,_size);}
    39 
    40     protected:
    41         Chunk (uint32_t size, uint16_t type)        :_size(size), _keyLength(0xFFFF), _type(type) { }
    42 
    43     private:
    44         // This is mapped to data in the file! Don't mess with it!
    45         LittleEndian<uint32_t> _size;
    46         LittleEndian<uint16_t> _keyLength;          // Used by KeyValueChunk; else 0xFFFF
    47         LittleEndian<uint16_t> _type;               // Not used by KeyValueChunk
    48         friend class KeyValueChunk;
    49     };
    50     
    51     
    52     /** A key/value pair as stored in the memory-mapped file. */
    53     class KeyValueChunk :public Chunk {
    54     public:
    55         Blob key() const;
    56         Blob value() const;
    57         
    58         bool hasKey (Blob key) const;
    59         
    60         void validate() const  throw(File::Error);
    61         
    62         /** Write a KeyValueChunk to a file. */
    63         static size_t write (File* file, FilePosition pos, Blob key, Blob value)  throw(File::Error);
    64 
    65         static const uint16_t kChunkType = 1;
    66 
    67     private:
    68         const char* valuePtr() const;
    69     };    
    70    
    71 #pragma options align=reset
    72     
    73     
    74     /** An iterator over all the Chunks in a file. */
    75     class ChunkIterator {
    76     public:
    77         ChunkIterator (File*, FilePosition start);
    78         
    79         const Chunk* chunk() const              {return _chunk;}
    80         FilePosition position() const           {return _pos;}
    81         bool atEOF() const;
    82         bool next();
    83         
    84         operator const Chunk*() const           {return _chunk;}
    85         virtual bool hasValue() const           {return _chunk != NULL;}
    86         operator bool() const                   {return hasValue();}
    87         ChunkIterator& operator++()             {next(); return *this;}
    88     private:
    89         void _loadChunk();
    90         File *_file;
    91         FilePosition _pos, _length;
    92         const Chunk* _chunk;
    93     };
    94     
    95 }
    96 
    97 #endif _MOOSEYARD_CHUNK_