src/Index.h
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 20:39:24 2009 -0700 (2009-09-20)
changeset 1 6cbad782d16a
parent 0 include/Index.h@31a43d94cc26
permissions -rw-r--r--
Moved non-public headers out of include/ directory.
jens@0
     1
/*
jens@0
     2
 *  Index.h
jens@0
     3
 *  Ottoman
jens@0
     4
 *
jens@0
     5
 *  Created by Jens Alfke on 8/27/09.
jens@0
     6
 *  Copyright 2009 Jens Alfke. All rights reserved.
jens@0
     7
 *  BSD-Licensed: See the file "LICENSE.txt" for details.
jens@0
     8
 */
jens@0
     9
jens@0
    10
#ifndef _MOOSEYARD_INDEX_
jens@0
    11
#define _MOOSEYARD_INDEX_
jens@0
    12
#include "Chunk.h"
jens@0
    13
#include "Dictionary.h"
jens@0
    14
jens@0
    15
namespace Mooseyard {
jens@0
    16
    
jens@0
    17
    class KeyValueChunk;
jens@0
    18
    
jens@0
    19
    /** An in-file hash table. This structure is stored directly in the file, memory-mapped.
jens@0
    20
        Index is normally only used internally by VersionDictionary. */
jens@0
    21
    class Index :public Chunk {
jens@0
    22
    public:
jens@0
    23
        
jens@0
    24
        class Entry;
jens@0
    25
        
jens@0
    26
        static const Index* indexMappedAt (const void*);
jens@0
    27
        static Index* create (int capacity);
jens@0
    28
        
jens@0
    29
        int count() const                                   {return _count;}
jens@0
    30
        
jens@0
    31
        Blob get (File *file, Key) const;
jens@0
    32
        bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition);
jens@0
    33
        bool remove (File *file, Key, FilePosition maxPosition);
jens@0
    34
        void removeAll();
jens@0
    35
        
jens@0
    36
        void copyFrom (File *file, const Index *index);
jens@0
    37
        
jens@0
    38
        class Iterator;
jens@0
    39
        
jens@0
    40
        void validate() const  throw(File::Error);
jens@0
    41
        void validateEntries (const File *file) const  throw(File::Error);
jens@0
    42
        static const uint16_t kChunkType = 2;
jens@0
    43
        
jens@0
    44
    private:
jens@0
    45
        Index (uint32_t tableSize);
jens@0
    46
        const Entry* table (int index) const;
jens@0
    47
        Entry* table (int index);
jens@0
    48
        const KeyValueChunk* _find (File *file, Key) const;
jens@0
    49
        bool _put (File *file, Key, FilePosition, FilePosition maxPosition);
jens@0
    50
jens@0
    51
        // This is mapped to data in the file! Don't mess with it!
jens@0
    52
        LittleEndian<uint32_t>  _magicNumber;
jens@0
    53
        LittleEndian<uint32_t>  _count;
jens@0
    54
        LittleEndian<uint32_t>  _tableSize;
jens@0
    55
        char                    _table[0];      // Actually Entry[]
jens@0
    56
    };
jens@0
    57
  
jens@0
    58
    
jens@0
    59
    
jens@0
    60
    class Index::Iterator :public Dictionary::Iterator {
jens@0
    61
    public:
jens@0
    62
        Iterator (File*, const Index*);
jens@0
    63
        virtual Key key() const;
jens@0
    64
        virtual Blob value() const;
jens@0
    65
        virtual bool next();
jens@0
    66
        virtual bool hasValue() const;
jens@0
    67
        
jens@0
    68
        FilePosition valuePosition();
jens@0
    69
    private:
jens@0
    70
        File* const _file;
jens@0
    71
        const Index::Entry *_current, *_end;
jens@0
    72
    };
jens@0
    73
    
jens@0
    74
}
jens@0
    75
jens@0
    76
#endif //_MOOSEYARD_INDEX_