/
file.go
330 lines (283 loc) · 8.3 KB
/
file.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"bufio"
"hash/crc32"
"time"
"io"
"log"
"strings"
"compress/gzip"
"sync"
)
var sep = '\x02'
var sep_str = string(sep)
var logger_task *log.Logger
var logger_err *log.Logger
var task_ch []chan string
var quit chan int
var count int
type strSlice []string
var BasePkgMap map[string]strSlice
//var BasePkgMapLocker sync.RWMutex
type UserId map[string]int
var FindUserId map[string]UserId
var FindUserIdLocker sync.RWMutex
var OpenedFile map[string]*os.File
var OpenedFileLocker sync.RWMutex
func main() {
var TaskFile string
var BaseFile string
if len(os.Args) > 2 {
TaskFile = os.Args[1]
BaseFile = os.Args[2]
} else {
fmt.Println("argument : 1.AnalysisFileName, 2.BasePkgFile")
return
}
fmt.Println("Start time :", time.Now().Unix())
runtime.GOMAXPROCS(runtime.NumCPU())
initBasePkgMap(BaseFile)
FindUserId = make(map[string]UserId)
OpenedFile = make(map[string]*os.File)
quit = make(chan int)
//initmylog()
initTaskCh()
for i := 0; i < 24; i++ {
tmpTaskFile:=fmt.Sprintf("%s-%02d", TaskFile, i)
go fillTask(tmpTaskFile)
}
//go fillTaskByOpen(TaskFile)
for i := 0; i < count; i++ {
go processTask(i)
}
for i := 0; i < count+24; i++ {
<- quit
if i == 23 {
for j := 0; j < count; j++ {
close(task_ch[j])
}
}
}
fmt.Println("Finish time :", time.Now().Unix())
}
func initBasePkgMap(BaseFile string) {
inputFile, inputError := os.Open(BaseFile)
if inputError != nil {
fmt.Println("An error occurred on opening the inputfile : ", inputError)
return
}
BasePkgMap = make(map[string]strSlice)
var PkgType string = ""
defer inputFile.Close()
inputReader := bufio.NewReader(inputFile)
for {
inputString, readerError := inputReader.ReadString('\n')
if readerError == io.EOF {
return
}
inputString = strings.Replace(inputString, string('\n'), "", -1)
if strings.Contains(inputString, "[") && strings.Contains(inputString, "]") {
inputString = strings.Replace(inputString, string("["), "", -1)
inputString = strings.Replace(inputString, string("]"), "", -1)
PkgType = inputString
} else if len(inputString)==0 {
PkgType = ""
//fmt.Println("Empty Line")
} else {
//fmt.Println(PkgType, inputString)
BasePkgMap[PkgType] = append(BasePkgMap[PkgType],inputString);
}
}
}
func initmylog() {
task_log_file := "./task.log"
tasklogfile,err := os.OpenFile(task_log_file,os.O_RDWR|os.O_CREATE,0)
if err!=nil {
fmt.Printf("%s\r\n",err.Error())
os.Exit(-1)
}
err_log_file := "./err.log"
errlogfile,err := os.OpenFile(err_log_file,os.O_RDWR|os.O_CREATE,0)
if err!=nil {
fmt.Printf("%s\r\n",err.Error())
os.Exit(-1)
}
logger_task = log.New(tasklogfile,"",0)
logger_err = log.New(errlogfile,"",0)
}
func processTask(idx int){
for {
str_log,ok := <- task_ch[idx]
if ok==false {
fmt.Println("task queue is closed!")
quit<-0
return
}
excute(str_log)
}
}
func excute(str_log string) {
var strarr []string = strings.Split(str_log,sep_str)
if len(strarr) < 42 {
return
}
ExchangeUserId := strarr[8]
AdPlaceType := strarr[14]
AppPkgName := strarr[25]
OSYSTEM := strarr[41]
if AdPlaceType != "2" || len(ExchangeUserId) == 0 || len(AppPkgName) == 0 || !strings.Contains(OSYSTEM, "ios"){
return
}
//fmt.Printf("ExchangeUserId:[%s],AdPlaceType:[%s],AppPkgName:[%s], OSYSTEM;[%s]\n", ExchangeUserId, AdPlaceType, AppPkgName, OSYSTEM)
var IsFind bool = false
for key,value:=range BasePkgMap {
//fmt.Println(key)
for _,PkgName:=range value {
if PkgName == AppPkgName {
IsFind = true
break;
}
}
if IsFind == true {
//TODO Write to file
//fmt.Println("find the Pkg :", key,ExchangeUserId,AdPlaceType,AppPkgName,OSYSTEM)
//if IsDuplicate(key,ExchangeUserId) {
// //fmt.Printf("the UserID[%s] is already write to the file.\n", ExchangeUserId)
// break
//}
fileName := key + ".UserId"
WriteToFile(fileName, ExchangeUserId)
break
}
}
}
func IsDuplicate(key, UserId string) bool {
var result bool
FindUserIdLocker.RLock()
//for _,value:=range FindUserId[key]{
// if value == UserId {
// FindUserIdLocker.RUnlock()
// return true
// }
//}
if FindUserId[key] == nil {
FindUserId[key] = make(map[string]int)
result = false
}else {
if FindUserId[key][UserId] == 0 {
FindUserId[key][UserId]=1
result = false
} else {
FindUserId[key][UserId]++
result = true
}
}
FindUserIdLocker.RUnlock()
//FindUserIdLocker.Lock()
//FindUserId[key] = append(FindUserId[key], UserId)
//FindUserIdLocker.Unlock()
return result
}
func WriteToFile(FileName, Content string){
var outputFile *os.File
OpenedFileLocker.RLock()
for key,value := range OpenedFile {
if key==FileName {
outputFile = value
break
}
}
OpenedFileLocker. RUnlock()
if outputFile == nil {
tmpFileName := fmt.Sprintf("../UserId/%s.%d",FileName,time.Now().Unix())
tmpoutputFile, outputError := os.OpenFile(tmpFileName,os.O_RDWR|os.O_APPEND|os.O_CREATE,0)
if outputError != nil {
fmt.Println("An error occurred on opening the outputFile : ", outputError)
return
}
outputFile = tmpoutputFile
//outputFile.WriteString("\n\n" + time.Now().String() + "\n")
OpenedFileLocker.Lock()
OpenedFile[FileName]=outputFile
OpenedFileLocker.Unlock()
}
outputFile.WriteString(Content+"\n")
//defer outputFile.Close()
}
func fillTaskByOpen(TaskFile string) {
gzFile, gzFileError := os.Open(TaskFile)
if gzFileError != nil {
fmt.Println("An error occurred on opening the gzFileError : ", gzFileError)
return
}
defer gzFile.Close()
gzReader,gzReaderr := gzip.NewReader(gzFile)
if gzReaderr != nil {
fmt.Println("An error occurred on NewReader gzReaderr : ", gzReaderr)
return
}
p := make([]byte, 10)
for {
num, err := gzReader.Read(p)
//inputString, readerError := gzReader.ReadString('\n')
if err == io.EOF {
time.Sleep(1*time.Second)
fmt.Println("AnalysisFile Finished !")
for i := 0; i < count; i++ {
close(task_ch[i])
}
break
//continue
}
fmt.Println(num, string(p[:10]))
time.Sleep(1*time.Second)
task_ch[getCrc("test")%uint32(count)] <- "test"
}
quit<-0
}
func fillTask(TaskFile string){
var tmpTaskFile string
if len(TaskFile)==0 {
tmpTaskFile="argument : AnalysisFileName"
} else {
tmpTaskFile=TaskFile
}
cmdStr := "zcat " + tmpTaskFile + "*.gz"
fmt.Println(cmdStr)
cmd := exec.Command("/bin/sh", "-c", cmdStr)
stdout, _ := cmd.StdoutPipe()
cmd.Start()
inputReader := bufio.NewReader(stdout)
for {
inputString, readerError := inputReader.ReadString('\n')
if readerError == io.EOF {
time.Sleep(1*time.Second)
fmt.Println("AnalysisFile Finished !")
//for i := 0; i < count; i++ {
// close(task_ch[i])
//}
break
//continue
}
task_ch[getCrc(inputString)%uint32(count)] <- inputString
}
quit<-0
}
func getCrc(key string) uint32 {
if len(key) < 64 {
var scratch [64]byte
copy(scratch[:], key)
return crc32.ChecksumIEEE(scratch[:len(key)])
}
return crc32.ChecksumIEEE([]byte(key))
}
func initTaskCh() {
count = 32
for i := 0; i < count; i++ {
task_ch = append(task_ch,make(chan string,300000))
}
}