Unnamed Fossil Project

Check-in [64eb4d0d7d]
Login

Check-in [64eb4d0d7d]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:mySQL backend for XDDB
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 64eb4d0d7d2bb173ea3503f708422c86f82e29ff
User & Date: monster 2012-08-09 01:06:25.890
Context
2012-08-09
02:55
updated check-in: 0f4ad7934f user: monster tags: trunk
01:06
mySQL backend for XDDB check-in: 64eb4d0d7d user: monster tags: trunk
2012-08-07
05:12
updated. check-in: 5efd835985 user: monster tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to buffer.hc.
247
248
249
250
251
252
253











































254
255
256
257
258
259
260
        bf->count += len*2;
        bf->at[bf->count] = 0;
      }
  }
#endif
  ;












































void Buffer_Quote_Append(YO_BUFFER *bf, void *S, int len, int brk)
#ifdef _YO_BUFFER_BUILTIN
  {
    byte_t *q = S;
    byte_t *p = q;
    byte_t *E;
    







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
        bf->count += len*2;
        bf->at[bf->count] = 0;
      }
  }
#endif
  ;

void Buffer_Esc_Append(YO_BUFFER *bf, void *S, int len)
#ifdef _YO_BUFFER_BUILTIN
  {
    byte_t *q = S;
    byte_t *p = q;
    byte_t *E;
    
    if ( len < 0 ) 
      len = S?strlen(S):0;
    
    E = p + len;
    
    while ( p != E )
      {
        do 
          { 
            if ( *p < 30 || *p > 127 ||*p == '\\' ||  *p == '"' || *p == '\'' ) 
              break; 
            ++p; 
          } 
        while ( p != E );
        
        if ( q != p )
          Buffer_Append(bf,q,p-q);
        
        if ( p != E )
          {
            char *t; 
            Buffer_Fill_Append(bf,0,4);
            STRICT_REQUIRE( bf->count >= 5 );
            t = bf->chars + bf->count - 4;
            t[0] = '\\';
            t[1] = ((*p>>6)%8) + '0';
            t[2] = ((*p>>3)%8) + '0';            
            t[3] = (*p%8) + '0';          
          }
          
        q = ++p;
      }
  }
#endif
  ;
  
void Buffer_Quote_Append(YO_BUFFER *bf, void *S, int len, int brk)
#ifdef _YO_BUFFER_BUILTIN
  {
    byte_t *q = S;
    byte_t *p = q;
    byte_t *E;
    
486
487
488
489
490
491
492











493
494
495
496
497
#ifdef _YO_BUFFER_BUILTIN
  {
    YO_BUFFER *bf = Buffer_Init(count);
    if ( count )
      memcpy(bf->at,S,count);
    return bf;
  }











#endif
  ;

#endif /* C_once_29A1C0D6_2792_4035_8D0E_9DB1797A4120 */








>
>
>
>
>
>
>
>
>
>
>





529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#ifdef _YO_BUFFER_BUILTIN
  {
    YO_BUFFER *bf = Buffer_Init(count);
    if ( count )
      memcpy(bf->at,S,count);
    return bf;
  }
#endif
  ;

void Buffer_Swap(YO_BUFFER *a, YO_BUFFER *b)
#ifdef _YO_BUFFER_BUILTIN
  {
    YO_BUFFER tmp;
    memcpy(&tmp,a,sizeof(YO_BUFFER));
    memcpy(a,b,sizeof(YO_BUFFER));
    memcpy(b,&tmp,sizeof(YO_BUFFER));
  }
#endif
  ;

#endif /* C_once_29A1C0D6_2792_4035_8D0E_9DB1797A4120 */

Changes to datetime.hc.
193
194
195
196
197
198
199



















200
201
      pft->dwHighDateTime = (DWORD)(ll >> 32);
    }
# endif
    ;
  #define Timet_To_Largetime(T,Li) Timet_To_Filetime(T,(FILETIME*)(Li))
#endif /*__windoze*/




















#endif /* C_once_A998DD5F_3579_4977_B115_DCCE42423C49 */








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


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
      pft->dwHighDateTime = (DWORD)(ll >> 32);
    }
# endif
    ;
  #define Timet_To_Largetime(T,Li) Timet_To_Filetime(T,(FILETIME*)(Li))
#endif /*__windoze*/

#define YmdHMS_Curr_Datetime(Sep1,Sep2,Sep3) YmdHMS_Datetime(Current_Gmt_Datetime(),Sep1,Sep2,Sep3) 
#define YmdHMS_Curr_Local_Datetime(Sep1,Sep2,Sep3) YmdHMS_Datetime(Current_Local_Datetime(),Sep1,Sep2,Sep3) 
char *YmdHMS_Datetime(quad_t dt,char dt_sep1,char dt_sep2,char dt_sep3)
#ifdef _YO_DATETIME_BUILTIN
  {
    char sp1[2] = {dt_sep1,0};
    char sp2[2] = {dt_sep2,0};
    char sp3[2] = {dt_sep3,0};
    char *S = __Malloc(20);
    int L = sprintf(S,"%04d%s%02d%s%02d%s%02d%s%02d%s%02d",
      Dt_Year(dt),sp1,Dt_Mon(dt),sp1,Dt_Mday(dt),sp2,
      Dt_Hour(dt),sp3,Dt_Min(dt),sp3,Dt_Sec(dt));  
    STRICT_REQUIRE(L <= 19);
    S[L] = 0;
    return S;
  }
#endif
  ;
    
#endif /* C_once_A998DD5F_3579_4977_B115_DCCE42423C49 */

Added mysql.hc.












































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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

#include <mysql.h>
#include <mysqld_error.h>

#ifdef _LIBYOYO
#define _YO_MYSQL_BUILTIN
#endif

void Msql_free_results(MYSQL_RES *rset)
#ifdef _YO_MYSQL_BUILTIN
  {
    mysql_free_result(rset);
  }
#endif
  ;
 
#define __Mysql_Results(Con,Rset) \
  switch ( 0 ) while ( 1 ) \
    if ( 1 ) \
      goto YO_LOCAL_ID(Free_Rset); \
    else if ( 1 ) \
      case 0: \
        { \
          if (( Rset = mysql_store_result(Con) )) \
            { \
              Yo_JmpBuf_Push_Cs(Rset,(Yo_JMPBUF_Unlock)Msql_free_results); \
              goto YO_LOCAL_ID(Do_Code); \
            } \
          else \
            __Raise_Format(YO_ERROR_IO,(__yoTa("mySQL failed to acquire results: %s",0),mysql_error(Con))); \
        YO_LOCAL_ID(Free_Rset): \
          Yo_JmpBuf_Pop_Cs(Rset); \
          Msql_free_results(Rset); \
          break; \
        } \
    else \
      YO_LOCAL_ID(Do_Code): \

