Generalize status setting (mute, etc.)
[blerg.git] / database / database.h
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #ifndef _DATABASE_H
5 #define _DATABASE_H
6
7 #include <stdint.h>
8 #include <time.h>
9
10 #define VALID_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
11 #define WHITESPACE(c) (c == ' ' || c == '\t' || c == '\n' || c == '\r')
12
13 struct record {
14         uint32_t offset;
15         uint16_t length;
16         uint16_t flags;
17         time_t timestamp;
18 };
19
20 struct meta {
21         uint64_t sequence;
22         uint64_t subscription_mark;
23         uint32_t status;
24 };
25
26 /* meta.status is a bitfield of these options */
27 /* Muted - if set, tags/refs and subscriptions are not calculated */
28 #define BLERGSTATUS_MUTED      0x00000001
29 /* Mentioned - if set, user has been mentioned.  Cleared by /status/<user> with
30  * {"clear": "mentioned"} */
31 #define BLERGSTATUS_MENTIONED  0x00000002
32
33 struct blerg {
34         int meta_fd;
35         int index_fd;
36         int data_fd;
37         char *name;
38         char *base_path;
39         struct meta *meta;
40         struct record *index;
41         char *data;
42         int current_segment;
43         int data_size;
44 };
45
46 #define BLERG_INVALID_RECORD 0xFFFFFFFFFFFFFFFF
47
48 int      blerg_init();
49 int      blerg_exists(const char *name);
50 struct   blerg *blerg_open(const char *name);
51 int      blerg_close(struct blerg *blerg);
52 uint64_t blerg_store(struct blerg *blerg, const char *data, int length);
53 int      blerg_fetch(struct blerg *blerg, uint64_t record, char **data, int *length);
54 uint64_t blerg_get_record_count(struct blerg *);
55 time_t   blerg_get_timestamp(struct blerg *blerg, uint64_t record);
56 int      blerg_set_subscription_mark(struct blerg *blerg);
57 uint64_t blerg_get_subscription_mark(struct blerg *blerg);
58 int      blerg_set_status(struct blerg *blerg, uint32_t status, int v);
59 int      blerg_get_status(struct blerg *blerg, uint32_t status);
60
61 #endif //_DATABASE_H