-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictlimiter.go
75 lines (65 loc) · 2.35 KB
/
dictlimiter.go
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
70
71
72
73
74
75
package pkg
// SizeLimiter tracks the current (approximate) byte size of dictionaries
// and of of the frame against the specified size limit.
type SizeLimiter struct {
dictByteSize uint
dictByteSizeLimit uint
dictSizeLimitReached bool
frameBitSize uint
frameBitSizeLimit uint
frameSizeLimitReached bool
}
// Init prepares limiter operation.
// dictByteSizeLimit specifies the limit of size in bytes. 0 means no limit,
// so the size won't be tracked at all.
func (d *SizeLimiter) Init(opts *WriterOptions) {
d.dictByteSize = 0
d.frameBitSize = 0
d.dictByteSizeLimit = opts.MaxTotalDictSize
d.frameBitSizeLimit = opts.MaxUncompressedFrameByteSize * 8
}
// AddDictElemSize accounts for adding an element of specified bytes size to the dictionary.
func (d *SizeLimiter) AddDictElemSize(elemByteSize uint) {
if d.dictByteSizeLimit != 0 {
d.dictByteSize += elemByteSize
if d.dictByteSize >= d.dictByteSizeLimit {
d.dictSizeLimitReached = true
}
}
}
// AddFrameBits accounts for adding bytes to the frame buffer.
func (d *SizeLimiter) AddFrameBits(bitCount uint) {
if d.frameBitSizeLimit != 0 {
d.frameBitSize += bitCount
if d.frameBitSize >= d.frameBitSizeLimit {
d.frameSizeLimitReached = true
}
}
}
func (d *SizeLimiter) AddFrameBytes(byteCount uint) {
d.AddFrameBits(byteCount * 8)
}
// DictLimitReached returns true if the accumulated added element sizes reaches the
// previously defined limit. If specified limit was 0 the limit is never reached
// and this function will always return false.
func (d *SizeLimiter) DictLimitReached() bool {
return d.dictSizeLimitReached
}
// FrameLimitReached returns true if the accumulated added byte sizes reaches the
// previously defined limit. If specified limit was 0 the limit is never reached
// and this function will always return false.
func (d *SizeLimiter) FrameLimitReached() bool {
return d.frameSizeLimitReached
}
// ResetDict resets accumulated sizes to 0 and DictLimitReached indicator to false.
// Normally used in conjunction with resetting the dictionary itself.
func (d *SizeLimiter) ResetDict() {
d.dictByteSize = 0
d.dictSizeLimitReached = false
}
// ResetFrameSize resets accumulated sizes to 0 and FrameLimitReached indicator to false.
// Normally used after restarting the frame.
func (d *SizeLimiter) ResetFrameSize() {
d.frameBitSize = 0
d.frameSizeLimitReached = false
}