Changes to tests/xddb.c.
1


2
3

4

5
6
7
8
9
10
11
12
13




14
15
16
17
18
19
20



#define _LIBYOYO


#include "../xddbfs.hc"

#include "../prog.hc"
#include "../tesuite.hc"

YO_XDDB *Open_Xddb(int create_new)
  {
    char *db_source = Prog_Arguments_Count()?Prog_Argument(0):"xddb"; 
    YO_XDDB *xddb;
    
    if ( 0 ) ;




    else
      {
        xddb = Xddbfs_Open(db_source,create_new,XDDB_FORMAT_TEXT,0);
      }
      
    return xddb;
  }

>
>


>

>






|


>
>
>
>







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

#include <crtdbg.h>

#define _LIBYOYO

#include "../tcpip.hc"
#include "../xddbfs.hc"
#include "../xddbmsql.hc"
#include "../prog.hc"
#include "../tesuite.hc"

YO_XDDB *Open_Xddb(int create_new)
  {
    char *db_source = Prog_Arguments_Count()?Prog_Argument(0):"xddb"; 
    YO_XDDB *xddb = 0;
    
    if ( 0 ) ;
    else if ( Str_Starts_With(db_source,"mysql://") )
      {
        xddb = XddbMsql_Connect(db_source,"123qwe",XDDB_FORMAT_ZIPPED_TEXT,create_new);
      }
    else
      {
        xddb = Xddbfs_Open(db_source,create_new,XDDB_FORMAT_TEXT,0);
      }
      
    return xddb;
  }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
                                    Xddb_Source_String(xddb),
                                    Xddb_Format_String(xddb)));
                                    
        unique = Xddb_Build_Unique_Key();
        doc = Xdata_Init();
        Xnode_Value_Set_Str(&doc->root,"unique",unique);
        key = Xddb_Unique(xddb,doc);
        doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIS);
        if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
          __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
        Xddb_Delete(xddb,key);
        if ( Xddb_Has(xddb,key) )
          __Raise(YO_ERROR_TESUITE_FAIL,"document still present in store");
        
        Tesuite_Case_Successed();







|







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
                                    Xddb_Source_String(xddb),
                                    Xddb_Format_String(xddb)));
                                    
        unique = Xddb_Build_Unique_Key();
        doc = Xdata_Init();
        Xnode_Value_Set_Str(&doc->root,"unique",unique);
        key = Xddb_Unique(xddb,doc);
        doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIST);
        if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
          __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
        Xddb_Delete(xddb,key);
        if ( Xddb_Has(xddb,key) )
          __Raise(YO_ERROR_TESUITE_FAIL,"document still present in store");
        
        Tesuite_Case_Successed();
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
          {
            Tesuite_Case("open db/get document");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIS);
            if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
            Tesuite_Case_Successed();
          }

        __Auto_Release
          {







|







88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
          {
            Tesuite_Case("open db/get document");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIST);
            if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
            Tesuite_Case_Successed();
          }

        __Auto_Release
          {
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
          {
            Tesuite_Case("open db/get document with pfx");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIS);
            if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
            Tesuite_Case_Successed();
          }

        __Auto_Release
          {







|







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
          {
            Tesuite_Case("open db/get document with pfx");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIST);
            if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
            Tesuite_Case_Successed();
          }

        __Auto_Release
          {
179
180
181
182
183
184
185





186
187
188
189
190
191
192
193
194
195
            Tesuite_Case_Successed();
          }
      }
  }

int main(int argc, char **argv)
  {





    Prog_Init(argc,argv,"fs,ldb,sql3,msql",0);
    
    __Try_Abort
      {
        Tesuite_Init();
        Tesuite_Perform(test_0());
        Tesuite_Perform(test_1());
        Tesuite_Perform(test_2());
      }
  }







>
>
>
>
>
|






|


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
            Tesuite_Case_Successed();
          }
      }
  }

int main(int argc, char **argv)
  {
    int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
    tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
    tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
    _CrtSetDbgFlag( tmpFlag );

    Prog_Init(argc,argv,0,0);
    
    __Try_Abort
      {
        Tesuite_Init();
        Tesuite_Perform(test_0());
        Tesuite_Perform(test_1());
        //Tesuite_Perform(test_2());
      }
  }
Changes to xddb.hc.
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

#ifndef C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB
#define C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB

#include "mime.hc"
#include "xdata.hc"
#include "xdef.hc"


#ifdef _LIBYOYO
#define _YO_XDDB_BUILTIN
#endif

#ifdef _YO_XDDB_BUILTIN
# define _YO_XDDB_BUILTIN_CODE(Code) Code
# define _YO_XDDB_EXTERN 
#else
# define _YO_XDDB_BUILTIN_CODE(Code)
# define _YO_XDDB_EXTERN extern 
#endif

typedef struct _YO_XDDB
  {
    int doc_format;





  } YO_XDDB;

#define YO_XDDB_KEY_PROPERTY "$$key"
#define YO_XDDB_REV_PROPERTY "$$rev"

