-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacopier.go
62 lines (57 loc) · 1.94 KB
/
datacopier.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
package main
import "github.com/httpreserve/httpreserve"
// This structure is used to communicate with the server
// we may also use some static storage in the form of Bolt DB
// the final signal to the webapp will be a empty payload
// but with the complete flag set to true so that we know there
// is no more work to be processed. ls contains a link stat
// data structure if we can recreate one from the JSON we receive
// else the js variable will contain a single JSON document to
// be processed.
type processLog struct {
complete bool
ls httpreserve.LinkStats
js string
lmap map[string]interface{}
}
// Thread safe data copy from one slice to another
func tsdatacopy(copyfrom *int, copyto *int, list []string) []string {
//protect memory by copying only what we know we've got
*copyto = len(list)
if *copyto > 0 && *copyto > *copyfrom {
var res []string
res = make([]string, *copyto-*copyfrom)
copy(res, list[*copyfrom:*copyto])
*copyfrom = *copyto
return res
}
return []string{}
}
// Thread safe data copy from one slice to another
func pldatacopy(copyfrom *int, copyto *int, list []processLog) []processLog {
//protect memory by copying only what we know we've got
*copyto = len(list)
if *copyto > 0 && *copyto > *copyfrom {
var res []processLog
res = make([]processLog, *copyto-*copyfrom)
copy(res, list[*copyfrom:*copyto])
*copyfrom = *copyto
return res
}
return []processLog{}
}
// Thread safe data copy from one slice to another
// This method allows us to specify a length which may be safer for
// us in the long run...
func pldatacopylen(copyfrom *int, copyto *int, list []processLog) []processLog {
//protect memory by copying only what we know we've got
*copyto = *copyto + 1
if *copyto > 0 && *copyto > *copyfrom && *copyto <= len(list) {
var res []processLog
res = make([]processLog, *copyto-*copyfrom)
copy(res, list[*copyfrom:*copyto])
*copyfrom = *copyto
return res
}
return []processLog{}
}