Skip to content

Commit

Permalink
Even more code that restores database
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Nasretdinov committed Jan 2, 2018
1 parent dd93b37 commit 3c8f2f4
Showing 1 changed file with 117 additions and 6 deletions.
123 changes: 117 additions & 6 deletions restore-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package main

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
Expand All @@ -20,6 +23,19 @@ var mapping = map[string]string{
"56": "userinfo",
}

func escape(q string) string {
var b bytes.Buffer
for _, c := range q {
b.WriteRune(c)

if c == '\'' {
b.WriteRune(c)
}
}

return b.String()
}

func main() {
db, err := engine.NewRocksDB(engine.RocksDBConfig{
Dir: "/Users/yuriy/tmp/vbambuke",
Expand All @@ -32,12 +48,19 @@ func main() {

cnt := 0

prevPK := ""
prevTable := ""

err = db.Iterate(
engine.MVCCKey{Timestamp: hlc.MinTimestamp},
engine.MVCCKeyMax,
func(kv engine.MVCCKeyValue) (bool, error) {
cnt++

if kv.Key.Key == nil {
return false, nil
}

k := fmt.Sprint(kv.Key)

if cnt%300000 == 0 {
Expand All @@ -59,14 +82,102 @@ func main() {
fp.WriteString(fmt.Sprintf("%s = %s\n", key, kv.Value))
fp.Close()

if table == "city" {
parts := strings.Split(key, "/")
typ := parts[0]
if typ == "1" {
pk := parts[1]
parts1 := strings.Split(key, "/")
typ := parts1[0]
pk := parts1[1]

if pk == prevPK && table == prevTable {
return false, nil
}

prevPK = pk
prevTable = table

if typ == "1" {
if table == "city" {
v := kv.Value[6:]
ln := v[0]

fmt.Printf(
"INSERT INTO city2(id, name, lon, lat) VALUES(%s, '%s', 0, 0);\n",
pk, escape(string(v[1:ln+1])),
)

// log.Printf("City %s = '%s'", pk, string(v[1:ln+1]))
} else if table == "socialuser" {
v := kv.Value[6:]
ln := v[0]
log.Printf("City %s = '%s'", pk, string(v[1:ln+1]))
email := string(v[1 : ln+1])

v = v[ln+2:]
ln = v[0]
password := string(v[1 : ln+1])

v = v[ln+2:]
ln = v[0]
name := string(v[1 : ln+1])

fmt.Printf(
"INSERT INTO socialuser2(id, email, password, name) VALUES(%s, '%s', '%s', '%s');\n",
pk, escape(email), escape(password), escape(name),
)

// log.Printf("User %s email=%s password=%s name=%s", pk, email, password, name)
} else if table == "userinfo" {
v := kv.Value[6:]
ln := int(v[0])
name := string(v[1 : ln+1])

if v[ln+1] != '\023' {
return true, errors.New("userinfo NOT 023 (name)")
}

v = v[ln+2:]
birthdate, ln := binary.Varint(v)

if v[ln] != '\023' {
return true, errors.New("userinfo NOT 023 (birthdate)")
}

v = v[ln+1:]
sex := v[0]

if v[1] != '\026' {
return true, errors.New("userinfo NOT 023 (sex)")
}

v = v[2:]
ln = int(v[0])
description := string(v[1 : ln+1])

if v[ln+1] != '\023' {
return true, errors.New("userinfo NOT 023 (description)")
}

v = v[ln+2:]
cityID, ln := binary.Varint(v)

if v[ln] != '\023' {
return true, errors.New("userinfo NOT 023 (city_id)")
}

v = v[ln+1:]
familyPosition := v[0]

ts := time.Unix(birthdate*86400, 0)

/*
log.Printf(
"User %s name='%s' birthdate=%v sex=%d description='%s' city_id=%d familyPosition=%d",
pk, name, birthdate, sex, description, cityID, familyPosition,
)
*/

fmt.Printf(
"INSERT INTO userinfo2(user_id, name, birthdate, sex, description, city_id, family_position) VALUES(%s, '%s', '%s', %d, '%s', %d, %d);\n",
pk, escape(name), fmt.Sprintf("%04d-%02d-%02d", ts.Year(), ts.Month(), ts.Day()),
sex, escape(description), cityID, familyPosition,
)
}
}
}
Expand Down

0 comments on commit 3c8f2f4

Please sign in to comment.