enum
  {
    XDDB_FORMAT_TEXT          = 'A',
    XDDB_FORMAT_BINARY        = 'B',

    XDDB_FORMAT_ZIPPED        = 'Z',
    YO_XDDB_MAX_KEYLEN        = 128,
    YO_XDDB_UUID_LENGTH       = 9,
    XDDB_OPEN_EXISTING        = 0,
    XDDB_OPEN_READONLY        = 1,
    XDDB_CREATE_IF_DSNT_EXIST = 2,
    XDDB_CREATE_NEW           = 3,
    XDDB_CREATE_ALWAYS        = 4,







>
















>
>
>
>
>









>
|







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

#ifndef C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB
#define C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB

#include "mime.hc"
#include "xdata.hc"
#include "xdef.hc"
#include "zlib.hc"

#ifdef _LIBYOYO
#define _YO_XDDB_BUILTIN
#endif

#ifdef _YO_XDDB_BUILTIN
# define _YO_XDDB_BUILTIN_CODE(Code) Code
# define _YO_XDDB_EXTERN 
#else
# define _YO_XDDB_BUILTIN_CODE(Code)
# define _YO_XDDB_EXTERN extern 
#endif

typedef struct _YO_XDDB
  {
    int doc_format;
    int esc_put: 1;
    int esc_get: 1;
    int encrypt: 1;
    void *encipher16;
    void *decipher16;
  } YO_XDDB;

#define YO_XDDB_KEY_PROPERTY "$$key"
#define YO_XDDB_REV_PROPERTY "$$rev"

enum
  {
    XDDB_FORMAT_TEXT          = 'A',
    XDDB_FORMAT_BINARY        = 'B',
    XDDB_FORMAT_ZIPPED_TEXT   = 'a',
    XDDB_FORMAT_ZIPPED_BINARY= 'b',
    YO_XDDB_MAX_KEYLEN        = 128,
    YO_XDDB_UUID_LENGTH       = 9,
    XDDB_OPEN_EXISTING        = 0,
    XDDB_OPEN_READONLY        = 1,
    XDDB_CREATE_IF_DSNT_EXIST = 2,
    XDDB_CREATE_NEW           = 3,
    XDDB_CREATE_ALWAYS        = 4,
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
void Xddb_Binary_Encode_Into(YO_BUFFER *bf, YO_XDATA *doc, int zipped)
#ifdef _YO_XDDB_BUILTIN 
  {
  }
#endif
  ;

YO_BUFFER *Xddb_Encode(YO_XDDB *xddb, YO_XDATA *doc)
#ifdef _YO_XDDB_BUILTIN 
  {
    char prefix[] = ".,.,.,.,_\n\n\n";
    YO_BUFFER *bf = Buffer_Init(0);
    
    Unsigned_To_Hex8(Xddb_Get_Revision(doc),prefix);
    prefix[8] = (char)xddb->doc_format;
    Buffer_Append(bf,prefix,12);
    
    if ( xddb->doc_format == XDDB_FORMAT_TEXT )

      Def_Format_Into(bf,&doc->root,0);

















    else if ( xddb->doc_format == XDDB_FORMAT_BINARY )
      ;//Xdata_Binary_Encode_Into(bf,doc,0);
    else if ( xddb->doc_format == XDDB_FORMAT_ZIPPED )
      ;//Xdata_Binary_Encode_Into(bf,doc,1);
    else
      __Raise(YO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));













      
    return bf;
  }
#endif
  ;

YO_XDATA *Xddb_Decode(YO_XDDB *xddb, byte_t *at,int count)
#ifdef _YO_XDDB_BUILTIN 
  {
    YO_XDATA *doc = 0;

    


    if ( count < 12 ) 
      __Raise(YO_ERROR_CORRUPTED,__yoTa("document is corrupted",0));
    














    switch ( at[8] )
      {
        case XDDB_FORMAT_TEXT:
          doc = Def_Parse_Str(at+9); /* Yes, 9! Not 12!! */
          break;














        case XDDB_FORMAT_BINARY:
        case XDDB_FORMAT_ZIPPED:
          //doc = Xdata_Binary_Decode(at+12,count-12);
          break;
        default:
          __Raise(YO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));
      }


    return doc;
  }
#endif
  ;

#define Xddb_Build_Unique_Key() __Pool(Xddb_Build_Unique_Key_Npl())
char *Xddb_Build_Unique_Key_Npl()







|


|

|
|

|


>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|



>
>
>
>
>
>
>
>
>
>
>
>
>










>

>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
>
>







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
void Xddb_Binary_Encode_Into(YO_BUFFER *bf, YO_XDATA *doc, int zipped)
#ifdef _YO_XDDB_BUILTIN 
  {
  }
#endif
  ;

YO_BUFFER *Xddb_Encode(YO_XDDB *xddb, YO_XDATA *doc, int revision)
#ifdef _YO_XDDB_BUILTIN 
  {
    char prefix[] = ".,.,.,.,_******\n";
    YO_BUFFER *bf = Buffer_Init(0);
    if ( !revision ) revision = Xddb_Get_Revision(doc);
    Unsigned_To_Hex8(revision,prefix);
    prefix[8] = (char)xddb->doc_format;
    Buffer_Append(bf,prefix,16);
    
    if ( xddb->doc_format == XDDB_FORMAT_TEXT )
      {
        Def_Format_Into(bf,&doc->root,0);
      }
    else if ( xddb->doc_format == XDDB_FORMAT_ZIPPED_TEXT )
      {
        int original;
        int compressed;
        YO_BUFFER *tmp = Buffer_Init(0);
        Def_Format_Into(tmp,&doc->root,0);
        original = tmp->count;
        /*Buffer_Reserve(bf,bf->count+original);
        Zlib_Compress()*/
        compressed = Zlib_Buffer_Compress(tmp);
        Buffer_Append(bf,tmp->at,tmp->count);
        if ( !compressed )
          bf->at[8] = XDDB_FORMAT_TEXT;
        else
          Unsigned_To_Hex4(original,bf->at+9);
      }
    else if ( xddb->doc_format == XDDB_FORMAT_BINARY )
      ;//Xdata_Binary_Encode_Into(bf,doc,0);
    else if ( xddb->doc_format == XDDB_FORMAT_ZIPPED_BINARY )
      ;//Xdata_Binary_Encode_Into(bf,doc,1);
    else
      __Raise(YO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));

    if ( xddb->esc_put || xddb->encrypt )
      {
        if ( xddb->encrypt )
          {
          }
        if ( xddb->esc_put ) __Auto_Release
          {
            YO_BUFFER *tmp = Buffer_Reserve(0,bf->count + bf->count/25);
            Buffer_Esc_Append(tmp,bf->at,bf->count);
            Buffer_Swap(tmp,bf);
          }
      }
      
    return bf;
  }
#endif
  ;

YO_XDATA *Xddb_Decode(YO_XDDB *xddb, byte_t *at,int count)
#ifdef _YO_XDDB_BUILTIN 
  {
    YO_XDATA *doc = 0;
    YO_BUFFER *bf = 0;
    
    __Auto_Ptr(doc)
      {
        if ( count < 16 ) 
          __Raise(YO_ERROR_CORRUPTED,__yoTa("document is corrupted",0));
        
        if ( xddb->esc_get || xddb->encrypt )
          {
            if ( xddb->encrypt )
              {
              }
            if ( xddb->esc_get )
              {
                bf = Buffer_Reserve(0,count);
                //Buffer_Unesc_Append(bf,at,count);
                at = bf->bytes;
                count = bf->count;
              }
          }
        
        switch ( at[8] )
          {
            case XDDB_FORMAT_TEXT:
              doc = Def_Parse_Str(at+16);
              break;
            case XDDB_FORMAT_ZIPPED_TEXT:
              {
                int zOk;
                long final_size = Hex4_To_Unsigned(at+9);
                bf = Buffer_Init(final_size);
                zOk = Zlib_Uncompress(bf->at,&final_size,at+16,count-16);
                if ( zOk != 0 )
                  __Raise_Format(YO_ERROR_DECOMPRESS_DATA,(__yoTa("failed to decompress document: %s",0),Zlib_Error(zOk)));
                /*doc = Def_Parse_Str(bf->at);
                bf = Buffer_Copy(at+16,count-16);
                Zlib_Buffer_Uncompress(bf,final_size);*/
                doc = Def_Parse_Str(bf->at);
                break;
              }
            case XDDB_FORMAT_BINARY:
            case XDDB_FORMAT_ZIPPED_BINARY:
              //doc = Xdata_Binary_Decode(at+16,count-16);
              break;
            default:
              __Raise(YO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));
          }
      }
      
    return doc;
  }
