Skip to content

Commit

Permalink
Merge pull request #193 from anyproto/clean-file-related-logs
Browse files Browse the repository at this point in the history
Clean file logs, add domain package
  • Loading branch information
deff7 authored Jul 17, 2023
2 parents 5a5f913 + d187273 commit 2650518
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 23 deletions.
5 changes: 5 additions & 0 deletions core/domain/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package domain

import "errors"

var ErrFileNotFound = errors.New("file not found")
7 changes: 3 additions & 4 deletions core/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/multiformats/go-base32"
mh "github.com/multiformats/go-multihash"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/filestorage"
"github.com/anyproto/anytype-heart/core/filestorage/filesync"
"github.com/anyproto/anytype-heart/pb"
Expand Down Expand Up @@ -853,15 +854,13 @@ func (s *service) StoreFileKeys(fileKeys ...FileKeys) error {
return s.fileStore.AddFileKeys(fks...)
}

var ErrFileNotFound = fmt.Errorf("file not found")

func (s *service) FileByHash(ctx context.Context, hash string) (File, error) {
ok, err := s.isDeleted(hash)
if err != nil {
return nil, fmt.Errorf("check if file is deleted: %w", err)
}
if ok {
return nil, ErrFileNotFound
return nil, domain.ErrFileNotFound
}

fileList, err := s.fileStore.ListByTarget(hash)
Expand All @@ -874,7 +873,7 @@ func (s *service) FileByHash(ctx context.Context, hash string) (File, error) {
fileList, err = s.fileIndexInfo(ctx, hash, false)
if err != nil {
log.With("cid", hash).Errorf("FileByHash: failed to retrieve from IPFS: %s", err.Error())
return nil, ErrFileNotFound
return nil, domain.ErrFileNotFound
}
ok, err := s.fileStore.IsFileImported(hash)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion core/files/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gogo/protobuf/types"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/mill"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
Expand Down Expand Up @@ -262,7 +263,7 @@ func (i *image) getFileForWidthFromCache(wantWidth int) (File, error) {
}, nil
}

return nil, ErrFileNotFound
return nil, domain.ErrFileNotFound
}

func (i *image) extractLastModifiedDate(ctx context.Context, imageExif *mill.ImageExifSchema) int64 {
Expand Down
7 changes: 3 additions & 4 deletions core/files/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"context"
"fmt"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pkg/lib/localstore/filestore"
"github.com/anyproto/anytype-heart/pkg/lib/mill/schema/anytype"
"github.com/anyproto/anytype-heart/pkg/lib/pb/storage"
)

var ErrImageNotFound = fmt.Errorf("image not found")

