commit 83a68d6

default  ·  2022-12-10 10:19:26 +0000 UTC
parent 6fd5d14
New function index_len().
2 files changed,  +43, -8
M data.c
M snac.h
M data.c
+39, -8
 1@@ -333,6 +333,19 @@ int index_first(const char *fn, char *line, int size)
 2 }
 3 
 4 
 5+int index_len(const char *fn)
 6+/* returns the number of elements in an index */
 7+{
 8+    struct stat st;
 9+    int len = 0;
10+
11+    if (stat(fn, &st) != -1)
12+        len = st.st_size / 33;
13+
14+    return len;
15+}
16+
17+
18 d_char *index_list(const char *fn, int max)
19 /* returns an index as a list */
20 {
21@@ -588,31 +601,49 @@ int object_del_if_unref(const char *id)
22 }
23 
24 
25-d_char *_object_metadata(const char *id, const char *idxsfx)
26-/* returns the content of a metadata index */
27+d_char *_object_index_fn(const char *id, const char *idxsfx)
28+/* returns the filename of an object's index */
29 {
30-    xs *fn = _object_fn(id);
31-    fn = xs_replace_i(fn, ".json", idxsfx);
32-    return index_list(fn, XS_ALL);
33+    d_char *fn = _object_fn(id);
34+    return xs_replace_i(fn, ".json", idxsfx);
35+}
36+
37+
38+int object_likes_len(const char *id)
39+/* returns the number of likes (without reading the index) */
40+{
41+    xs *fn = _object_index_fn(id, "_l.idx");
42+    return index_len(fn);
43+}
44+
45+
46+int object_announces_len(const char *id)
47+/* returns the number of announces (without reading the index) */
48+{
49+    xs *fn = _object_index_fn(id, "_a.idx");
50+    return index_len(fn);
51 }
52 
53 
54 d_char *object_children(const char *id)
55 /* returns the list of an object's children */
56 {
57-    return _object_metadata(id, "_c.idx");
58+    xs *fn = _object_index_fn(id, "_c.idx");
59+    return index_list(fn, XS_ALL);
60 }
61 
62 
63 d_char *object_likes(const char *id)
64 {
65-    return _object_metadata(id, "_l.idx");
66+    xs *fn = _object_index_fn(id, "_l.idx");
67+    return index_list(fn, XS_ALL);
68 }
69 
70 
71 d_char *object_announces(const char *id)
72 {
73-    return _object_metadata(id, "_a.idx");
74+    xs *fn = _object_index_fn(id, "_a.idx");
75+    return index_list(fn, XS_ALL);
76 }
77 
78 
M snac.h
+4, -0
 1@@ -57,6 +57,7 @@ double mtime_nl(const char *fn, int *n_link);
 2 int index_add(const char *fn, const char *md5);
 3 int index_del(const char *fn, const char *md5);
 4 int index_first(const char *fn, char *buf, int size);
 5+int index_len(const char *fn);
 6 d_char *index_list(const char *fn, int max);
 7 d_char *index_list_desc(const char *fn, int skip, int show);
 8 
 9@@ -70,6 +71,9 @@ int object_del(const char *id);
10 int object_del_if_unref(const char *id);
11 int object_admire(const char *id, const char *actor, int like);
12 
13+int object_likes_len(const char *id);
14+int object_announces_len(const char *id);
15+
16 d_char *object_children(const char *id);
17 d_char *object_likes(const char *id);
18 d_char *object_announces(const char *id);