#endif
  ;

#define Xddb_Build_Unique_Key() __Pool(Xddb_Build_Unique_Key_Npl())
char *Xddb_Build_Unique_Key_Npl()
254
255
256
257
258
259
260

261
262
263
264
265
266
267
268
char *Xddb_Format_String(YO_XDDB *xddb)
#ifdef _YO_XDDB_BUILTIN 
  {
    switch ( xddb->doc_format )
      {
      case XDDB_FORMAT_TEXT:   return "text";
      case XDDB_FORMAT_BINARY: return "binary";

      case XDDB_FORMAT_ZIPPED: return "zipped";
      }
    return "unknown";
  }
#endif
  ;

char *Xddb_Key_Prefix(char *key)







>
|







325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
char *Xddb_Format_String(YO_XDDB *xddb)
#ifdef _YO_XDDB_BUILTIN 
  {
    switch ( xddb->doc_format )
      {
      case XDDB_FORMAT_TEXT:   return "text";
      case XDDB_FORMAT_BINARY: return "binary";
      case XDDB_FORMAT_ZIPPED_BINARY: return "zbinary";
      case XDDB_FORMAT_ZIPPED_TEXT: return "ztext";
      }
    return "unknown";
  }
#endif
  ;

char *Xddb_Key_Prefix(char *key)
Changes to xddbfs.hc.
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
        __Pfd_Lock(&xddb->jur_fd)
          bf = Xddbfs_Read_Id(xddb,coid,-1);
        
        if ( bf )
          doc = Xddb_Decode(&xddb->xddb,bf->at,bf->count);
      }
    
    if ( doc == XDDB_RAISE_IF_DSNT_EXSIS )
      __Raise_Format(YO_ERROR_DSNT_EXIST,(__yoTa("Xddbfs doesn't have document '%s'",0),key));
    return doc;
  }
#endif
  ;

int Xddbfs_Has(YO_XDDBFS *xddb, char *key)







|







298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
        __Pfd_Lock(&xddb->jur_fd)
          bf = Xddbfs_Read_Id(xddb,coid,-1);
        
        if ( bf )
          doc = Xddb_Decode(&xddb->xddb,bf->at,bf->count);
      }
    
    if ( doc == XDDB_RAISE_IF_DSNT_EXSIST )
      __Raise_Format(YO_ERROR_DSNT_EXIST,(__yoTa("Xddbfs doesn't have document '%s'",0),key));
    return doc;
  }
#endif
  ;

int Xddbfs_Has(YO_XDDBFS *xddb, char *key)
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
                  __Format(__yoTa("inconsistent revision of document '%s', store:%d != docu:%d",0),
                           coid,
                           revision,
                           Xddb_Get_Revision(doc)));              
            
            ++revision;
            Xddb_Set_Revision(doc,revision);
            bf = Xddb_Encode(&xddb->xddb,doc);
            Xddbfs_Write_Id(xddb,coid,bf->at,bf->count);
          }
          
        if ( pfx )
          Xddbfs_Pfx_Add_Key(xddb,pfx,key);
      }
      







|







375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
                  __Format(__yoTa("inconsistent revision of document '%s', store:%d != docu:%d",0),
                           coid,
                           revision,
                           Xddb_Get_Revision(doc)));              
            
            ++revision;
            Xddb_Set_Revision(doc,revision);
            bf = Xddb_Encode(&xddb->xddb,doc,0);
            Xddbfs_Write_Id(xddb,coid,bf->at,bf->count);
          }
          
        if ( pfx )
          Xddbfs_Pfx_Add_Key(xddb,pfx,key);
      }
      
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
#endif
  ;

void Xddbfs_Pfx_Add_Key(YO_XDDBFS *xddb, char *pfx, char *key)
  {
    __Auto_Release
      {
        YO_XDDBFS_SEQ *seq = Xddbfs_Seq_Open(xddb,pfx,XDDB_CREATE_IF_NOT_EXISTS);
        Xddbfs_Seq_Push_Key(seq,key);
      }
  }

YO_XDDB *Xddbfs_Open(char *path, int create_new, int format, int depth)
#ifdef _YO_XDDBFS_BUILTIN 
  {







|







1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
#endif
  ;

void Xddbfs_Pfx_Add_Key(YO_XDDBFS *xddb, char *pfx, char *key)
  {
    __Auto_Release
      {
        YO_XDDBFS_SEQ *seq = Xddbfs_Seq_Open(xddb,pfx,XDDB_CREATE_IF_DSNT_EXIST);
        Xddbfs_Seq_Push_Key(seq,key);
      }
  }

YO_XDDB *Xddbfs_Open(char *path, int create_new, int format, int depth)
#ifdef _YO_XDDBFS_BUILTIN 
  {
Changes to xddbmsql.hc.








































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_694F4096_7219_47D5_8C2F_7456AB482DDB
#define C_once_694F4096_7219_47D5_8C2F_7456AB482DDB

#include "yoyo.hc"
#include "xddb.hc"
#include "xddbsql_.hc"
#include "datetime.hc"
#include "url.hc"
#include "datetime.hc"

#ifdef _LIBYOYO
#define _YO_XDDBmSQL_BUILTIN
#endif

#ifdef _YO_XDDBmSQL_BUILTIN
#include "mysql.hc"
#endif
  
typedef struct _YO_XDDBmSQL
  {
    char *dbpfx;
    void *con;
  } YO_XDDBmSQL;

void YO_XDDBmSQL_Destruct(YO_XDDBmSQL *sqldb)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    free(sqldb->dbpfx);
    mysql_close(sqldb->con);
    __Destruct(sqldb);
  }
#endif
  ;
    
YO_BUFFER *XddbMsql_Query_Doc(YO_XDDBmSQL *sqldb, char *colid, char *docid)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    MYSQL_ROW row;
    MYSQL_RES *rset;
    YO_BUFFER *bf = 0;
   
    __Auto_Ptr(bf)
      { 
        if ( mysql_query(sqldb->con,__Format("SELECT body FROM %s_%s WHERE docid = '%s'",sqldb->dbpfx,colid,docid)) )
          {
            int err = mysql_errno(sqldb->con);
            if ( err != ER_NO_SUCH_TABLE )
              __Raise_Format(YO_ERROR_INVALID_PARAM,
                  (__yoTa("failed to retrive document: %s",0),mysql_error(sqldb->con)));      
          }
        else
          __Mysql_Results(sqldb->con,rset)
            {
              row = mysql_fetch_row(rset);
              if ( row )
                {
                  long *lengths = mysql_fetch_lengths(rset);
                  bf = Buffer_Init(lengths[0]);
                  memcpy(bf->at,row[0],bf->count);
                }
            }
      }
        
    return bf;
  }
