]> mj.ucw.cz Git - libucw.git/blob - images/image-test.c
- parts of duplicates search, still very slow and full of insects
[libucw.git] / images / image-test.c
1 #undef LOCAL_DEBUG
2
3 #include "sherlock/sherlock.h"
4 #include "lib/fastbuf.h"
5 #include "images/images.h"
6 #include "images/image-sig.h"
7 #include "images/kd-tree.h"
8 #include "sherlock/index.h"
9 #include "lib/mempool.h"
10 #include "sherlock/object.h"
11 #include "sherlock/lizard-fb.h"
12 #include <fcntl.h>
13 #include <stdio.h>
14
15 #include "sherlock/sherlock.h"
16 #include "lib/fastbuf.h"
17 #include "images/images.h"
18 #include "sherlock/index.h"
19
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <alloca.h>
23
24 #define BEST_CNT 30
25
26 struct image_tree image_tree;
27
28 static void
29 image_tree_init(void)
30 {
31   DBG("Initializing image search structures");
32   struct fastbuf *fb = bopen("index/image-tree", O_RDONLY, 1 << 16); /* FIXME: filename hack */
33   image_tree.count = bgetl(fb);
34   image_tree.depth = bgetl(fb);
35   ASSERT(image_tree.count < 0x80000000 && image_tree.depth > 0 && image_tree.depth < 30);
36   image_tree.nodes = xmalloc((1 << image_tree.depth) * sizeof(struct image_node));
37   image_tree.leaves = xmalloc(image_tree.count * sizeof(struct image_leaf));
38   bread(fb, &image_tree.bbox, sizeof(struct image_bbox));
39   bread(fb, image_tree.nodes + 1, ((1 << image_tree.depth) - 1) * sizeof(struct image_node));
40   bread(fb, image_tree.leaves, image_tree.count * sizeof(struct image_leaf));
41   DBG("Search tree with depth %d and %d leaves loaded", image_tree.depth, image_tree.count);
42   bclose(fb);
43 }
44
45 static void
46 image_tree_done(void)
47 {
48   DBG("Freeing image search structures");
49   xfree(image_tree.nodes);
50   xfree(image_tree.leaves);
51 }
52   
53 int
54 main(int argc, char **argv)
55 {
56   struct image_vector query;
57   if (argc != IMAGE_VEC_K + 1)
58     die("Invalid number of arguments");
59
60   for (uns i = 0; i < IMAGE_VEC_K; i++)
61     {
62       uns v;
63       if (sscanf(argv[i + 1], "%d", &v) != 1)
64         die("Invalid numeric format");
65       query.f[i] = v;
66     }
67   
68   
69   struct image_search is;
70   oid_t best[BEST_CNT];
71   uns dist[BEST_CNT];
72   uns cardpos[BEST_CNT];
73   uns best_n = 0;
74   
75   image_tree_init();
76   log(L_INFO, "Executing query (%s)", stk_print_image_vector(&query));
77   image_search_init(&is, &image_tree, &query, IMAGE_SEARCH_DIST_UNLIMITED);
78   for (uns i = 0; i < BEST_CNT; i++)
79     {
80       if (!image_search_next(&is, best + i, dist + i))
81         {
82           log(L_INFO, "No more images");
83           break;
84         }
85       DBG("*** Found %d. best image with oid=%d", i + 1, best[i]);
86       best_n++;
87     }
88   image_search_done(&is);
89   image_tree_done();
90
91   log(L_INFO, "Resolving URLs");
92   struct mempool *pool = mp_new(1 << 16);
93   struct buck2obj_buf *bob = buck2obj_alloc();
94   struct fastbuf *fb = bopen("index/card-attrs", O_RDONLY, 1 << 10);
95   for (uns i = 0; i < best_n; i++)
96     {
97       bsetpos(fb, best[i] * sizeof(struct card_attr));
98       struct card_attr ca;
99       bread(fb, &ca, sizeof(ca));
100       cardpos[i] = ca.card;
101     }
102   bclose(fb);
103   fb = bopen("index/cards", O_RDONLY, 1 << 14);
104   for (uns i = 0; i < best_n; i++)
105     {
106       bsetpos(fb, (sh_off_t)cardpos[i] << CARD_POS_SHIFT);
107       uns buck_len = bgetl(fb) - (LIZARD_COMPRESS_HEADER - 1);
108       uns buck_type = bgetc(fb) + BUCKET_TYPE_PLAIN;
109       mp_flush(pool);
110       struct odes *obj = obj_read_bucket(bob, pool, buck_type, buck_len, fb, NULL);
111
112       printf("%2d. match: dist=%-8d oid=%-8d url=%s\n", i + 1, dist[i], best[i],
113           obj_find_aval(obj_find_attr(obj, 'U' + OBJ_ATTR_SON)->son, 'U'));
114     }
115   bclose(fb);
116   buck2obj_free(bob);
117   mp_delete(pool);
118      
119   return 0;
120 }
121