summaryrefslogtreecommitdiff
path: root/src/unstable/myriadcache.h
blob: 9bc926fd8ad757a61e57b7066ac8cc2769dd82ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_MYRIAD_CACHE_H
#define BU_MYRIAD_CACHE_H

#include "bu/cachebase.h"
#include "bu/myriad.h"
#include "bu/myriadstream.h"
#include "bu/file.h"
#include "bu/streamstack.h"

namespace Bu
{
    template<typename keytype, typename obtype>
    class MyriadCache : public Bu::CacheBase<keytype, obtype>
    {
    public:
        MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) :
            sStore( sStore ),
            mStore( sStore, iBlockSize, iPreallocate ),
            bStructureChanged( false )
        {
            try
            {
                Bu::ReadWriteMutex::ReadLocker l( rwStore );
                Bu::MyriadStream ms = mStore.openStream( 1 );
                Bu::Archive ar( ms, Bu::Archive::load );
                uint8_t uVer;
                ar >> uVer;
                switch( uVer )
                {
                    case 0:
                        ar >> hIndex;
                        break;
                }
            }
            catch(...)
            {
                if( mStore.createStreamWithId( 1 ) != 1 )
                    throw Bu::ExceptionBase("Error creating index stream.");

                _sync();
            }
        }

        virtual ~MyriadCache()
        {
            Bu::CacheBase<keytype,obtype>::sync();
        }
       
        using typename Bu::CacheBase<keytype,obtype>::KeyList;
        
        virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const
        {
            Bu::ReadWriteMutex::ReadLocker rl( rwStore );
            return hIndex.getKeys();
        }
        
        virtual int getSize() const
        {
            Bu::ReadWriteMutex::ReadLocker rl( rwStore );
            return hIndex.getSize();
        }

    protected:
        virtual bool _has( const keytype &key )
        {
            Bu::ReadWriteMutex::ReadLocker rl( rwStore );
            return hIndex.has( key );
        }

        virtual void _create( const obtype *o )
        {
            Bu::ReadWriteMutex::WriteLocker wl( rwStore );
            hIndex.insert( o->getKey(), mStore.createStream() );
            _save( o );

            bStructureChanged = true;
        }

        virtual void _erase( const keytype &k )
        {
            Bu::ReadWriteMutex::WriteLocker wl( rwStore );
            mStore.deleteStream( hIndex.get( k ) );
            hIndex.erase( k );

            bStructureChanged = true;
        }

        virtual obtype *_load( const keytype &k )
        {
            Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) );
            return _cacheObjectLoad<obtype>( ms );
        }

        virtual void _save( const obtype *o )
        {
            Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) );
            _cacheObjectSave( ms, o );
            ms.setSize( ms.tell() );
            
            mStore.sync();
        }

        virtual void _sync()
        {
            Bu::ReadWriteMutex::WriteLocker wl( rwStore );
            if( !bStructureChanged )
                return;

            Bu::MyriadStream ms = mStore.openStream( 1 );
            Bu::Archive ar( ms, Bu::Archive::save );
            ar << (uint8_t)0 << hIndex;
            ar.close();
            ms.setSize( ms.tell() );

            bStructureChanged = false;

            mStore.sync();
        }

    private:
        Bu::Stream &sStore;
        Bu::Myriad mStore;
        Bu::Hash<keytype, int> hIndex;
        mutable Bu::ReadWriteMutex rwStore;
        bool bStructureChanged;
    };
}

#endif