1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00

Fix builtin objects indexing

This commit is contained in:
mcrakhman 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent faa3095cab
commit b1bf6c7201
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
3 changed files with 10 additions and 137 deletions

View file

@ -16,6 +16,7 @@ import (
coresb "github.com/anytypeio/go-anytype-middleware/pkg/lib/core/smartblock"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/datastore"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/files"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/addr"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/filestore"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/objectstore"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/logging"
@ -28,6 +29,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync"
)
@ -195,6 +197,12 @@ func (a *Anytype) GetAllWorkspaces() ([]string, error) {
}
func (a *Anytype) GetWorkspaceIdForObject(objectId string) (string, error) {
if strings.HasPrefix(objectId, "_") {
return addr.AnytypeMarketplaceWorkspace, nil
}
if a.predefinedBlockIds.IsAccount(objectId) {
return "", ErrObjectDoesNotBelongToWorkspace
}
return a.predefinedBlockIds.Account, nil
}
@ -231,13 +239,13 @@ func (a *Anytype) start() error {
func (a *Anytype) EnsurePredefinedBlocks(ctx context.Context, newAccount bool) (err error) {
sbTypes := []coresb.SmartBlockType{
coresb.SmartBlockTypeWorkspace,
coresb.SmartBlockTypeArchive,
coresb.SmartblockTypeMarketplaceType,
coresb.SmartblockTypeMarketplaceRelation,
coresb.SmartblockTypeMarketplaceTemplate,
coresb.SmartBlockTypeWidget,
coresb.SmartBlockTypeProfilePage,
coresb.SmartBlockTypeWorkspace,
coresb.SmartBlockTypeHome,
}
for _, sbt := range sbTypes {

View file

@ -279,14 +279,6 @@ type ObjectStore interface {
GetCurrentWorkspaceId() (string, error)
SetCurrentWorkspaceId(threadId string) (err error)
RemoveCurrentWorkspaceId() (err error)
AddThreadQueueEntry(entry *model.ThreadCreateQueueEntry) (err error)
RemoveThreadQueueEntry(threadId string) (err error)
GetAllQueueEntries() ([]*model.ThreadCreateQueueEntry, error)
AddThreadToWorkspace(threadId, workspaceId string) error
RemoveThreadForWorkspace(threadId, workspaceId string) error
GetThreadQueueState() (map[string]map[string]struct{}, map[string]map[string]struct{}, error)
}
type relationObjectType struct {
@ -344,134 +336,6 @@ type dsObjectStore struct {
depSubscriptions []database.Subscription
}
func (m *dsObjectStore) AddThreadToWorkspace(threadId, workspaceId string) error {
txn, err := m.ds.NewTransaction(false)
if err != nil {
return fmt.Errorf("error creating txn in datastore: %w", err)
}
defer txn.Discard()
workspaceKey := workspaceMapBase.Child(ds.NewKey(workspaceId)).Child(ds.NewKey(threadId))
if err := txn.Put(workspaceKey, nil); err != nil {
return fmt.Errorf("failed to put into ds: %w", err)
}
return txn.Commit()
}
func (m *dsObjectStore) RemoveThreadForWorkspace(threadId, workspaceId string) error {
txn, err := m.ds.NewTransaction(false)
if err != nil {
return fmt.Errorf("error creating txn in datastore: %w", err)
}
defer txn.Discard()
workspaceKey := workspaceMapBase.Child(ds.NewKey(workspaceId)).Child(ds.NewKey(threadId))
if err := txn.Delete(workspaceKey); err != nil {
return fmt.Errorf("failed to put into ds: %w", err)
}
return txn.Commit()
}
func (m *dsObjectStore) GetThreadQueueState() (map[string]map[string]struct{}, map[string]map[string]struct{}, error) {
txn, err := m.ds.NewTransaction(true)
if err != nil {
return nil, nil, fmt.Errorf("error creating txn in datastore: %w", err)
}
defer txn.Discard()
res, err := txn.Query(query.Query{
Prefix: workspaceMapBase.String(),
KeysOnly: true,
})
if err != nil {
return nil, nil, err
}
threadWorkspaces := make(map[string]map[string]struct{})
workspaceThreads := make(map[string]map[string]struct{})
for entry := range res.Next() {
split := strings.Split(entry.Key, "/")
if len(split) < 2 {
continue
}
workspaceId := split[len(split)-2]
threadId := split[len(split)-1]
threadKV, exists := threadWorkspaces[threadId]
if !exists {
threadKV = make(map[string]struct{})
threadWorkspaces[threadId] = threadKV
}
workspaceKV, exists := workspaceThreads[workspaceId]
if !exists {
workspaceKV = make(map[string]struct{})
workspaceThreads[workspaceId] = workspaceKV
}
threadKV[workspaceId] = struct{}{}
workspaceKV[threadId] = struct{}{}
}
return workspaceThreads, threadWorkspaces, nil
}
func (m *dsObjectStore) AddThreadQueueEntry(entry *model.ThreadCreateQueueEntry) (err error) {
txn, err := m.ds.NewTransaction(false)
if err != nil {
return fmt.Errorf("error creating txn in datastore: %w", err)
}
defer txn.Discard()
b, err := entry.Marshal()
if err != nil {
return fmt.Errorf("failed to marshal entry into binary: %w", err)
}
key := threadCreateQueueBase.Child(ds.NewKey(entry.ThreadId))
if err := txn.Put(key, b); err != nil {
return fmt.Errorf("failed to put into ds: %w", err)
}
return txn.Commit()
}
func (m *dsObjectStore) RemoveThreadQueueEntry(threadId string) (err error) {
txn, err := m.ds.NewTransaction(false)
if err != nil {
return fmt.Errorf("error creating txn in datastore: %w", err)
}
defer txn.Discard()
key := threadCreateQueueBase.Child(ds.NewKey(threadId))
if err = txn.Delete(key); err != nil {
return fmt.Errorf("failed to delete entry from ds: %w", err)
}
return txn.Commit()
}
func (m *dsObjectStore) GetAllQueueEntries() ([]*model.ThreadCreateQueueEntry, error) {
txn, err := m.ds.NewTransaction(true)
if err != nil {
return nil, fmt.Errorf("error creating txn in datastore: %w", err)
}
defer txn.Discard()
res, err := txn.Query(query.Query{
Prefix: threadCreateQueueBase.String(),
})
if err != nil {
return nil, fmt.Errorf("error query txn in datastore: %w", err)
}
var models []*model.ThreadCreateQueueEntry
for entry := range res.Next() {
var qEntry model.ThreadCreateQueueEntry
err = proto.Unmarshal(entry.Value, &qEntry)
if err != nil {
log.Errorf("failed to unmarshal thread create entry")
continue
}
models = append(models, &qEntry)
}
return models, nil
}
func (m *dsObjectStore) GetCurrentWorkspaceId() (string, error) {
txn, err := m.ds.NewTransaction(true)
if err != nil {

View file

@ -184,6 +184,7 @@ func (b *builtinObjects) createObject(ctx context.Context, rd io.ReadCloser) (er
st.RemoveDetail(bundle.RelationKeyCreator.String(), bundle.RelationKeyLastModifiedBy.String())
st.SetLocalDetail(bundle.RelationKeyCreator.String(), pbtypes.String(addr.AnytypeProfileId))
st.SetLocalDetail(bundle.RelationKeyLastModifiedBy.String(), pbtypes.String(addr.AnytypeProfileId))
st.SetLocalDetail(bundle.RelationKeyWorkspaceId.String(), pbtypes.String(b.service.Anytype().PredefinedBlocks().Account))
st.InjectDerivedDetails()
if err = b.validate(st); err != nil {
return