include/VersionDictionary.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  *  VersionDictionary.h
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 8/21/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #ifndef _MOOSEYARD_VERSIONDICTIONARY_
    11 #define _MOOSEYARD_VERSIONDICTIONARY_
    12 #include "Dictionary.h"
    13 
    14 namespace Mooseyard {
    15     
    16     class File;
    17     class Index;
    18     
    19     /** A persistent Dictionary embedded in a memory-mapped file, 
    20         representing one complete version of an Ottoman dictionary. */
    21     class VersionDictionary :public Dictionary {
    22     public:
    23         
    24         /** The generation number. The first version in a new file is zero. */
    25         int generation() const;
    26         
    27         /** The absolute time at which this version was written to the file. */
    28         time_t timestamp() const;
    29         
    30         /** The previous version, before this one was saved. */
    31         const VersionDictionary* previousVersion() const;
    32         
    33         /** Is this the latest version in the file? */
    34         bool isLatestVersion() const;
    35         
    36         virtual int count() const;
    37         virtual Blob get (Key) const;
    38         class Iterator;
    39         virtual Dictionary::Iterator* iterate() const;
    40         
    41         class ChangeIterator;
    42         /** Iterates over the changes between this dictionary and the previous version. */
    43         virtual ChangeIterator* iterateChanges() const;
    44 
    45         
    46         // Stuff you probably won't need to use:
    47         
    48         File* file() const                                          {return _file;}
    49         explicit VersionDictionary (File*);
    50         VersionDictionary (File *file, FilePosition trailerPosition);
    51         
    52     private:
    53         /** The type of the trailer chunk that's used as the position of the dictionary. */
    54         static const uint16_t kChunkType = 3;
    55         
    56         struct IndexPositions {
    57             LittleEndian<FilePosition> position[256];
    58             LittleEndian<FilePosition>& operator[] (int i)              {return position[i];}
    59             LittleEndian<FilePosition> const& operator[] (int i) const  {return position[i];}
    60         };
    61                 
    62         class Trailer;
    63         const Trailer* _trailer() const;
    64         const Index* _index (int i) const;
    65         void _read (FilePosition trailerPosition =0);
    66         static FilePosition _append (const VersionDictionary *baseDict, 
    67                                      const Dictionary *addDict, 
    68                                      File *dstFile, 
    69                                      bool replace);
    70         
    71         File *_file;
    72         FilePosition _trailerPosition, _previousTrailerPosition;
    73         IndexPositions _indexPositions;
    74         int _count;
    75         mutable const VersionDictionary *_previousVersion;
    76         
    77 #if VERSIONDICTIONARY_TESTING
    78     public:
    79 #endif
    80         static VersionDictionary* create (File *file, const Dictionary *srcDict);
    81         VersionDictionary* byAppending (const Dictionary* d) const     {return _appendAndOpen(d,_file,false);}
    82         VersionDictionary* byReplacing (const Dictionary* d) const     {return _appendAndOpen(d,_file,true);}
    83         VersionDictionary* _appendAndOpen (const Dictionary *addDict, File *dstFile, bool replace) const;
    84         
    85         friend class Ottoman;
    86         UNCOPYABLE(VersionDictionary);
    87     };
    88     
    89     
    90     class VersionDictionary::Iterator :public Dictionary::Iterator {
    91     public:
    92         explicit Iterator (const VersionDictionary*);
    93         virtual ~Iterator();
    94         virtual Key key() const;
    95         virtual Blob value() const;
    96         virtual bool next();
    97         virtual bool hasValue() const;
    98     private:
    99         bool nextIndex();
   100         
   101         const VersionDictionary *_file;
   102         int _bucket;
   103         Dictionary::Iterator *_iter;
   104         UNCOPYABLE(Iterator);
   105     };
   106     
   107     
   108     class VersionDictionary::ChangeIterator :public Dictionary::Iterator {
   109     public:
   110         explicit ChangeIterator (const VersionDictionary*);
   111         virtual ~ChangeIterator();
   112         virtual Key key() const;
   113         virtual Blob value() const;
   114         virtual bool next();
   115         virtual bool hasValue() const;
   116     private:
   117         bool nextIndex();
   118         
   119         const VersionDictionary *_file;
   120         int _bucket;
   121         Dictionary::Iterator *_iter;
   122         UNCOPYABLE(ChangeIterator);
   123     };
   124 
   125 }
   126 
   127 #endif //_MOOSEYARD_VERSIONDICTIONARY_