Unnamed Fossil Project

Check-in [ddb10dc9ab]
Login

Check-in [ddb10dc9ab]

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

Overview
Comment:xddb noSQL database layer
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ddb10dc9ab2e84ba1dfef0e190c6adfb9c707054
User & Date: monster 2012-07-23 07:30:42.754
Context
2012-07-23
07:39
CGI support check-in: c9b15f33ab user: monster tags: trunk
07:30
xddb noSQL database layer check-in: ddb10dc9ab user: monster tags: trunk
02:57
(no comment) check-in: 0543846e85 user: monster tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to datetime.hc.
50
51
52
53
54
55
56

57
58
59
60
61
62
63
#define _YOYO_DATETIME_BUILTIN
#endif

#include "yoyo.hc"

typedef quad_t datetime_t;


#define Get_Curr_Datetime()    Current_Gmt_Datetime()
#define Get_Posix_Datetime(Dt) Timet_Of_Datetime(Dt)
#define Get_Gmtime_Datetime(T) Gmtime_Datetime(T)
#define Get_System_Useconds()  System_Useconds()
#define Get_System_Millis()    (Get_System_Useconds()/1000)

#define System_Millis() (System_Useconds()/1000)







>







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#define _YOYO_DATETIME_BUILTIN
#endif

#include "yoyo.hc"

typedef quad_t datetime_t;

#define Get_Curr_Date()        ((uint_t)(Current_Gmt_Datetime()>>32))
#define Get_Curr_Datetime()    Current_Gmt_Datetime()
#define Get_Posix_Datetime(Dt) Timet_Of_Datetime(Dt)
#define Get_Gmtime_Datetime(T) Gmtime_Datetime(T)
#define Get_System_Useconds()  System_Useconds()
#define Get_System_Millis()    (Get_System_Useconds()/1000)

#define System_Millis() (System_Useconds()/1000)
Changes to file.hc.
595
596
597
598
599
600
601






602
603
604
605
606
607
608
      #endif
      }
      
    return L;
  }
#endif
  ;







void File_Unlink(char *name, int force)
#ifdef _YOYO_FILE_BUILTIN
  {
    YOYO_FILE_STATS st;
    int i, err = 0;
    







>
>
>
>
>
>







595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
      #endif
      }
      
    return L;
  }
#endif
  ;

#ifdef __windoze
#define utf8_unlink(S) _wunlink(Str_Utf8_To_Unicode(S));
#else
#define utf8_unlink(S) unlink(S)
#endif

void File_Unlink(char *name, int force)
#ifdef _YOYO_FILE_BUILTIN
  {
    YOYO_FILE_STATS st;
    int i, err = 0;
    
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
              {
                YOYO_ARRAY *L = File_List_Directory(name,0);
                for ( i = 0; i < L->count; ++i )
                  File_Unlink(Path_Join(name,L->at[i]),force);
                Delete_Directory(name);
              }
            else
              err =
              #ifdef __windoze
                _wunlink(Str_Utf8_To_Unicode(name));
              #else
                unlink(name);
              #endif
          }
      
        if ( err < 0 )
          File_Check_Error("unlink",0,name,1); 
      }
  }
#endif







<
<
<
<
|
<







622
623
624
625
626
627
628




629

630
631
632
633
634
635
636
              {
                YOYO_ARRAY *L = File_List_Directory(name,0);
                for ( i = 0; i < L->count; ++i )
                  File_Unlink(Path_Join(name,L->at[i]),force);
                Delete_Directory(name);
              }
            else




              err = utf8_unlink(name);

          }
      
        if ( err < 0 )
          File_Check_Error("unlink",0,name,1); 
      }
  }
#endif
Changes to mime.hc.
50
51
52
53
54
55
56

57
58

59
60
61
62
63
64
65
#include "string.hc"

#ifdef _LIBYOYO
#define _YOYO_MIME_BUILTIN
#endif

#ifdef _YOYO_MIME_BUILTIN

  #define _YOYO_MIME_EXTERN 
#else

  #define _YOYO_MIME_EXTERN extern
#endif

enum
  {
    YOYO_MIME_NONE  = __FOUR_CHARS('`','`','`','`'),
    YOYO_MIME_OCTS  = __FOUR_CHARS('O','C','T','S'),







>


>







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "string.hc"

#ifdef _LIBYOYO
#define _YOYO_MIME_BUILTIN
#endif

#ifdef _YOYO_MIME_BUILTIN
  #define _YOYO_MIME_BUILTIN_CODE(Code) Code
  #define _YOYO_MIME_EXTERN 
#else
  #define _YOYO_MIME_BUILTIN_CODE(Code)
  #define _YOYO_MIME_EXTERN extern
#endif

enum
  {
    YOYO_MIME_NONE  = __FOUR_CHARS('`','`','`','`'),
    YOYO_MIME_OCTS  = __FOUR_CHARS('O','C','T','S'),
249
250
251
252
253
254
255





256
257
258
        case YOYO_MIME_EXE:  return "application/x-msdownload";
        case YOYO_MIME_JSON: return "application/json";
      }
    return "application/octet-stream";
  }
