initial commit
[goodguy/history.git] / cinelerra-5.0 / db / s.C
1 #include "s.h"
2
3
4 int Video_frameLoc::ikey_Frame_weight::
5 cmpr(char *a, char *b)
6 {
7   ikey_Frame_weight *kp = (ikey_Frame_weight *)a;
8   int v = *(int*)b;
9   if( kp->v_id == v ) return 0;
10   Video_frameLoc vloc(kp->loc.entity);
11   if( vloc.FindId(v) )
12     vloc.err_(Db::errCorrupt);
13   v = cmpr_double( kp->v_Frame_mean.addr(), kp->v_Frame_mean.size(),
14                   vloc._Frame_mean(), vloc->v_Frame_mean.size());
15   if( v != 0 ) return v;
16   if( kp->v_id >= 0 ) {
17     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
18                  vloc._id(), vloc._id_size());
19     if( v != 0 ) return v;
20   }
21   return 0;
22 }
23
24 int Video_frameLoc::rkey_Frame_weight::
25 cmpr(char *a, char *b)
26 {
27   rkey_Frame_weight *kp = (rkey_Frame_weight *)a;
28   Video_frameLoc &kloc = (Video_frameLoc&)kp->loc;
29   int v = kloc->id, b_id = *(int*)b;
30   if( v == b_id ) return 0;
31   Video_frameLoc vloc(kloc.entity);
32   if( vloc.FindId(b_id) )
33     kloc.err_(Db::errCorrupt);
34   v = cmpr_double( kloc._Frame_mean(), kloc->v_Frame_mean.size(),
35                    vloc._Frame_mean(), vloc->v_Frame_mean.size());
36   if( v != 0 ) return v;
37   v = cmpr_int(kloc._id(), kloc._id_size(),
38                vloc._id(), vloc._id_size());
39   if( v != 0 ) return v;
40   return 0;
41 }
42
43 int Video_frameLoc::ikey_Frame_center::
44 cmpr(char *a, char *b)
45 {
46   ikey_Frame_center *kp = (ikey_Frame_center *)a;
47   int v = *(int*)b;
48   if( kp->v_id == v ) return 0;
49   Video_frameLoc vloc(kp->loc.entity);
50   if( vloc.FindId(v) )
51     vloc.err_(Db::errCorrupt);
52   v = cmpr_double( kp->v_Frame_moment.addr(), kp->v_Frame_moment.size(),
53                   vloc._Frame_moment(), vloc->v_Frame_moment.size());
54   if( v != 0 ) return v;
55   if( kp->v_id >= 0 ) {
56     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
57                  vloc._id(), vloc._id_size());
58     if( v != 0 ) return v;
59   }
60   return 0;
61 }
62
63 int Video_frameLoc::rkey_Frame_center::
64 cmpr(char *a, char *b)
65 {
66   rkey_Frame_center *kp = (rkey_Frame_center *)a;
67   Video_frameLoc &kloc = (Video_frameLoc&)kp->loc;
68   int v = kloc->id, b_id = *(int*)b;
69   if( v == b_id ) return 0;
70   Video_frameLoc vloc(kloc.entity);
71   if( vloc.FindId(b_id) )
72     kloc.err_(Db::errCorrupt);
73   v = cmpr_double( kloc._Frame_moment(), kloc->v_Frame_moment.size(),
74                    vloc._Frame_moment(), vloc->v_Frame_moment.size());
75   if( v != 0 ) return v;
76   v = cmpr_int(kloc._id(), kloc._id_size(),
77                vloc._id(), vloc._id_size());
78   if( v != 0 ) return v;
79   return 0;
80 }
81
82 int Video_frameLoc::Allocate()
83 {
84   if_err( allocate() );
85   if( !addr_wr() ) return err_(Db::errNoMemory);
86   v_init();
87   return 0;
88 }
89
90 int Video_frameLoc::Construct()
91 {
92   if_err( insertProhibit() );
93   if_err( construct() );
94   int id = this->id();
95   { rkey_Frame_weight rkey(*this);
96     if_err( entity->index("Frame_weight")->Insert(rkey,&id) ); }
97   { rkey_Frame_center rkey(*this);
98     if_err( entity->index("Frame_center")->Insert(rkey,&id) ); }
99   if_err( insertCascade() );
100   return 0;
101 }
102
103 int Video_frameLoc::Destruct()
104 {
105   if_err( deleteProhibit() );
106   { rkey_Frame_weight rkey(*this);
107     if_err( entity->index("Frame_weight")->Delete(rkey) ); }
108   { rkey_Frame_center rkey(*this);
109     if_err( entity->index("Frame_center")->Delete(rkey) ); }
110   if_err( destruct() );
111   if_err( deleteCascade() );
112   return 0;
113 }
114
115 void Video_frameLoc::Deallocate()
116 {
117   v_del();
118   deallocate();
119 }
120
121
122 int TimelineLoc::ikey_Timelines::
123 cmpr(char *a, char *b)
124 {
125   ikey_Timelines *kp = (ikey_Timelines *)a;
126   int v = *(int*)b;
127   if( kp->v_id == v ) return 0;
128   TimelineLoc vloc(kp->loc.entity);
129   if( vloc.FindId(v) )
130     vloc.err_(Db::errCorrupt);
131   v = cmpr_uint( kp->v_Frame_id.addr(), kp->v_Frame_id.size(),
132                   vloc._Frame_id(), vloc->v_Frame_id.size());
133   if( v != 0 ) return v;
134   if( kp->v_id >= 0 ) {
135     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
136                  vloc._id(), vloc._id_size());
137     if( v != 0 ) return v;
138   }
139   return 0;
140 }
141
142 int TimelineLoc::rkey_Timelines::
143 cmpr(char *a, char *b)
144 {
145   rkey_Timelines *kp = (rkey_Timelines *)a;
146   TimelineLoc &kloc = (TimelineLoc&)kp->loc;
147   int v = kloc->id, b_id = *(int*)b;
148   if( v == b_id ) return 0;
149   TimelineLoc vloc(kloc.entity);
150   if( vloc.FindId(b_id) )
151     kloc.err_(Db::errCorrupt);
152   v = cmpr_uint( kloc._Frame_id(), kloc->v_Frame_id.size(),
153                    vloc._Frame_id(), vloc->v_Frame_id.size());
154   if( v != 0 ) return v;
155   v = cmpr_int(kloc._id(), kloc._id_size(),
156                vloc._id(), vloc._id_size());
157   if( v != 0 ) return v;
158   return 0;
159 }
160
161 int TimelineLoc::ikey_Sequences::
162 cmpr(char *a, char *b)
163 {
164   ikey_Sequences *kp = (ikey_Sequences *)a;
165   int v = *(int*)b;
166   TimelineLoc vloc(kp->loc.entity);
167   if( vloc.FindId(v) )
168     vloc.err_(Db::errCorrupt);
169   v = cmpr_uint( kp->v_Clip_id.addr(), kp->v_Clip_id.size(),
170                   vloc._Clip_id(), vloc->v_Clip_id.size());
171   if( v != 0 ) return v;
172   v = cmpr_uint( kp->v_Sequence_no.addr(), kp->v_Sequence_no.size(),
173                   vloc._Sequence_no(), vloc->v_Sequence_no.size());
174   if( v != 0 ) return v;
175   return 0;
176 }
177
178 int TimelineLoc::rkey_Sequences::
179 cmpr(char *a, char *b)
180 {
181   rkey_Sequences *kp = (rkey_Sequences *)a;
182   TimelineLoc &kloc = (TimelineLoc&)kp->loc;
183   int v = kloc->id, b_id = *(int*)b;
184   if( v == b_id ) return 0;
185   TimelineLoc vloc(kloc.entity);
186   if( vloc.FindId(b_id) )
187     kloc.err_(Db::errCorrupt);
188   v = cmpr_uint( kloc._Clip_id(), kloc->v_Clip_id.size(),
189                    vloc._Clip_id(), vloc->v_Clip_id.size());
190   if( v != 0 ) return v;
191   v = cmpr_uint( kloc._Sequence_no(), kloc->v_Sequence_no.size(),
192                    vloc._Sequence_no(), vloc->v_Sequence_no.size());
193   if( v != 0 ) return v;
194   return 0;
195 }
196
197 int TimelineLoc::Allocate()
198 {
199   if_err( allocate() );
200   if( !addr_wr() ) return err_(Db::errNoMemory);
201   return 0;
202 }
203
204 int TimelineLoc::Construct()
205 {
206   if_err( insertProhibit() );
207   if_err( construct() );
208   int id = this->id();
209   { rkey_Timelines rkey(*this);
210     if_err( entity->index("Timelines")->Insert(rkey,&id) ); }
211   { rkey_Sequences rkey(*this);
212     if_err( entity->index("Sequences")->Insert(rkey,&id) ); }
213   if_err( insertCascade() );
214   return 0;
215 }
216
217 int TimelineLoc::Destruct()
218 {
219   if_err( deleteProhibit() );
220   { rkey_Timelines rkey(*this);
221     if_err( entity->index("Timelines")->Delete(rkey) ); }
222   { rkey_Sequences rkey(*this);
223     if_err( entity->index("Sequences")->Delete(rkey) ); }
224   if_err( destruct() );
225   if_err( deleteCascade() );
226   return 0;
227 }
228
229 void TimelineLoc::Deallocate()
230 {
231   deallocate();
232 }
233
234
235 int Clip_setLoc::ikey_Clip_title::
236 cmpr(char *a, char *b)
237 {
238   ikey_Clip_title *kp = (ikey_Clip_title *)a;
239   int v = *(int*)b;
240   if( kp->v_id == v ) return 0;
241   Clip_setLoc vloc(kp->loc.entity);
242   if( vloc.FindId(v) )
243     vloc.err_(Db::errCorrupt);
244   v = cmpr_char( kp->v_Title.addr(), kp->v_Title.size(),
245                   vloc._Title(), vloc->v_Title.size());
246   if( v != 0 ) return v;
247   if( kp->v_id >= 0 ) {
248     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
249                  vloc._id(), vloc._id_size());
250     if( v != 0 ) return v;
251   }
252   return 0;
253 }
254
255 int Clip_setLoc::rkey_Clip_title::
256 cmpr(char *a, char *b)
257 {
258   rkey_Clip_title *kp = (rkey_Clip_title *)a;
259   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
260   int v = kloc->id, b_id = *(int*)b;
261   if( v == b_id ) return 0;
262   Clip_setLoc vloc(kloc.entity);
263   if( vloc.FindId(b_id) )
264     kloc.err_(Db::errCorrupt);
265   v = cmpr_char( kloc._Title(), kloc->v_Title.size(),
266                    vloc._Title(), vloc->v_Title.size());
267   if( v != 0 ) return v;
268   v = cmpr_int(kloc._id(), kloc._id_size(),
269                vloc._id(), vloc._id_size());
270   if( v != 0 ) return v;
271   return 0;
272 }
273
274 int Clip_setLoc::ikey_Clip_system_time::
275 cmpr(char *a, char *b)
276 {
277   ikey_Clip_system_time *kp = (ikey_Clip_system_time *)a;
278   int v = *(int*)b;
279   if( kp->v_id == v ) return 0;
280   Clip_setLoc vloc(kp->loc.entity);
281   if( vloc.FindId(v) )
282     vloc.err_(Db::errCorrupt);
283   v = cmpr_long( kp->v_System_time.addr(), kp->v_System_time.size(),
284                   vloc._System_time(), vloc->v_System_time.size());
285   if( v != 0 ) return v;
286   if( kp->v_id >= 0 ) {
287     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
288                  vloc._id(), vloc._id_size());
289     if( v != 0 ) return v;
290   }
291   return 0;
292 }
293
294 int Clip_setLoc::rkey_Clip_system_time::
295 cmpr(char *a, char *b)
296 {
297   rkey_Clip_system_time *kp = (rkey_Clip_system_time *)a;
298   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
299   int v = kloc->id, b_id = *(int*)b;
300   if( v == b_id ) return 0;
301   Clip_setLoc vloc(kloc.entity);
302   if( vloc.FindId(b_id) )
303     kloc.err_(Db::errCorrupt);
304   v = cmpr_long( kloc._System_time(), kloc->v_System_time.size(),
305                    vloc._System_time(), vloc->v_System_time.size());
306   if( v != 0 ) return v;
307   v = cmpr_int(kloc._id(), kloc._id_size(),
308                vloc._id(), vloc._id_size());
309   if( v != 0 ) return v;
310   return 0;
311 }
312
313 int Clip_setLoc::ikey_Clip_creation_time::
314 cmpr(char *a, char *b)
315 {
316   ikey_Clip_creation_time *kp = (ikey_Clip_creation_time *)a;
317   int v = *(int*)b;
318   if( kp->v_id == v ) return 0;
319   Clip_setLoc vloc(kp->loc.entity);
320   if( vloc.FindId(v) )
321     vloc.err_(Db::errCorrupt);
322   v = cmpr_long( kp->v_Creation_time.addr(), kp->v_Creation_time.size(),
323                   vloc._Creation_time(), vloc->v_Creation_time.size());
324   if( v != 0 ) return v;
325   if( kp->v_id >= 0 ) {
326     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
327                  vloc._id(), vloc._id_size());
328     if( v != 0 ) return v;
329   }
330   return 0;
331 }
332
333 int Clip_setLoc::rkey_Clip_creation_time::
334 cmpr(char *a, char *b)
335 {
336   rkey_Clip_creation_time *kp = (rkey_Clip_creation_time *)a;
337   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
338   int v = kloc->id, b_id = *(int*)b;
339   if( v == b_id ) return 0;
340   Clip_setLoc vloc(kloc.entity);
341   if( vloc.FindId(b_id) )
342     kloc.err_(Db::errCorrupt);
343   v = cmpr_long( kloc._Creation_time(), kloc->v_Creation_time.size(),
344                    vloc._Creation_time(), vloc->v_Creation_time.size());
345   if( v != 0 ) return v;
346   v = cmpr_int(kloc._id(), kloc._id_size(),
347                vloc._id(), vloc._id_size());
348   if( v != 0 ) return v;
349   return 0;
350 }
351
352 int Clip_setLoc::ikey_Clip_path_pos::
353 cmpr(char *a, char *b)
354 {
355   ikey_Clip_path_pos *kp = (ikey_Clip_path_pos *)a;
356   int v = *(int*)b;
357   if( kp->v_id == v ) return 0;
358   Clip_setLoc vloc(kp->loc.entity);
359   if( vloc.FindId(v) )
360     vloc.err_(Db::errCorrupt);
361   v = cmpr_char( kp->v_Asset_path.addr(), kp->v_Asset_path.size(),
362                   vloc._Asset_path(), vloc->v_Asset_path.size());
363   if( v != 0 ) return v;
364   v = cmpr_double( kp->v_Position.addr(), kp->v_Position.size(),
365                   vloc._Position(), vloc->v_Position.size());
366   if( v != 0 ) return v;
367   if( kp->v_id >= 0 ) {
368     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
369                  vloc._id(), vloc._id_size());
370     if( v != 0 ) return v;
371   }
372   return 0;
373 }
374
375 int Clip_setLoc::rkey_Clip_path_pos::
376 cmpr(char *a, char *b)
377 {
378   rkey_Clip_path_pos *kp = (rkey_Clip_path_pos *)a;
379   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
380   int v = kloc->id, b_id = *(int*)b;
381   if( v == b_id ) return 0;
382   Clip_setLoc vloc(kloc.entity);
383   if( vloc.FindId(b_id) )
384     kloc.err_(Db::errCorrupt);
385   v = cmpr_char( kloc._Asset_path(), kloc->v_Asset_path.size(),
386                    vloc._Asset_path(), vloc->v_Asset_path.size());
387   if( v != 0 ) return v;
388   v = cmpr_double( kloc._Position(), kloc->v_Position.size(),
389                    vloc._Position(), vloc->v_Position.size());
390   if( v != 0 ) return v;
391   v = cmpr_int(kloc._id(), kloc._id_size(),
392                vloc._id(), vloc._id_size());
393   if( v != 0 ) return v;
394   return 0;
395 }
396
397 int Clip_setLoc::Allocate()
398 {
399   if_err( allocate() );
400   if( !addr_wr() ) return err_(Db::errNoMemory);
401   v_init();
402   Title((char *)"",0);
403   Asset_path((char *)"",0);
404   return 0;
405 }
406
407 int Clip_setLoc::Construct()
408 {
409   if_err( insertProhibit() );
410   if_err( construct() );
411   int id = this->id();
412   { rkey_Clip_title rkey(*this);
413     if_err( entity->index("Clip_title")->Insert(rkey,&id) ); }
414   { rkey_Clip_system_time rkey(*this);
415     if_err( entity->index("Clip_system_time")->Insert(rkey,&id) ); }
416   { rkey_Clip_creation_time rkey(*this);
417     if_err( entity->index("Clip_creation_time")->Insert(rkey,&id) ); }
418   { rkey_Clip_path_pos rkey(*this);
419     if_err( entity->index("Clip_path_pos")->Insert(rkey,&id) ); }
420   if_err( insertCascade() );
421   return 0;
422 }
423
424 int Clip_setLoc::Destruct()
425 {
426   if_err( deleteProhibit() );
427   { rkey_Clip_title rkey(*this);
428     if_err( entity->index("Clip_title")->Delete(rkey) ); }
429   { rkey_Clip_system_time rkey(*this);
430     if_err( entity->index("Clip_system_time")->Delete(rkey) ); }
431   { rkey_Clip_creation_time rkey(*this);
432     if_err( entity->index("Clip_creation_time")->Delete(rkey) ); }
433   { rkey_Clip_path_pos rkey(*this);
434     if_err( entity->index("Clip_path_pos")->Delete(rkey) ); }
435   if_err( destruct() );
436   if_err( deleteCascade() );
437   return 0;
438 }
439
440 void Clip_setLoc::Deallocate()
441 {
442   v_del();
443   deallocate();
444 }
445
446
447 int Clip_viewsLoc::ikey_Clip_access::
448 cmpr(char *a, char *b)
449 {
450   ikey_Clip_access *kp = (ikey_Clip_access *)a;
451   int v = *(int*)b;
452   Clip_viewsLoc vloc(kp->loc.entity);
453   if( vloc.FindId(v) )
454     vloc.err_(Db::errCorrupt);
455   v = cmpr_uint( kp->v_Access_clip_id.addr(), kp->v_Access_clip_id.size(),
456                   vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
457   if( v != 0 ) return v;
458   return 0;
459 }
460
461 int Clip_viewsLoc::rkey_Clip_access::
462 cmpr(char *a, char *b)
463 {
464   rkey_Clip_access *kp = (rkey_Clip_access *)a;
465   Clip_viewsLoc &kloc = (Clip_viewsLoc&)kp->loc;
466   int v = kloc->id, b_id = *(int*)b;
467   if( v == b_id ) return 0;
468   Clip_viewsLoc vloc(kloc.entity);
469   if( vloc.FindId(b_id) )
470     kloc.err_(Db::errCorrupt);
471   v = cmpr_uint( kloc._Access_clip_id(), kloc->v_Access_clip_id.size(),
472                    vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
473   if( v != 0 ) return v;
474   return 0;
475 }
476
477 int Clip_viewsLoc::ikey_Last_view::
478 cmpr(char *a, char *b)
479 {
480   ikey_Last_view *kp = (ikey_Last_view *)a;
481   int v = *(int*)b;
482   if( kp->v_id == v ) return 0;
483   Clip_viewsLoc vloc(kp->loc.entity);
484   if( vloc.FindId(v) )
485     vloc.err_(Db::errCorrupt);
486   v = cmpr_long( kp->v_Access_time.addr(), kp->v_Access_time.size(),
487                   vloc._Access_time(), vloc->v_Access_time.size());
488   if( v != 0 ) return v;
489   if( kp->v_id >= 0 ) {
490     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
491                  vloc._id(), vloc._id_size());
492     if( v != 0 ) return v;
493   }
494   return 0;
495 }
496
497 int Clip_viewsLoc::rkey_Last_view::
498 cmpr(char *a, char *b)
499 {
500   rkey_Last_view *kp = (rkey_Last_view *)a;
501   Clip_viewsLoc &kloc = (Clip_viewsLoc&)kp->loc;
502   int v = kloc->id, b_id = *(int*)b;
503   if( v == b_id ) return 0;
504   Clip_viewsLoc vloc(kloc.entity);
505   if( vloc.FindId(b_id) )
506     kloc.err_(Db::errCorrupt);
507   v = cmpr_long( kloc._Access_time(), kloc->v_Access_time.size(),
508                    vloc._Access_time(), vloc->v_Access_time.size());
509   if( v != 0 ) return v;
510   v = cmpr_int(kloc._id(), kloc._id_size(),
511                vloc._id(), vloc._id_size());
512   if( v != 0 ) return v;
513   return 0;
514 }
515
516 int Clip_viewsLoc::ikey_Total_views::
517 cmpr(char *a, char *b)
518 {
519   ikey_Total_views *kp = (ikey_Total_views *)a;
520   int v = *(int*)b;
521   if( kp->v_id == v ) return 0;
522   Clip_viewsLoc vloc(kp->loc.entity);
523   if( vloc.FindId(v) )
524     vloc.err_(Db::errCorrupt);
525   v = cmpr_uint( kp->v_Access_count.addr(), kp->v_Access_count.size(),
526                   vloc._Access_count(), vloc->v_Access_count.size());
527   if( v != 0 ) return v;
528   v = cmpr_uint( kp->v_Access_clip_id.addr(), kp->v_Access_clip_id.size(),
529                   vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
530   if( v != 0 ) return v;
531   if( kp->v_id >= 0 ) {
532     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
533                  vloc._id(), vloc._id_size());
534     if( v != 0 ) return v;
535   }
536   return 0;
537 }
538
539 int Clip_viewsLoc::rkey_Total_views::
540 cmpr(char *a, char *b)
541 {
542   rkey_Total_views *kp = (rkey_Total_views *)a;
543   Clip_viewsLoc &kloc = (Clip_viewsLoc&)kp->loc;
544   int v = kloc->id, b_id = *(int*)b;
545   if( v == b_id ) return 0;
546   Clip_viewsLoc vloc(kloc.entity);
547   if( vloc.FindId(b_id) )
548     kloc.err_(Db::errCorrupt);
549   v = cmpr_uint( kloc._Access_count(), kloc->v_Access_count.size(),
550                    vloc._Access_count(), vloc->v_Access_count.size());
551   if( v != 0 ) return v;
552   v = cmpr_uint( kloc._Access_clip_id(), kloc->v_Access_clip_id.size(),
553                    vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
554   if( v != 0 ) return v;
555   v = cmpr_int(kloc._id(), kloc._id_size(),
556                vloc._id(), vloc._id_size());
557   if( v != 0 ) return v;
558   return 0;
559 }
560
561 int Clip_viewsLoc::Allocate()
562 {
563   if_err( allocate() );
564   if( !addr_wr() ) return err_(Db::errNoMemory);
565   return 0;
566 }
567
568 int Clip_viewsLoc::Construct()
569 {
570   if_err( insertProhibit() );
571   if_err( construct() );
572   int id = this->id();
573   { rkey_Clip_access rkey(*this);
574     if_err( entity->index("Clip_access")->Insert(rkey,&id) ); }
575   { rkey_Last_view rkey(*this);
576     if_err( entity->index("Last_view")->Insert(rkey,&id) ); }
577   { rkey_Total_views rkey(*this);
578     if_err( entity->index("Total_views")->Insert(rkey,&id) ); }
579   if_err( insertCascade() );
580   return 0;
581 }
582
583 int Clip_viewsLoc::Destruct()
584 {
585   if_err( deleteProhibit() );
586   { rkey_Clip_access rkey(*this);
587     if_err( entity->index("Clip_access")->Delete(rkey) ); }
588   { rkey_Last_view rkey(*this);
589     if_err( entity->index("Last_view")->Delete(rkey) ); }
590   { rkey_Total_views rkey(*this);
591     if_err( entity->index("Total_views")->Delete(rkey) ); }
592   if_err( destruct() );
593   if_err( deleteCascade() );
594   return 0;
595 }
596
597 void Clip_viewsLoc::Deallocate()
598 {
599   deallocate();
600 }
601
602
603 int theDb::
604 create(const char *dfn)
605 {
606   dfd = ::open(dfn,O_RDWR+O_CREAT+O_TRUNC+no_atime,0666);
607   if( dfd < 0 ) { perror(dfn); return -1; }
608   int ret = db_create();
609   close();
610   return ret;
611 }
612
613 int theDb::
614 db_create()
615 {
616   if_ret( Db::make(dfd) );
617   if_ret( Video_frame.new_entity("Video_frame", sizeof(Video_frameObj)) );
618   if_ret( Video_frame.add_kindex("Frame_weight") );
619   if_ret( Video_frame.add_kindex("Frame_center") );
620
621   if_ret( Timeline.new_entity("Timeline", sizeof(TimelineObj)) );
622   if_ret( Timeline.add_kindex("Timelines") );
623   if_ret( Timeline.add_kindex("Sequences") );
624
625   if_ret( Clip_set.new_entity("Clip_set", sizeof(Clip_setObj)) );
626   if_ret( Clip_set.add_kindex("Clip_title") );
627   if_ret( Clip_set.add_kindex("Clip_system_time") );
628   if_ret( Clip_set.add_kindex("Clip_creation_time") );
629   if_ret( Clip_set.add_kindex("Clip_path_pos") );
630
631   if_ret( Clip_views.new_entity("Clip_views", sizeof(Clip_viewsObj)) );
632   if_ret( Clip_views.add_kindex("Clip_access") );
633   if_ret( Clip_views.add_kindex("Last_view") );
634   if_ret( Clip_views.add_kindex("Total_views") );
635
636   if_ret( Db::commit(1) );
637   return 0;
638 }
639
640 theDb::
641 theDb()
642  : dfd(-1), dkey(-1), no_atime(getuid()?0:O_NOATIME), objects(0),
643    Video_frame(this), video_frame(Video_frame),
644    Timeline(this), timeline(Timeline),
645    Clip_set(this), clip_set(Clip_set),
646    Clip_views(this), clip_views(Clip_views)
647 {
648   objects = new ObjectList(objects, video_frame);
649   objects = new ObjectList(objects, timeline);
650   objects = new ObjectList(objects, clip_set);
651   objects = new ObjectList(objects, clip_views);
652   Video_frame.add_vref((vRef)&Video_frameObj::v_Frame_data);
653   Clip_set.add_vref((vRef)&Clip_setObj::v_Title);
654   Clip_set.add_vref((vRef)&Clip_setObj::v_Asset_path);
655   Clip_set.add_vref((vRef)&Clip_setObj::v_Weights);
656 }
657
658 int theDb::
659 open(const char *dfn, int key)
660 {
661   dfd = ::open(dfn,O_RDWR+no_atime);
662   if( dfd < 0 ) { perror(dfn); return errNotFound; }
663   if( (dkey=key) >= 0 ) Db::use_shm(1);
664   int ret = Db::open(dfd, dkey);
665   if( !ret ) ret = db_open();
666   if( ret ) close();
667   return ret;
668 }
669
670 int theDb::
671 db_open()
672 {
673   if_ret( Video_frame.get_entity("Video_frame") );
674   if_ret( Video_frame.key_index("Frame_weight") );
675   if_ret( Video_frame.key_index("Frame_center") );
676
677   if_ret( Timeline.get_entity("Timeline") );
678   if_ret( Timeline.key_index("Timelines") );
679   if_ret( Timeline.key_index("Sequences") );
680
681   if_ret( Clip_set.get_entity("Clip_set") );
682   if_ret( Clip_set.key_index("Clip_title") );
683   if_ret( Clip_set.key_index("Clip_system_time") );
684   if_ret( Clip_set.key_index("Clip_creation_time") );
685   if_ret( Clip_set.key_index("Clip_path_pos") );
686
687   if_ret( Clip_views.get_entity("Clip_views") );
688   if_ret( Clip_views.key_index("Clip_access") );
689   if_ret( Clip_views.key_index("Last_view") );
690   if_ret( Clip_views.key_index("Total_views") );
691
692   if_ret( Db::start_transaction() );
693   return 0;
694 }
695
696 void theDb::
697 close()
698 {
699   Db::close();
700   if( dfd >= 0 ) { ::close(dfd); dfd = -1; }
701 }
702
703 int theDb::
704 access(const char *dfn, int key, int rw)
705 {
706   if( key < 0 ) return Db::errInvalid;
707   dfd = ::open(dfn,O_RDWR+no_atime);
708   if( dfd < 0 ) { perror(dfn); return Db::errNotFound; }
709   dkey = key;  Db::use_shm(1);
710   int ret = Db::attach(dfd, dkey, rw);
711   if( !ret ) ret = db_access();
712   else if( ret == errNotFound ) {
713     ret = Db::open(dfd, dkey);
714     if( !ret ) ret = db_open();
715     if( !ret ) ret = Db::attach(rw);
716   }
717   if( ret ) close();
718   return ret;
719 }
720
721 int theDb::
722 db_access()
723 {
724   if_ret( Video_frame.get_entity("Video_frame") );
725   if_ret( Timeline.get_entity("Timeline") );
726   if_ret( Clip_set.get_entity("Clip_set") );
727   if_ret( Clip_views.get_entity("Clip_views") );
728   return 0;
729 }
730
731