func (s *service) ImageByHash(ctx context.Context, hash string) (Image, error) {
ok, err := s.isDeleted(hash)
if err != nil {
return nil, fmt.Errorf("check if file is deleted: %w", err)
}
if ok {
return nil, ErrFileNotFound
return nil, domain.ErrFileNotFound
}

files, err := s.fileStore.ListByTarget(hash)
Expand All @@ -31,7 +30,7 @@ func (s *service) ImageByHash(ctx context.Context, hash string) (Image, error) {
files, err = s.fileIndexInfo(ctx, hash, true)
if err != nil {
log.Errorf("ImageByHash: failed to retrieve from IPFS: %s", err.Error())
return nil, ErrImageNotFound
return nil, domain.ErrFileNotFound
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/filestorage/filesync/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/samber/lo"
"go.uber.org/zap"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/util/conc"
)
Expand Down Expand Up @@ -139,7 +140,7 @@ func (f *fileSync) FileStat(ctx context.Context, spaceId, fileId string) (fs Fil
return
}
if len(fi) == 0 {
return FileStat{}, fmt.Errorf("file not found")
return FileStat{}, domain.ErrFileNotFound
}
file := fi[0]

Expand Down
5 changes: 3 additions & 2 deletions core/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/anyproto/anytype-heart/core/block/editor"
smartblock2 "github.com/anyproto/anytype-heart/core/block/editor/smartblock"
"github.com/anyproto/anytype-heart/core/block/source"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/files"
"github.com/anyproto/anytype-heart/core/relation/relationutils"
"github.com/anyproto/anytype-heart/metrics"
Expand Down Expand Up @@ -318,8 +319,8 @@ func (i *indexer) indexLinkedFiles(ctx context.Context, fileHashes []string) {
}
// file's hash is id
idxErr := i.reindexDoc(ctx, id)
if idxErr != nil {
log.With("id", id).Errorf("failed to reindex file: %s", idxErr.Error())
if idxErr != nil && !errors.Is(idxErr, domain.ErrFileNotFound) {
log.With("id", id).Errorf("failed to reindex file: %s", idxErr)
}
idxErr = i.store.AddToIndexQueue(id)
if idxErr != nil {
Expand Down
8 changes: 4 additions & 4 deletions core/syncstatus/file_status_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package syncstatus

import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/anyproto/anytype-heart/core/block/editor/basic"
"github.com/anyproto/anytype-heart/core/block/getblock"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/filestorage/filesync"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
Expand Down Expand Up @@ -127,8 +129,6 @@ func validStatusTransition(from, to FileStatus) bool {
}
}

var errFileNotFound = fmt.Errorf("file is not found")

func (r *fileStatusRegistry) updateFileStatus(ctx context.Context, status fileStatus, key fileWithSpace) (fileStatus, error) {
now := time.Now()
if status.status == FileStatusSynced {
Expand Down Expand Up @@ -163,7 +163,7 @@ func (r *fileStatusRegistry) updateFileStatus(ctx context.Context, status fileSt
return status, fmt.Errorf("check that file is in store: %w", err)
}
if !ok {
return status, errFileNotFound
return status, domain.ErrFileNotFound
}
fstat, err := r.fileSyncService.FileStat(ctx, key.spaceID, key.fileID)
if err != nil {
Expand All @@ -185,7 +185,7 @@ func (r *fileStatusRegistry) indexFileSyncStatus(fileID string, status FileStatu
},
}, true)
})
if err != nil {
if err != nil && !errors.Is(err, domain.ErrFileNotFound) {
log.With("fileID", fileID, "status", status).Errorf("failed to index file sync status: %v", err)
}
}
4 changes: 3 additions & 1 deletion core/syncstatus/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package syncstatus
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"sync"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/dgraph-io/badger/v3"
"go.uber.org/zap"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pkg/lib/datastore"
"github.com/anyproto/anytype-heart/space"
)
Expand Down Expand Up @@ -164,7 +166,7 @@ func (s *fileWatcher) list() []fileWithSpace {

func (s *fileWatcher) updateFileStatus(ctx context.Context, key fileWithSpace) error {
status, err := s.registry.GetFileStatus(ctx, key.spaceID, key.fileID)
if err == errFileNotFound {
if errors.Is(err, domain.ErrFileNotFound) {
s.Unwatch(key.spaceID, key.fileID)
return err
}
Expand Down
4 changes: 3 additions & 1 deletion core/syncstatus/linked_files_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package syncstatus

import (
"context"
"errors"
"sync"
"time"

"go.uber.org/zap"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/space"
)
Expand Down Expand Up @@ -88,7 +90,7 @@ func (w *linkedFilesWatcher) updateLinkedFilesSummary(parentObjectID string, fil
var pinStatus pb.EventStatusThreadCafePinStatus
for _, fileID := range fileIDs {
status, err := w.fileStatusRegistry.GetFileStatus(context.Background(), w.spaceService.AccountId(), fileID)
if err == errFileNotFound {
if errors.Is(err, domain.ErrFileNotFound) {
continue
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/VividCortex/ewma v1.2.0
github.com/adrium/goheif v0.0.0-20230113233934-ca402e77a786
github.com/anyproto/any-sync v0.2.13
github.com/anyproto/any-sync v0.2.14
github.com/anyproto/go-naturaldate/v2 v2.0.2-0.20230524105841-9829cfd13438
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/blevesearch/bleve/v2 v2.3.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxB
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/anyproto/any-sync v0.2.13 h1:g6nVGRZvg6jzYYuenKXgTNBXvaJ+ibDDUmQJ47PtSe8=
github.com/anyproto/any-sync v0.2.13/go.mod h1:DSylRQYM/tTWZ8B6hZwsOFgLFupakVvhbZh7thE8V+g=
github.com/anyproto/any-sync v0.2.14 h1:nu9y+JkeD8UQXpRMDqRK9PyfEhCFaMhzXSlUoG/gmqw=
github.com/anyproto/any-sync v0.2.14/go.mod h1:DSylRQYM/tTWZ8B6hZwsOFgLFupakVvhbZh7thE8V+g=
github.com/anyproto/go-chash v0.1.0 h1:I9meTPjXFRfXZHRJzjOHC/XF7Q5vzysKkiT/grsogXY=
github.com/anyproto/go-chash v0.1.0/go.mod h1:0UjNQi3PDazP0fINpFYu6VKhuna+W/V+1vpXHAfNgLY=
github.com/anyproto/go-ds-badger3 v0.3.1-0.20230524095230-434cf6346d9b h1:SMizb43hfILk2bpMgpTd30n6yQQdxW0ZbDti0wqfsBw=
Expand Down
5 changes: 3 additions & 2 deletions pkg/lib/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/anyproto/any-sync/app"

"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/files"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
Expand Down Expand Up @@ -224,7 +225,7 @@ func (g *gateway) fileHandler(w http.ResponseWriter, r *http.Request) {
file, reader, err := g.getFile(ctx, r)
if err != nil {
log.With("path", r.URL.Path).Errorf("error getting file: %s", err)
if strings.Contains(err.Error(), "file not found") {
if errors.Is(err, domain.ErrFileNotFound) {
http.NotFound(w, r)
return
}
Expand Down Expand Up @@ -271,7 +272,7 @@ func (g *gateway) imageHandler(w http.ResponseWriter, r *http.Request) {
file, reader, err := g.getImage(ctx, r)
if err != nil {
log.With("path", r.URL.Path).Errorf("error getting image: %s", err)
if strings.Contains(err.Error(), "file not found") {
if errors.Is(err, domain.ErrFileNotFound) {
http.NotFound(w, r)
return
}
Expand Down

0 comments on commit 2650518

Please sign in to comment.