#endif
  ;





  
#endif /* C_once_E5DC0CBD_6EF6_4E8C_A4F5_CF78FA971011 */








>
>
>
>
>



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
        case YOYO_MIME_EXE:  return "application/x-msdownload";
        case YOYO_MIME_JSON: return "application/json";
      }
    return "application/octet-stream";
  }
#endif
  ;

_YOYO_MIME_EXTERN char Oj_Mimetype_Of_OjMID[] _YOYO_MIME_BUILTIN_CODE( = "mimetype_of/@"); 
int Oj_Mimetype_Of(void *o) _YOYO_MIME_BUILTIN_CODE(
  { return ((int(*)(void*,void*))Yo_Find_Method_Of(&o,Oj_Mimetype_Of_OjMID,YO_RAISE_ERROR))
        (o); });
  
#endif /* C_once_E5DC0CBD_6EF6_4E8C_A4F5_CF78FA971011 */

Added xddb.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

/*

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_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB
#define C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB

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

#ifdef _LIBYOYO
#define _YOYO_XDDB_BUILTIN
#endif

#ifdef _YOYO_XDDB_BUILTIN
# define _YOYO_XDDB_BUILTIN_CODE(Code) Code
# define _YOYO_XDDB_EXTERN 
#else
# define _YOYO_XDDB_BUILTIN_CODE(Code)
# define _YOYO_XDDB_EXTERN extern 
#endif

typedef struct _YOYO_XDDB
  {
    int doc_format;
  } YOYO_XDDB;

enum
  {
    YOYO_XDDB_DOC_TEXT   = 'A',
    YOYO_XDDB_DOC_BINARY = 'B',
    YOYO_XDDB_DOC_ZIPPED = 'Z',
    YOYO_XDDB_MAX_KEYLEN = 128,
    YOYO_XDDB_UUID_LENGTH = 9,
  };

char *Xddb_Get_Key(YOYO_XDATA *doc)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    return Xnode_Value_Get_Str(&doc->root,"$key$",0);
  }
#endif
  ;
  
int Xddb_Get_Revision(YOYO_XDATA *doc)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    return Xnode_Value_Get_Int((YOYO_XNODE*)doc,"$rev$",0);
  }
#endif
  ;
  
void Xddb_Set_Revision(YOYO_XDATA *doc, int rev)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    Xnode_Value_Set_Int((YOYO_XNODE*)doc,"$rev$",rev);
  }
#endif
  ;

YOYO_XDATA *Xddb_Binary_Decode(byte_t *at,int count,int zipped)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    YOYO_XDATA *doc = Xdata_Init();
    return doc;
  }
#endif
  ;

void Xddb_Binary_Encode_Into(YOYO_BUFFER *bf, YOYO_XDATA *doc, int zipped)
#ifdef _YOYO_XDDB_BUILTIN 
  {
  }
#endif
  ;

YOYO_BUFFER *Xddb_Encode(YOYO_XDDB *xddb, YOYO_XDATA *doc)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    char prefix[] = ".,.,.,.,_\n\n\n";
    YOYO_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 == YOYO_XDDB_DOC_TEXT )
      Def_Format_Into(bf,&doc->root,0);
    else if ( xddb->doc_format == YOYO_XDDB_DOC_BINARY )
      Xddb_Binary_Encode_Into(bf,doc,0);
    else if ( xddb->doc_format == YOYO_XDDB_DOC_ZIPPED )
      Xddb_Binary_Encode_Into(bf,doc,1);
    else
      __Raise(YOYO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));
      
    return bf;
  }
#endif
  ;

YOYO_XDATA *Xddb_Decode(YOYO_XDDB *xddb, byte_t *at,int count)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    YOYO_XDATA *doc = 0;
    
    if ( count < 12 ) 
      __Raise(YOYO_ERROR_CORRUPTED,__yoTa("document is corrupted",0));
    
    switch ( at[8] )
      {
        case YOYO_XDDB_DOC_TEXT:
          doc = Def_Parse_Str(at+9); /* Yes, 9! Not 12!! */
          break;
        case YOYO_XDDB_DOC_BINARY:
          doc = Xdata_Co_Binary_Decode(at+12,count-12,0);
          break;
        case YOYO_XDDB_DOC_ZIPPED:
          doc = Xdata_Co_Binary_Decode(at+12,count-12,1);
          break;
        default:
          __Raise(YOYO_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()
#ifdef _YOYO_XDDB_BUILTIN 
  {
    int pid = getpid();
    time_t tmx = time(0);
    double clo = clock();
    
    byte_t uuid[YOYO_XDATA_CO_UUID_LENGTH] = {0,0,0,0,0,0,0,0}; /* 8 bytes garanty */
    char   out[(YOYO_XDATA_CO_UUID_LENGTH*8+5)/6 + 3 ] = {'=','=',0};
    
    Unsigned_To_Two(pid,uuid);
    Unsigned_To_Four((uint_t)tmx,uuid+2);
    Unsigned_To_Two((uint_t)((clo/CLOCKS_PER_SEC)*10000),uuid+6);
    System_Random(uuid+6,sizeof(uuid)-6);
    Str_Xbit_Encode(uuid,sizeof(uuid)*8,6,Str_6bit_Encoding_Table,out+2);
    
    STRICT_REQUIRE( out[sizeof(out)-1] == 0 );
    
    return __Memcopy_Npl(out,sizeof(out));
  }
#endif
  ;

_YOYO_XDDB_EXTERN char Xddb_Delete_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_delete/@*"); 
void Xddb_Delete(void *xddb, char *key) _YOYO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Delete_OjMID,YO_RAISE_ERROR))
        (xddb,key); });