#endif
  ;
     
YO_XDDBSQL_STATS XddbMsql_Stats_Doc(YO_XDDBmSQL *sqldb, char *colid, char *docid)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    YO_XDDBSQL_STATS stats;
    MYSQL_ROW row;
    MYSQL_RES *rset;
    
    memset(&stats,0,sizeof(stats));
    
    __Auto_Release
      { 
        if ( mysql_query(sqldb->con,__Format("SELECT revision,dtt FROM %s_%s WHERE docid = '%s'",sqldb->dbpfx,colid,docid)) )
          {
            int err = mysql_errno(sqldb->con);
            if ( err != ER_NO_SUCH_TABLE )
              __Raise_Format(YO_ERROR_INVALID_PARAM,
                  (__yoTa("failed to retrive document stats: %s",0),mysql_error(sqldb->con)));      
          }
        else
          __Mysql_Results(sqldb->con,rset)
            {
              row = mysql_fetch_row(rset);
              if ( row )
                {
                  stats.revision = Str_To_Int(row[0]);
                  stats.datetime = 0;
                  stats.f.exists = 1;
                }
            }
      }
        
    return stats;
  }
#endif
  ;

void XddbMsql_Create_Doc_Table(YO_XDDBmSQL *sqldb, char *colid)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    if ( mysql_query(sqldb->con,
      __Format("CREATE TABLE IF NOT EXISTS %s_%s ( docid char(%d) PRIMARY KEY, revision INTEGER, dtt VARCHAR(20), body BLOB)",
                sqldb->dbpfx,colid,XDDBSQL_DOC_ENCODED_ID_LEN)) )
      {
        __Raise_Format(YO_ERROR_IO,
            (__yoTa("failed to create collection: %s",0),mysql_error(sqldb->con)));      
      }
  }
#endif
  ;

void XddbMsql_Quote_Append(YO_BUFFER *bf, void *S, int len)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    byte_t *q = S;
    byte_t *p = q;
    byte_t *E;
    
    if ( len < 0 ) 
      len = S?strlen(S):0;
    
    E = p + len;
    
    while ( p != E )
      {
        do 
          { 
            if ( *p == '\\' || *p == '"' 
              || *p == '\'' || *p == 0 
              || *p == '\b' || *p == '\n'
              || *p == '\r' || *p == '\t'
              || *p == 26   || *p == '%' 
              || *p == '_' ) 
              break; 
            ++p; 
          } 
        while ( p != E );
        
        if ( q != p )
          Buffer_Append(bf,q,p-q);
        
        if ( p != E )
          {
            if ( !*p )             Buffer_Append(bf,"\\0",2);
            else if ( *p == '\n' ) Buffer_Append(bf,"\\n",2);
            else if ( *p == '\t' ) Buffer_Append(bf,"\\t",2);
            else if ( *p == '\r' ) Buffer_Append(bf,"\\r",2);
            else if ( *p == '\\' ) Buffer_Append(bf,"\\\\",2);
            else if ( *p == '"'  ) Buffer_Append(bf,"\\\"",2);
            else if ( *p == '\'' ) Buffer_Append(bf,"\\'",2);
            else if ( *p == '\b' ) Buffer_Append(bf,"\\b",2);
            else if ( *p == '%'  ) Buffer_Append(bf,"\\%",2);
            else if ( *p == '_'  ) Buffer_Append(bf,"\\_",2);
            else if ( *p == 26   ) Buffer_Append(bf,"\\Z",2);
            else PANICA("");
            ++p;
          }
          
        q = p;
      }
  }
#endif
  ;
  
int XddbMsql_Insert_Doc(YO_XDDBmSQL *sqldb, char *colid, char *docid, int newrev, void *data, int len )
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    int success = 0;
    __Auto_Release
      {
        char *dtt = YmdHMS_Curr_Datetime(0,0,0);
        YO_BUFFER *bf = Buffer_Grow_Reserve(0,len+len/25+128);
        Buffer_Printf(bf,"INSERT INTO %s_%s VALUES( '%s', %d, '%s', '",sqldb->dbpfx,colid,docid,newrev,dtt);
        XddbMsql_Quote_Append(bf,data,len);
        Buffer_Append(bf,"')",2);
      repeat:
        if ( mysql_real_query(sqldb->con,bf->at,bf->count) )
          {
            int err = mysql_errno(sqldb->con);
            if ( err != ER_NO_SUCH_TABLE )
              __Raise_Format(YO_ERROR_IO,
                  (__yoTa("failed to insert new document: %s",0),mysql_error(sqldb->con)));      
            XddbMsql_Create_Doc_Table(sqldb,colid);
            goto repeat;
          }
        else
          success = mysql_affected_rows(sqldb->con);
      }
    return success;
  }
#endif
  ;
  
int XddbMsql_Update_Doc(YO_XDDBmSQL *sqldb, char *colid, char *docid, int newrev, void *data, int len, int oldrev )
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    int success = 0;
    __Auto_Release
      {
        char *dtt = YmdHMS_Curr_Datetime(0,0,0);
        YO_BUFFER *bf = Buffer_Grow_Reserve(0,len+len/25+128);
        Buffer_Printf(bf,"UPDATE %s_%s SET revision = %d, dtt = '%s', body = '",sqldb->dbpfx,colid,newrev,dtt);
        XddbMsql_Quote_Append(bf,data,len);
        Buffer_Printf(bf,"' WHERE docid = '%s'",docid);
        if ( oldrev )
          Buffer_Printf(bf,"and revision = %d",oldrev);
        if ( mysql_real_query(sqldb->con,bf->at,bf->count) )
          {
            int err = mysql_errno(sqldb->con);
            if ( err != ER_NO_SUCH_TABLE )
              __Raise_Format(YO_ERROR_IO,
                  (__yoTa("failed to insert new document: %s",0),mysql_error(sqldb->con)));      
            // exists or table dsnt exist is not error, only unsuccesful operation
          }
        else
          success = mysql_affected_rows(sqldb->con);
      }
    return success;
  }
#endif
  ;
  
int XddbMsql_Delete_Doc(YO_XDDBmSQL *sqldb, char *colid, char *docid)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    int success = 0;
    __Auto_Release
      {
        if ( mysql_query(sqldb->con,__Format("DELETE FROM %s_%s WHERE docid = '%s'",sqldb->dbpfx,colid,docid)) )
          {
            int err = mysql_errno(sqldb->con);
            if ( err != ER_NO_SUCH_TABLE )
              __Raise_Format(YO_ERROR_IO,
                  (__yoTa("failed to insert new document: %s",0),mysql_error(sqldb->con)));      
            // exists or table dsnt exist is not error, only unsuccesful operation
          }
        else
          success = mysql_affected_rows(sqldb->con);
      }
    return success;
  }
