wip
This commit is contained in:
@@ -3,26 +3,43 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"os"
|
||||||
"pmapp/pmscanner"
|
"pmapp/pmscanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
err := run()
|
err := run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error:", err)
|
fmt.Println("error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const bSize int = 512
|
const bSize int = 1024 * 4
|
||||||
|
const dbname string = "tmp.db"
|
||||||
var key []byte = []byte("01234567890012345678900123456789012")
|
var key []byte = []byte("01234567890012345678900123456789012")
|
||||||
|
|
||||||
|
func help() {
|
||||||
|
fmt.Println("usage:", filepath.Base(os.Args[0]), "dbdir scandir")
|
||||||
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
var err error
|
var err error
|
||||||
scanner, err := pmscanner.NewScanner("tmpdb", key, bSize)
|
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
help()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dbdir := os.Args[1]
|
||||||
|
scandir := os.Args[2]
|
||||||
|
|
||||||
|
dbpath := filepath.Join(dbdir, dbname)
|
||||||
|
scanner, err := pmscanner.NewScanner(dbpath, key, bSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = scanner.Scan("data.bin")
|
err = scanner.Scan(scandir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+90
-10
@@ -3,16 +3,16 @@ package pmscanner
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/hex"
|
"strings"
|
||||||
|
|
||||||
"pmapp/pmhasher"
|
"pmapp/pmhasher"
|
||||||
"pmapp/pmkeyvdb"
|
"pmapp/pmkeyvdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Scanner struct {
|
type Scanner struct {
|
||||||
bSize int
|
bSize int
|
||||||
keyvdb *pmkeyvdb.DB
|
keyvdb *pmkeyvdb.DB
|
||||||
@@ -43,18 +43,32 @@ func (this *Scanner) Close() {
|
|||||||
this.keyvdb.Close()
|
this.keyvdb.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Scanner) Scan(filename string) error {
|
const maxDepth int = 7
|
||||||
var err error
|
|
||||||
|
|
||||||
if this.keyvdb == nil {
|
func (this *Scanner) Scan(basedir string) error {
|
||||||
return errors.New("db yet not open")
|
var err error
|
||||||
}
|
|
||||||
|
|
||||||
err = this.keyvdb.Clean()
|
err = this.keyvdb.Clean()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
executor := func(filename string) {
|
||||||
|
fmt.Print(".")
|
||||||
|
this.scanFile(filename)
|
||||||
|
}
|
||||||
|
_ = this.FileTreeScanner(basedir, maxDepth, executor)
|
||||||
|
fmt.Println()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Scanner) scanFile(filename string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if this.keyvdb == nil {
|
||||||
|
return errors.New("db yet not open")
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.OpenFile(filename, os.O_RDONLY, 0)
|
file, err := os.OpenFile(filename, os.O_RDONLY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -67,6 +81,12 @@ func (this *Scanner) Scan(filename string) error {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//if read < this.bSize {
|
||||||
|
// continue
|
||||||
|
//}
|
||||||
hash, err := this.hasher.Hash(buffer[:read])
|
hash, err := this.hasher.Hash(buffer[:read])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -81,11 +101,13 @@ func (this *Scanner) Scan(filename string) error {
|
|||||||
|
|
||||||
func (this *Scanner) Print() error {
|
func (this *Scanner) Print() error {
|
||||||
var err error
|
var err error
|
||||||
|
dupSize := 0
|
||||||
|
dupCounter := 0
|
||||||
executor := func(key []byte, val []byte) bool {
|
executor := func(key []byte, val []byte) bool {
|
||||||
keyHex := hex.EncodeToString(key)
|
|
||||||
counter, _ := strconv.Atoi(string(val))
|
counter, _ := strconv.Atoi(string(val))
|
||||||
if counter > 1 {
|
if counter > 1 {
|
||||||
fmt.Println(keyHex, counter)
|
dupSize += (counter - 1) * this.bSize
|
||||||
|
dupCounter += (counter - 1)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -93,6 +115,20 @@ func (this *Scanner) Print() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
totalCounter := 0
|
||||||
|
executor = func(key []byte, val []byte) bool {
|
||||||
|
totalCounter++
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
err = this.keyvdb.Iter(executor)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(totalCounter, dupCounter, (100 * float32(dupCounter))/float32(totalCounter))
|
||||||
|
//totalSize := totalCounter * this.bSize
|
||||||
|
//fmt.Println(totalSize, dupSize, (100 * float32(dupSize))/float32(totalSize))
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,4 +163,48 @@ func (this *Scanner) Inc(key []byte) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pathSeparator string = "/"
|
||||||
|
|
||||||
|
type FileExecutor = func(filePath string)
|
||||||
|
|
||||||
|
func (this *Scanner) FileTreeScanner(basePath string, depth int, executor FileExecutor) error {
|
||||||
|
|
||||||
|
pathLength := func(path string) int {
|
||||||
|
if len(path) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
path = filepath.ToSlash(path)
|
||||||
|
path = strings.Trim(path, pathSeparator)
|
||||||
|
if len(path) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
list := strings.Split(path, pathSeparator)
|
||||||
|
return len(list)
|
||||||
|
}
|
||||||
|
|
||||||
|
depth = depth + pathLength(basePath)
|
||||||
|
|
||||||
|
resolver := func(fullPath string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if pathLength(fullPath) > depth {
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
if !info.IsDir(){
|
||||||
|
executor(fullPath)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err := filepath.Walk(basePath, resolver)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//EOF
|
//EOF
|
||||||
|
|||||||
Reference in New Issue
Block a user