From 87b3b84471c7041c29474840616cd3f15705eabe Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 26 May 2018 21:24:03 -0700 Subject: [PATCH] simplifying dirhash to 64bit integer --- .../filer2/abstract_sql/abstract_sql_store.go | 10 ++++---- weed/filer2/abstract_sql/hashing.go | 25 ++++++++++++++++--- weed/filer2/configuration.go | 19 +++++++++----- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/weed/filer2/abstract_sql/abstract_sql_store.go b/weed/filer2/abstract_sql/abstract_sql_store.go index 3d15c632d..900056dae 100644 --- a/weed/filer2/abstract_sql/abstract_sql_store.go +++ b/weed/filer2/abstract_sql/abstract_sql_store.go @@ -21,7 +21,7 @@ func (store *AbstractSqlStore) InsertEntry(entry *filer2.Entry) (err error) { } res, err := store.DB.Exec("INSERT INTO filemeta (dirhash,name,directory,meta) VALUES(?,?,?,?)", - md5hash(dir), name, dir, meta) + hashToLong(dir), name, dir, meta) if err != nil { return fmt.Errorf("mysql insert %s: %s", entry.FullPath, err) } @@ -42,7 +42,7 @@ func (store *AbstractSqlStore) UpdateEntry(entry *filer2.Entry) (err error) { } res, err := store.DB.Exec("UPDATE filemeta SET meta=? WHERE dirhash=? AND name=? AND directory=?", - meta, md5hash(dir), name, dir) + meta, hashToLong(dir), name, dir) if err != nil { return fmt.Errorf("mysql update %s: %s", entry.FullPath, err) } @@ -58,7 +58,7 @@ func (store *AbstractSqlStore) FindEntry(fullpath filer2.FullPath) (*filer2.Entr dir, name := fullpath.DirAndName() row := store.DB.QueryRow("SELECT meta FROM filemeta WHERE dirhash=? AND name=? AND directory=?", - md5hash(dir), name, dir) + hashToLong(dir), name, dir) var data []byte if err := row.Scan(&data); err != nil { return nil, fmt.Errorf("mysql read entry %s: %v", fullpath, err) @@ -81,7 +81,7 @@ func (store *AbstractSqlStore) DeleteEntry(fullpath filer2.FullPath) (*filer2.En dir, name := fullpath.DirAndName() res, err := store.DB.Exec("DELETE FROM filemeta WHERE dirhash=? AND name=? AND directory=?", - md5hash(dir), name, dir) + hashToLong(dir), name, dir) if err != nil { return nil, fmt.Errorf("mysql delete %s: %s", fullpath, err) } @@ -101,7 +101,7 @@ func (store *AbstractSqlStore) ListDirectoryEntries(fullpath filer2.FullPath, st sqlText = "SELECT NAME, meta FROM filemeta WHERE dirhash=? AND name>=? AND directory=? LIMIT ?" } - rows, err := store.DB.Query(sqlText, md5hash(string(fullpath)), startFileName, string(fullpath), limit) + rows, err := store.DB.Query(sqlText, hashToLong(string(fullpath)), startFileName, string(fullpath), limit) if err != nil { return nil, fmt.Errorf("mysql list %s : %v", fullpath, err) } diff --git a/weed/filer2/abstract_sql/hashing.go b/weed/filer2/abstract_sql/hashing.go index 5e1390233..5c982c537 100644 --- a/weed/filer2/abstract_sql/hashing.go +++ b/weed/filer2/abstract_sql/hashing.go @@ -5,9 +5,28 @@ import ( "io" ) -// returns a 128 bit hash -func md5hash(dir string) []byte { +// returns a 64 bit big int +func hashToLong(dir string) (v int64) { h := md5.New() io.WriteString(h, dir) - return h.Sum(nil) + + b := h.Sum(nil) + + v += int64(b[0]) + v <<= 8 + v += int64(b[1]) + v <<= 8 + v += int64(b[2]) + v <<= 8 + v += int64(b[3]) + v <<= 8 + v += int64(b[4]) + v <<= 8 + v += int64(b[5]) + v <<= 8 + v += int64(b[6]) + v <<= 8 + v += int64(b[7]) + + return } diff --git a/weed/filer2/configuration.go b/weed/filer2/configuration.go index 910ddcbbc..14ca61745 100644 --- a/weed/filer2/configuration.go +++ b/weed/filer2/configuration.go @@ -27,9 +27,9 @@ dir = "." # directory to store level db files # need to manually create a table "filemeta". # # CREATE TABLE IF NOT EXISTS filemeta ( -# dirhash BINARY(16) COMMENT 'MD5 hash value of directory field', -# name VARCHAR(1000) NOT NULL DEFAULT "" COMMENT 'directory or file name', -# directory VARCHAR(4096) NOT NULL DEFAULT "" COMMENT 'full path to parent directory', +# dirhash BIGINT COMMENT 'first 64 bits of MD5 hash value of directory field', +# name VARCHAR(1000) COMMENT 'directory or file name', +# directory VARCHAR(4096) COMMENT 'full path to parent directory', # meta BLOB, # PRIMARY KEY (dirhash, name) # ) DEFAULT CHARSET=utf8; @@ -44,10 +44,17 @@ connection_max_idle = 2 connection_max_open = 100 [postgres] +# CREATE TABLE IF NOT EXISTS filemeta ( +# dirhash BIGINT, +# name VARCHAR(1000), +# directory VARCHAR(4096), +# meta bytea, +# PRIMARY KEY (dirhash, name) +# ); enabled = false -server = "192.168.1.1" -port = 8080 -username = "" +server = "localhost" +port = 5432 +username = "postgres" password = "" database = "" connection_max_idle = 100