#endif
  ;
    
// user@host:port/dbname

YO_XDDB *XddbMsql_Connect(char *source, char *pwd, int format, int kind)
#ifdef _YO_XDDBmSQL_BUILTIN
  {
    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,       YO_XDDBmSQL_Destruct},
        {Xddbsql_Query_Doc_OjMID, XddbMsql_Query_Doc},
        {Xddbsql_Stats_Doc_OjMID, XddbMsql_Stats_Doc},
        {Xddbsql_Delete_Doc_OjMID,XddbMsql_Delete_Doc},
        {Xddbsql_Update_Doc_OjMID,XddbMsql_Update_Doc},
        {Xddbsql_Insert_Doc_OjMID,XddbMsql_Insert_Doc},
        {0}
      };

    YO_XDDB *ret = 0;
  
    __Auto_Ptr(ret)
      {
        YO_XDDBmSQL *sqldb = __Object(sizeof(YO_XDDBmSQL),funcs);
        YO_URL *url = Url_Parse(source);
        char *dbp, *dbname = 0, *dbpfx = 0;

        dbp = url->query;
        while ( dbp && *dbp == '/' ) ++dbp;
        if ( dbp && *dbp )
          {
            dbpfx = Path_Basename(dbp);
            dbname = Path_Dirname(dbp);
            if ( !dbname ) { dbname = dbpfx; dbpfx = "xddb";} 
          }
        if ( !dbname )
          __Raise_Format(YO_ERROR_INVALID_PARAM,(__yoTa("mySQL source %s doesn't contain database name",0),source));      
        
        sqldb->con = mysql_init(0);
        sqldb->dbpfx = Str_Copy_Npl(dbpfx,-1);
        
        if ( !mysql_real_connect(sqldb->con,url->host,url->user,pwd,0,0,0,0) )
          __Raise_Format(YO_ERROR_IO,(__yoTa("failed to connect to %s: %s",0),source,mysql_error(sqldb->con)));

        mysql_autocommit(sqldb->con, 1);
        
        if ( kind == XDDB_CREATE_ALWAYS )
          {
            if ( mysql_query(sqldb->con,__Format("DROP DATABASE IF EXISTS %s",dbname))
               ||mysql_query(sqldb->con,__Format("CREATE DATABASE %s",dbname)) )
             __Raise_Format(YO_ERROR_IO,(__yoTa("failed to (re)create databsae %s: %s",0),dbname,mysql_error(sqldb->con)));
          }
        else if ( kind == XDDB_CREATE_NEW )
          {
            if ( mysql_query(sqldb->con,__Format("CREATE DATABASE %s",dbname)) )
             __Raise_Format(YO_ERROR_IO,(__yoTa("failed to create databsae %s: %s",0),dbname,mysql_error(sqldb->con)));
          }
        else if ( kind == XDDB_CREATE_IF_DSNT_EXIST )
          {
            if ( mysql_query(sqldb->con,__Format("CREATE DATABASE IF NOT EXISTS %s",dbname)) )
             __Raise_Format(YO_ERROR_IO,(__yoTa("failed to create databsae %s: %s",0),dbname,mysql_error(sqldb->con)));
          }
        else if ( kind == XDDB_OPEN_EXISTING || kind == XDDB_OPEN_READONLY )
          {
            ; /* nothing */
          }
        if ( mysql_select_db(sqldb->con,dbname) )
          __Raise_Format(YO_ERROR_IO,(__yoTa("failed to select databsae %s: %s",0),dbname,mysql_error(sqldb->con)));
        if ( mysql_query(sqldb->con,__Format("CREATE TABLE IF NOT EXISTS %s_XDDB_INFO ( name varchar(128) UNIQUE, value varchar(128) )",sqldb->dbpfx)) )
         __Raise_Format(YO_ERROR_IO,(__yoTa("failed to create INFO table: %s",0),mysql_error(sqldb->con)));
        if ( mysql_query(sqldb->con,__Format("SELECT value FROM %s_XDDB_INFO WHERE name = 'doc_format'",sqldb->dbpfx)) )
          __Raise_Format(YO_ERROR_IO,(__yoTa("failed to query doc format: %s",0),mysql_error(sqldb->con)));
        else
          {
            MYSQL_RES *rset;
            MYSQL_ROW row;
            __Mysql_Results(sqldb->con,rset)
              {
                if (( row = mysql_fetch_row(rset) ))
                  format = Str_To_Int(*row);
                else
                  if ( mysql_query(sqldb->con,__Format("INSERT INTO %s_XDDB_INFO VALUES ('doc_format','%d')",sqldb->dbpfx,format)) )
                    __Raise_Format(YO_ERROR_IO,(__yoTa("failed to set doc format: %s",0),mysql_error(sqldb->con)));
              }
          }
          
        ret = Xddbsql_Init(sqldb,format,source);
      }
      
    return ret;
  }
#endif
  ;
  
#endif /* C_once_694F4096_7219_47D5_8C2F_7456AB482DDB */
Added xddbsql_.hc.






















































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_7B87C216_9DAA_4229_9B48_28562A89CD0D
#define C_once_7B87C216_9DAA_4229_9B48_28562A89CD0D

#include "xddb.hc"
#include "datetime.hc"
#include "sha1.hc"

#ifdef _LIBYOYO
#define _YO_XDDBSQL_BUILTIN
#endif

#ifdef _YO_XDDBSQL_BUILTIN
# define _YO_XDDBSQL_BUILTIN_CODE(Code) Code
# define _YO_XDDBSQL_EXTERN 
#else
# define _YO_XDDBSQL_BUILTIN_CODE(Code)
# define _YO_XDDBSQL_EXTERN extern 
#endif

enum
  {
    XDDBSQL_DOC_ID_LEN = 20,
    XDDBSQL_COL_ID_LEN = 20,
    XDDBSQL_DOC_ENCODED_ID_LEN = (20*8+4)/5,
    XDDBSQL_COL_ENCODED_ID_LEN = (20*8+4)/5,
  };

typedef struct _YO_XDDBSQL_STATS
  {
    quad_t datetime;
    int revision;
    struct {
      int exists : 1;
    } f;
  } YO_XDDBSQL_STATS;

typedef struct _YO_XDDBSQL
  {
    YO_XDDB xddb;
    void *sqldb;
    char *source;
  } YO_XDDBSQL;

void YO_XDDBSQL_Destruct(YO_XDDBSQL *xddb)
  {
    free(xddb->source);
    __Unrefe(xddb->sqldb);
    __Destruct(xddb);
  };

