initial commit
[goodguy/history.git] / cinelerra-5.0 / db / utils / cpdb.C
1 #include <cstdio>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <time.h>
6 #include <sys/time.h>
7
8 #include "../s.C"
9
10 using namespace std;
11
12 // c++ -ggdb cpydb.C
13
14 double
15 runtime(struct timeval *st)
16 {
17   struct timeval tv;
18   gettimeofday(&tv,0);
19   double dt = (tv.tv_sec - st->tv_sec) +
20     (tv.tv_usec - st->tv_usec) / 1000000.;
21   return dt;
22 }
23
24 int del_clip_set(theDb *db, int clip_id)
25 {
26 printf("del clip %d\n",clip_id);
27         if( db->clip_set.FindId(clip_id) ) return 1;
28         db->clip_set.Destruct();
29         db->clip_set.Deallocate();
30
31         if( Clip_viewsLoc::ikey_Clip_access(db->clip_views,clip_id).Find() ) return 1;
32         db->clip_views.Destruct();
33         db->clip_views.Deallocate();
34
35         while( !TimelineLoc::ikey_Sequences(db->timeline,clip_id,0).Locate() ) {
36                 if( clip_id != (int)db->timeline.Clip_id() ) break;
37                 int frame_id = db->timeline.Frame_id();
38                 db->timeline.Destruct();
39                 db->timeline.Deallocate();
40                 if( !TimelineLoc::ikey_Timelines(db->timeline, frame_id).Locate() &&
41                         frame_id == (int)db->timeline.Frame_id() ) continue;
42                 if( db->video_frame.FindId(frame_id) ) continue;
43                 db->video_frame.Destruct();
44                 db->video_frame.Deallocate();
45         }
46         return 0;
47 }
48
49
50 int main(int ac, char **av)
51 {
52   setbuf(stdout,0);
53   setbuf(stderr,0);
54
55   theDb idb;
56   Db odb;
57   if( ac < 3 ) { printf("usage: %s in.db out.db\n",av[0]); exit(1); }
58   const char *ifn = av[1];
59   if( idb.open(ifn) ) { perror(ifn); exit(1); }
60   const char *ofn = av[2];
61   remove(ofn);
62   int ofd = open(ofn,O_RDWR+O_CREAT+O_TRUNC,0666);
63   if( ofd < 0 ) { perror(ofn); return 1; }
64   if( odb.make(ofd) ) { perror(ofn); exit(1); }
65   struct timeval st;
66   gettimeofday(&st,0);
67
68 #if 1
69   int next_id = 0;
70   while( !idb.clip_set.LocateId(Db::keyGE,next_id) ) {
71     next_id = idb.clip_set.id() + 1;
72     if( Clip_viewsLoc::ikey_Clip_access(idb.clip_views,idb.clip_set.id()).Find() ) {
73       printf("clip %d, missed\n", idb.clip_set.id()); continue;
74     }
75     time_t t = (time_t) idb.clip_views.Access_time();
76     long dt = st.tv_sec - t;
77     // 3 weeks + access count days
78     if( dt < 3*7*24*60*60 + idb.clip_views.Access_count()*24*60*60 )
79       continue;
80     del_clip_set(&idb, idb.clip_set.id());
81   }
82 #endif
83
84   odb.copy(&idb,idb.objects);
85   odb.commit(1);
86   odb.commit(1);
87   odb.close();
88   idb.close();
89
90   double secs = runtime(&st);
91   printf("%f secs\n",secs);
92   return 0;
93 }
94