-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExecutePack.go
More file actions
77 lines (67 loc) · 1.64 KB
/
ExecutePack.go
File metadata and controls
77 lines (67 loc) · 1.64 KB
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
76
77
package VkApi
import (
"fmt"
"strings"
)
const overhead = "return[%s];"
const overheadSize = len(overhead)
const maxPackSize = 15000
type ExecutePack struct {
calls []string
size int
}
func (ep *ExecutePack) IsFull() bool {
if len(ep.calls) >= 25 {
return true
}
if ep.Size() >= maxPackSize {
return true
}
return false
}
// Проверяет возможно ли добавить метод в пакет
func (ep *ExecutePack) CanAdd(method Method) bool {
data, err := method.toExecute()
if err != nil {
return false
}
if len(data)+ep.Size() > maxPackSize {
return false
}
return true
}
// Добавляет метод в пакет и возворящает индекс, если индекс = -1
// значи добавить метод не получилось, пакет уже полный
func (ep *ExecutePack) Add(method Method) (int, error) {
if ep.IsFull() {
return -1, nil
}
data, err := method.toExecute()
if err != nil {
return -1, err
}
if len(data)+ep.Size() > maxPackSize {
return -1, nil
}
ep.calls = append(ep.calls, data)
ep.size += len(data) + 1
return len(ep.calls) - 1, nil
}
// Размер execute кода в символах
func (ep *ExecutePack) Size() int {
return ep.size + overheadSize
}
// Код execute запроса
func (ep *ExecutePack) GetCode() string {
str := strings.Join(ep.calls, ",")
return fmt.Sprintf(overhead, str)
}
// Количество запросов в пакете
func (ep *ExecutePack) Count() int {
return len(ep.calls)
}
// Удаляет все запросы из пакета
func (ep *ExecutePack) Clear() {
ep.calls = make([]string, 0)
ep.size = 0
}