_YO_XDDBSQL_EXTERN char Xddbsql_Query_Doc_OjMID[] _YO_XDDBSQL_BUILTIN_CODE( = "xddbsql_query_doc/@**"); 
YO_BUFFER *Xddbsql_Query_Doc(void *sqldb, char *colid, char *docid)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    return ((void*(*)(void*,void*,void*))Yo_Find_Method_Of(&sqldb,Xddbsql_Query_Doc_OjMID,YO_RAISE_ERROR))
        (sqldb,colid,docid);
  }
#endif
  ;
  
_YO_XDDBSQL_EXTERN char Xddbsql_Stats_Doc_OjMID[] _YO_XDDBSQL_BUILTIN_CODE( = "xddbsql_query_stats/@**"); 
YO_XDDBSQL_STATS Xddbsql_Stats_Doc(void *sqldb, char *colid, char *docid)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    return ((YO_XDDBSQL_STATS(*)(void*,void*,void*))Yo_Find_Method_Of(&sqldb,Xddbsql_Stats_Doc_OjMID,YO_RAISE_ERROR))
        (sqldb,colid,docid);
  }
#endif
  ;

_YO_XDDBSQL_EXTERN char Xddbsql_Delete_Doc_OjMID[] _YO_XDDBSQL_BUILTIN_CODE( = "xddbsql_delete_doc/@**"); 
int Xddbsql_Delete_Doc(void *sqldb, char *colid, char *docid)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    return ((int(*)(void*,void*,void*))Yo_Find_Method_Of(&sqldb,Xddbsql_Delete_Doc_OjMID,YO_RAISE_ERROR))
        (sqldb,colid,docid);
  }
#endif
  ;

_YO_XDDBSQL_EXTERN char Xddbsql_Update_Doc_OjMID[] _YO_XDDBSQL_BUILTIN_CODE( = "xddbsql_update_doc/@**i*ii"); 
int Xddbsql_Update_Doc(void *sqldb, char *colid, char *docid, int newrev, void *data, int len, int oldrev )
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    return ((int(*)(void*,void*,void*,int,void*,int,int))Yo_Find_Method_Of(&sqldb,Xddbsql_Update_Doc_OjMID,YO_RAISE_ERROR))
        (sqldb,colid,docid,newrev,data,len,oldrev);
  }
#endif
  ;

_YO_XDDBSQL_EXTERN char Xddbsql_Insert_Doc_OjMID[] _YO_XDDBSQL_BUILTIN_CODE( = "xddbsql_insert_doc/@**i*i"); 
int Xddbsql_Insert_Doc(void *sqldb, char *colid, char *docid, int newrev, void *data, int len )
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    return ((int(*)(void*,void*,void*,int,void*,int))Yo_Find_Method_Of(&sqldb,Xddbsql_Insert_Doc_OjMID,YO_RAISE_ERROR))
        (sqldb,colid,docid,newrev,data,len);
  }
#endif
  ;

char *Xddbsql_Doc_ID(YO_XDDBSQL *xddb, char *key)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    byte_t sha1[20];
    Sha1_Digest(key,strlen(key),sha1);
    return Str_5bit_Encode_Upper(sha1,20);
  }
#endif
  ;
    
char *Xddbsql_Col_ID(YO_XDDBSQL *xddb, char *key)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    byte_t sha1[20] = {0};
    char *pfx = Xddb_Key_Prefix(key);
    if ( pfx )
      Sha1_Digest(pfx,strlen(pfx),sha1);
    return Str_5bit_Encode_Upper(sha1,20);
  }
#endif
  ;
    
YO_XDATA *Xddbsql_Get(YO_XDDBSQL *xddb, char *key, YO_XDATA *dflt)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    YO_XDATA *doc = dflt;
    
    __Auto_Ptr(doc)
      {
        char *doc_id = Xddbsql_Doc_ID(xddb,key);
        char *col_id = Xddbsql_Col_ID(xddb,key);
        YO_BUFFER *bf = Xddbsql_Query_Doc(xddb->sqldb,col_id,doc_id);
        
        if ( bf )
          doc = Xddb_Decode(&xddb->xddb,bf->at,bf->count);
      }
    
    if ( doc == XDDB_RAISE_IF_DSNT_EXSIST )
      __Raise_Format(YO_ERROR_DSNT_EXIST,(__yoTa("Xddb doesn't have document '%s'",0),key));
    return doc;
  }
#endif
  ;

int Xddbsql_Has(YO_XDDBSQL *xddb, char *key)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    int exists = 0;
    __Auto_Release
      {
        char *doc_id = Xddbsql_Doc_ID(xddb,key);
        char *col_id = Xddbsql_Col_ID(xddb,key);
        YO_XDDBSQL_STATS stats = Xddbsql_Stats_Doc(xddb->sqldb,col_id,doc_id);
        exists = stats.f.exists;
      }
    return exists;
  }
#endif
  ;

void Xddbsql_Delete(YO_XDDBSQL *xddb, char *key)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    __Auto_Release
      {
        char *doc_id = Xddbsql_Doc_ID(xddb,key);
        char *col_id = Xddbsql_Col_ID(xddb,key);
        Xddbsql_Delete_Doc(xddb->sqldb,col_id,doc_id);
      }
  }
#endif
  ;

int Xddbsql_Store(YO_XDDBSQL *xddb, YO_XDATA *doc, int strict_revision)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    int new_revision;
    
    __Auto_Release
      {
        YO_BUFFER *bf;
        YO_XDDBSQL_STATS stats;
        void *sqldb = xddb->sqldb;
        char *key, *doc_id, *col_id;
        int success;
        key = Xnode_Value_Get_Str(&doc->root,YO_XDDB_KEY_PROPERTY,0);
        
        if ( !key )
          __Raise_Format(YO_ERROR_ILLFORMED,
             (__yoTa("%s property doesn't exist or has invalid value",0),
                     YO_XDDB_KEY_PROPERTY));
        
        doc_id = Xddbsql_Doc_ID(xddb,key);
        col_id = Xddbsql_Col_ID(xddb,key);
        stats  = Xddbsql_Stats_Doc(xddb->sqldb,col_id,doc_id);
        
        if ( strict_revision )
          if ( stats.f.exists && stats.revision != Xddb_Get_Revision(doc) )
            inconsist:
              __Raise(YO_ERROR_INCONSISTENT,
                __Format(__yoTa("inconsistent revision of document '%s', store:%d != docu:%d",0),
                         key,
                         stats.revision,
                         Xddb_Get_Revision(doc)));              
        
        new_revision = ++stats.revision;
        bf = Xddb_Encode(&xddb->xddb,doc,new_revision);
        
        if ( stats.f.exists )
          success = Xddbsql_Update_Doc(xddb->sqldb, col_id, doc_id, new_revision, bf->at, bf->count, stats.revision );         
        else
          success = Xddbsql_Insert_Doc(xddb->sqldb, col_id, doc_id, new_revision, bf->at, bf->count );         
      
        if ( !success ) // revision inconsistent
          {
            stats = Xddbsql_Stats_Doc(xddb->sqldb,col_id,doc_id);
            if ( stats.f.exists && stats.revision != Xddb_Get_Revision(doc) )
              goto inconsist;
            else
              __Raise(YO_ERROR_ALREADY_EXISTS,
                __Format(__yoTa("document '%s' already exists or couldn´t be updated, docu:%d",0),
                         key,
                         stats.revision));              
          }
          
        Xddb_Set_Revision(doc,new_revision);
      }
      
    return new_revision;
  }
#endif
  ;

char *Xddbsql_Source_String(YO_XDDBSQL *xddb)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    return xddb->source;
  } 
#endif 
  ;

YO_XDDB *Xddbsql_Init(void *sqldb, int format, char *source)
#ifdef _YO_XDDBSQL_BUILTIN 
  {
    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,       YO_XDDBSQL_Destruct},
        {Xddb_Delete_OjMID,       Xddbsql_Delete},
        {Xddb_Get_OjMID,          Xddbsql_Get},
        {Xddb_Has_OjMID,          Xddbsql_Has},
        {Xddb_Store_OjMID,        Xddbsql_Store},
      /*{Xddb_Strm_Open_OjMID,    Xddbsql_Strm_Open},
        {Xddb_Strm_Create_OjMID,  Xddbsql_Strm_Create},
        {Xddb_Strm_Delete_OjMID,  Xddbsql_Strm_Delete},
        {Xddb_Seq_Open_OjMID,     Xddbsql_Seq_Open},
        {Xddb_Seq_Delete_OjMID,   Xddbsql_Seq_Delete},*/
        {Xddb_Source_String_OjMID,Xddbsql_Source_String},
        {0}
      };

    YO_XDDBSQL *xddb = __Object(sizeof(YO_XDDBSQL),funcs);
    xddb->sqldb  = __Refe(sqldb);
    xddb->source = Str_Copy_Npl(source,-1);
    xddb->xddb.doc_format = format;
    return &xddb->xddb;
  }
#endif
  ;

#endif /* C_once_7B87C216_9DAA_4229_9B48_28562A89CD0D */
Added zlib.hc.


































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_EB7941BA_38FE_4713_AFDA_5DB7AC656213
#define C_once_EB7941BA_38FE_4713_AFDA_5DB7AC656213

#include "yoyo.hc"
#include "buffer.hc"

#ifdef _LIBYOYO
# define _YO_ZLIB_BUILTIN
#endif

#ifdef _YO_ZLIB_BUILTIN
/*# include <zlib.h>*/

enum 
  {
    ZLIB_OK           = 0,
    ZLIB_STREAM_END   = 1,
    ZLIB_NEED_DICT    = 2,
    ZLIB_ERRNO        = -1,
    ZLIB_STREAM_ERROR = -2,
    ZLIB_DATA_ERROR   = -3,
    ZLIB_MEM_ERROR    = -4,
    ZLIB_BUF_ERROR    = -5,
    ZLIB_VERSION_ERROR= -6,
  };

#endif

char *Zlib_Error(int zOk)
#ifdef _YO_ZLIB_BUILTIN
  {
    switch ( zOk )
      {
        case ZLIB_OK:            return "succeeded";
        case ZLIB_STREAM_END:    return "Z_STREAM_END";
        case ZLIB_NEED_DICT:     return "Z_NEED_DICT";
        case ZLIB_ERRNO:         return "Z_ERRNO";
        case ZLIB_STREAM_ERROR:  return "Z_STREAM_ERROR";
        case ZLIB_DATA_ERROR:    return "Z_DATA_ERROR";
        case ZLIB_MEM_ERROR:     return "Z_MEM_ERROR";
        case ZLIB_BUF_ERROR:     return "Z_BUF_ERROR";
        case ZLIB_VERSION_ERROR: return "Z_VERSION_ERROR";
      }
    return "unknown error";
  }
#endif
  ;
  
extern int compress(void *dst, long *dst_len, void *src, long src);
extern int uncompress(void *dst, long *dst_len, void *src, long src);

int Zlib_Compress(void *dst, long *dst_len, void *src, long src_len)
#ifdef _YO_ZLIB_BUILTIN
  {
    return compress(dst,dst_len,src,src_len);
  }
#endif
  ;
  
int Zlib_Uncompress(void *dst, long *dst_len, void *src, long src_len)
#ifdef _YO_ZLIB_BUILTIN
  {
    return uncompress(dst,dst_len,src,src_len);
  }
#endif
  ;
  
int Zlib_Buffer_Compress(YO_BUFFER *bf)
#ifdef _YO_ZLIB_BUILTIN
  {
    int succeded = 0;
    __Auto_Release 
      {
        ulong_t tmp_len = bf->count;
        YO_BUFFER *tmp = Buffer_Init(bf->count); 
        int zOk = compress(tmp->at,&tmp_len,bf->at,bf->count);
        if ( zOk != ZLIB_OK && zOk != ZLIB_BUF_ERROR )
          __Raise_Format(YO_ERROR_INCONSISTENT,("Zlib failed to compress buffer: %s", Zlib_Error(zOk)) );
        if ( zOk == ZLIB_OK )
          {
            tmp->count = tmp_len;
            Buffer_Swap(tmp,bf);
            succeded = tmp_len;
          }
      }
    return succeded;
  }
#endif
  ;
  
void Zlib_Buffer_Uncompress(YO_BUFFER *bf, int final_size)
#ifdef _YO_ZLIB_BUILTIN
  {
    __Auto_Release 
      {
        int zOk;
        ulong_t tmp_len;
        YO_BUFFER *tmp = Buffer_Init(final_size?final_size:bf->count*3); 
     repeat:
        tmp_len = tmp->count;
        zOk = uncompress(tmp->at,&tmp_len,bf->at,bf->count);
        if ( zOk != ZLIB_OK && zOk != ZLIB_BUF_ERROR )
          __Raise_Format(YO_ERROR_INCONSISTENT,("Zlib failed to uncompress buffer: %s", Zlib_Error(zOk)) );
        if ( zOk == ZLIB_BUF_ERROR )
          {          
            Buffer_Resize(tmp,tmp->count+bf->count);
            goto repeat;
          }
        Buffer_Swap(tmp,bf);
      }
  }
#endif
  ;
  
#endif /* C_once_EB7941BA_38FE_4713_AFDA_5DB7AC656213 */