2020-09-02 04:58:57 +00:00
|
|
|
package abstract_sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-02 05:48:36 +00:00
|
|
|
"database/sql"
|
2020-10-16 18:10:12 +00:00
|
|
|
"encoding/base64"
|
2020-09-02 05:48:36 +00:00
|
|
|
"fmt"
|
2020-09-02 04:58:57 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2020-09-02 05:48:36 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2020-12-14 03:45:47 +00:00
|
|
|
"strings"
|
2020-09-02 04:58:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (store *AbstractSqlStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
2020-09-02 05:48:36 +00:00
|
|
|
|
2021-01-19 20:34:58 +00:00
|
|
|
db, _, _, err := store.getTxOrDB(ctx, "", false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("findDB: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 05:48:36 +00:00
|
|
|
dirStr, dirHash, name := genDirAndName(key)
|
|
|
|
|
2021-01-19 21:53:16 +00:00
|
|
|
res, err := db.ExecContext(ctx, store.GetSqlInsert(DEFAULT_TABLE), dirHash, name, dirStr, value)
|
2020-12-14 03:45:47 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(strings.ToLower(err.Error()), "duplicate") {
|
2020-12-14 04:49:44 +00:00
|
|
|
// return fmt.Errorf("kv insert: %s", err)
|
|
|
|
// skip this since the error can be in a different language
|
2020-09-02 05:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now the insert failed possibly due to duplication constraints
|
|
|
|
glog.V(1).Infof("kv insert falls back to update: %s", err)
|
|
|
|
|
2021-01-19 21:53:16 +00:00
|
|
|
res, err = db.ExecContext(ctx, store.GetSqlUpdate(DEFAULT_TABLE), value, dirHash, name, dirStr)
|
2020-09-02 05:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("kv upsert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = res.RowsAffected()
|
2020-12-22 10:34:08 +00:00
|
|
|
if err != nil {
|
2020-09-02 05:48:36 +00:00
|
|
|
return fmt.Errorf("kv upsert no rows affected: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2020-09-02 04:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *AbstractSqlStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
|
2020-09-02 05:48:36 +00:00
|
|
|
|
2021-01-19 20:34:58 +00:00
|
|
|
db, _, _, err := store.getTxOrDB(ctx, "", false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("findDB: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 05:48:36 +00:00
|
|
|
dirStr, dirHash, name := genDirAndName(key)
|
2021-01-19 21:53:16 +00:00
|
|
|
row := db.QueryRowContext(ctx, store.GetSqlFind(DEFAULT_TABLE), dirHash, name, dirStr)
|
2020-09-02 05:48:36 +00:00
|
|
|
|
|
|
|
err = row.Scan(&value)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, filer.ErrKvNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("kv get: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-09-02 04:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *AbstractSqlStore) KvDelete(ctx context.Context, key []byte) (err error) {
|
2020-09-02 05:48:36 +00:00
|
|
|
|
2021-01-19 20:34:58 +00:00
|
|
|
db, _, _, err := store.getTxOrDB(ctx, "", false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("findDB: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 05:48:36 +00:00
|
|
|
dirStr, dirHash, name := genDirAndName(key)
|
|
|
|
|
2021-01-19 21:53:16 +00:00
|
|
|
res, err := db.ExecContext(ctx, store.GetSqlDelete(DEFAULT_TABLE), dirHash, name, dirStr)
|
2020-09-02 05:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("kv delete: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = res.RowsAffected()
|
|
|
|
if err != nil {
|
2020-09-02 06:21:41 +00:00
|
|
|
return fmt.Errorf("kv delete no rows affected: %s", err)
|
2020-09-02 05:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2020-09-02 04:58:57 +00:00
|
|
|
}
|
2020-09-02 05:48:36 +00:00
|
|
|
|
|
|
|
func genDirAndName(key []byte) (dirStr string, dirHash int64, name string) {
|
|
|
|
for len(key) < 8 {
|
|
|
|
key = append(key, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirHash = int64(util.BytesToUint64(key[:8]))
|
2020-10-16 18:10:12 +00:00
|
|
|
dirStr = base64.StdEncoding.EncodeToString(key[:8])
|
|
|
|
name = base64.StdEncoding.EncodeToString(key[8:])
|
2020-09-02 05:48:36 +00:00
|
|
|
|
|
|
|
return
|
2020-09-02 17:12:44 +00:00
|
|
|
}
|