commit 0e066a8491e590a3ddfc39b0bcd64a2b080a1a2d
parent bd8fc0b87c3e478ef239ade86970fe518348d0ba
Author: Peter Kosyh <p.kosyh@gmail.com>
Date: Wed, 9 Sep 2020 17:23:14 +0300
clean db cmd
Diffstat:
2 files changed, 75 insertions(+), 4 deletions(-)
diff --git a/ii-tool/main.go b/ii-tool/main.go
@@ -163,6 +163,77 @@ Options:
fmt.Printf("Can not add user: %s\n", err)
os.Exit(1)
}
+ case "clean":
+ hash := make(map[string]int)
+ nr := 0
+ dup := 0
+ fmt.Printf("Pass 1...\n")
+ err := ii.FileLines(*db_opt, func(line string) bool {
+ nr ++
+ a := strings.Split(line, ":")
+ if len(a) != 2 {
+ ii.Error.Printf("Error in line: %d", nr)
+ return true
+ }
+ if !ii.IsMsgId(a[0]) {
+ ii.Error.Printf("Error in line: %d", nr)
+ return true
+ }
+ if _, ok := hash[a[0]]; ok {
+ hash[a[0]] ++
+ dup ++
+ } else {
+ hash[a[0]] = 1
+ }
+ return true
+ })
+ fmt.Printf("%d lines... %d dups... Pass 2...\n", nr, dup)
+ if dup == 0 {
+ os.Exit(0)
+ }
+ nr = 0
+ f, err := os.OpenFile(*db_opt + ".new", os.O_CREATE|os.O_WRONLY, 0644)
+ if err != nil {
+ fmt.Printf("Error: %s\n", err)
+ os.Exit(1)
+ }
+ defer f.Close()
+ skip := 0
+ err = ii.FileLines(*db_opt, func(line string) bool {
+ nr ++
+ a := strings.Split(line, ":")
+ if len(a) != 2 {
+ fmt.Printf("Error in line: %d\n", nr)
+ skip ++
+ return true
+ }
+ if !ii.IsMsgId(a[0]) {
+ fmt.Printf("Error in line: %d\n", nr)
+ skip ++
+ return true
+ }
+ if v, ok := hash[a[0]]; !ok || v == 0 {
+ fmt.Printf("Error. DB has changed. Aborted.\n")
+ os.Exit(1)
+ }
+ hash[a[0]] --
+ if hash[a[0]] == 0 {
+ if _, err := f.WriteString(line + "\n"); err != nil {
+ fmt.Printf("Error: %s\n", err)
+ os.Exit(1)
+ }
+ } else {
+ skip ++
+ }
+ return true
+ })
+ for _, v := range hash {
+ if v != 0 {
+ fmt.Printf("Error. DB shrinked. Aborted.\n")
+ os.Exit(1)
+ }
+ }
+ fmt.Printf("%d messages removed\n", skip)
case "fetch":
var echolist []string
if len(args) < 2 {
diff --git a/ii/db.go b/ii/db.go
@@ -91,7 +91,7 @@ func (db *DB) CreateIndex() error {
return db._CreateIndex()
}
-func file_lines(path string, fn func(string) bool) error {
+func FileLines(path string, fn func(string) bool) error {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
@@ -137,7 +137,7 @@ func (db *DB) _CreateIndex() error {
}
defer fidx.Close()
var off int64
- return file_lines(db.BundlePath(), func(line string) bool {
+ return FileLines(db.BundlePath(), func(line string) bool {
msg, _ := DecodeBundle(line)
if msg == nil {
off += int64(len(line) + 1)
@@ -871,7 +871,7 @@ func (db *UDB) LoadUsers() error {
db.Secrets = make(map[string]string)
db.ById = make(map[int32]string)
db.List = nil
- err = file_lines(db.Path, func(line string) bool {
+ err = FileLines(db.Path, func(line string) bool {
a := strings.Split(line, ":")
if len(a) < 4 {
Error.Printf("Wrong entry in user DB: %s", line)
@@ -922,7 +922,7 @@ func LoadEcholist(path string) *EDB {
db.Path = path
db.Info = make(map[string]string)
- err := file_lines(path, func(line string) bool {
+ err := FileLines(path, func(line string) bool {
a := strings.SplitN(line, ":", 3)
if len(a) < 2 {
Error.Printf("Wrong entry in echo DB: %s", line)