1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/Index.h Sun Sep 20 20:39:24 2009 -0700
1.3 @@ -0,0 +1,76 @@
1.4 +/*
1.5 + * Index.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_INDEX_
1.14 +#define _MOOSEYARD_INDEX_
1.15 +#include "Chunk.h"
1.16 +#include "Dictionary.h"
1.17 +
1.18 +namespace Mooseyard {
1.19 +
1.20 + class KeyValueChunk;
1.21 +
1.22 + /** An in-file hash table. This structure is stored directly in the file, memory-mapped.
1.23 + Index is normally only used internally by VersionDictionary. */
1.24 + class Index :public Chunk {
1.25 + public:
1.26 +
1.27 + class Entry;
1.28 +
1.29 + static const Index* indexMappedAt (const void*);
1.30 + static Index* create (int capacity);
1.31 +
1.32 + int count() const {return _count;}
1.33 +
1.34 + Blob get (File *file, Key) const;
1.35 + bool put (File *file, Key, FilePosition valuePosition, FilePosition maxPosition);
1.36 + bool remove (File *file, Key, FilePosition maxPosition);
1.37 + void removeAll();
1.38 +
1.39 + void copyFrom (File *file, const Index *index);
1.40 +
1.41 + class Iterator;
1.42 +
1.43 + void validate() const throw(File::Error);
1.44 + void validateEntries (const File *file) const throw(File::Error);
1.45 + static const uint16_t kChunkType = 2;
1.46 +
1.47 + private:
1.48 + Index (uint32_t tableSize);
1.49 + const Entry* table (int index) const;
1.50 + Entry* table (int index);
1.51 + const KeyValueChunk* _find (File *file, Key) const;
1.52 + bool _put (File *file, Key, FilePosition, FilePosition maxPosition);
1.53 +
1.54 + // This is mapped to data in the file! Don't mess with it!
1.55 + LittleEndian<uint32_t> _magicNumber;
1.56 + LittleEndian<uint32_t> _count;
1.57 + LittleEndian<uint32_t> _tableSize;
1.58 + char _table[0]; // Actually Entry[]
1.59 + };
1.60 +
1.61 +
1.62 +
1.63 + class Index::Iterator :public Dictionary::Iterator {
1.64 + public:
1.65 + Iterator (File*, const Index*);
1.66 + virtual Key key() const;
1.67 + virtual Blob value() const;
1.68 + virtual bool next();
1.69 + virtual bool hasValue() const;
1.70 +
1.71 + FilePosition valuePosition();
1.72 + private:
1.73 + File* const _file;
1.74 + const Index::Entry *_current, *_end;
1.75 + };
1.76 +
1.77 +}
1.78 +
1.79 +#endif //_MOOSEYARD_INDEX_