_YOYO_XDDB_EXTERN char Xddb_Get_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_get/@**"); 
void *Xddb_Get(void *xddb, char *key, YOYO_XDATA *dflt) _YOYO_XDDB_BUILTIN_CODE(
  { return ((void*(*)(void*,void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Get_OjMID,YO_RAISE_ERROR))
        (xddb,key,dflt); });

_YOYO_XDDB_EXTERN char Xddb_Store_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_store/@*i"); 
int Xddb_Store(void *xddb, YOYO_XDATA *doc, int strict_revision) _YOYO_XDDB_BUILTIN_CODE(
  { return ((int(*)(void*,void*,int))Yo_Find_Method_Of(&xddb,Xddb_Store_OjMID,YO_RAISE_ERROR))
        (xddb,doc,strict_revision); });

#define Xddb_Update(Xddb,Doc) Xddb_Store(Xddb,Doc,1)
#define Xddb_Unique(Xddb,Doc) Xddb_Overwrite(Xddb,Doc,0)

int Xddb_Overwrite(YOYO_XDDB *xddb, YOYO_XDATA *doc, char *key)
#ifdef _YOYO_XDDB_BUILTIN 
  {
    if ( key )
      Xnode_Value_Set_Str(&doc->root,"$key$",key);
    else if ( !Xnode_Value_Get_Str(&doc->root,"$key$",0) )
      {
        char *k = Xddb_Build_Unique_Key_Npl();
        Xnode_Value_Put_Str(&doc->root,"$key$",k);
      }
    return Xddb_Store(xddb,doc,0);
  }
#endif
  ;

_YOYO_XDDB_EXTERN char Xddb_Strm_Cancel_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_strm_cancel/@"); 
void Xddb_Strm_Cancel(void *strm) _YOYO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*))Yo_Find_Method_Of(&strm,Xddb_Strm_Cancel_OjMID,YO_RAISE_ERROR))
        (strm); });

_YOYO_XDDB_EXTERN char Xddb_Strm_Commit_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_strm_commit/@*i"); 
char *Xddb_Strm_Commit(void *strm, char *key, int overwrite) _YOYO_XDDB_BUILTIN_CODE(
  { return ((char*(*)(void*,void*,int))Yo_Find_Method_Of(&strm,Xddb_Strm_Commit_OjMID,YO_RAISE_ERROR))
        (strm,key,overwrite); });

#define Xddb_Strm_Commit_Overwrite(Strm,Key) Xddb_Strm_Commit(Strm,Key,1)
#define Xddb_Strm_Commit_New(Strm,Key) Xddb_Strm_Commit(Strm,Key,0)
#define Xddb_Strm_Commit_Unique(Strm) Xddb_Strm_Commit(Strm,0,0)

