aboutsummaryrefslogtreecommitdiff
path: root/src/tests/cachedel.cpp
blob: fd40994d09c0c617ea0265847d9c41c250fbb45b (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "bu/myriadcache.h"
#include "bu/uuid.h"
#include "bu/string.h"
#include "bu/sio.h"
#include "bu/membuf.h"

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

    virtual ~Something()
    {
    }

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

    Bu::String getName() const
    {
        return sName;
    }

    void setName( const Bu::String &sNewName )
    {
        sName = sNewName;
        changed();
    }

    virtual Bu::String toString() const=0;

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

class SubSomethingA : public Something
{
friend Bu::Archive &operator>>( Bu::Archive &ar, SubSomethingA &s );
friend Bu::Archive &operator<<( Bu::Archive &ar, const SubSomethingA &s );
public:
    SubSomethingA()
    {
    }

    SubSomethingA( const Bu::String &sName, int iNumber ) :
        Something( sName ),
        iNumber( iNumber )
    {
    }
    
    virtual Bu::String toString() const
    {
        return Bu::String("[typeA] %1 (%2)").arg( getName() ).arg( iNumber );
    }

private:
    int iNumber;
};

class SubSomethingB : public Something
{
friend Bu::Archive &operator>>( Bu::Archive &ar, SubSomethingB &s );
friend Bu::Archive &operator<<( Bu::Archive &ar, const SubSomethingB &s );
public:
    SubSomethingB()
    {
    }

    SubSomethingB( const Bu::String &sName, const Bu::String &sString ) :
        Something( sName ),
        sString( sString )
    {
    }
    
    virtual Bu::String toString() const
    {
        return Bu::String("[typeB] %1 (%2)").arg( getName() ).arg( sString );
    }

private:
    Bu::String sString;
};

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

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

Bu::Archive &operator>>( Bu::Archive &ar, SubSomethingA &s )
{
    return ar >> (Something &)s >> s.iNumber;
}

Bu::Archive &operator<<( Bu::Archive &ar, const SubSomethingA &s )
{
    return ar << (Something &)s << s.iNumber;
}

Bu::Archive &operator>>( Bu::Archive &ar, SubSomethingB &s )
{
    return ar >> (Something &)s >> s.sString;
}

Bu::Archive &operator<<( Bu::Archive &ar, const SubSomethingB &s )
{
    return ar << (Something &)s << s.sString;
}
    
namespace Bu
{
    template<>
    void _cacheObjectSave<const Something>( Bu::Stream &s, const Something *pObject )
    {
        Bu::ArchiveBinary ar( s, Bu::ArchiveBinary::save );
        if( typeid(*pObject) == typeid(SubSomethingA) )
        {
            ar << (uint8_t)1 << (SubSomethingA &)*pObject;
        }
        else if( typeid(*pObject) == typeid(SubSomethingB) )
        {
            ar << (uint8_t)2 << (SubSomethingB &)*pObject;
        }
        else
        {
            Bu::println("Not a recognized type!");
            throw Bu::ExceptionBase("Not recognized type!");
        }
    }

    template<>
    Something *_cacheObjectLoad<Bu::Uuid, Something>( Bu::CacheObject<Bu::Uuid, Something>::Initializer &initObj, const Bu::Uuid &rKey, Bu::Stream &s )
    {
        Bu::ArchiveBinary ar( s, Bu::ArchiveBinary::load );
        uint8_t uType;
        ar >> uType;
        switch( uType )
        {
            case 1:
                {
                    SubSomethingA *ret = initObj(new SubSomethingA());
                    ar >> *ret;
                    return ret;
                }
                break;

            case 2:
                {
                    SubSomethingB *ret = initObj(new SubSomethingB());
                    ar >> *ret;
                    return ret;
                }
                break;

            default:
                throw Bu::ExceptionBase("Flagrant error! Invalid type!");
        }

        return NULL;
    }
}

typedef Bu::CachePtr<Bu::Uuid, Something> SomethingPtr;
typedef Bu::CachePtr<Bu::Uuid, SubSomethingA, Something> SomethingAPtr;
typedef Bu::CachePtr<Bu::Uuid, SubSomethingB, Something> SomethingBPtr;
typedef Bu::MyriadCache<Bu::Uuid, Something> SomethingCache;

int main( int, char *[] )
{
    Bu::MemBuf mbStore;
    SomethingCache c( mbStore );

    SomethingPtr ptr;
    if( time(NULL)%2 )
        ptr = c.insert( new SubSomethingA("Hello", 55) ).cast<Something>();
    else
        ptr = c.insert( new SubSomethingB("Goodbye", "Things") ).cast<Something>();

    Bu::Uuid id = ptr.getKey();
    Bu::println("Something[%1]: %2").
        arg( ptr.getKey() ).
        arg( ptr->getName() );
   
    Bu::println("has %1: %2").arg( id ).arg( c.has( id ) );
    c.erase( ptr.getKey() );
    Bu::println("has %1: %2").arg( id ).arg( c.has( id ) );
    SomethingPtr b = ptr;

    SomethingPtr p2 = c.insert( new SubSomethingA("new test", 123) ).cast<Something>();
    id = p2.getKey();
    p2.unbind();
    c.erase( id );

    return 0;
}

/*
int main( int argc, char *argv[] )
{
    Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite);
    SomethingCache c( fStore );

    for( int j = 1; j < argc; j++ )
    {
        SomethingPtr ptr = c.insert( new Something(argv[j]) );

        Bu::println("Something[%1]: %2").
            arg( ptr.getKey() ).
            arg( ptr->getName() );
    }
    
    SomethingCache::KeyList lKeys = c.getKeys();
    Bu::println("Count: %1").arg( lKeys.getSize() );
    int j = 0;
    for( SomethingCache::KeyList::iterator i = lKeys.begin(); i; i++ )
    {
        Bu::println(" - %1: '%2'").arg( *i ).arg( c.get( *i )->getName() );
        if( ((j++)%2) )
            c.erase( *i );
    }
    Bu::println("Count: %1").arg( c.getSize() );

    c._debug();

    SomethingPtr p2;
    SomethingPtr p1( c.get( lKeys.first() ) );

    c._debug();

    {
        SomethingPtr p2( p1 );
        c._debug();
    }
    
    c._debug();

    p2 = p1;
    
    c._debug();

    p1.unbind();
    
    c._debug();

    Bu::println("Name: %1").arg( p1->getName() );

    p1.unbind();
    p1.lock();
    p1.unlock();
    
    c._debug();

    SomethingPtr p3 = c.getLazy( lKeys.first() );

    c._debug();

    {
    SomethingPtr::Locker l( p3 );

    Bu::println("Name again: %1").arg( p3->getName() );

    p3->setName( p3->getName() + " - again" );
    }

    c.sync();

    c._debug();

    return 0;
}
*/