initial db refactoring
This commit is contained in:
parent
196b6959da
commit
e47a874711
5 changed files with 192 additions and 57 deletions
151
pkg/db/index.go
151
pkg/db/index.go
|
|
@ -16,82 +16,131 @@ const (
|
|||
)
|
||||
|
||||
func (db *DB) GetPaths(res core.Results) {
|
||||
for i, v := range res {
|
||||
err := db.store.Get(pathPrefix+v.ID, &res[i].Path)
|
||||
if err != nil {
|
||||
res[i].Path = err.Error()
|
||||
db.store.Tx(false, func(tx store.KVStore) error {
|
||||
for i, v := range res {
|
||||
err := tx.Get(pathPrefix+v.ID, &res[i].Path)
|
||||
if err != nil {
|
||||
res[i].Path = err.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (db *DB) GetIndexed(p core.Paths) {
|
||||
for i, v := range p {
|
||||
p[i].Flag = db.IsIndexed(v.Abs)
|
||||
p[i].Tags, _ = db.GetTag(v.Abs)
|
||||
db.store.Tx(false, func(tx store.KVStore) error {
|
||||
for i, v := range p {
|
||||
p[i].Flag = db.IsIndexed(tx, v.Abs)
|
||||
p[i].Tags, _ = db.GetTag(tx, v.Abs)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (db *DB) IsIndexed(tx store.KVStore, path string) bool {
|
||||
f := func(tx store.KVStore) error {
|
||||
return tx.Get(idPrefix+path, nil)
|
||||
}
|
||||
if tx != nil {
|
||||
return f(tx) == nil
|
||||
}
|
||||
return f(db.store) == nil
|
||||
}
|
||||
|
||||
func (db *DB) IsIndexed(path string) bool {
|
||||
return db.store.Get(idPrefix+path, nil) == nil
|
||||
}
|
||||
|
||||
func (db *DB) GetPath(id string) (path string, err error) {
|
||||
err = db.store.Get(pathPrefix+id, &path)
|
||||
func (db *DB) GetPath(tx store.KVStore, id string) (path string, err error) {
|
||||
f := func(tx store.KVStore) error {
|
||||
return tx.Get(pathPrefix+id, &path)
|
||||
}
|
||||
if tx != nil {
|
||||
err = f(tx)
|
||||
return
|
||||
}
|
||||
err = f(db.store)
|
||||
return
|
||||
}
|
||||
|
||||
func (db *DB) GetID(path string) (id string, err error) {
|
||||
err = db.store.Get(idPrefix+path, &id)
|
||||
func (db *DB) GetID(tx store.KVStore, path string) (id string, err error) {
|
||||
f := func(tx store.KVStore) error {
|
||||
return tx.Get(idPrefix+path, &id)
|
||||
}
|
||||
if tx != nil {
|
||||
err = f(tx)
|
||||
return
|
||||
}
|
||||
err = f(db.store)
|
||||
return
|
||||
}
|
||||
|
||||
func (db *DB) DeletePath(id string) error {
|
||||
return db.store.Delete(pathPrefix + id)
|
||||
func (db *DB) DeletePath(tx store.KVStore, id string) error {
|
||||
f := func(tx store.KVStore) error {
|
||||
return tx.Delete(pathPrefix + id)
|
||||
}
|
||||
if tx != nil {
|
||||
return f(tx)
|
||||
}
|
||||
return f(db.store)
|
||||
}
|
||||
|
||||
func (db *DB) DeleteID(path string) error {
|
||||
return db.store.Delete(idPrefix + path)
|
||||
func (db *DB) DeleteID(tx store.KVStore, path string) error {
|
||||
f := func(tx store.KVStore) error {
|
||||
return tx.Delete(idPrefix + path)
|
||||
}
|
||||
if tx != nil {
|
||||
return f(tx)
|
||||
}
|
||||
return f(db.store)
|
||||
}
|
||||
|
||||
func (db *DB) NewFile(id, path string, tag []string) (err error) {
|
||||
err = db.store.Set(idPrefix+path, id)
|
||||
if err != nil {
|
||||
return
|
||||
func (db *DB) NewFile(tx store.KVStore, id, path string, tag []string) (err error) {
|
||||
f := func(tx store.KVStore) error {
|
||||
err = tx.Set(idPrefix+path, id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = tx.Set(pathPrefix+id, path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return tx.Set(tagPrefix+path, tag)
|
||||
}
|
||||
err = db.store.Set(pathPrefix+id, path)
|
||||
if err != nil {
|
||||
return
|
||||
if tx != nil {
|
||||
return f(tx)
|
||||
}
|
||||
return db.SetTag(path, tag)
|
||||
return db.store.Tx(true, f)
|
||||
}
|
||||
|
||||
func (db *DB) MoveFile(op, np string) (err error) {
|
||||
if !db.IsIndexed(op) {
|
||||
return nil
|
||||
}
|
||||
id, tag, err := db.DeleteFile(op)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return db.NewFile(id, np, tag)
|
||||
return db.store.Tx(true, func(tx store.KVStore) error {
|
||||
id, tag, err := db.DeleteFile(tx, op)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return db.NewFile(tx, id, np, tag)
|
||||
})
|
||||
}
|
||||
|
||||
func (db *DB) DeleteFile(path string) (id string, tag []string, err error) {
|
||||
if !db.IsIndexed(path) {
|
||||
func (db *DB) DeleteFile(tx store.KVStore, path string) (id string, tags []string, err error) {
|
||||
f := func(tx store.KVStore) error {
|
||||
err = tx.Get(idPrefix+path, &id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = tx.Delete(idPrefix + path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = tx.Get(tagPrefix+path, &tags)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = tx.Delete(tagPrefix + path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return tx.Delete(pathPrefix + id)
|
||||
}
|
||||
if tx != nil {
|
||||
err = f(tx)
|
||||
return
|
||||
}
|
||||
id, err = db.GetID(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.DeleteID(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tag, err = db.UnsetTag(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.DeletePath(id)
|
||||
err = db.store.Tx(true, f)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue