PKGBUILD fix libva/vdpau deps
[goodguy/cinelerra.git] / cinelerra-5.1 / db / x.C
1 #include "tdb.h"
2
3 // Video_frame
4 DbObj(Video_frame)
5   basic_def(double,Frame_mean);
6   basic_def(double,Frame_std_dev);
7   basic_def(double,Frame_cx);
8   basic_def(double,Frame_cy);
9   basic_def(double,Frame_moment);
10   varray_def(unsigned char,Frame_data);
11 };
12
13 DbLoc(Video_frame)
14   basic_ref(double,Frame_mean);
15   basic_ref(double,Frame_std_dev);
16   basic_ref(double,Frame_cx);
17   basic_ref(double,Frame_cy);
18   basic_ref(double,Frame_moment);
19   varray_ref(unsigned char,Frame_data);
20
21   class ikey_Frame_weight : public Db::iKey { public:
22     Video_frameObj::t_Frame_mean v_Frame_mean;
23     int v_id;
24     static int cmpr(char *a, char *b);
25     ikey_Frame_weight(ObjectLoc &loc,
26         double Frame_mean, int id=-1)
27     : iKey("Frame_weight",loc,cmpr),
28       v_Frame_mean(Frame_mean),
29       v_id(id) {}
30   };
31   class rkey_Frame_weight : public Db::rKey { public:
32     static int cmpr(char *a, char *b);
33     rkey_Frame_weight(ObjectLoc &loc) : rKey("Frame_weight",loc,cmpr) {}
34   };
35
36   class ikey_Frame_center : public Db::iKey { public:
37     Video_frameObj::t_Frame_moment v_Frame_moment;
38     int v_id;
39     static int cmpr(char *a, char *b);
40     ikey_Frame_center(ObjectLoc &loc,
41         double Frame_moment, int id=-1)
42     : iKey("Frame_center",loc,cmpr),
43       v_Frame_moment(Frame_moment),
44       v_id(id) {}
45   };
46   class rkey_Frame_center : public Db::rKey { public:
47     static int cmpr(char *a, char *b);
48     rkey_Frame_center(ObjectLoc &loc) : rKey("Frame_center",loc,cmpr) {}
49   };
50
51   int Allocate();
52   int Construct();
53   int Destruct();
54   void Deallocate();
55 };
56 // Timeline
57 DbObj(Timeline)
58   basic_def(unsigned int,Clip_id);
59   basic_def(unsigned int,Sequence_no);
60   basic_def(unsigned int,Frame_id);
61   basic_def(unsigned int,Group);
62   basic_def(double,Time_offset);
63 };
64
65 DbLoc(Timeline)
66   basic_ref(unsigned int,Clip_id);
67   basic_ref(unsigned int,Sequence_no);
68   basic_ref(unsigned int,Frame_id);
69   basic_ref(unsigned int,Group);
70   basic_ref(double,Time_offset);
71
72   class ikey_Timelines : public Db::iKey { public:
73     TimelineObj::t_Frame_id v_Frame_id;
74     int v_id;
75     static int cmpr(char *a, char *b);
76     ikey_Timelines(ObjectLoc &loc,
77         unsigned int Frame_id, int id=-1)
78     : iKey("Timelines",loc,cmpr),
79       v_Frame_id(Frame_id),
80       v_id(id) {}
81   };
82   class rkey_Timelines : public Db::rKey { public:
83     static int cmpr(char *a, char *b);
84     rkey_Timelines(ObjectLoc &loc) : rKey("Timelines",loc,cmpr) {}
85   };
86
87   class ikey_Sequences : public Db::iKey { public:
88     TimelineObj::t_Clip_id v_Clip_id;
89     TimelineObj::t_Sequence_no v_Sequence_no;
90     static int cmpr(char *a, char *b);
91     ikey_Sequences(ObjectLoc &loc,
92         unsigned int Clip_id,
93         unsigned int Sequence_no)
94     : iKey("Sequences",loc,cmpr),
95       v_Clip_id(Clip_id),
96       v_Sequence_no(Sequence_no) {}
97   };
98   class rkey_Sequences : public Db::rKey { public:
99     static int cmpr(char *a, char *b);
100     rkey_Sequences(ObjectLoc &loc) : rKey("Sequences",loc,cmpr) {}
101   };
102
103   int Allocate();
104   int Construct();
105   int Destruct();
106   void Deallocate();
107 };
108 // Clip_set
109 DbObj(Clip_set)
110   sarray_def(char,Title);
111   sarray_def(char,Asset_path);
112   basic_def(double,Position);
113   basic_def(double,Framerate);
114   basic_def(double,Average_weight);
115   basic_def(unsigned int,Frames);
116   basic_def(unsigned int,Prefix_size);
117   basic_def(unsigned int,Suffix_size);
118   varray_def(unsigned char,Weights);
119   basic_def(long,System_time);
120   basic_def(long,Creation_time);
121 };
122
123 DbLoc(Clip_set)
124   sarray_ref(char,Title);
125   sarray_ref(char,Asset_path);
126   basic_ref(double,Position);
127   basic_ref(double,Framerate);
128   basic_ref(double,Average_weight);
129   basic_ref(unsigned int,Frames);
130   basic_ref(unsigned int,Prefix_size);
131   basic_ref(unsigned int,Suffix_size);
132   varray_ref(unsigned char,Weights);
133   basic_ref(long,System_time);
134   basic_ref(long,Creation_time);
135
136   class ikey_Clip_title : public Db::iKey { public:
137     Clip_setObj::t_Title v_Title;
138     int v_id;
139     static int cmpr(char *a, char *b);
140     ikey_Clip_title(ObjectLoc &loc,
141         const Clip_setObj::t_Title &Title, int id=-1)
142     : iKey("Clip_title",loc,cmpr),
143       v_Title(Title),
144       v_id(id) {}
145   };
146   class rkey_Clip_title : public Db::rKey { public:
147     static int cmpr(char *a, char *b);
148     rkey_Clip_title(ObjectLoc &loc) : rKey("Clip_title",loc,cmpr) {}
149   };
150
151   class ikey_Clip_system_time : public Db::iKey { public:
152     Clip_setObj::t_System_time v_System_time;
153     int v_id;
154     static int cmpr(char *a, char *b);
155     ikey_Clip_system_time(ObjectLoc &loc,
156         long System_time, int id=-1)
157     : iKey("Clip_system_time",loc,cmpr),
158       v_System_time(System_time),
159       v_id(id) {}
160   };
161   class rkey_Clip_system_time : public Db::rKey { public:
162     static int cmpr(char *a, char *b);
163     rkey_Clip_system_time(ObjectLoc &loc) : rKey("Clip_system_time",loc,cmpr) {}
164   };
165
166   class ikey_Clip_creation_time : public Db::iKey { public:
167     Clip_setObj::t_Creation_time v_Creation_time;
168     int v_id;
169     static int cmpr(char *a, char *b);
170     ikey_Clip_creation_time(ObjectLoc &loc,
171         long Creation_time, int id=-1)
172     : iKey("Clip_creation_time",loc,cmpr),
173       v_Creation_time(Creation_time),
174       v_id(id) {}
175   };
176   class rkey_Clip_creation_time : public Db::rKey { public:
177     static int cmpr(char *a, char *b);
178     rkey_Clip_creation_time(ObjectLoc &loc) : rKey("Clip_creation_time",loc,cmpr) {}
179   };
180
181   int Allocate();
182   int Construct();
183   int Destruct();
184   void Deallocate();
185 };
186 // Clip_views
187 DbObj(Clip_views)
188   basic_def(unsigned int,Access_clip_id);
189   basic_def(long,Access_time);
190   basic_def(unsigned int,Access_count);
191 };
192
193 DbLoc(Clip_views)
194   basic_ref(unsigned int,Access_clip_id);
195   basic_ref(long,Access_time);
196   basic_ref(unsigned int,Access_count);
197
198   class ikey_Clip_access : public Db::iKey { public:
199     Clip_viewsObj::t_Access_clip_id v_Access_clip_id;
200     static int cmpr(char *a, char *b);
201     ikey_Clip_access(ObjectLoc &loc,
202         unsigned int Access_clip_id)
203     : iKey("Clip_access",loc,cmpr),
204       v_Access_clip_id(Access_clip_id) {}
205   };
206   class rkey_Clip_access : public Db::rKey { public:
207     static int cmpr(char *a, char *b);
208     rkey_Clip_access(ObjectLoc &loc) : rKey("Clip_access",loc,cmpr) {}
209   };
210
211   class ikey_Last_view : public Db::iKey { public:
212     Clip_viewsObj::t_Access_time v_Access_time;
213     int v_id;
214     static int cmpr(char *a, char *b);
215     ikey_Last_view(ObjectLoc &loc,
216         long Access_time, int id=-1)
217     : iKey("Last_view",loc,cmpr),
218       v_Access_time(Access_time),
219       v_id(id) {}
220   };
221   class rkey_Last_view : public Db::rKey { public:
222     static int cmpr(char *a, char *b);
223     rkey_Last_view(ObjectLoc &loc) : rKey("Last_view",loc,cmpr) {}
224   };
225
226   class ikey_Total_views : public Db::iKey { public:
227     Clip_viewsObj::t_Access_count v_Access_count;
228     Clip_viewsObj::t_Access_clip_id v_Access_clip_id;
229     int v_id;
230     static int cmpr(char *a, char *b);
231     ikey_Total_views(ObjectLoc &loc,
232         unsigned int Access_count,
233         unsigned int Access_clip_id, int id=-1)
234     : iKey("Total_views",loc,cmpr),
235       v_Access_count(Access_count),
236       v_Access_clip_id(Access_clip_id),
237       v_id(id) {}
238   };
239   class rkey_Total_views : public Db::rKey { public:
240     static int cmpr(char *a, char *b);
241     rkey_Total_views(ObjectLoc &loc) : rKey("Total_views",loc,cmpr) {}
242   };
243
244   int Allocate();
245   int Construct();
246   int Destruct();
247   void Deallocate();
248 };
249
250 int Video_frameLoc::ikey_Frame_weight::
251 cmpr(char *a, char *b)
252 {
253   ikey_Frame_weight *kp = (ikey_Frame_weight *)a;
254   int v = *(int*)b;
255   if( kp->v_id == v ) return 0;
256   Video_frameLoc vloc(kp->loc.entity);
257   if( vloc.FindId(v) )
258     vloc.err_(Db::errCorrupt);
259   v = cmpr_double( kp->v_Frame_mean.addr(), kp->v_Frame_mean.size(),
260                   vloc._Frame_mean(), vloc->v_Frame_mean.size());
261   if( v != 0 ) return v;
262   if( kp->v_id >= 0 ) {
263     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
264                  vloc._id(), vloc._id_size());
265     if( v != 0 ) return v;
266   }
267   return 0;
268 }
269
270 int Video_frameLoc::rkey_Frame_weight::
271 cmpr(char *a, char *b)
272 {
273   rkey_Frame_weight *kp = (rkey_Frame_weight *)a;
274   Video_frameLoc &kloc = (Video_frameLoc&)kp->loc;
275   int v = kloc->id, b_id = *(int*)b;
276   if( v == b_id ) return 0;
277   Video_frameLoc vloc(kloc.entity);
278   if( vloc.FindId(b_id) )
279     kloc.err_(Db::errCorrupt);
280   v = cmpr_double( kloc._Frame_mean(), kloc->v_Frame_mean.size(),
281                    vloc._Frame_mean(), vloc->v_Frame_mean.size());
282   if( v != 0 ) return v;
283   v = cmpr_int(kloc._id(), kloc._id_size(),
284                vloc._id(), vloc._id_size());
285   if( v != 0 ) return v;
286   return 0;
287 }
288
289 int Video_frameLoc::ikey_Frame_center::
290 cmpr(char *a, char *b)
291 {
292   ikey_Frame_center *kp = (ikey_Frame_center *)a;
293   int v = *(int*)b;
294   if( kp->v_id == v ) return 0;
295   Video_frameLoc vloc(kp->loc.entity);
296   if( vloc.FindId(v) )
297     vloc.err_(Db::errCorrupt);
298   v = cmpr_double( kp->v_Frame_moment.addr(), kp->v_Frame_moment.size(),
299                   vloc._Frame_moment(), vloc->v_Frame_moment.size());
300   if( v != 0 ) return v;
301   if( kp->v_id >= 0 ) {
302     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
303                  vloc._id(), vloc._id_size());
304     if( v != 0 ) return v;
305   }
306   return 0;
307 }
308
309 int Video_frameLoc::rkey_Frame_center::
310 cmpr(char *a, char *b)
311 {
312   rkey_Frame_center *kp = (rkey_Frame_center *)a;
313   Video_frameLoc &kloc = (Video_frameLoc&)kp->loc;
314   int v = kloc->id, b_id = *(int*)b;
315   if( v == b_id ) return 0;
316   Video_frameLoc vloc(kloc.entity);
317   if( vloc.FindId(b_id) )
318     kloc.err_(Db::errCorrupt);
319   v = cmpr_double( kloc._Frame_moment(), kloc->v_Frame_moment.size(),
320                    vloc._Frame_moment(), vloc->v_Frame_moment.size());
321   if( v != 0 ) return v;
322   v = cmpr_int(kloc._id(), kloc._id_size(),
323                vloc._id(), vloc._id_size());
324   if( v != 0 ) return v;
325   return 0;
326 }
327
328 int Video_frameLoc::Allocate()
329 {
330   if_err( allocate() );
331   if( !addr_wr() ) return err_(Db::errNoMemory);
332   v_init();
333   return 0;
334 }
335
336 int Video_frameLoc::Construct()
337 {
338   if_err( insertProhibit() );
339   if_err( construct() );
340   int id = this->id();
341   { rkey_Frame_weight rkey(*this);
342     if_err( entity->index("Frame_weight")->Insert(rkey,&id) ); }
343   { rkey_Frame_center rkey(*this);
344     if_err( entity->index("Frame_center")->Insert(rkey,&id) ); }
345   if_err( insertCascade() );
346   return 0;
347 }
348
349 int Video_frameLoc::Destruct()
350 {
351   if_err( deleteProhibit() );
352   { rkey_Frame_weight rkey(*this);
353     if_err( entity->index("Frame_weight")->Delete(rkey) ); }
354   { rkey_Frame_center rkey(*this);
355     if_err( entity->index("Frame_center")->Delete(rkey) ); }
356   if_err( destruct() );
357   if_err( deleteCascade() );
358   return 0;
359 }
360
361 void Video_frameLoc::Deallocate()
362 {
363   v_del();
364   deallocate();
365 }
366
367
368 int TimelineLoc::ikey_Timelines::
369 cmpr(char *a, char *b)
370 {
371   ikey_Timelines *kp = (ikey_Timelines *)a;
372   int v = *(int*)b;
373   if( kp->v_id == v ) return 0;
374   TimelineLoc vloc(kp->loc.entity);
375   if( vloc.FindId(v) )
376     vloc.err_(Db::errCorrupt);
377   v = cmpr_uint( kp->v_Frame_id.addr(), kp->v_Frame_id.size(),
378                   vloc._Frame_id(), vloc->v_Frame_id.size());
379   if( v != 0 ) return v;
380   if( kp->v_id >= 0 ) {
381     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
382                  vloc._id(), vloc._id_size());
383     if( v != 0 ) return v;
384   }
385   return 0;
386 }
387
388 int TimelineLoc::rkey_Timelines::
389 cmpr(char *a, char *b)
390 {
391   rkey_Timelines *kp = (rkey_Timelines *)a;
392   TimelineLoc &kloc = (TimelineLoc&)kp->loc;
393   int v = kloc->id, b_id = *(int*)b;
394   if( v == b_id ) return 0;
395   TimelineLoc vloc(kloc.entity);
396   if( vloc.FindId(b_id) )
397     kloc.err_(Db::errCorrupt);
398   v = cmpr_uint( kloc._Frame_id(), kloc->v_Frame_id.size(),
399                    vloc._Frame_id(), vloc->v_Frame_id.size());
400   if( v != 0 ) return v;
401   v = cmpr_int(kloc._id(), kloc._id_size(),
402                vloc._id(), vloc._id_size());
403   if( v != 0 ) return v;
404   return 0;
405 }
406
407 int TimelineLoc::ikey_Sequences::
408 cmpr(char *a, char *b)
409 {
410   ikey_Sequences *kp = (ikey_Sequences *)a;
411   int v = *(int*)b;
412   TimelineLoc vloc(kp->loc.entity);
413   if( vloc.FindId(v) )
414     vloc.err_(Db::errCorrupt);
415   v = cmpr_uint( kp->v_Clip_id.addr(), kp->v_Clip_id.size(),
416                   vloc._Clip_id(), vloc->v_Clip_id.size());
417   if( v != 0 ) return v;
418   v = cmpr_uint( kp->v_Sequence_no.addr(), kp->v_Sequence_no.size(),
419                   vloc._Sequence_no(), vloc->v_Sequence_no.size());
420   if( v != 0 ) return v;
421   return 0;
422 }
423
424 int TimelineLoc::rkey_Sequences::
425 cmpr(char *a, char *b)
426 {
427   rkey_Sequences *kp = (rkey_Sequences *)a;
428   TimelineLoc &kloc = (TimelineLoc&)kp->loc;
429   int v = kloc->id, b_id = *(int*)b;
430   if( v == b_id ) return 0;
431   TimelineLoc vloc(kloc.entity);
432   if( vloc.FindId(b_id) )
433     kloc.err_(Db::errCorrupt);
434   v = cmpr_uint( kloc._Clip_id(), kloc->v_Clip_id.size(),
435                    vloc._Clip_id(), vloc->v_Clip_id.size());
436   if( v != 0 ) return v;
437   v = cmpr_uint( kloc._Sequence_no(), kloc->v_Sequence_no.size(),
438                    vloc._Sequence_no(), vloc->v_Sequence_no.size());
439   if( v != 0 ) return v;
440   return 0;
441 }
442
443 int TimelineLoc::Allocate()
444 {
445   if_err( allocate() );
446   if( !addr_wr() ) return err_(Db::errNoMemory);
447   return 0;
448 }
449
450 int TimelineLoc::Construct()
451 {
452   if_err( insertProhibit() );
453   if_err( construct() );
454   int id = this->id();
455   { rkey_Timelines rkey(*this);
456     if_err( entity->index("Timelines")->Insert(rkey,&id) ); }
457   { rkey_Sequences rkey(*this);
458     if_err( entity->index("Sequences")->Insert(rkey,&id) ); }
459   if_err( insertCascade() );
460   return 0;
461 }
462
463 int TimelineLoc::Destruct()
464 {
465   if_err( deleteProhibit() );
466   { rkey_Timelines rkey(*this);
467     if_err( entity->index("Timelines")->Delete(rkey) ); }
468   { rkey_Sequences rkey(*this);
469     if_err( entity->index("Sequences")->Delete(rkey) ); }
470   if_err( destruct() );
471   if_err( deleteCascade() );
472   return 0;
473 }
474
475 void TimelineLoc::Deallocate()
476 {
477   deallocate();
478 }
479
480
481 int Clip_setLoc::ikey_Clip_title::
482 cmpr(char *a, char *b)
483 {
484   ikey_Clip_title *kp = (ikey_Clip_title *)a;
485   int v = *(int*)b;
486   if( kp->v_id == v ) return 0;
487   Clip_setLoc vloc(kp->loc.entity);
488   if( vloc.FindId(v) )
489     vloc.err_(Db::errCorrupt);
490   v = cmpr_char( kp->v_Title.addr(), kp->v_Title.size(),
491                   vloc._Title(), vloc->v_Title.size());
492   if( v != 0 ) return v;
493   if( kp->v_id >= 0 ) {
494     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
495                  vloc._id(), vloc._id_size());
496     if( v != 0 ) return v;
497   }
498   return 0;
499 }
500
501 int Clip_setLoc::rkey_Clip_title::
502 cmpr(char *a, char *b)
503 {
504   rkey_Clip_title *kp = (rkey_Clip_title *)a;
505   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
506   int v = kloc->id, b_id = *(int*)b;
507   if( v == b_id ) return 0;
508   Clip_setLoc vloc(kloc.entity);
509   if( vloc.FindId(b_id) )
510     kloc.err_(Db::errCorrupt);
511   v = cmpr_char( kloc._Title(), kloc->v_Title.size(),
512                    vloc._Title(), vloc->v_Title.size());
513   if( v != 0 ) return v;
514   v = cmpr_int(kloc._id(), kloc._id_size(),
515                vloc._id(), vloc._id_size());
516   if( v != 0 ) return v;
517   return 0;
518 }
519
520 int Clip_setLoc::ikey_Clip_system_time::
521 cmpr(char *a, char *b)
522 {
523   ikey_Clip_system_time *kp = (ikey_Clip_system_time *)a;
524   int v = *(int*)b;
525   if( kp->v_id == v ) return 0;
526   Clip_setLoc vloc(kp->loc.entity);
527   if( vloc.FindId(v) )
528     vloc.err_(Db::errCorrupt);
529   v = cmpr_long( kp->v_System_time.addr(), kp->v_System_time.size(),
530                   vloc._System_time(), vloc->v_System_time.size());
531   if( v != 0 ) return v;
532   if( kp->v_id >= 0 ) {
533     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
534                  vloc._id(), vloc._id_size());
535     if( v != 0 ) return v;
536   }
537   return 0;
538 }
539
540 int Clip_setLoc::rkey_Clip_system_time::
541 cmpr(char *a, char *b)
542 {
543   rkey_Clip_system_time *kp = (rkey_Clip_system_time *)a;
544   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
545   int v = kloc->id, b_id = *(int*)b;
546   if( v == b_id ) return 0;
547   Clip_setLoc vloc(kloc.entity);
548   if( vloc.FindId(b_id) )
549     kloc.err_(Db::errCorrupt);
550   v = cmpr_long( kloc._System_time(), kloc->v_System_time.size(),
551                    vloc._System_time(), vloc->v_System_time.size());
552   if( v != 0 ) return v;
553   v = cmpr_int(kloc._id(), kloc._id_size(),
554                vloc._id(), vloc._id_size());
555   if( v != 0 ) return v;
556   return 0;
557 }
558
559 int Clip_setLoc::ikey_Clip_creation_time::
560 cmpr(char *a, char *b)
561 {
562   ikey_Clip_creation_time *kp = (ikey_Clip_creation_time *)a;
563   int v = *(int*)b;
564   if( kp->v_id == v ) return 0;
565   Clip_setLoc vloc(kp->loc.entity);
566   if( vloc.FindId(v) )
567     vloc.err_(Db::errCorrupt);
568   v = cmpr_long( kp->v_Creation_time.addr(), kp->v_Creation_time.size(),
569                   vloc._Creation_time(), vloc->v_Creation_time.size());
570   if( v != 0 ) return v;
571   if( kp->v_id >= 0 ) {
572     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
573                  vloc._id(), vloc._id_size());
574     if( v != 0 ) return v;
575   }
576   return 0;
577 }
578
579 int Clip_setLoc::rkey_Clip_creation_time::
580 cmpr(char *a, char *b)
581 {
582   rkey_Clip_creation_time *kp = (rkey_Clip_creation_time *)a;
583   Clip_setLoc &kloc = (Clip_setLoc&)kp->loc;
584   int v = kloc->id, b_id = *(int*)b;
585   if( v == b_id ) return 0;
586   Clip_setLoc vloc(kloc.entity);
587   if( vloc.FindId(b_id) )
588     kloc.err_(Db::errCorrupt);
589   v = cmpr_long( kloc._Creation_time(), kloc->v_Creation_time.size(),
590                    vloc._Creation_time(), vloc->v_Creation_time.size());
591   if( v != 0 ) return v;
592   v = cmpr_int(kloc._id(), kloc._id_size(),
593                vloc._id(), vloc._id_size());
594   if( v != 0 ) return v;
595   return 0;
596 }
597
598 int Clip_setLoc::Allocate()
599 {
600   if_err( allocate() );
601   if( !addr_wr() ) return err_(Db::errNoMemory);
602   v_init();
603   Title((char *)"",0);
604   Asset_path((char *)"",0);
605   return 0;
606 }
607
608 int Clip_setLoc::Construct()
609 {
610   if_err( insertProhibit() );
611   if_err( construct() );
612   int id = this->id();
613   { rkey_Clip_title rkey(*this);
614     if_err( entity->index("Clip_title")->Insert(rkey,&id) ); }
615   { rkey_Clip_system_time rkey(*this);
616     if_err( entity->index("Clip_system_time")->Insert(rkey,&id) ); }
617   { rkey_Clip_creation_time rkey(*this);
618     if_err( entity->index("Clip_creation_time")->Insert(rkey,&id) ); }
619   if_err( insertCascade() );
620   return 0;
621 }
622
623 int Clip_setLoc::Destruct()
624 {
625   if_err( deleteProhibit() );
626   { rkey_Clip_title rkey(*this);
627     if_err( entity->index("Clip_title")->Delete(rkey) ); }
628   { rkey_Clip_system_time rkey(*this);
629     if_err( entity->index("Clip_system_time")->Delete(rkey) ); }
630   { rkey_Clip_creation_time rkey(*this);
631     if_err( entity->index("Clip_creation_time")->Delete(rkey) ); }
632   if_err( destruct() );
633   if_err( deleteCascade() );
634   return 0;
635 }
636
637 void Clip_setLoc::Deallocate()
638 {
639   v_del();
640   deallocate();
641 }
642
643
644 int Clip_viewsLoc::ikey_Clip_access::
645 cmpr(char *a, char *b)
646 {
647   ikey_Clip_access *kp = (ikey_Clip_access *)a;
648   int v = *(int*)b;
649   Clip_viewsLoc vloc(kp->loc.entity);
650   if( vloc.FindId(v) )
651     vloc.err_(Db::errCorrupt);
652   v = cmpr_uint( kp->v_Access_clip_id.addr(), kp->v_Access_clip_id.size(),
653                   vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
654   if( v != 0 ) return v;
655   return 0;
656 }
657
658 int Clip_viewsLoc::rkey_Clip_access::
659 cmpr(char *a, char *b)
660 {
661   rkey_Clip_access *kp = (rkey_Clip_access *)a;
662   Clip_viewsLoc &kloc = (Clip_viewsLoc&)kp->loc;
663   int v = kloc->id, b_id = *(int*)b;
664   if( v == b_id ) return 0;
665   Clip_viewsLoc vloc(kloc.entity);
666   if( vloc.FindId(b_id) )
667     kloc.err_(Db::errCorrupt);
668   v = cmpr_uint( kloc._Access_clip_id(), kloc->v_Access_clip_id.size(),
669                    vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
670   if( v != 0 ) return v;
671   return 0;
672 }
673
674 int Clip_viewsLoc::ikey_Last_view::
675 cmpr(char *a, char *b)
676 {
677   ikey_Last_view *kp = (ikey_Last_view *)a;
678   int v = *(int*)b;
679   if( kp->v_id == v ) return 0;
680   Clip_viewsLoc vloc(kp->loc.entity);
681   if( vloc.FindId(v) )
682     vloc.err_(Db::errCorrupt);
683   v = cmpr_long( kp->v_Access_time.addr(), kp->v_Access_time.size(),
684                   vloc._Access_time(), vloc->v_Access_time.size());
685   if( v != 0 ) return v;
686   if( kp->v_id >= 0 ) {
687     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
688                  vloc._id(), vloc._id_size());
689     if( v != 0 ) return v;
690   }
691   return 0;
692 }
693
694 int Clip_viewsLoc::rkey_Last_view::
695 cmpr(char *a, char *b)
696 {
697   rkey_Last_view *kp = (rkey_Last_view *)a;
698   Clip_viewsLoc &kloc = (Clip_viewsLoc&)kp->loc;
699   int v = kloc->id, b_id = *(int*)b;
700   if( v == b_id ) return 0;
701   Clip_viewsLoc vloc(kloc.entity);
702   if( vloc.FindId(b_id) )
703     kloc.err_(Db::errCorrupt);
704   v = cmpr_long( kloc._Access_time(), kloc->v_Access_time.size(),
705                    vloc._Access_time(), vloc->v_Access_time.size());
706   if( v != 0 ) return v;
707   v = cmpr_int(kloc._id(), kloc._id_size(),
708                vloc._id(), vloc._id_size());
709   if( v != 0 ) return v;
710   return 0;
711 }
712
713 int Clip_viewsLoc::ikey_Total_views::
714 cmpr(char *a, char *b)
715 {
716   ikey_Total_views *kp = (ikey_Total_views *)a;
717   int v = *(int*)b;
718   if( kp->v_id == v ) return 0;
719   Clip_viewsLoc vloc(kp->loc.entity);
720   if( vloc.FindId(v) )
721     vloc.err_(Db::errCorrupt);
722   v = cmpr_uint( kp->v_Access_count.addr(), kp->v_Access_count.size(),
723                   vloc._Access_count(), vloc->v_Access_count.size());
724   if( v != 0 ) return v;
725   v = cmpr_uint( kp->v_Access_clip_id.addr(), kp->v_Access_clip_id.size(),
726                   vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
727   if( v != 0 ) return v;
728   if( kp->v_id >= 0 ) {
729     v = cmpr_int(&kp->v_id, sizeof(kp->v_id),
730                  vloc._id(), vloc._id_size());
731     if( v != 0 ) return v;
732   }
733   return 0;
734 }
735
736 int Clip_viewsLoc::rkey_Total_views::
737 cmpr(char *a, char *b)
738 {
739   rkey_Total_views *kp = (rkey_Total_views *)a;
740   Clip_viewsLoc &kloc = (Clip_viewsLoc&)kp->loc;
741   int v = kloc->id, b_id = *(int*)b;
742   if( v == b_id ) return 0;
743   Clip_viewsLoc vloc(kloc.entity);
744   if( vloc.FindId(b_id) )
745     kloc.err_(Db::errCorrupt);
746   v = cmpr_uint( kloc._Access_count(), kloc->v_Access_count.size(),
747                    vloc._Access_count(), vloc->v_Access_count.size());
748   if( v != 0 ) return v;
749   v = cmpr_uint( kloc._Access_clip_id(), kloc->v_Access_clip_id.size(),
750                    vloc._Access_clip_id(), vloc->v_Access_clip_id.size());
751   if( v != 0 ) return v;
752   v = cmpr_int(kloc._id(), kloc._id_size(),
753                vloc._id(), vloc._id_size());
754   if( v != 0 ) return v;
755   return 0;
756 }
757
758 int Clip_viewsLoc::Allocate()
759 {
760   if_err( allocate() );
761   if( !addr_wr() ) return err_(Db::errNoMemory);
762   return 0;
763 }
764
765 int Clip_viewsLoc::Construct()
766 {
767   if_err( insertProhibit() );
768   if_err( construct() );
769   int id = this->id();
770   { rkey_Clip_access rkey(*this);
771     if_err( entity->index("Clip_access")->Insert(rkey,&id) ); }
772   { rkey_Last_view rkey(*this);
773     if_err( entity->index("Last_view")->Insert(rkey,&id) ); }
774   { rkey_Total_views rkey(*this);
775     if_err( entity->index("Total_views")->Insert(rkey,&id) ); }
776   if_err( insertCascade() );
777   return 0;
778 }
779
780 int Clip_viewsLoc::Destruct()
781 {
782   if_err( deleteProhibit() );
783   { rkey_Clip_access rkey(*this);
784     if_err( entity->index("Clip_access")->Delete(rkey) ); }
785   { rkey_Last_view rkey(*this);
786     if_err( entity->index("Last_view")->Delete(rkey) ); }
787   { rkey_Total_views rkey(*this);
788     if_err( entity->index("Total_views")->Delete(rkey) ); }
789   if_err( destruct() );
790   if_err( deleteCascade() );
791   return 0;
792 }
793
794 void Clip_viewsLoc::Deallocate()
795 {
796   deallocate();
797 }
798
799 class theDb : public Db {
800   int dfd, dkey;
801   int db_create();
802   int db_open();
803   int db_access();
804 public:
805   Objects objects;
806   Entity Video_frame;  Video_frameLoc video_frame;
807   Entity Timeline;  TimelineLoc timeline;
808   Entity Clip_set;  Clip_setLoc clip_set;
809   Entity Clip_views;  Clip_viewsLoc clip_views;
810
811   int create(const char *dfn);
812   int open(const char *dfn, int key=-1);
813   int access(const char *dfn, int key=-1, int rw=0);
814   void close();
815   int attach(int rw=0) { return Db::attach(rw); }
816   int detach() { return Db::detach(); }
817
818   theDb();
819   ~theDb() { finit(objects); }
820 };
821
822
823
824 #include <stdio.h>
825 #include <stdlib.h>
826 #include <unistd.h>
827 #include <fcntl.h>
828 #include <errno.h>
829
830 int theDb::
831 create(const char *dfn)
832 {
833   dfd = ::open(dfn,O_RDWR+O_CREAT+O_TRUNC+O_NOATIME,0666);
834   if( dfd < 0 ) { perror(dfn); return -1; }
835   int ret = db_create();
836   close();
837   return ret;
838 }
839
840 int theDb::
841 db_create()
842 {
843   if_ret( Db::make(dfd) );
844   if_ret( Video_frame.new_entity("Video_frame", sizeof(Video_frameObj)) );
845   if_ret( Video_frame.add_kindex("Frame_weight") );
846   if_ret( Video_frame.add_kindex("Frame_center") );
847
848   if_ret( Timeline.new_entity("Timeline", sizeof(TimelineObj)) );
849   if_ret( Timeline.add_kindex("Timelines") );
850   if_ret( Timeline.add_kindex("Sequences") );
851
852   if_ret( Clip_set.new_entity("Clip_set", sizeof(Clip_setObj)) );
853   if_ret( Clip_set.add_kindex("Clip_title") );
854   if_ret( Clip_set.add_kindex("Clip_system_time") );
855   if_ret( Clip_set.add_kindex("Clip_creation_time") );
856
857   if_ret( Clip_views.new_entity("Clip_views", sizeof(Clip_viewsObj)) );
858   if_ret( Clip_views.add_kindex("Clip_access") );
859   if_ret( Clip_views.add_kindex("Last_view") );
860   if_ret( Clip_views.add_kindex("Total_views") );
861
862   if_ret( Db::commit(1) );
863   return 0;
864 }
865
866 theDb::
867 theDb()
868  : dfd(-1), dkey(-1), objects(0),
869    Video_frame(this), video_frame(Video_frame),
870    Timeline(this), timeline(Timeline),
871    Clip_set(this), clip_set(Clip_set),
872    Clip_views(this), clip_views(Clip_views)
873 {
874   objects = new ObjectList(objects, video_frame);
875   objects = new ObjectList(objects, timeline);
876   objects = new ObjectList(objects, clip_set);
877   objects = new ObjectList(objects, clip_views);
878   Video_frame.add_vref((vRef)&Video_frameObj::v_Frame_data);
879   Clip_set.add_vref((vRef)&Clip_setObj::v_Title);
880   Clip_set.add_vref((vRef)&Clip_setObj::v_Asset_path);
881   Clip_set.add_vref((vRef)&Clip_setObj::v_Weights);
882 }
883
884 int theDb::
885 open(const char *dfn, int key)
886 {
887   dfd = ::open(dfn,O_RDWR+O_NOATIME);
888   if( dfd < 0 ) { perror(dfn); return errNotFound; }
889   if( (dkey=key) >= 0 ) Db::use_shm(1);
890   int ret = Db::open(dfd, dkey);
891   if( !ret ) ret = db_open();
892   if( ret ) close();
893   return ret;
894 }
895
896 int theDb::
897 db_open()
898 {
899   if_ret( Video_frame.get_entity("Video_frame") );
900   if_ret( Video_frame.key_index("Frame_weight") );
901   if_ret( Video_frame.key_index("Frame_center") );
902
903   if_ret( Timeline.get_entity("Timeline") );
904   if_ret( Timeline.key_index("Timelines") );
905   if_ret( Timeline.key_index("Sequences") );
906
907   if_ret( Clip_set.get_entity("Clip_set") );
908   if_ret( Clip_set.key_index("Clip_title") );
909   if_ret( Clip_set.key_index("Clip_system_time") );
910   if_ret( Clip_set.key_index("Clip_creation_time") );
911
912   if_ret( Clip_views.get_entity("Clip_views") );
913   if_ret( Clip_views.key_index("Clip_access") );
914   if_ret( Clip_views.key_index("Last_view") );
915   if_ret( Clip_views.key_index("Total_views") );
916
917   if_ret( Db::start_transaction() );
918   return 0;
919 }
920
921 void theDb::
922 close()
923 {
924   Db::close();
925   if( dfd >= 0 ) { ::close(dfd); dfd = -1; }
926 }
927
928 int theDb::
929 access(const char *dfn, int key, int rw)
930 {
931   if( key < 0 ) return Db::errInvalid;
932   dfd = ::open(dfn,O_RDWR+O_NOATIME);
933   if( dfd < 0 ) { perror(dfn); return Db::errNotFound; }
934   dkey = key;  Db::use_shm(1);
935   int ret = Db::attach(dfd, dkey, rw);
936   if( !ret ) ret = db_access();
937   else if( ret == errNotFound ) {
938     ret = Db::open(dfd, dkey);
939     if( !ret ) ret = db_open();
940     if( !ret ) ret = Db::attach(rw);
941   }
942   if( ret ) close();
943   return ret;
944 }
945
946 int theDb::
947 db_access()
948 {
949   if_ret( Video_frame.get_entity("Video_frame") );
950   if_ret( Timeline.get_entity("Timeline") );
951   if_ret( Clip_set.get_entity("Clip_set") );
952   if_ret( Clip_views.get_entity("Clip_views") );
953   return 0;
954 }
955
956