aboutsummaryrefslogtreecommitdiffstats
path: root/src/statfile.h
blob: 624ea64ee62d9e4ff0a93bc32ea6f0adb868e237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Describes common methods in accessing statistics files and caching them in memory
 */

#ifndef RSPAMD_STATFILE_H
#define RSPAMD_STATFILE_H

#include "config.h"
#include <sys/types.h>
#include <glib.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include "mem_pool.h"

#define CHAIN_LENGTH 128

struct stat_file_header {
	u_char magic[3];
	u_char version[2];
	u_char padding[3];
	uint64_t create_time;
} __attribute__((__packed__));

struct stat_file_block {
	uint32_t hash1;
	uint32_t hash2;
	uint32_t value; /* In fact this is float */
	uint32_t last_access;
};

struct stat_file {
	struct stat_file_header header;
	struct stat_file_block blocks[1];
};

typedef struct stat_file_s {
	char *filename;
	int fd;
	void *map;
	time_t open_time;
	time_t access_time;
	size_t len;
	/* Length is in blocks */
	size_t blocks;
	gint *lock;
} stat_file_t;

typedef struct statfile_pool_s {
	GHashTable *files;
	int opened;
	size_t max;
	size_t occupied;
	memory_pool_t *pool;
} statfile_pool_t;

statfile_pool_t* statfile_pool_new (size_t max_size);
int statfile_pool_open (statfile_pool_t *pool, char *filename);
int statfile_pool_create (statfile_pool_t *pool, char *filename, size_t len);
int statfile_pool_close (statfile_pool_t *pool, char *filename);
void statfile_pool_delete (statfile_pool_t *pool);
void statfile_pool_lock_file (statfile_pool_t *pool, char *filename);
void statfile_pool_unlock_file (statfile_pool_t *pool, char *filename);
uint32_t statfile_pool_get_block (statfile_pool_t *pool, char *filename, uint32_t h1, uint32_t h2, time_t now);
void statfile_pool_set_block (statfile_pool_t *pool, char *filename, uint32_t h1, uint32_t h2, time_t now, uint32_t value);
int statfile_pool_is_open (statfile_pool_t *pool, char *filename);

#endif