First official checkin.
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.
10 #ifndef _MOOSEYARD_CHUNK_
11 #define _MOOSEYARD_CHUNK_
20 /** An item stored in a file. It's prefixed with its length, so that the entire file
21 can be scanned for chunks easily. */
24 uint32_t size() const {return _size;}
25 uint16_t type() const;
27 /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */
28 const KeyValueChunk* asKeyValue() const;
30 /** Write a Chunk to the file, in pieces a la writev. */
31 static size_t writeMultiple (File *file, uint16_t type,
32 Blob blobs[], int count) throw(File::Error);
34 static size_t writePadding (File *file);
36 static const uint16_t kChunkTypePadding = 0;
38 const void* end() const {return offsetBy(this,_size);}
41 Chunk (uint32_t size, uint16_t type) :_size(size), _keyLength(0xFFFF), _type(type) { }
44 // This is mapped to data in the file! Don't mess with it!
45 LittleEndian<uint32_t> _size;
46 LittleEndian<uint16_t> _keyLength; // Used by KeyValueChunk; else 0xFFFF
47 LittleEndian<uint16_t> _type; // Not used by KeyValueChunk
48 friend class KeyValueChunk;
52 /** A key/value pair as stored in the memory-mapped file. */
53 class KeyValueChunk :public Chunk {
58 bool hasKey (Blob key) const;
60 void validate() const throw(File::Error);
62 /** Write a KeyValueChunk to a file. */
63 static size_t write (File* file, FilePosition pos, Blob key, Blob value) throw(File::Error);
65 static const uint16_t kChunkType = 1;
68 const char* valuePtr() const;
71 #pragma options align=reset
74 /** An iterator over all the Chunks in a file. */
77 ChunkIterator (File*, FilePosition start);
79 const Chunk* chunk() const {return _chunk;}
80 FilePosition position() const {return _pos;}
84 operator const Chunk*() const {return _chunk;}
85 virtual bool hasValue() const {return _chunk != NULL;}
86 operator bool() const {return hasValue();}
87 ChunkIterator& operator++() {next(); return *this;}
91 FilePosition _pos, _length;
97 #endif _MOOSEYARD_CHUNK_