This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (130 loc) · 3.01 KB
/
main.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
package main
import (
"bytes"
"compress/gzip"
"compress/zlib"
"fmt"
"io"
"log"
"os"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
type SchemaPart struct {
ID int64 `db:"id"`
SchemaID int64 `db:"schema_id"`
OffsetX int `db:"offset_x"`
OffsetY int `db:"offset_y"`
OffsetZ int `db:"offset_z"`
Mtime int64 `db:"mtime"`
Data []byte `db:"data"`
MetaData []byte `db:"metadata"`
}
func GzipToDeflate(data []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte{})
w := zlib.NewWriter(buf)
_, err = io.Copy(w, r)
if err != nil {
return nil, err
}
w.Close()
return buf.Bytes(), nil
}
func Gunzip(data []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte{})
_, err = io.Copy(buf, r)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func Deflate(data []byte) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
w := zlib.NewWriter(buf)
r := bytes.NewReader(data)
_, err := io.Copy(w, r)
if err != nil {
return nil, err
}
w.Close()
return buf.Bytes(), nil
}
func ConvertMapblockData(data []byte) {
if len(data)%4 != 0 {
panic("mapblock data not aligned")
}
size := len(data) / 4
for i := 0; i < size; i++ {
node_id_low := data[(i*2)+0]
node_id_high := data[(i*2)+1]
param1 := data[(2*size)+i]
param2 := data[(3*size)+i]
var node_id = uint(node_id_low) + (uint(node_id_high) * 256)
node_id += 32768
node_id_low_new := byte(node_id % 256)
node_id_high_new := byte((node_id - uint(node_id_low_new)) / 256)
data[(i*2)+0] = node_id_high_new
data[(i*2)+1] = node_id_low_new
data[(2*size)+i] = param1 + 0x80
data[(3*size)+i] = param2 + 0x80
}
}
func main() {
// setup db connection
connStr := fmt.Sprintf(
"user=%s password=%s port=%s host=%s dbname=%s sslmode=disable",
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGPORT"),
os.Getenv("PGHOST"),
os.Getenv("PGDATABASE"))
log.Printf("Connecting to %s", connStr)
var err error
DB, err := sqlx.Open("postgres", connStr)
if err != nil {
panic(err)
}
// query existing schemaparts
list := []SchemaPart{}
query := `select * from schemapart`
err = DB.Select(&list, query)
if err != nil {
panic(err)
}
log.Printf("found %d schema parts", len(list))
// convert each from gzip to deflate compression
for _, schemapart := range list {
log.Printf("Converting part id=%d", schemapart.ID)
data, err := Gunzip(schemapart.Data)
if err != nil {
panic(err)
}
ConvertMapblockData(data)
deflated_data, err := Deflate(data)
if err != nil {
panic(err)
}
metadata, err := GzipToDeflate(schemapart.MetaData)
if err != nil {
panic(err)
}
query = `
update schemapart
set data = $2, metadata = $3
where id = $1
`
// insert re-compressed data and metadata again
_, err = DB.Exec(query, schemapart.ID, deflated_data, metadata)
if err != nil {
panic(err)
}
}
}