1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/Chunk.h Sun Sep 20 20:39:24 2009 -0700
1.3 @@ -0,0 +1,97 @@
1.4 +/*
1.5 + * Chunk.h
1.6 + * Ottoman
1.7 + *
1.8 + * Created by Jens Alfke on 8/27/09.
1.9 + * Copyright 2009 Jens Alfke. All rights reserved.
1.10 + * BSD-Licensed: See the file "LICENSE.txt" for details.
1.11 + */
1.12 +
1.13 +#ifndef _MOOSEYARD_CHUNK_
1.14 +#define _MOOSEYARD_CHUNK_
1.15 +#include "File.h"
1.16 +
1.17 +namespace Mooseyard {
1.18 +
1.19 + class KeyValueChunk;
1.20 +
1.21 +#pragma pack(2)
1.22 +
1.23 + /** An item stored in a file. It's prefixed with its length, so that the entire file
1.24 + can be scanned for chunks easily. */
1.25 + class Chunk {
1.26 + public:
1.27 + uint32_t size() const {return _size;}
1.28 + uint16_t type() const;
1.29 +
1.30 + /** If this is a KeyValueChunk, returns itself cast to that type, else NULL. */
1.31 + const KeyValueChunk* asKeyValue() const;
1.32 +
1.33 + /** Write a Chunk to the file, in pieces a la writev. */
1.34 + static size_t writeMultiple (File *file, uint16_t type,
1.35 + Blob blobs[], int count) throw(File::Error);
1.36 +
1.37 + static size_t writePadding (File *file);
1.38 +
1.39 + static const uint16_t kChunkTypePadding = 0;
1.40 +
1.41 + const void* end() const {return offsetBy(this,_size);}
1.42 +
1.43 + protected:
1.44 + Chunk (uint32_t size, uint16_t type) :_size(size), _keyLength(0xFFFF), _type(type) { }
1.45 +
1.46 + private:
1.47 + // This is mapped to data in the file! Don't mess with it!
1.48 + LittleEndian<uint32_t> _size;
1.49 + LittleEndian<uint16_t> _keyLength; // Used by KeyValueChunk; else 0xFFFF
1.50 + LittleEndian<uint16_t> _type; // Not used by KeyValueChunk
1.51 + friend class KeyValueChunk;
1.52 + };
1.53 +
1.54 +
1.55 + /** A key/value pair as stored in the memory-mapped file. */
1.56 + class KeyValueChunk :public Chunk {
1.57 + public:
1.58 + Blob key() const;
1.59 + Blob value() const;
1.60 +
1.61 + bool hasKey (Blob key) const;
1.62 +
1.63 + void validate() const throw(File::Error);
1.64 +
1.65 + /** Write a KeyValueChunk to a file. */
1.66 + static size_t write (File* file, FilePosition pos, Blob key, Blob value) throw(File::Error);
1.67 +
1.68 + static const uint16_t kChunkType = 1;
1.69 +
1.70 + private:
1.71 + const char* valuePtr() const;
1.72 + };
1.73 +
1.74 +#pragma options align=reset
1.75 +
1.76 +
1.77 + /** An iterator over all the Chunks in a file. */
1.78 + class ChunkIterator {
1.79 + public:
1.80 + ChunkIterator (File*, FilePosition start);
1.81 +
1.82 + const Chunk* chunk() const {return _chunk;}
1.83 + FilePosition position() const {return _pos;}
1.84 + bool atEOF() const;
1.85 + bool next();
1.86 +
1.87 + operator const Chunk*() const {return _chunk;}
1.88 + virtual bool hasValue() const {return _chunk != NULL;}
1.89 + operator bool() const {return hasValue();}
1.90 + ChunkIterator& operator++() {next(); return *this;}
1.91 + private:
1.92 + void _loadChunk();
1.93 + File *_file;
1.94 + FilePosition _pos, _length;
1.95 + const Chunk* _chunk;
1.96 + };
1.97 +
1.98 +}
1.99 +
1.100 +#endif _MOOSEYARD_CHUNK_