_YOYO_XDDB_EXTERN char Xddb_Strm_Open_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_strm_open/@*"); 
void *Xddb_Strm_Open(void *xddb, char *key) _YOYO_XDDB_BUILTIN_CODE(
  { return ((void*(*)(void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Strm_Open_OjMID,YO_RAISE_ERROR))
        (xddb,key); });

_YOYO_XDDB_EXTERN char Xddb_Strm_Create_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_strm_create/@i"); 
void *Xddb_Strm_Create(void *xddb, int mime) _YOYO_XDDB_BUILTIN_CODE(
  { return ((void*(*)(void*,int))Yo_Find_Method_Of(&xddb,Xddb_Strm_Create_OjMID,YO_RAISE_ERROR))
        (xddb,mime); });

_YOYO_XDDB_EXTERN char Xddb_Strm_Delete_OjMID[] _YOYO_XDDB_BUILTIN_CODE( = "xddb_strm_delete/@*"); 
void Xddb_Strm_Delete(void *xddb,char *key) _YOYO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Strm_Delete_OjMID,YO_RAISE_ERROR))
        (xddb,key); });

#endif /* C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB */


Added xddbfs.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730

/*

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_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF
#define C_once_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF

#include "xddb.hc"
#include "file.hc"
#include "datetime.hc"

#ifdef _LIBYOYO
#define _YOYO_XDDBFS_BUILTIN
#endif

typedef struct _YOYO_XDDBFS
  {
    YOYO_XDDB xddb;
    char *basepath;
    int  jur_fd;
    int  fs_depth;
  } YOYO_XDDBFS;

void YOYO_XDDBFS_Destruct(YOYO_XDDBFS *xddb)
  {
    if ( xddb->jur_fd >= 0 ) close(xddb->jur_fd);
    free(xddb->basepath);
    __Destruct(xddb);
  };

typedef struct _YOYO_XDDBFS_STREAM
  {
    YOYO_XDDBFS *xddb;
    char *key;
    char *tmp_path;
    int  mimetype;
    int  length;
    int  finished;
    int  fd;
    int  curpos;
  } YOYO_XDDBFS_STREAM;
  
typedef struct _YOYO_XDDBFS_STREAM_HEADER
  {
    char cr;
    char nl;
    char mimetype[4];
    char length[8];
    char lastacs[8];
    char created[8];
    char compressed;
    char keylen[2];
    char key[YOYO_XDDB_MAX_KEYLEN];
    char nulterm;
  } YOYO_XDDBFS_STREAM_HEADER;

void YOYO_XDDBFS_STREAM_Destruct(YOYO_XDDBFS_STREAM *strm)
  {
    if ( strm->tmp_path )
      {
        utf8_unlink(strm->tmp_path);
        free(strm->tmp_path);
      }
    if ( strm->fd >= 0 ) close(strm->fd);
    free(strm->key);
    __Unrefe(strm->xddb);
    __Destruct(strm);    
  };

char *Xddbfs_Tmp_File_Path_Sfx(YOYO_XDDBFS *xdb, char *sfx)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    char *S = Xddb_Build_Unique_Key();
    if ( sfx )
      S = Str_Concat(S,sfx);
    return Str_Join_3('/',xdb->basepath,"tmp",S);
  }
#endif
  ;

char *Xddbfs_File_Path_Sfx_Md5(YOYO_XDDBFS *xddb, byte_t *md5, char *sfx)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    char *S = 0;
    int i;
    char md5_hex[33] = {0};
    char hash[4*3] = "\0\0/\0\0/\0\0/\0\0";
    for ( i = 0; i < 16; ++i )
      Str_Hex_Byte(md5[i],0,md5_hex+i*2);
    if ( !xddb->fs_depth )
      {
        S = Str_Join_2('/',xddb->basepath,md5_hex);
      }
    else
      {
        switch ( xddb->fs_depth )
          {
            case 1:
              {
                byte_t c = Crc_8(0,md5,16);
                Str_Hex_Byte(c,0,hash);
                hash[2] = 0;
                break;
              }
            case 2:
              {
                ushort_t c = Crc_16(0,md5,16);
                for ( i = 0; i < 2; ++i )
                  Str_Hex_Byte((byte_t)(c>>(i*8)),0,hash+i*3);
                hash[5] = 0;
                break;
              }
            default:
              {
                uint_t c = Crc_32(0,md5,16);
                for ( i = 0; i < 4; ++i )
                  Str_Hex_Byte((byte_t)(c>>(i*8)),0,hash+i*3);
                break;
              }
          }
      
        S = Str_Join_3('/',xddb->basepath,hash,md5_hex);
      }
      
    if ( sfx )
      S = Str_Concat(S,sfx);

    return S;
  }
#endif
  ;
  
#define Xddbfs_File_Path(Co,Key) Xddbfs_File_Path_Sfx(Co,Key,0)
char *Xddbfs_File_Path_Sfx(YOYO_XDDBFS *xddb, char *key, char *sfx)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    char *S = 0;
    int i;

    if ( xddb->fs_depth < 0 )
      {
        int L = strlen(key);
        S = Str_Copy(key,L);
        for ( i = 0; i < L; ++i )
          {
            if ( S[i] == '/' )
              S[i] = '|';
            else if ( S[i] == '?' ) 
              S[i] = '&';
            else if ( S[i] == '*' ) 
              S[i] = '+';
          }
        S = Str_Join_2('/',xddb->basepath,S);
        if ( sfx )
          S = Str_Concat(S,sfx);
      }
    else
      {
        byte_t md5[16];
        Md5_Digest(key,strlen(key),md5);
        S = Xddbfs_File_Path_Sfx_Md5(xddb,md5,sfx);
      }
      
    return S;
  }
#endif
  ;

YOYO_BUFFER *Xddbfs_Read_Id(YOYO_XDDBFS *xddb, char *coid, int count)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    YOYO_BUFFER *bf = 0;
    YOYO_FILE_STATS st = {0};
    File_Get_Stats(coid,&st,0);
        
    if ( st.f.exists )
      {
        void *foj = Cfile_Open_Raw(coid,"rb");
        if ( !foj )
          Raise_If_File_Error("open",coid);
        if ( count < 0 )
          bf = Cfile_Read_All(foj);
        else
          {
            bf = Buffer_Init(count);
            Cfile_Read(foj,bf->at,count,count);
          }
        Cfile_Close(foj);
      }
    
    return bf;
  }
#endif
  ;

void Xddbfs_Write_Id(YOYO_XDDBFS *xddb, char *coid, void *data, int count)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    void *foj = Cfile_Open(coid,"w+bP");
    Cfile_Write(foj,data,count,count);
    Cfile_Close(foj);
  }
#endif
  ;

YOYO_XDATA *Xddbfs_Get(YOYO_XDDBFS *xddb, char *key, YOYO_XDATA *dflt)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    YOYO_XDATA *doc = dflt;
    
    __Auto_Ptr(doc)
      {
        char *coid;
        YOYO_BUFFER *bf;
        
        coid = Xddbfs_File_Path(xddb,key);
        __Pfd_Lock(&xddb->jur_fd)
          bf = Xddbfs_Read_Id(xddb,coid,-1);
        
        if ( bf )
          doc = Xddb_Decode(xddb,bf->at,bf->count);
      }
    
    return doc;
  }
#endif
  ;

void Xddbfs_Delete(YOYO_XDDBFS *xddb, char *key)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    __Auto_Release
      {
        char *coid = Xddbfs_File_Path(xddb,key);
        __Pfd_Lock(&xddb->jur_fd)
          File_Unlink(coid,0);
      }
  }
#endif
  ;

int Xddbfs_Store(YOYO_XDDBFS *xddb, YOYO_XDATA *doc, int strict_revision)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    int revision = 0; 
    
    __Auto_Release
      {
        //byte_t md5[16];
        char *key;
        char *coid;
        YOYO_BUFFER *bf;
        
        key = Xnode_Value_Get_Str(&doc->root,"$key$",0);
        if ( !key )
          __Raise(YOYO_ERROR_ILLFORMED,"$key$ property doesn't present or has invalid value");
        
        //Md5_Sign_Data(key,strlen(key),md5);
        coid = Xddbfs_File_Path(xddb,key);
        
        __Pfd_Lock(&xddb->jur_fd)
          {
            bf = Xddbfs_Read_Id(xddb,coid,8);
            if ( bf ) revision = Hex8_To_Unsigned(bf->at);
            
            if ( strict_revision )
              if ( revision && revision != Xdata_Get_Revision(doc) )
                __Raise(YOYO_ERROR_INCONSISTENT,
                  __Format("Inconsistent revision of document '%s', store:%d != docu:%d",
                           coid,
                           revision,
                           Xdata_Get_Revision(doc)));              
            
            ++revision;
            Xdata_Set_Revision(doc,revision);
            bf = Xddb_Encode(xddb,doc);
            Xddbfs_Write_Id(xddb,coid,bf->at,bf->count);
          }
      }
      
    return revision;
  }
#endif
  ;

void Xddbfs_Strm_Delete(YOYO_XDDBFS *co, char *key)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    __Auto_Release
      {
        char *path = Xddbfs_File_Path_Sfx(co,key,".strm");
        File_Unlink(path,0);
      }
  }
#endif
  ;

char *Xddbfs_Strm_Commit(YOYO_XDDBFS_STREAM *strm, char *key, int overwrite)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    __Auto_Ptr(key)
      {
        int keylen; 
        char *coid;
        YOYO_XDATA_CO_FS_STRM_HEADER hdr = {0};
        uint_t now;

        if ( key && strlen(key) > YOYO_XDATA_CO_MAX_FS_KEYLEN )
          __Raise_Format(YOYO_ERROR_ILLFORMED,
              (__yoTa("key to long, max key len is %d symbols",0),YOYO_XDATA_CO_MAX_FS_KEYLEN));
        
        if ( strm->key )
          __Raise(YOYO_ERROR_IO,__yoTa("stream already commited",0));
          
        if ( strm->fd >= 0 )
          {
            now = Get_Curr_Date();
            Unsigned_To_Hex8(now,hdr.created);
            Unsigned_To_Hex8(now,hdr.lastacs);
            hdr.compressed = '.';
            hdr.cr = '\r';
            hdr.nl = '\n';
            hdr.nulterm = '!';
            Unsigned_To_Four(strm->mimetype,hdr.mimetype);

            if ( !key )
              {
                overwrite = 0;
                strm->key = Xddb_Build_Unique_Key_Npl();
              }
            else
              strm->key = Str_Copy_Npl(key,-1);
            
            keylen = strlen(strm->key);
            Unsigned_To_Hex8(strm->length,hdr.length);
            Unsigned_To_Hex2(keylen,hdr.keylen);
            memset(hdr.key,'#',sizeof(hdr.key));
            memcpy(hdr.key,strm->key,keylen);

            Write_Out_Raise(strm->fd,&hdr,sizeof(hdr));

            close(strm->fd);
            strm->fd = -1;
            strm->finished = 1;
            
            coid = Xddbfs_File_Path_Sfx(strm->xddb,strm->key,".strm");
            if ( File_Exists(coid) )
              {
                if ( overwrite ) 
                  File_Unlink(coid,0);
                else
                  __Raise(YOYO_ERROR_IO,__yoTa("stream conflicts with one exist",0));
              }
            Create_Required_Dirs(coid);
            File_Rename(strm->tmp_path,coid);
          }
        else
          __Raise(YOYO_ERROR_IO,__yoTa("stream is not ready",0));
      }
      
    return strm->key;
  }
#endif
  ;

int Xddbfs_Strm_Read(YOYO_XDDBFS_STREAM *strm,void *data, int count,int mincount)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    int i;
    
    if ( !strm->finished )
      __Raise(YOYO_ERROR_ACCESS_DENAIED,__yoTa("writeonly stream!",0));
    if ( strm->fd < 0 )
      __Raise(YOYO_ERROR_IO,__yoTa("stream alrady closed",0));
    if ( count > strm->length - strm->curpos )
      count = strm->length - strm->curpos;
    
    for ( i=0; i<count; )
      {
        int j = read(strm->fd,(char*)data+i,count-i);
        if ( j < 0 )
          {
            int err = errno;
            __Raise(YOYO_ERROR_IO,
              __Format(__yoTa("failed to read stream: %s",0),strerror(err)));
          }
        else if ( j )
          i+=j;
        else
          break;
      }
    
    if ( i < mincount )
      __Raise(YOYO_ERROR_IO_EOF,__yoTa("doesn't have enough bytes",0));
      
    strm->curpos += i;
    return i;
  }
#endif
  ;

int Xddbfs_Strm_Write(YOYO_XDDBFS_STREAM *strm,void *data, int count,int mincount)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    int i;
    
    if ( strm->finished )
      __Raise(YOYO_ERROR_ACCESS_DENAIED,__yoTa("readonly stream!",0));
    if ( strm->fd < 0 )
      __Raise(YOYO_ERROR_IO,__yoTa("stream alrady closed",0));
    
    for ( i=0; i<count; )
      {
        int j = write(strm->fd,(char*)data+i,count-i);
        if ( j < 0 )
          {
            int err = errno;
            __Raise(YOYO_ERROR_IO,__Format(__yoTa("failed to write stream: %s",0),strerror(err)));
          }
        else
          i+=j;
      }
    
    strm->length += i;
    return i;
  }
#endif
  ;

void Xddbfs_Strm_Rewind(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    if ( !strm->finished )
      __Raise(YOYO_ERROR_ACCESS_DENAIED,__yoTa("writeonly stream!",0));
    if ( strm->fd < 0 )
      __Raise(YOYO_ERROR_IO,__yoTa("stream alrady closed",0));
    Lseek_Raise(strm->fd,0);
    strm->curpos = 0;
  }
#endif
  ;

void Xddbfs_Strm_Skip(YOYO_XDDBFS_STREAM *strm, int count)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    if ( !strm->finished )
      __Raise(YOYO_ERROR_ACCESS_DENAIED,__yoTa("writeonly stream!",0));
    if ( strm->fd < 0 )
      __Raise(YOYO_ERROR_IO,__yoTa("stream alrady closed",0));
    if ( count > strm->length - strm->curpos )
      __Raise(YOYO_ERROR_IO,__yoTa("skip out of stream",0));
    if ( count )
      Lseek_Raise(strm->fd,strm->curpos+count);
    strm->curpos += count;
  }
#endif
  ;

int Xddbfs_Strm_Available(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    if ( !strm->finished )
      __Raise(YOYO_ERROR_ACCESS_DENAIED,__yoTa("writeonly stream!",0));
    if ( strm->fd < 0 )
      __Raise(YOYO_ERROR_IO,__yoTa("stream alrady closed",0));
    return strm->length - strm->curpos;
  }
#endif
  ;

int Xddbfs_Strm_Mimetype(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    return strm->mimetype;
  }
#endif
  ;

int Xddbfs_Strm_Length(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    if ( !strm->finished )
      __Raise(YOYO_ERROR_ACCESS_DENAIED,__yoTa("writeonly stream!",0));
    return strm->length;
  }
#endif
  ;

void Xddbfs_Strm_Close(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    if ( strm->fd >= 0 ) 
      {
        close(strm->fd);
        strm->fd = -1;
      }
  }
#endif
  ;

int Xddbfs_Strm_Eof(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    return !Xddbfs_Strm_Available(strm);
  }
#endif
  ;

void Xddbfs_Strm_Cancel(YOYO_XDDBFS_STREAM *strm)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    Xddbfs_Strm_Close(strm);
  }
#endif
  ;

YOYO_XDDBFS_STREAM *Xddbfs_Strm_Init(YOYO_XDDBFS *xddb, char *key, int mimetype, int mknew)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    static YOYO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,       YOYO_XDDBFS_STREAM_Destruct},
        {Oj_Close_OjMID,          Xddbfs_Strm_Close},
        {Oj_Read_OjMID,           Xddbfs_Strm_Read},
        {Oj_Write_OjMID,          Xddbfs_Strm_Write},
        {Oj_Available_OjMID,      Xddbfs_Strm_Available},
        {Oj_Eof_OjMID,            Xddbfs_Strm_Eof},
        {Oj_Length_OjMID,         Xddbfs_Strm_Length},
        {Xddb_Strm_Cancel_OjMID,  Xddbfs_Strm_Cancel},
        {Xddb_Strm_Commit_OjMID,  Xddbfs_Strm_Commit},
        {Oj_Mimetype_Of_OjMID,    Xddbfs_Strm_Mimetype},
        {0}
      };
    YOYO_XDDBFS_STREAM *self = __Object(sizeof(YOYO_XDDBFS_STREAM),funcs);
    self->fd = -1;
    self->xddb = __Refe(xddb);

    if ( mknew )
      {
        if ( key )
          __Raise(YOYO_ERROR_INVALID_PARAM,__yoTa("key can't be specified on stream creation",0));
        self->mimetype = mimetype;
        self->tmp_path = __Retain(Xddbfs_Tmp_File_Path_Sfx(xddb,".strm"));
        if ( mknew != - 1)
          {
            self->fd = Open_File(self->tmp_path,O_CREAT|O_WRONLY);
            if ( self->fd < 0 )
              {
                int err = errno;
                __Raise_Format(YOYO_ERROR_IO,
                      (__yoTa("failed to create temprary file '%s': %s",0),self->tmp_path,strerror(err)));
              }
          }
      }
    else
      {
        char *coid;
        coid = Xdata_Co_Fs_File_Path_Sfx(xddb,key,".strm");

        self->key = Str_Copy_Npl(key,-1);
        self->finished = 1;
        self->fd = Open_File(coid,O_RDONLY);
        if ( self->fd < 0 )
          {
            int err = errno;
            __Raise_Format(YOYO_ERROR_DOESNT_EXIST,
                  (__yoTa("failed to open stream object '%s'(%s): %s",0),key,coid,strerror(err)));
          }
        __Gogo
          {
            int keylen;
            YOYO_XDATA_CO_FS_STRM_HEADER hdr = {0};
            Lseek_Raise(self->fd,-sizeof(hdr));
            Read_Into_Raise(self->fd,&hdr,sizeof(hdr));
            Lseek_Raise(self->fd,0);
            self->mimetype = Four_To_Unsigned(hdr.mimetype);
            self->length = Hex8_To_Unsigned(hdr.length);
            keylen = Hex2_To_Unsigned(hdr.keylen);
            if ( keylen < sizeof(hdr.key) )
              hdr.key[keylen] = 0;
            hdr.nulterm = 0;
            if ( keylen != strlen(self->key) || 0 != strncmp(self->key,hdr.key,keylen) )
              __Raise_Format(YOYO_ERROR_CORRUPTED,
                  (__yoTa("failed to open stream '%s': specified key does not match to '%s'/%d",0),key,hdr.key,keylen));
          }
      }
    return self;
  }
#endif
  ;

YOYO_XDDBFS_STREAM *Xddbfs_Strm_Open(YOYO_XDDBFS *xddb, char *key)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    return Xddbfs_Strm_Init(xddb,key,0,0);
  }
#endif
  ;
    
YOYO_XDDBFS_STREAM *Xddbfs_Strm_Create(YOYO_XDDBFS *xddb, int mimetype)
#ifdef _YOYO_XDDBFS_BUILTIN 
  {
    return Xddbfs_Strm_Init(xddb,,mimetype,1);
  }
#ifdef _YOYO_XDDBFS_BUILTIN 
  ;
  
YOYO_XDATA_CO *Xddbfs_Open(char *path, int create_new, int format, int depth)
#ifdef _YOYO_XDATACO_BUILTIN 
  {
    static YOYO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,       YOYO_XDDBFS_Destruct},
        {Xddb_Delete_OjMID,       Xddbfs_Delete},
        {Xddb_Get_OjMID,          Xddbfs_Get},
        {Xddb_Store_OjMID,        Xddbfs_Store},
        {Xddb_Strm_Open_OjMID,    Xddbfs_Strm_Open},
        {Xddb_Strm_Create_OjMID,  Xddbfs_Strm_Create},
        {Xddb_Strm_Delete_OjMID,  Xddbfs_Strm_Delete},
        {0}
      };

    YOYO_XDATA *xcf;
    YOYO_XDDBFS *xddb = 0;
    YOYO_FILE_STATS bp_stats = {0};
    YOYO_FILE_STATS jr_stats = {0};
    YOYO_FILE_STATS cf_stats = {0};
    char *cf_name,*jr_name,*tmp_dir;
    
    if ( !format ) format = YOYO_XDDB_DOC_TEXT;
    if ( depth & ( depth != 1 && depth != 2 && depth != 4 ) )
      __Raise_Format(YOYO_ERROR_INVALID_PARAM,(__yoTa("invalid depth value %d",0),depth));
    
    __Auto_Ptr(xddb)
      {
        cf_name = Str_Join_2('/',path,".coconf");
        jr_name = Str_Join_2('/',path,".cojur");
        tmp_dir = Str_Join_2('/',path,"tmp");
    
        File_Get_Stats(path,&bp_stats,1);
        File_Get_Stats(cf_name,&cf_stats,1);
        File_Get_Stats(jr_name,&jr_stats,1);
    
        if ( create_new )
          {
            int foo;
            if ( jr_stats.f.exists )
              __Raise_Format(YOYO_ERROR_ALREADY_EXISTS,
                              (__yoTa("collection '%s' already exists",0),path));
            if ( !cf_stats.f.exists ) __Auto_Release 
              {
                char *cf = __Format(__yoTa("depth = %d\nformat = %d\n",0),depth,format);
                int cf_len = strlen(cf);
                Cfile_Write(Cfile_Open(cf_name,"nP"),cf,cf_len,cf_len);
              }
            foo = open(jr_name,O_CREAT|O_WRONLY,0660);
            if ( foo >= 0 )
              close(foo);
            else
              Raise_If_File_Error(__yoTa("create",0),jr_name);
          }
        else
          {
            if ( !bp_stats.f.exists || !cf_stats.f.exists || !jr_stats.f.exists )
              __Raise_Format(YOYO_ERROR_DOESNT_EXIST,
                              (__yoTa("collection '%s' doesn't exist",0),path));
          }
        
        xcf = Def_Parse_File(cf_name);
        xddb = __Object(sizeof(YOYO_XDDBFS),funcs);
        xddb->basepath = Str_Copy_Npl(path,-1);
        xddb->fs_depth = Xnode_Value_Get_Int(xcf,"depth",0);
        xddb->xddb.doc_format = Xnode_Value_Get_Int(xcf,"format",0);
        
        xddb->jur_fd = open(jr_name,O_WRONLY|O_APPEND);
        if ( xddb->jur_fd < 0 ) 
          Raise_If_File_Error("open",jr_name); 
        
        if ( !File_Exists(tmp_dir) )
          Create_Directory(tmp_dir);
      }
      
    return xddb;
  }
#endif
  ;

#endif /* C_once_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF */