summaryrefslogtreecommitdiff
path: root/src/tests/cache.cpp
blob: 00a32ddce4b0db55482e7a17471a7d1f25682162 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <bu/archive.h>
#include <bu/hash.h>
#include <bu/uuid.h>
#include <bu/myriad.h>
#include <bu/myriadstream.h>
#include <bu/file.h>
#include <bu/streamstack.h>

//
// New Cache Implementation
//

template<typename keytype, typename obtype> class CacheBase;

template<typename keytype, typename obtype>
class CacheObject
{
friend class CacheBase<keytype, obtype>;
public:
    CacheObject() :
        pCache( NULL ),
        bChanged( false )
    {
    }

    virtual ~CacheObject()
    {
    }

    typedef CacheBase<keytype, obtype> CacheType;

    virtual const keytype getKey() const=0;
    virtual const int getPersistenceScore() const { return 0; }

    void changed( bool bChanged=true )
    {
        if( this->bChanged == false && bChanged == true )
            pCache->objectChanged( getKey() );

        this->bChanged = bChanged;
    }

private:
    void setCache( CacheType *pCache )
    {
        this->pCache = pCache;
    }

private:
    CacheType *pCache;
    bool bChanged;
};

template<typename keytype, typename obtype, typename basetype=obtype>
class CachePtr
{
friend class CacheBase<keytype, basetype>;
public:
    CachePtr()
    {
    }

    virtual ~CachePtr()
    {
    }
};

template<typename keytype, typename obtype>
class CacheBase
{
public:
    CacheBase()
    {
    }

    virtual ~CacheBase()
    {
    }

protected:
    virtual void _create( obtype *o )=0;
    virtual void _erase( const keytype &k )=0;

    virtual obtype *_load( const keytype &k )=0;
    virtual void _save( obtype *o )=0;
};

template<typename keytype, typename obtype>
class MyriadCache : public CacheBase<keytype, obtype>
{
public:
    MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) :
        sStore( sStore ),
        mStore( sStore, iBlockSize, iPreallocate )
    {
        try
        {
            Bu::MyriadStream ms = mStore.openStream( 1 );
            Bu::Archive ar( ms, Bu::Archive::load );
            ar >> hIndex;
        }
        catch(...)
        {
            if( mStore.createStreamWithId( 1 ) != 1 )
                throw Bu::ExceptionBase("Error creating index stream.");

            Bu::MyriadStream ms = mStore.openStream( 1 );
            Bu::Archive ar( ms, Bu::Archive::save );
            ar << hIndex;
        }
    }

    virtual ~MyriadCache()
    {
    }

protected:
    virtual void _create( obtype *o )
    {
        hIndex.insert( o->getKey(), mStore.createStream() );
        _save( o );
    }

    virtual void _erase( const keytype &k )
    {
        mStore.deleteStream( hIndex.get( k ) );
        hIndex.erase( k );
    }

    virtual obtype *_load( const keytype &k )
    {
        Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) );
        Bu::Archive ar( ms, Bu::Archive::load );
        obtype *ret = new obtype();
        ar >> *ret;
        return ret;
    }

    virtual void _save( obtype *o )
    {
        Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) );
        {
            Bu::Archive ar( ms, Bu::Archive::save );
            ar << *o;
        }
        ms.setSize( ms.tell() );
    }

private:
    Bu::Stream &sStore;
    Bu::Myriad mStore;
    Bu::Hash<keytype, int> hIndex;
};

//
// Test
//

class Something : public CacheObject<Bu::Uuid, Something>
{
friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s );
friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s );
public:
    Something()
    {
    }
    
    Something( const Bu::String &sName ) :
        uId( Bu::Uuid::generate() ),
        sName( sName )
    {
    }

    virtual ~Something()
    {
    }

    virtual const Bu::Uuid getKey() const
    {
        return uId;
    }

private:
    Bu::Uuid uId;
    Bu::String sName;
};

Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s )
{
    return ar >> s.uId >> s.sName;
}

Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s )
{
    return ar << s.uId << s.sName;
}

int main()
{
    Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite);
    MyriadCache<Bu::Uuid, Something> c( fStore );

    return 0;
}