1 /*
2  *  Beansdb - A high available distributed key-value storage system:
3  *
4  *      http://beansdb.googlecode.com
5  *
6  *  Copyright 2010 Douban Inc.  All rights reserved.
7  *
8  *  Use and distribution licensed under the BSD license.  See
9  *  the LICENSE file for full text.
10  *
11  *  Authors:
12  *      Davies Liu <davies.liu@gmail.com>
13  *      Hurricane Lee <hurricane1026@gmail.com>
14  *
15  */
16 
17 #ifndef __RECORD_H__
18 #define __RECORD_H__
19 
20 #include <stdio.h>
21 #include <time.h>
22 
23 #include "htree.h"
24 #include "util.h"
25 #include "diskmgr.h"
26 
27 
28 typedef struct data_record
29 {
30     char *value;
31     union
32     {
33         bool free_value;    // free value or not
34         uint32_t crc;
35     };
36     int32_t tstamp;
37     int32_t flag;
38     int32_t version;
39     uint32_t ksz;
40     uint32_t vsz;
41     char key[0];
42 } DataRecord;
43 
44 typedef bool (*RecordVisitor)(DataRecord *r, void *arg1, void *arg2);
45 
46 uint32_t gen_hash(char *buf, int size);
47 
48 char* record_value(DataRecord *r);
49 void free_record(DataRecord **r);
50 
51 // on bad record, return NULL and set *fail_reason to one of these
52 #define BAD_REC_SIZE  1
53 #define BAD_REC_END  2
54 #define BAD_REC_CRC  3
55 #define BAD_REC_DECOMPRESS 4
56 DataRecord* decode_record(char *buf, uint32_t size, bool decomp, const char *path, uint32_t pos, const char *key, bool do_logging, int *fail_reason);
57 
58 char* encode_record(DataRecord *r, unsigned int *size);
59 DataRecord* read_record(FILE *f, bool decomp, const char *path, const char *key);
60 DataRecord* fast_read_record(int fd, off_t offset, bool decomp, const char *path, const char *key);
61 
62 void scanDataFile(HTree *tree, int bucket, const char *path, const char *hintpath);
63 void scanDataFileBefore(HTree *tree, int bucket, const char *path, time_t before);
64 int optimizeDataFile(HTree *tree, Mgr *mgr, int bucket, const char *path, const char *hintpath,
65         int last_bucket, const char *lastdata, const char *lasthint_real, uint32_t max_data_size,
66         bool skipped, bool isnewfile, uint32_t *deleted_bytes);
67 void visit_record(const char *path, RecordVisitor visitor, void *arg1, void *arg2, bool decomp);
68 
69 #endif
70