Moved non-public headers out of include/ directory.
authorJens Alfke <jens@mooseyard.com>
Sun Sep 20 20:39:24 2009 -0700 (2009-09-20)
changeset 16cbad782d16a
parent 0 31a43d94cc26
child 2 851de24ecb61
Moved non-public headers out of include/ directory.
Ottoman.xcodeproj/project.pbxproj
include/Chunk.h
include/Hash.h
include/Index.h
include/MemoryMap.h
src/Chunk.h
src/Hash.h
src/Index.h
src/MemoryMap.h
     1.1 --- a/Ottoman.xcodeproj/project.pbxproj	Sun Sep 20 15:14:12 2009 -0700
     1.2 +++ b/Ottoman.xcodeproj/project.pbxproj	Sun Sep 20 20:39:24 2009 -0700
     1.3 @@ -41,12 +41,12 @@
     1.4  		27156CA9104C9C44009EBD39 /* gtest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = gtest.framework; path = /Library/Frameworks/gtest.framework; sourceTree = "<absolute>"; };
     1.5  		27603900105AC81200D931A7 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
     1.6  		276E5BBA1066D135008A2171 /* Base.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Base.h; sourceTree = "<group>"; };
     1.7 -		276E5BBB1066D135008A2171 /* Chunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Chunk.h; path = ../include/Chunk.h; sourceTree = "<group>"; };
     1.8 +		276E5BBB1066D135008A2171 /* Chunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Chunk.h; sourceTree = "<group>"; };
     1.9  		276E5BBC1066D135008A2171 /* Dictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dictionary.h; sourceTree = "<group>"; };
    1.10  		276E5BBD1066D135008A2171 /* File.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = File.h; sourceTree = "<group>"; };
    1.11 -		276E5BBE1066D135008A2171 /* Hash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Hash.h; path = ../include/Hash.h; sourceTree = "<group>"; };
    1.12 -		276E5BBF1066D135008A2171 /* Index.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Index.h; path = ../include/Index.h; sourceTree = "<group>"; };
    1.13 -		276E5BC01066D135008A2171 /* MemoryMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MemoryMap.h; path = ../include/MemoryMap.h; sourceTree = "<group>"; };
    1.14 +		276E5BBE1066D135008A2171 /* Hash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Hash.h; sourceTree = "<group>"; };
    1.15 +		276E5BBF1066D135008A2171 /* Index.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Index.h; sourceTree = "<group>"; };
    1.16 +		276E5BC01066D135008A2171 /* MemoryMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryMap.h; sourceTree = "<group>"; };
    1.17  		276E5BC11066D135008A2171 /* Ottoman.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Ottoman.h; sourceTree = "<group>"; };
    1.18  		276E5BC21066D135008A2171 /* VersionDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VersionDictionary.h; sourceTree = "<group>"; };
    1.19  		276E5BC41066D13D008A2171 /* Base.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Base.cpp; sourceTree = "<group>"; };
     2.1 --- a/include/Chunk.h	Sun Sep 20 15:14:12 2009 -0700
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,97 +0,0 @@
     2.4 -/*
     2.5 - *  Chunk.h
     2.6 - *  Ottoman
     2.7 - *
     2.8 - *  Created by Jens Alfke on 8/27/09.
     2.9 - *  Copyright 2009 Jens Alfke. All rights reserved.
    2.10 - *  BSD-Licensed: See the file "LICENSE.txt" for details.
    2.11 - */
    2.12 -
    2.13 -#ifndef _MOOSEYARD_CHUNK_
    2.14 -#define _MOOSEYARD_CHUNK_
    2.15 -#include "File.h"
    2.16 -
    2.17 -namespace Mooseyard {
    2.18 -    
    2.19 -    class KeyValueChunk;
    2.20 -    
    2.21 -#pragma pack(2)
    2.22 -    
    2.23 -    /** An item stored in a file. It's prefixed with its length, so that the entire file
    2.24 -        can be scanned for chunks easily. */
    2.25 -    class Chunk {
    2.26 -    public:
    2.27 -        uint32_t size() const                       {return _size;}
    2.28 -        uint16_t type() const;
    2.29 -        
    2.30 -        /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */
    2.31 -        const KeyValueChunk* asKeyValue() const;
    2.32 -        
    2.33 -        /** Write a Chunk to the file, in pieces a la writev. */
    2.34 -        static size_t writeMultiple (File *file, uint16_t type,
    2.35 -                                     Blob blobs[], int count)  throw(File::Error);
    2.36 -        
    2.37 -        static size_t writePadding (File *file);
    2.38 -
    2.39 -        static const uint16_t kChunkTypePadding = 0;
    2.40 -
    2.41 -        const void* end() const                     {return offsetBy(this,_size);}
    2.42 -
    2.43 -    protected:
    2.44 -        Chunk (uint32_t size, uint16_t type)        :_size(size), _keyLength(0xFFFF), _type(type) { }
    2.45 -
    2.46 -    private:
    2.47 -        // This is mapped to data in the file! Don't mess with it!
    2.48 -        LittleEndian<uint32_t> _size;
    2.49 -        LittleEndian<uint16_t> _keyLength;          // Used by KeyValueChunk; else 0xFFFF
    2.50 -        LittleEndian<uint16_t> _type;               // Not used by KeyValueChunk
    2.51 -        friend class KeyValueChunk;
    2.52 -    };
    2.53 -    
    2.54 -    
    2.55 -    /** A key/value pair as stored in the memory-mapped file. */
    2.56 -    class KeyValueChunk :public Chunk {
    2.57 -    public:
    2.58 -        Blob key() const;
    2.59 -        Blob value() const;
    2.60 -        
    2.61 -        bool hasKey (Blob key) const;
    2.62 -        
    2.63 -        void validate() const  throw(File::Error);
    2.64 -        
    2.65 -        /** Write a KeyValueChunk to a file. */
    2.66 -        static size_t write (File* file, FilePosition pos, Blob key, Blob value)  throw(File::Error);
    2.67 -
    2.68 -        static const uint16_t kChunkType = 1;
    2.69 -
    2.70 -    private:
    2.71 -        const char* valuePtr() const;
    2.72 -    };    
    2.73 -   
    2.74 -#pragma options align=reset
    2.75 -    
    2.76 -    
    2.77 -    /** An iterator over all the Chunks in a file. */
    2.78 -    class ChunkIterator {
    2.79 -    public:
    2.80 -        ChunkIterator (File*, FilePosition start);
    2.81 -        
    2.82 -        const Chunk* chunk() const              {return _chunk;}
    2.83 -        FilePosition position() const           {return _pos;}
    2.84 -        bool atEOF() const;
    2.85 -        bool next();
    2.86 -        
    2.87 -        operator const Chunk*() const           {return _chunk;}
    2.88 -        virtual bool hasValue() const           {return _chunk != NULL;}
    2.89 -        operator bool() const                   {return hasValue();}
    2.90 -        ChunkIterator& operator++()             {next(); return *this;}
    2.91 -    private:
    2.92 -        void _loadChunk();
    2.93 -        File *_file;
    2.94 -        FilePosition _pos, _length;
    2.95 -        const Chunk* _chunk;
    2.96 -    };
    2.97 -    
    2.98 -}
    2.99 -
   2.100 -#endif _MOOSEYARD_CHUNK_
     3.1 --- a/include/Hash.h	Sun Sep 20 15:14:12 2009 -0700
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,119 +0,0 @@
     3.4 -/*
     3.5 - *  Hash.h
     3.6 - *  Ottoman
     3.7 - *
     3.8 - *  Created by Jens Alfke on 8/20/09.
     3.9 - *  Copyright 2009 Jens Alfke. All rights reserved.
    3.10 - *  BSD-Licensed: See the file "LICENSE.txt" for details.
    3.11 - */
    3.12 -
    3.13 -#ifndef _MOOSEYARD_HASH_
    3.14 -#define _MOOSEYARD_HASH_
    3.15 -#include "Base.h"
    3.16 -#include "Dictionary.h"
    3.17 -
    3.18 -namespace Mooseyard {
    3.19 -        
    3.20 -    /** A fairly low-level hash-table class whose keys and values are arbitrary data blobs.
    3.21 -        This class does no memory management of the keys and values: the memory ranges
    3.22 -        pointed to by the key and value parameters to put() must remain valid as long as
    3.23 -        that key or value remains in the hash-table. */
    3.24 -    class Hash {
    3.25 -        
    3.26 -    public:
    3.27 -        typedef void* Value;
    3.28 -        
    3.29 -        /** Creates a new, empty Hash. */
    3.30 -        explicit Hash (int capacity = 50);
    3.31 -        
    3.32 -        ~Hash();
    3.33 -        
    3.34 -        /** Gets the value associated with the given key. */
    3.35 -        Value get(Key) const;
    3.36 -        Value operator[] (Key key) const                        {return get(key);}
    3.37 -        
    3.38 -        /** Returns the number of values in the Hash. */
    3.39 -        int count() const                                       {return _count;}
    3.40 -        
    3.41 -        /** Sets the given value for the given key, replacing any existing value. */
    3.42 -        void put(Key, Value);
    3.43 -        
    3.44 -        /** Removes the value with the given key. */
    3.45 -        bool remove(Key);
    3.46 -        
    3.47 -        /** Removes all values. */
    3.48 -        void removeAll();
    3.49 -        
    3.50 -        void dump() const;
    3.51 -        
    3.52 -        class IndexEntry;
    3.53 -        
    3.54 -        class Iterator {
    3.55 -        public:
    3.56 -            Iterator (const Hash *h);
    3.57 -            Iterator (const Hash &h);
    3.58 -            operator bool() const                           {return hasValue();}
    3.59 -            Value operator*() const                         {return value();}
    3.60 -            Iterator& operator++()                          {next(); return *this;}
    3.61 -            
    3.62 -            Key key() const;
    3.63 -            Value value() const;
    3.64 -            bool hasValue() const                           {return _entry < _end;}
    3.65 -            bool next();
    3.66 -        private:
    3.67 -            Iterator (IndexEntry *start, IndexEntry*end);
    3.68 -            IndexEntry* _entry;
    3.69 -            IndexEntry* const _end;
    3.70 -            UNCOPYABLE(Iterator);
    3.71 -        };
    3.72 -        
    3.73 -    private:
    3.74 -        IndexEntry& _find (Key key) const;
    3.75 -        IndexEntry& _findForPut (const IndexEntry &newEntry);
    3.76 -        void _resize(int newSize);
    3.77 -        void _read();
    3.78 -        UNCOPYABLE(Hash);
    3.79 -        
    3.80 -        int _count;
    3.81 -        int _tableSize;
    3.82 -        IndexEntry *_table;
    3.83 -        
    3.84 -        friend class Iterator;
    3.85 -    };
    3.86 -    
    3.87 -
    3.88 -
    3.89 -    /** A concrete Dictionary implemented with a Hash. */
    3.90 -    class HashDictionary :public MutableDictionary {
    3.91 -    public:
    3.92 -        explicit HashDictionary (int capacity =50)  :_hash(capacity) { }
    3.93 -        virtual ~HashDictionary();
    3.94 -        virtual int count() const                   {return _hash.count();}
    3.95 -        virtual bool contains (Key) const;
    3.96 -        virtual Blob get (Key key) const            {return _convertValue(_hash.get(key));}
    3.97 -        virtual void put (Key, Blob value);
    3.98 -        virtual bool remove (Key);
    3.99 -        virtual void removeAll();
   3.100 -        virtual Iterator* iterate() const           {return new Iterator(*this);}
   3.101 -        
   3.102 -        class Iterator :public Dictionary::Iterator {
   3.103 -        public:
   3.104 -            explicit Iterator (const HashDictionary &d)      :_it(d._hash) { }
   3.105 -            virtual Key key() const                 {return _it.key();}
   3.106 -            virtual Blob value() const              {return _convertValue(_it.value());}
   3.107 -            virtual bool hasValue() const           {return _it.hasValue();}
   3.108 -            virtual bool next()                     {return _it.next();}
   3.109 -        private:
   3.110 -            Hash::Iterator _it;
   3.111 -        };
   3.112 -        
   3.113 -    private:
   3.114 -        static Blob _convertValue (void*);
   3.115 -        Hash _hash;
   3.116 -        friend class Iterator;
   3.117 -    };
   3.118 -    
   3.119 -    
   3.120 -}
   3.121 -
   3.122 -#endif _MOOSEYARD_HASH_
     4.1 --- a/include/Index.h	Sun Sep 20 15:14:12 2009 -0700
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,76 +0,0 @@
     4.4 -/*
     4.5 - *  Index.h
     4.6 - *  Ottoman
     4.7 - *
     4.8 - *  Created by Jens Alfke on 8/27/09.
     4.9 - *  Copyright 2009 Jens Alfke. All rights reserved.
    4.10 - *  BSD-Licensed: See the file "LICENSE.txt" for details.
    4.11 - */
    4.12 -
    4.13 -#ifndef _MOOSEYARD_INDEX_
    4.14 -#define _MOOSEYARD_INDEX_
    4.15 -#include "Chunk.h"
    4.16 -#include "Dictionary.h"
    4.17 -
    4.18 -namespace Mooseyard {
    4.19 -    
    4.20 -    class KeyValueChunk;
    4.21 -    
    4.22 -    /** An in-file hash table. This structure is stored directly in the file, memory-mapped.
    4.23 -        Index is normally only used internally by VersionDictionary. */
    4.24 -    class Index :public Chunk {
    4.25 -    public:
    4.26 -        
    4.27 -        class Entry;
    4.28 -        
    4.29 -        static const Index* indexMappedAt (const void*);
    4.30 -        static Index* create (int capacity);
    4.31 -        
    4.32 -        int count() const                                   {return _count;}
    4.33 -        
    4.34 -        Blob get (File *file, Key) const;
    4.35 -        bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition);
    4.36 -        bool remove (File *file, Key, FilePosition maxPosition);
    4.37 -        void removeAll();
    4.38 -        
    4.39 -        void copyFrom (File *file, const Index *index);
    4.40 -        
    4.41 -        class Iterator;
    4.42 -        
    4.43 -        void validate() const  throw(File::Error);
    4.44 -        void validateEntries (const File *file) const  throw(File::Error);
    4.45 -        static const uint16_t kChunkType = 2;
    4.46 -        
    4.47 -    private:
    4.48 -        Index (uint32_t tableSize);
    4.49 -        const Entry* table (int index) const;
    4.50 -        Entry* table (int index);
    4.51 -        const KeyValueChunk* _find (File *file, Key) const;
    4.52 -        bool _put (File *file, Key, FilePosition, FilePosition maxPosition);
    4.53 -
    4.54 -        // This is mapped to data in the file! Don't mess with it!
    4.55 -        LittleEndian<uint32_t>  _magicNumber;
    4.56 -        LittleEndian<uint32_t>  _count;
    4.57 -        LittleEndian<uint32_t>  _tableSize;
    4.58 -        char                    _table[0];      // Actually Entry[]
    4.59 -    };
    4.60 -  
    4.61 -    
    4.62 -    
    4.63 -    class Index::Iterator :public Dictionary::Iterator {
    4.64 -    public:
    4.65 -        Iterator (File*, const Index*);
    4.66 -        virtual Key key() const;
    4.67 -        virtual Blob value() const;
    4.68 -        virtual bool next();
    4.69 -        virtual bool hasValue() const;
    4.70 -        
    4.71 -        FilePosition valuePosition();
    4.72 -    private:
    4.73 -        File* const _file;
    4.74 -        const Index::Entry *_current, *_end;
    4.75 -    };
    4.76 -    
    4.77 -}
    4.78 -
    4.79 -#endif //_MOOSEYARD_INDEX_
     5.1 --- a/include/MemoryMap.h	Sun Sep 20 15:14:12 2009 -0700
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,53 +0,0 @@
     5.4 -/*
     5.5 - *  MemoryMap.h
     5.6 - *  Ottoman
     5.7 - *
     5.8 - *  Created by Jens Alfke on 9/17/09.
     5.9 - *  Copyright 2009 Jens Alfke. All rights reserved.
    5.10 - *  BSD-Licensed: See the file "LICENSE.txt" for details.
    5.11 - */
    5.12 -
    5.13 -#include "File.h"
    5.14 -
    5.15 -namespace Mooseyard {
    5.16 -    
    5.17 -    /** A flexible memory-map on a file, which can handle multiple discontiguous mapped regions.
    5.18 -        Clients usually access this functionality through File, which delegates to it. */
    5.19 -    class MemoryMap {
    5.20 -    public:
    5.21 -        MemoryMap (File* file)                          :_file(file), _nRegions(0), _regions(NULL) { }
    5.22 -        ~MemoryMap();
    5.23 -        
    5.24 -        void mapRegion (off_t position, size_t length);
    5.25 -        const void* mappedPosition (off_t position) const;
    5.26 -        
    5.27 -    private:
    5.28 -        UNCOPYABLE(MemoryMap);
    5.29 -        class Region;
    5.30 -        const void* _at (off_t) const;
    5.31 -        
    5.32 -        File *_file;
    5.33 -        int _nRegions;
    5.34 -        Region **_regions;
    5.35 -    };
    5.36 -    
    5.37 -    
    5.38 -    class MemoryMap::Region {
    5.39 -    public:
    5.40 -        Region (File*, off_t position, size_t length);
    5.41 -        ~Region();
    5.42 -        off_t position() const                  {return _position;}
    5.43 -        size_t length() const                   {return _length;}
    5.44 -        off_t end() const                       {return _position + _length;}
    5.45 -        bool setLength (size_t);          // Returns false if it failed
    5.46 -        const void* mappedPosition(off_t);
    5.47 -    private:
    5.48 -        UNCOPYABLE(Region);
    5.49 -        File* const _file;
    5.50 -        const off_t _position;
    5.51 -        size_t _length;
    5.52 -        const uint8_t *_start;
    5.53 -    };
    5.54 -    
    5.55 -    
    5.56 -}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/Chunk.h	Sun Sep 20 20:39:24 2009 -0700
     6.3 @@ -0,0 +1,97 @@
     6.4 +/*
     6.5 + *  Chunk.h
     6.6 + *  Ottoman
     6.7 + *
     6.8 + *  Created by Jens Alfke on 8/27/09.
     6.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    6.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    6.11 + */
    6.12 +
    6.13 +#ifndef _MOOSEYARD_CHUNK_
    6.14 +#define _MOOSEYARD_CHUNK_
    6.15 +#include "File.h"
    6.16 +
    6.17 +namespace Mooseyard {
    6.18 +    
    6.19 +    class KeyValueChunk;
    6.20 +    
    6.21 +#pragma pack(2)
    6.22 +    
    6.23 +    /** An item stored in a file. It's prefixed with its length, so that the entire file
    6.24 +        can be scanned for chunks easily. */
    6.25 +    class Chunk {
    6.26 +    public:
    6.27 +        uint32_t size() const                       {return _size;}
    6.28 +        uint16_t type() const;
    6.29 +        
    6.30 +        /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */
    6.31 +        const KeyValueChunk* asKeyValue() const;
    6.32 +        
    6.33 +        /** Write a Chunk to the file, in pieces a la writev. */
    6.34 +        static size_t writeMultiple (File *file, uint16_t type,
    6.35 +                                     Blob blobs[], int count)  throw(File::Error);
    6.36 +        
    6.37 +        static size_t writePadding (File *file);
    6.38 +
    6.39 +        static const uint16_t kChunkTypePadding = 0;
    6.40 +
    6.41 +        const void* end() const                     {return offsetBy(this,_size);}
    6.42 +
    6.43 +    protected:
    6.44 +        Chunk (uint32_t size, uint16_t type)        :_size(size), _keyLength(0xFFFF), _type(type) { }
    6.45 +
    6.46 +    private:
    6.47 +        // This is mapped to data in the file! Don't mess with it!
    6.48 +        LittleEndian<uint32_t> _size;
    6.49 +        LittleEndian<uint16_t> _keyLength;          // Used by KeyValueChunk; else 0xFFFF
    6.50 +        LittleEndian<uint16_t> _type;               // Not used by KeyValueChunk
    6.51 +        friend class KeyValueChunk;
    6.52 +    };
    6.53 +    
    6.54 +    
    6.55 +    /** A key/value pair as stored in the memory-mapped file. */
    6.56 +    class KeyValueChunk :public Chunk {
    6.57 +    public:
    6.58 +        Blob key() const;
    6.59 +        Blob value() const;
    6.60 +        
    6.61 +        bool hasKey (Blob key) const;
    6.62 +        
    6.63 +        void validate() const  throw(File::Error);
    6.64 +        
    6.65 +        /** Write a KeyValueChunk to a file. */
    6.66 +        static size_t write (File* file, FilePosition pos, Blob key, Blob value)  throw(File::Error);
    6.67 +
    6.68 +        static const uint16_t kChunkType = 1;
    6.69 +
    6.70 +    private:
    6.71 +        const char* valuePtr() const;
    6.72 +    };    
    6.73 +   
    6.74 +#pragma options align=reset
    6.75 +    
    6.76 +    
    6.77 +    /** An iterator over all the Chunks in a file. */
    6.78 +    class ChunkIterator {
    6.79 +    public:
    6.80 +        ChunkIterator (File*, FilePosition start);
    6.81 +        
    6.82 +        const Chunk* chunk() const              {return _chunk;}
    6.83 +        FilePosition position() const           {return _pos;}
    6.84 +        bool atEOF() const;
    6.85 +        bool next();
    6.86 +        
    6.87 +        operator const Chunk*() const           {return _chunk;}
    6.88 +        virtual bool hasValue() const           {return _chunk != NULL;}
    6.89 +        operator bool() const                   {return hasValue();}
    6.90 +        ChunkIterator& operator++()             {next(); return *this;}
    6.91 +    private:
    6.92 +        void _loadChunk();
    6.93 +        File *_file;
    6.94 +        FilePosition _pos, _length;
    6.95 +        const Chunk* _chunk;
    6.96 +    };
    6.97 +    
    6.98 +}
    6.99 +
   6.100 +#endif _MOOSEYARD_CHUNK_
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/Hash.h	Sun Sep 20 20:39:24 2009 -0700
     7.3 @@ -0,0 +1,119 @@
     7.4 +/*
     7.5 + *  Hash.h
     7.6 + *  Ottoman
     7.7 + *
     7.8 + *  Created by Jens Alfke on 8/20/09.
     7.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    7.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    7.11 + */
    7.12 +
    7.13 +#ifndef _MOOSEYARD_HASH_
    7.14 +#define _MOOSEYARD_HASH_
    7.15 +#include "Base.h"
    7.16 +#include "Dictionary.h"
    7.17 +
    7.18 +namespace Mooseyard {
    7.19 +        
    7.20 +    /** A fairly low-level hash-table class whose keys and values are arbitrary data blobs.
    7.21 +        This class does no memory management of the keys and values: the memory ranges
    7.22 +        pointed to by the key and value parameters to put() must remain valid as long as
    7.23 +        that key or value remains in the hash-table. */
    7.24 +    class Hash {
    7.25 +        
    7.26 +    public:
    7.27 +        typedef void* Value;
    7.28 +        
    7.29 +        /** Creates a new, empty Hash. */
    7.30 +        explicit Hash (int capacity = 50);
    7.31 +        
    7.32 +        ~Hash();
    7.33 +        
    7.34 +        /** Gets the value associated with the given key. */
    7.35 +        Value get(Key) const;
    7.36 +        Value operator[] (Key key) const                        {return get(key);}
    7.37 +        
    7.38 +        /** Returns the number of values in the Hash. */
    7.39 +        int count() const                                       {return _count;}
    7.40 +        
    7.41 +        /** Sets the given value for the given key, replacing any existing value. */
    7.42 +        void put(Key, Value);
    7.43 +        
    7.44 +        /** Removes the value with the given key. */
    7.45 +        bool remove(Key);
    7.46 +        
    7.47 +        /** Removes all values. */
    7.48 +        void removeAll();
    7.49 +        
    7.50 +        void dump() const;
    7.51 +        
    7.52 +        class IndexEntry;
    7.53 +        
    7.54 +        class Iterator {
    7.55 +        public:
    7.56 +            Iterator (const Hash *h);
    7.57 +            Iterator (const Hash &h);
    7.58 +            operator bool() const                           {return hasValue();}
    7.59 +            Value operator*() const                         {return value();}
    7.60 +            Iterator& operator++()                          {next(); return *this;}
    7.61 +            
    7.62 +            Key key() const;
    7.63 +            Value value() const;
    7.64 +            bool hasValue() const                           {return _entry < _end;}
    7.65 +            bool next();
    7.66 +        private:
    7.67 +            Iterator (IndexEntry *start, IndexEntry*end);
    7.68 +            IndexEntry* _entry;
    7.69 +            IndexEntry* const _end;
    7.70 +            UNCOPYABLE(Iterator);
    7.71 +        };
    7.72 +        
    7.73 +    private:
    7.74 +        IndexEntry& _find (Key key) const;
    7.75 +        IndexEntry& _findForPut (const IndexEntry &newEntry);
    7.76 +        void _resize(int newSize);
    7.77 +        void _read();
    7.78 +        UNCOPYABLE(Hash);
    7.79 +        
    7.80 +        int _count;
    7.81 +        int _tableSize;
    7.82 +        IndexEntry *_table;
    7.83 +        
    7.84 +        friend class Iterator;
    7.85 +    };
    7.86 +    
    7.87 +
    7.88 +
    7.89 +    /** A concrete Dictionary implemented with a Hash. */
    7.90 +    class HashDictionary :public MutableDictionary {
    7.91 +    public:
    7.92 +        explicit HashDictionary (int capacity =50)  :_hash(capacity) { }
    7.93 +        virtual ~HashDictionary();
    7.94 +        virtual int count() const                   {return _hash.count();}
    7.95 +        virtual bool contains (Key) const;
    7.96 +        virtual Blob get (Key key) const            {return _convertValue(_hash.get(key));}
    7.97 +        virtual void put (Key, Blob value);
    7.98 +        virtual bool remove (Key);
    7.99 +        virtual void removeAll();
   7.100 +        virtual Iterator* iterate() const           {return new Iterator(*this);}
   7.101 +        
   7.102 +        class Iterator :public Dictionary::Iterator {
   7.103 +        public:
   7.104 +            explicit Iterator (const HashDictionary &d)      :_it(d._hash) { }
   7.105 +            virtual Key key() const                 {return _it.key();}
   7.106 +            virtual Blob value() const              {return _convertValue(_it.value());}
   7.107 +            virtual bool hasValue() const           {return _it.hasValue();}
   7.108 +            virtual bool next()                     {return _it.next();}
   7.109 +        private:
   7.110 +            Hash::Iterator _it;
   7.111 +        };
   7.112 +        
   7.113 +    private:
   7.114 +        static Blob _convertValue (void*);
   7.115 +        Hash _hash;
   7.116 +        friend class Iterator;
   7.117 +    };
   7.118 +    
   7.119 +    
   7.120 +}
   7.121 +
   7.122 +#endif _MOOSEYARD_HASH_
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/Index.h	Sun Sep 20 20:39:24 2009 -0700
     8.3 @@ -0,0 +1,76 @@
     8.4 +/*
     8.5 + *  Index.h
     8.6 + *  Ottoman
     8.7 + *
     8.8 + *  Created by Jens Alfke on 8/27/09.
     8.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    8.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    8.11 + */
    8.12 +
    8.13 +#ifndef _MOOSEYARD_INDEX_
    8.14 +#define _MOOSEYARD_INDEX_
    8.15 +#include "Chunk.h"
    8.16 +#include "Dictionary.h"
    8.17 +
    8.18 +namespace Mooseyard {
    8.19 +    
    8.20 +    class KeyValueChunk;
    8.21 +    
    8.22 +    /** An in-file hash table. This structure is stored directly in the file, memory-mapped.
    8.23 +        Index is normally only used internally by VersionDictionary. */
    8.24 +    class Index :public Chunk {
    8.25 +    public:
    8.26 +        
    8.27 +        class Entry;
    8.28 +        
    8.29 +        static const Index* indexMappedAt (const void*);
    8.30 +        static Index* create (int capacity);
    8.31 +        
    8.32 +        int count() const                                   {return _count;}
    8.33 +        
    8.34 +        Blob get (File *file, Key) const;
    8.35 +        bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition);
    8.36 +        bool remove (File *file, Key, FilePosition maxPosition);
    8.37 +        void removeAll();
    8.38 +        
    8.39 +        void copyFrom (File *file, const Index *index);
    8.40 +        
    8.41 +        class Iterator;
    8.42 +        
    8.43 +        void validate() const  throw(File::Error);
    8.44 +        void validateEntries (const File *file) const  throw(File::Error);
    8.45 +        static const uint16_t kChunkType = 2;
    8.46 +        
    8.47 +    private:
    8.48 +        Index (uint32_t tableSize);
    8.49 +        const Entry* table (int index) const;
    8.50 +        Entry* table (int index);
    8.51 +        const KeyValueChunk* _find (File *file, Key) const;
    8.52 +        bool _put (File *file, Key, FilePosition, FilePosition maxPosition);
    8.53 +
    8.54 +        // This is mapped to data in the file! Don't mess with it!
    8.55 +        LittleEndian<uint32_t>  _magicNumber;
    8.56 +        LittleEndian<uint32_t>  _count;
    8.57 +        LittleEndian<uint32_t>  _tableSize;
    8.58 +        char                    _table[0];      // Actually Entry[]
    8.59 +    };
    8.60 +  
    8.61 +    
    8.62 +    
    8.63 +    class Index::Iterator :public Dictionary::Iterator {
    8.64 +    public:
    8.65 +        Iterator (File*, const Index*);
    8.66 +        virtual Key key() const;
    8.67 +        virtual Blob value() const;
    8.68 +        virtual bool next();
    8.69 +        virtual bool hasValue() const;
    8.70 +        
    8.71 +        FilePosition valuePosition();
    8.72 +    private:
    8.73 +        File* const _file;
    8.74 +        const Index::Entry *_current, *_end;
    8.75 +    };
    8.76 +    
    8.77 +}
    8.78 +
    8.79 +#endif //_MOOSEYARD_INDEX_
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/MemoryMap.h	Sun Sep 20 20:39:24 2009 -0700
     9.3 @@ -0,0 +1,53 @@
     9.4 +/*
     9.5 + *  MemoryMap.h
     9.6 + *  Ottoman
     9.7 + *
     9.8 + *  Created by Jens Alfke on 9/17/09.
     9.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    9.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    9.11 + */
    9.12 +
    9.13 +#include "File.h"
    9.14 +
    9.15 +namespace Mooseyard {
    9.16 +    
    9.17 +    /** A flexible memory-map on a file, which can handle multiple discontiguous mapped regions.
    9.18 +        Clients usually access this functionality through File, which delegates to it. */
    9.19 +    class MemoryMap {
    9.20 +    public:
    9.21 +        MemoryMap (File* file)                          :_file(file), _nRegions(0), _regions(NULL) { }
    9.22 +        ~MemoryMap();
    9.23 +        
    9.24 +        void mapRegion (off_t position, size_t length);
    9.25 +        const void* mappedPosition (off_t position) const;
    9.26 +        
    9.27 +    private:
    9.28 +        UNCOPYABLE(MemoryMap);
    9.29 +        class Region;
    9.30 +        const void* _at (off_t) const;
    9.31 +        
    9.32 +        File *_file;
    9.33 +        int _nRegions;
    9.34 +        Region **_regions;
    9.35 +    };
    9.36 +    
    9.37 +    
    9.38 +    class MemoryMap::Region {
    9.39 +    public:
    9.40 +        Region (File*, off_t position, size_t length);
    9.41 +        ~Region();
    9.42 +        off_t position() const                  {return _position;}
    9.43 +        size_t length() const                   {return _length;}
    9.44 +        off_t end() const                       {return _position + _length;}
    9.45 +        bool setLength (size_t);          // Returns false if it failed
    9.46 +        const void* mappedPosition(off_t);
    9.47 +    private:
    9.48 +        UNCOPYABLE(Region);
    9.49 +        File* const _file;
    9.50 +        const off_t _position;
    9.51 +        size_t _length;
    9.52 +        const uint8_t *_start;
    9.53 +    };
    9.54 +    
    9.55 +    
    9.56 +}