# HG changeset patch # User Jens Alfke # Date 1253504364 25200 # Node ID 6cbad782d16a703f0866015df3d7f8d509ae3fea # Parent 31a43d94cc262eb9cfd598dd623f54e3cc4137dd Moved non-public headers out of include/ directory. diff -r 31a43d94cc26 -r 6cbad782d16a Ottoman.xcodeproj/project.pbxproj --- a/Ottoman.xcodeproj/project.pbxproj Sun Sep 20 15:14:12 2009 -0700 +++ b/Ottoman.xcodeproj/project.pbxproj Sun Sep 20 20:39:24 2009 -0700 @@ -41,12 +41,12 @@ 27156CA9104C9C44009EBD39 /* gtest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = gtest.framework; path = /Library/Frameworks/gtest.framework; sourceTree = ""; }; 27603900105AC81200D931A7 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; }; 276E5BBA1066D135008A2171 /* Base.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Base.h; sourceTree = ""; }; - 276E5BBB1066D135008A2171 /* Chunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Chunk.h; path = ../include/Chunk.h; sourceTree = ""; }; + 276E5BBB1066D135008A2171 /* Chunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Chunk.h; sourceTree = ""; }; 276E5BBC1066D135008A2171 /* Dictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dictionary.h; sourceTree = ""; }; 276E5BBD1066D135008A2171 /* File.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = File.h; sourceTree = ""; }; - 276E5BBE1066D135008A2171 /* Hash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Hash.h; path = ../include/Hash.h; sourceTree = ""; }; - 276E5BBF1066D135008A2171 /* Index.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Index.h; path = ../include/Index.h; sourceTree = ""; }; - 276E5BC01066D135008A2171 /* MemoryMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MemoryMap.h; path = ../include/MemoryMap.h; sourceTree = ""; }; + 276E5BBE1066D135008A2171 /* Hash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Hash.h; sourceTree = ""; }; + 276E5BBF1066D135008A2171 /* Index.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Index.h; sourceTree = ""; }; + 276E5BC01066D135008A2171 /* MemoryMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryMap.h; sourceTree = ""; }; 276E5BC11066D135008A2171 /* Ottoman.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Ottoman.h; sourceTree = ""; }; 276E5BC21066D135008A2171 /* VersionDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VersionDictionary.h; sourceTree = ""; }; 276E5BC41066D13D008A2171 /* Base.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Base.cpp; sourceTree = ""; }; diff -r 31a43d94cc26 -r 6cbad782d16a include/Chunk.h --- a/include/Chunk.h Sun Sep 20 15:14:12 2009 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - * Chunk.h - * Ottoman - * - * Created by Jens Alfke on 8/27/09. - * Copyright 2009 Jens Alfke. All rights reserved. - * BSD-Licensed: See the file "LICENSE.txt" for details. - */ - -#ifndef _MOOSEYARD_CHUNK_ -#define _MOOSEYARD_CHUNK_ -#include "File.h" - -namespace Mooseyard { - - class KeyValueChunk; - -#pragma pack(2) - - /** An item stored in a file. It's prefixed with its length, so that the entire file - can be scanned for chunks easily. */ - class Chunk { - public: - uint32_t size() const {return _size;} - uint16_t type() const; - - /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */ - const KeyValueChunk* asKeyValue() const; - - /** Write a Chunk to the file, in pieces a la writev. */ - static size_t writeMultiple (File *file, uint16_t type, - Blob blobs[], int count) throw(File::Error); - - static size_t writePadding (File *file); - - static const uint16_t kChunkTypePadding = 0; - - const void* end() const {return offsetBy(this,_size);} - - protected: - Chunk (uint32_t size, uint16_t type) :_size(size), _keyLength(0xFFFF), _type(type) { } - - private: - // This is mapped to data in the file! Don't mess with it! - LittleEndian _size; - LittleEndian _keyLength; // Used by KeyValueChunk; else 0xFFFF - LittleEndian _type; // Not used by KeyValueChunk - friend class KeyValueChunk; - }; - - - /** A key/value pair as stored in the memory-mapped file. */ - class KeyValueChunk :public Chunk { - public: - Blob key() const; - Blob value() const; - - bool hasKey (Blob key) const; - - void validate() const throw(File::Error); - - /** Write a KeyValueChunk to a file. */ - static size_t write (File* file, FilePosition pos, Blob key, Blob value) throw(File::Error); - - static const uint16_t kChunkType = 1; - - private: - const char* valuePtr() const; - }; - -#pragma options align=reset - - - /** An iterator over all the Chunks in a file. */ - class ChunkIterator { - public: - ChunkIterator (File*, FilePosition start); - - const Chunk* chunk() const {return _chunk;} - FilePosition position() const {return _pos;} - bool atEOF() const; - bool next(); - - operator const Chunk*() const {return _chunk;} - virtual bool hasValue() const {return _chunk != NULL;} - operator bool() const {return hasValue();} - ChunkIterator& operator++() {next(); return *this;} - private: - void _loadChunk(); - File *_file; - FilePosition _pos, _length; - const Chunk* _chunk; - }; - -} - -#endif _MOOSEYARD_CHUNK_ diff -r 31a43d94cc26 -r 6cbad782d16a include/Hash.h --- a/include/Hash.h Sun Sep 20 15:14:12 2009 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,119 +0,0 @@ -/* - * Hash.h - * Ottoman - * - * Created by Jens Alfke on 8/20/09. - * Copyright 2009 Jens Alfke. All rights reserved. - * BSD-Licensed: See the file "LICENSE.txt" for details. - */ - -#ifndef _MOOSEYARD_HASH_ -#define _MOOSEYARD_HASH_ -#include "Base.h" -#include "Dictionary.h" - -namespace Mooseyard { - - /** A fairly low-level hash-table class whose keys and values are arbitrary data blobs. - This class does no memory management of the keys and values: the memory ranges - pointed to by the key and value parameters to put() must remain valid as long as - that key or value remains in the hash-table. */ - class Hash { - - public: - typedef void* Value; - - /** Creates a new, empty Hash. */ - explicit Hash (int capacity = 50); - - ~Hash(); - - /** Gets the value associated with the given key. */ - Value get(Key) const; - Value operator[] (Key key) const {return get(key);} - - /** Returns the number of values in the Hash. */ - int count() const {return _count;} - - /** Sets the given value for the given key, replacing any existing value. */ - void put(Key, Value); - - /** Removes the value with the given key. */ - bool remove(Key); - - /** Removes all values. */ - void removeAll(); - - void dump() const; - - class IndexEntry; - - class Iterator { - public: - Iterator (const Hash *h); - Iterator (const Hash &h); - operator bool() const {return hasValue();} - Value operator*() const {return value();} - Iterator& operator++() {next(); return *this;} - - Key key() const; - Value value() const; - bool hasValue() const {return _entry < _end;} - bool next(); - private: - Iterator (IndexEntry *start, IndexEntry*end); - IndexEntry* _entry; - IndexEntry* const _end; - UNCOPYABLE(Iterator); - }; - - private: - IndexEntry& _find (Key key) const; - IndexEntry& _findForPut (const IndexEntry &newEntry); - void _resize(int newSize); - void _read(); - UNCOPYABLE(Hash); - - int _count; - int _tableSize; - IndexEntry *_table; - - friend class Iterator; - }; - - - - /** A concrete Dictionary implemented with a Hash. */ - class HashDictionary :public MutableDictionary { - public: - explicit HashDictionary (int capacity =50) :_hash(capacity) { } - virtual ~HashDictionary(); - virtual int count() const {return _hash.count();} - virtual bool contains (Key) const; - virtual Blob get (Key key) const {return _convertValue(_hash.get(key));} - virtual void put (Key, Blob value); - virtual bool remove (Key); - virtual void removeAll(); - virtual Iterator* iterate() const {return new Iterator(*this);} - - class Iterator :public Dictionary::Iterator { - public: - explicit Iterator (const HashDictionary &d) :_it(d._hash) { } - virtual Key key() const {return _it.key();} - virtual Blob value() const {return _convertValue(_it.value());} - virtual bool hasValue() const {return _it.hasValue();} - virtual bool next() {return _it.next();} - private: - Hash::Iterator _it; - }; - - private: - static Blob _convertValue (void*); - Hash _hash; - friend class Iterator; - }; - - -} - -#endif _MOOSEYARD_HASH_ diff -r 31a43d94cc26 -r 6cbad782d16a include/Index.h --- a/include/Index.h Sun Sep 20 15:14:12 2009 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * Index.h - * Ottoman - * - * Created by Jens Alfke on 8/27/09. - * Copyright 2009 Jens Alfke. All rights reserved. - * BSD-Licensed: See the file "LICENSE.txt" for details. - */ - -#ifndef _MOOSEYARD_INDEX_ -#define _MOOSEYARD_INDEX_ -#include "Chunk.h" -#include "Dictionary.h" - -namespace Mooseyard { - - class KeyValueChunk; - - /** An in-file hash table. This structure is stored directly in the file, memory-mapped. - Index is normally only used internally by VersionDictionary. */ - class Index :public Chunk { - public: - - class Entry; - - static const Index* indexMappedAt (const void*); - static Index* create (int capacity); - - int count() const {return _count;} - - Blob get (File *file, Key) const; - bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition); - bool remove (File *file, Key, FilePosition maxPosition); - void removeAll(); - - void copyFrom (File *file, const Index *index); - - class Iterator; - - void validate() const throw(File::Error); - void validateEntries (const File *file) const throw(File::Error); - static const uint16_t kChunkType = 2; - - private: - Index (uint32_t tableSize); - const Entry* table (int index) const; - Entry* table (int index); - const KeyValueChunk* _find (File *file, Key) const; - bool _put (File *file, Key, FilePosition, FilePosition maxPosition); - - // This is mapped to data in the file! Don't mess with it! - LittleEndian _magicNumber; - LittleEndian _count; - LittleEndian _tableSize; - char _table[0]; // Actually Entry[] - }; - - - - class Index::Iterator :public Dictionary::Iterator { - public: - Iterator (File*, const Index*); - virtual Key key() const; - virtual Blob value() const; - virtual bool next(); - virtual bool hasValue() const; - - FilePosition valuePosition(); - private: - File* const _file; - const Index::Entry *_current, *_end; - }; - -} - -#endif //_MOOSEYARD_INDEX_ diff -r 31a43d94cc26 -r 6cbad782d16a include/MemoryMap.h --- a/include/MemoryMap.h Sun Sep 20 15:14:12 2009 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -/* - * MemoryMap.h - * Ottoman - * - * Created by Jens Alfke on 9/17/09. - * Copyright 2009 Jens Alfke. All rights reserved. - * BSD-Licensed: See the file "LICENSE.txt" for details. - */ - -#include "File.h" - -namespace Mooseyard { - - /** A flexible memory-map on a file, which can handle multiple discontiguous mapped regions. - Clients usually access this functionality through File, which delegates to it. */ - class MemoryMap { - public: - MemoryMap (File* file) :_file(file), _nRegions(0), _regions(NULL) { } - ~MemoryMap(); - - void mapRegion (off_t position, size_t length); - const void* mappedPosition (off_t position) const; - - private: - UNCOPYABLE(MemoryMap); - class Region; - const void* _at (off_t) const; - - File *_file; - int _nRegions; - Region **_regions; - }; - - - class MemoryMap::Region { - public: - Region (File*, off_t position, size_t length); - ~Region(); - off_t position() const {return _position;} - size_t length() const {return _length;} - off_t end() const {return _position + _length;} - bool setLength (size_t); // Returns false if it failed - const void* mappedPosition(off_t); - private: - UNCOPYABLE(Region); - File* const _file; - const off_t _position; - size_t _length; - const uint8_t *_start; - }; - - -} diff -r 31a43d94cc26 -r 6cbad782d16a src/Chunk.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Chunk.h Sun Sep 20 20:39:24 2009 -0700 @@ -0,0 +1,97 @@ +/* + * Chunk.h + * Ottoman + * + * Created by Jens Alfke on 8/27/09. + * Copyright 2009 Jens Alfke. All rights reserved. + * BSD-Licensed: See the file "LICENSE.txt" for details. + */ + +#ifndef _MOOSEYARD_CHUNK_ +#define _MOOSEYARD_CHUNK_ +#include "File.h" + +namespace Mooseyard { + + class KeyValueChunk; + +#pragma pack(2) + + /** An item stored in a file. It's prefixed with its length, so that the entire file + can be scanned for chunks easily. */ + class Chunk { + public: + uint32_t size() const {return _size;} + uint16_t type() const; + + /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */ + const KeyValueChunk* asKeyValue() const; + + /** Write a Chunk to the file, in pieces a la writev. */ + static size_t writeMultiple (File *file, uint16_t type, + Blob blobs[], int count) throw(File::Error); + + static size_t writePadding (File *file); + + static const uint16_t kChunkTypePadding = 0; + + const void* end() const {return offsetBy(this,_size);} + + protected: + Chunk (uint32_t size, uint16_t type) :_size(size), _keyLength(0xFFFF), _type(type) { } + + private: + // This is mapped to data in the file! Don't mess with it! + LittleEndian _size; + LittleEndian _keyLength; // Used by KeyValueChunk; else 0xFFFF + LittleEndian _type; // Not used by KeyValueChunk + friend class KeyValueChunk; + }; + + + /** A key/value pair as stored in the memory-mapped file. */ + class KeyValueChunk :public Chunk { + public: + Blob key() const; + Blob value() const; + + bool hasKey (Blob key) const; + + void validate() const throw(File::Error); + + /** Write a KeyValueChunk to a file. */ + static size_t write (File* file, FilePosition pos, Blob key, Blob value) throw(File::Error); + + static const uint16_t kChunkType = 1; + + private: + const char* valuePtr() const; + }; + +#pragma options align=reset + + + /** An iterator over all the Chunks in a file. */ + class ChunkIterator { + public: + ChunkIterator (File*, FilePosition start); + + const Chunk* chunk() const {return _chunk;} + FilePosition position() const {return _pos;} + bool atEOF() const; + bool next(); + + operator const Chunk*() const {return _chunk;} + virtual bool hasValue() const {return _chunk != NULL;} + operator bool() const {return hasValue();} + ChunkIterator& operator++() {next(); return *this;} + private: + void _loadChunk(); + File *_file; + FilePosition _pos, _length; + const Chunk* _chunk; + }; + +} + +#endif _MOOSEYARD_CHUNK_ diff -r 31a43d94cc26 -r 6cbad782d16a src/Hash.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Hash.h Sun Sep 20 20:39:24 2009 -0700 @@ -0,0 +1,119 @@ +/* + * Hash.h + * Ottoman + * + * Created by Jens Alfke on 8/20/09. + * Copyright 2009 Jens Alfke. All rights reserved. + * BSD-Licensed: See the file "LICENSE.txt" for details. + */ + +#ifndef _MOOSEYARD_HASH_ +#define _MOOSEYARD_HASH_ +#include "Base.h" +#include "Dictionary.h" + +namespace Mooseyard { + + /** A fairly low-level hash-table class whose keys and values are arbitrary data blobs. + This class does no memory management of the keys and values: the memory ranges + pointed to by the key and value parameters to put() must remain valid as long as + that key or value remains in the hash-table. */ + class Hash { + + public: + typedef void* Value; + + /** Creates a new, empty Hash. */ + explicit Hash (int capacity = 50); + + ~Hash(); + + /** Gets the value associated with the given key. */ + Value get(Key) const; + Value operator[] (Key key) const {return get(key);} + + /** Returns the number of values in the Hash. */ + int count() const {return _count;} + + /** Sets the given value for the given key, replacing any existing value. */ + void put(Key, Value); + + /** Removes the value with the given key. */ + bool remove(Key); + + /** Removes all values. */ + void removeAll(); + + void dump() const; + + class IndexEntry; + + class Iterator { + public: + Iterator (const Hash *h); + Iterator (const Hash &h); + operator bool() const {return hasValue();} + Value operator*() const {return value();} + Iterator& operator++() {next(); return *this;} + + Key key() const; + Value value() const; + bool hasValue() const {return _entry < _end;} + bool next(); + private: + Iterator (IndexEntry *start, IndexEntry*end); + IndexEntry* _entry; + IndexEntry* const _end; + UNCOPYABLE(Iterator); + }; + + private: + IndexEntry& _find (Key key) const; + IndexEntry& _findForPut (const IndexEntry &newEntry); + void _resize(int newSize); + void _read(); + UNCOPYABLE(Hash); + + int _count; + int _tableSize; + IndexEntry *_table; + + friend class Iterator; + }; + + + + /** A concrete Dictionary implemented with a Hash. */ + class HashDictionary :public MutableDictionary { + public: + explicit HashDictionary (int capacity =50) :_hash(capacity) { } + virtual ~HashDictionary(); + virtual int count() const {return _hash.count();} + virtual bool contains (Key) const; + virtual Blob get (Key key) const {return _convertValue(_hash.get(key));} + virtual void put (Key, Blob value); + virtual bool remove (Key); + virtual void removeAll(); + virtual Iterator* iterate() const {return new Iterator(*this);} + + class Iterator :public Dictionary::Iterator { + public: + explicit Iterator (const HashDictionary &d) :_it(d._hash) { } + virtual Key key() const {return _it.key();} + virtual Blob value() const {return _convertValue(_it.value());} + virtual bool hasValue() const {return _it.hasValue();} + virtual bool next() {return _it.next();} + private: + Hash::Iterator _it; + }; + + private: + static Blob _convertValue (void*); + Hash _hash; + friend class Iterator; + }; + + +} + +#endif _MOOSEYARD_HASH_ diff -r 31a43d94cc26 -r 6cbad782d16a src/Index.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Index.h Sun Sep 20 20:39:24 2009 -0700 @@ -0,0 +1,76 @@ +/* + * Index.h + * Ottoman + * + * Created by Jens Alfke on 8/27/09. + * Copyright 2009 Jens Alfke. All rights reserved. + * BSD-Licensed: See the file "LICENSE.txt" for details. + */ + +#ifndef _MOOSEYARD_INDEX_ +#define _MOOSEYARD_INDEX_ +#include "Chunk.h" +#include "Dictionary.h" + +namespace Mooseyard { + + class KeyValueChunk; + + /** An in-file hash table. This structure is stored directly in the file, memory-mapped. + Index is normally only used internally by VersionDictionary. */ + class Index :public Chunk { + public: + + class Entry; + + static const Index* indexMappedAt (const void*); + static Index* create (int capacity); + + int count() const {return _count;} + + Blob get (File *file, Key) const; + bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition); + bool remove (File *file, Key, FilePosition maxPosition); + void removeAll(); + + void copyFrom (File *file, const Index *index); + + class Iterator; + + void validate() const throw(File::Error); + void validateEntries (const File *file) const throw(File::Error); + static const uint16_t kChunkType = 2; + + private: + Index (uint32_t tableSize); + const Entry* table (int index) const; + Entry* table (int index); + const KeyValueChunk* _find (File *file, Key) const; + bool _put (File *file, Key, FilePosition, FilePosition maxPosition); + + // This is mapped to data in the file! Don't mess with it! + LittleEndian _magicNumber; + LittleEndian _count; + LittleEndian _tableSize; + char _table[0]; // Actually Entry[] + }; + + + + class Index::Iterator :public Dictionary::Iterator { + public: + Iterator (File*, const Index*); + virtual Key key() const; + virtual Blob value() const; + virtual bool next(); + virtual bool hasValue() const; + + FilePosition valuePosition(); + private: + File* const _file; + const Index::Entry *_current, *_end; + }; + +} + +#endif //_MOOSEYARD_INDEX_ diff -r 31a43d94cc26 -r 6cbad782d16a src/MemoryMap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/MemoryMap.h Sun Sep 20 20:39:24 2009 -0700 @@ -0,0 +1,53 @@ +/* + * MemoryMap.h + * Ottoman + * + * Created by Jens Alfke on 9/17/09. + * Copyright 2009 Jens Alfke. All rights reserved. + * BSD-Licensed: See the file "LICENSE.txt" for details. + */ + +#include "File.h" + +namespace Mooseyard { + + /** A flexible memory-map on a file, which can handle multiple discontiguous mapped regions. + Clients usually access this functionality through File, which delegates to it. */ + class MemoryMap { + public: + MemoryMap (File* file) :_file(file), _nRegions(0), _regions(NULL) { } + ~MemoryMap(); + + void mapRegion (off_t position, size_t length); + const void* mappedPosition (off_t position) const; + + private: + UNCOPYABLE(MemoryMap); + class Region; + const void* _at (off_t) const; + + File *_file; + int _nRegions; + Region **_regions; + }; + + + class MemoryMap::Region { + public: + Region (File*, off_t position, size_t length); + ~Region(); + off_t position() const {return _position;} + size_t length() const {return _length;} + off_t end() const {return _position + _length;} + bool setLength (size_t); // Returns false if it failed + const void* mappedPosition(off_t); + private: + UNCOPYABLE(Region); + File* const _file; + const off_t _position; + size_t _length; + const uint8_t *_start; + }; + + +}