1 #ifndef BYTE_OFFSETS_H
2 #define BYTE_OFFSETS_H
3
4 #include "redisearch.h"
5 #include "varint.h"
6 #include "rmalloc.h"
7
8 typedef struct __attribute__((packed)) RSByteOffsetMap {
9 // ID this belongs to.
10 uint16_t fieldId;
11
12 // The position of the first token for this field.
13 uint32_t firstTokPos;
14
15 // Position of last token for this field
16 uint32_t lastTokPos;
17 } RSByteOffsetField;
18
19 typedef struct RSByteOffsets {
20 // By-Byte offsets
21 RSOffsetVector offsets;
22 // List of field-id <-> position mapping
23 RSByteOffsetField *fields;
24 // How many fields
25 uint8_t numFields;
26 } RSByteOffsets;
27
28 RSByteOffsets *NewByteOffsets();
29
30 void RSByteOffsets_Free(RSByteOffsets *offsets);
31
32 // Reserve memory for this many fields
33 void RSByteOffsets_ReserveFields(RSByteOffsets *offsets, size_t numFields);
34
35 // Add a field to the offset map. Note that you cannot add more fields than
36 // initially declared via ReserveFields
37 // The start position is the position of the first token in this field.
38 // The field info is returned, and the last position should be written to it
39 // when done.
40 RSByteOffsetField *RSByteOffsets_AddField(RSByteOffsets *offsets, uint32_t fieldId,
41 uint32_t startPos);
42
43 void RSByteOffsets_Serialize(const RSByteOffsets *offsets, Buffer *b);
44 RSByteOffsets *LoadByteOffsets(Buffer *buf);
45
46 typedef VarintVectorWriter ByteOffsetWriter;
47
48 void ByteOffsetWriter_Move(ByteOffsetWriter *w, RSByteOffsets *offsets);
49
ByteOffsetWriter_Init(ByteOffsetWriter * w)50 static inline void ByteOffsetWriter_Init(ByteOffsetWriter *w) {
51 VVW_Init(w, 16);
52 }
53
ByteOffsetWriter_Cleanup(ByteOffsetWriter * w)54 static inline void ByteOffsetWriter_Cleanup(ByteOffsetWriter *w) {
55 VVW_Cleanup(w);
56 }
57
ByteOffsetWriter_Write(ByteOffsetWriter * w,uint32_t offset)58 static inline void ByteOffsetWriter_Write(ByteOffsetWriter *w, uint32_t offset) {
59 VVW_Write(w, offset);
60 }
61
62 /**
63 * Iterator which yields the byte offset for a given position
64 */
65 typedef struct {
66 BufferReader rdr;
67 Buffer buf;
68 uint32_t lastValue;
69 uint32_t curPos;
70 uint32_t endPos;
71 } RSByteOffsetIterator;
72
73 #define RSBYTEOFFSET_EOF ((uint32_t)-1)
74
75 /**
76 * Begin iterating over the byte offsets for a given field. Returns REDISMODULE_ERR
77 * if the field does not exist in the current byte offset
78 */
79 int RSByteOffset_Iterate(const RSByteOffsets *offsets, uint32_t fieldId,
80 RSByteOffsetIterator *iter);
81
82 /**
83 * Returns the next byte offset for the given position. The current position
84 * can be obtained using the curPos variable. If this function returns
85 * RSBYTEOFFSET_EOF then the iterator is at the end of the token stream.
86 */
87 uint32_t RSByteOffsetIterator_Next(RSByteOffsetIterator *iter);
88
89 #endif