First official checkin.
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.
10 #ifndef _MOOSEYARD_VERSIONDICTIONARY_
11 #define _MOOSEYARD_VERSIONDICTIONARY_
12 #include "Dictionary.h"
19 /** A persistent Dictionary embedded in a memory-mapped file,
20 representing one complete version of an Ottoman dictionary. */
21 class VersionDictionary :public Dictionary {
24 /** The generation number. The first version in a new file is zero. */
25 int generation() const;
27 /** The absolute time at which this version was written to the file. */
28 time_t timestamp() const;
30 /** The previous version, before this one was saved. */
31 const VersionDictionary* previousVersion() const;
33 /** Is this the latest version in the file? */
34 bool isLatestVersion() const;
36 virtual int count() const;
37 virtual Blob get (Key) const;
39 virtual Dictionary::Iterator* iterate() const;
42 /** Iterates over the changes between this dictionary and the previous version. */
43 virtual ChangeIterator* iterateChanges() const;
46 // Stuff you probably won't need to use:
48 File* file() const {return _file;}
49 explicit VersionDictionary (File*);
50 VersionDictionary (File *file, FilePosition trailerPosition);
53 /** The type of the trailer chunk that's used as the position of the dictionary. */
54 static const uint16_t kChunkType = 3;
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];}
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,
72 FilePosition _trailerPosition, _previousTrailerPosition;
73 IndexPositions _indexPositions;
75 mutable const VersionDictionary *_previousVersion;
77 #if VERSIONDICTIONARY_TESTING
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;
86 UNCOPYABLE(VersionDictionary);
90 class VersionDictionary::Iterator :public Dictionary::Iterator {
92 explicit Iterator (const VersionDictionary*);
94 virtual Key key() const;
95 virtual Blob value() const;
97 virtual bool hasValue() const;
101 const VersionDictionary *_file;
103 Dictionary::Iterator *_iter;
104 UNCOPYABLE(Iterator);
108 class VersionDictionary::ChangeIterator :public Dictionary::Iterator {
110 explicit ChangeIterator (const VersionDictionary*);
111 virtual ~ChangeIterator();
112 virtual Key key() const;
113 virtual Blob value() const;
115 virtual bool hasValue() const;
119 const VersionDictionary *_file;
121 Dictionary::Iterator *_iter;
122 UNCOPYABLE(ChangeIterator);
127 #endif //_MOOSEYARD_VERSIONDICTIONARY_