about summary refs log tree commit diff
path: root/import.go
blob: c3c1e965fa5f113403b227c3e4e5d1c571202c4d (plain)
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
package main

import (
	"encoding/csv"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"strings"

	"github.com/gorilla/mux"

	"git.darknebu.la/GalaxySimulator/structs"
)

// fastInsertHandler gets a tree index and a filename and tries to read insert all the stars from the file into the tree
func fastInsertJSONHandler(w http.ResponseWriter, r *http.Request) {
	// read the mux variables
	vars := mux.Vars(r)
	filename, _ := vars["filename"]

	// read the content using the given filename
	content, readErr := ioutil.ReadFile(fmt.Sprintf("/db/%s", filename))
	if readErr != nil {
		panic(readErr)
	}

	// unmarshal the file content
	tree := &structs.Node{}
	jsonUnmarshalErr := json.Unmarshal(content, tree)
	if jsonUnmarshalErr != nil {
		panic(jsonUnmarshalErr)
	}

	// append the tree to the treeArray
	treeArray = append(treeArray, tree)

	// return the treeArray index the tree was inserted into (== the length of the array)
	_, _ = fmt.Fprintln(w, len(treeArray))
}

func fastInsertListHandler(w http.ResponseWriter, r *http.Request) {
	// read the mux variables
	vars := mux.Vars(r)
	filename, _ := vars["filename"]
	treeindex, _ := strconv.ParseInt(vars["filename"], 10, 64)

	// read the content using the given filename
	content, readErr := ioutil.ReadFile(fmt.Sprintf("/home/db/%s", filename))
	if readErr != nil {
		panic(readErr)
	}

	in := string(content)
	reader := csv.NewReader(strings.NewReader(in))

	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Println("------------------------")
			log.Println("error:")
			log.Println(record)
			log.Println(err)
			log.Println("------------------------")
		}
		fmt.Println(record)

		x, _ := strconv.ParseFloat(record[0], 64)
		y, _ := strconv.ParseFloat(record[1], 64)

		fmt.Println("___________________")
		fmt.Println(record[1])
		fmt.Println(y)
		fmt.Println("___________________")

		star := structs.NewStar2D(structs.Vec2{x, y}, structs.Vec2{0, 0}, 42)

		fmt.Printf("Star: %v", star)

		err = treeArray[treeindex].Insert(star)
		if err != nil {
			log.Println(err)
			errorCount[treeindex] += 1
		}
		fmt.Printf("Inserted %v\n", star)
		starCount[treeindex] += 1
	}
}