include/Index.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  *  Index.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_INDEX_
    11 #define _MOOSEYARD_INDEX_
    12 #include "Chunk.h"
    13 #include "Dictionary.h"
    14 
    15 namespace Mooseyard {
    16     
    17     class KeyValueChunk;
    18     
    19     /** An in-file hash table. This structure is stored directly in the file, memory-mapped.
    20         Index is normally only used internally by VersionDictionary. */
    21     class Index :public Chunk {
    22     public:
    23         
    24         class Entry;
    25         
    26         static const Index* indexMappedAt (const void*);
    27         static Index* create (int capacity);
    28         
    29         int count() const                                   {return _count;}
    30         
    31         Blob get (File *file, Key) const;
    32         bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition);
    33         bool remove (File *file, Key, FilePosition maxPosition);
    34         void removeAll();
    35         
    36         void copyFrom (File *file, const Index *index);
    37         
    38         class Iterator;
    39         
    40         void validate() const  throw(File::Error);
    41         void validateEntries (const File *file) const  throw(File::Error);
    42         static const uint16_t kChunkType = 2;
    43         
    44     private:
    45         Index (uint32_t tableSize);
    46         const Entry* table (int index) const;
    47         Entry* table (int index);
    48         const KeyValueChunk* _find (File *file, Key) const;
    49         bool _put (File *file, Key, FilePosition, FilePosition maxPosition);
    50 
    51         // This is mapped to data in the file! Don't mess with it!
    52         LittleEndian<uint32_t>  _magicNumber;
    53         LittleEndian<uint32_t>  _count;
    54         LittleEndian<uint32_t>  _tableSize;
    55         char                    _table[0];      // Actually Entry[]
    56     };
    57   
    58     
    59     
    60     class Index::Iterator :public Dictionary::Iterator {
    61     public:
    62         Iterator (File*, const Index*);
    63         virtual Key key() const;
    64         virtual Blob value() const;
    65         virtual bool next();
    66         virtual bool hasValue() const;
    67         
    68         FilePosition valuePosition();
    69     private:
    70         File* const _file;
    71         const Index::Entry *_current, *_end;
    72     };
    73     
    74 }
    75 
    76 #endif //_MOOSEYARD_INDEX_