func validateConfig(logger *log.Logger) { c := nbadConfig if c.MessageInitBufferTimeSeconds > c.MessageCacheTTLInSeconds { logger.Fatalln("init buffer ttl cannot be greater than message cache ttl") } }
func InitDB(logger *log.Logger) { var err interface{} Datab, err = sql.Open("sqlite3", "C:\\Users\\Layne\\Documents\\code\\test\\blub.db") if err != nil { logger.Fatalln(err) } for _, stmt := range openingStatements { Datab.Exec(stmt) } }
// CountElementsInXML возвращает количество узлов в XML-файле func CountElementsInXML(w *sync.WaitGroup, c chan int, tableName string, countedElement string, logger *log.Logger) { w.Add(1) defer w.Done() var err error format := "xml" fileName, err2 := SearchFile(tableName, format) if err2 != nil { logger.Fatalln("Error searching file:", err) } pathToFile := format + "/" + fileName xmlFile, err := os.Open(pathToFile) if err != nil { logger.Fatalln("Error opening file:", err) } defer xmlFile.Close() decoder := xml.NewDecoder(xmlFile) total := 0 var inElement string for { // Read tokens from the XML document in a stream. t, _ := decoder.Token() if t == nil { break } // Inspect the type of the token just read. switch se := t.(type) { case xml.StartElement: // If we just read a StartElement token inElement = se.Name.Local if inElement == countedElement { total++ c <- total } default: } } }
func main() { flag.Parse() if *backendURL == "" { flag.Usage() os.Exit(1) } var errorLog *log.Logger if *errorLogName == "" { errorLog = log.New(os.Stderr, "", log.LstdFlags) } else { errorLogFile, err := os.OpenFile(*errorLogName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("Error opening error log file: %v", err) } defer errorLogFile.Close() errorLog = log.New(errorLogFile, "", log.LstdFlags) } var accessLog *log.Logger if *accessLogName != "" { accessLogFile, err := os.OpenFile(*accessLogName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("Error opening access log file: %v", err) } defer accessLogFile.Close() accessLog = log.New(accessLogFile, "", log.LstdFlags) } udpAddr, err := net.ResolveUDPAddr("udp", *listenAddr) if err != nil { errorLog.Fatalln("Can't resolve UDP addr") } udpListener, err := net.ListenUDP("udp", udpAddr) if err != nil { errorLog.Fatalln("Can't listen on UDP") } defer udpListener.Close() errorLog.Printf("crosscoap started: Listening for CoAP on UDP %v ...", *listenAddr) p := crosscoap.Proxy{ Listener: udpListener, BackendURL: *backendURL, ErrorLog: errorLog, AccessLog: accessLog, } err = p.Serve() if err != nil { errorLog.Fatalln(err) } }
// ExportBulk экспортирует данные из xml-файла в таблицу указанную в описании xml-структуры func ExportBulk(schema func(tableName string) string, xmlObject interface{}, w *sync.WaitGroup, c chan string, db *sqlx.DB, format *string, logger *log.Logger) { defer w.Done() const dateformat = "2006-01-02" objName := extractXMLObjectName(xmlObject) fields := extractFeilds(xmlObject) searchFileName := objName.tableName objName.tableName = strings.TrimSuffix(objName.tableName, "_") DropAndCreateTable(schema(objName.tableName), objName.tableName, db) var format2 string format2 = *format fileName, err2 := SearchFile(searchFileName, format2) if err2 != nil { logger.Fatalln("Error searching file:", err2) } pathToFile := format2 + "/" + fileName xmlFile, err := os.Open(pathToFile) if err != nil { logger.Fatalln("Error opening file:", err) } defer xmlFile.Close() decoder := xml.NewDecoder(xmlFile) var inElement string total := 0 i := 0 txn, err := db.Begin() if err != nil { logger.Fatalln(err) } query := pq.CopyIn(objName.tableName, fields...) stmt, err := txn.Prepare(query) if err != nil { logger.Fatalln(err) } for { if i == 50000 { i = 0 _, err = stmt.Exec() if err != nil { logger.Fatalln(err) } err = stmt.Close() if err != nil { logger.Fatalln(err) } err = txn.Commit() if err != nil { logger.Fatalln(err) } txn, err = db.Begin() if err != nil { logger.Fatalln(err) } stmt, err = txn.Prepare(query) if err != nil { logger.Fatalln(err) } } t, _ := decoder.Token() // Если достигли конца xml-файла if t == nil { if i > 0 { _, err = stmt.Exec() if err != nil { logger.Fatalln(err) } err = stmt.Close() if err != nil { logger.Fatalln(err) } err = txn.Commit() if err != nil { logger.Fatalln(err) } } //c <- helpers.PrintRowsAffected(elementName, total) break } // Inspect the type of the token just read. switch se := t.(type) { case xml.StartElement: // If we just read a StartElement token inElement = se.Name.Local if inElement == objName.elementName { total++ //var item xmlObject // decode a whole chunk of following XML into the // variable item which is a ActualStatus (se above) err = decoder.DecodeElement(&xmlObject, &se) if err != nil { logger.Fatalln("Error in decode element:", err) } values := extractValues(xmlObject) //logger.Println(values) _, err = stmt.Exec(values...) if err != nil { logger.Fatalln(err) } c <- PrintRowsAffected(objName.elementName, total) i++ } default: } } // Если закончили обработку всех записей, ставим вначале строки восклицательный знак. // Признак того, что все записи в данном файле обработаны c <- "!" + PrintRowsAffected(objName.elementName, total) }