diff --git a/core/anytype/bootstrap.go b/core/anytype/bootstrap.go index 0d4bf6b1d..53015a4df 100644 --- a/core/anytype/bootstrap.go +++ b/core/anytype/bootstrap.go @@ -13,6 +13,7 @@ import ( "github.com/anytypeio/go-anytype-middleware/space" "github.com/anytypeio/go-anytype-middleware/space/storage" "github.com/anytypeio/go-anytype-middleware/space/typeprovider" + "github.com/anytypeio/go-anytype-middleware/util/builtintemplate" "os" "github.com/anytypeio/any-sync/app" @@ -136,7 +137,7 @@ func Bootstrap(a *app.App, components ...app.Component) { Register(process.New()). Register(source.New()). Register(core.New()). - //Register(builtintemplate.New()). + Register(builtintemplate.New()). Register(pin.New()). Register(status.New()). Register(block.New()). diff --git a/core/block/cache.go b/core/block/cache.go index 270a12c24..f132fdb8c 100644 --- a/core/block/cache.go +++ b/core/block/cache.go @@ -36,10 +36,17 @@ func (s *Service) createCache() ocache.OCache { if err != nil { return } + // creating tree if needed createPayload, exists := ctx.Value(treeCreateKey).(treeCreateCache) if exists { var ot objecttree.ObjectTree + if _, err := spc.Storage().TreeStorage(id); err == nil { + return s.objectFactory.InitObject(id, &smartblock.InitContext{ + Ctx: ctx, + }) + } + // TODO: [MR] Make put tree return error ot, err = spc.PutTree(ctx, createPayload.treeCreate, nil) if err != nil { return @@ -47,11 +54,13 @@ func (s *Service) createCache() ocache.OCache { ot.Close() return s.objectFactory.InitObject(id, createPayload.initFunc(id)) } + // putting object through cache putObject, exists := ctx.Value(putObjectKey).(smartblock.SmartBlock) if exists { return putObject, nil } + // if it is subObject if sbt, _ := coresb.SmartBlockTypeFromID(id); sbt == coresb.SmartBlockTypeSubObject { return s.initSubObject(ctx, id) diff --git a/core/block/object/creator.go b/core/block/object/creator.go index a0280d040..0e8436a4c 100644 --- a/core/block/object/creator.go +++ b/core/block/object/creator.go @@ -163,6 +163,7 @@ func (c *Creator) CreateSmartBlockFromState(ctx context.Context, sbType coresb.S if err != nil { return } + id = sb.Id() defer release() ev.SmartblockCreateMs = time.Since(startTime).Milliseconds() - ev.SetDetailsMs - ev.WorkspaceCreateMs - ev.GetWorkspaceBlockWaitMs ev.SmartblockType = int(sbType) diff --git a/core/indexer/indexer.go b/core/indexer/indexer.go index 37b39da60..0663fd036 100644 --- a/core/indexer/indexer.go +++ b/core/indexer/indexer.go @@ -44,16 +44,16 @@ const ( // ForceThreadsObjectsReindexCounter reindex thread-based objects ForceThreadsObjectsReindexCounter int32 = 8 // ForceFilesReindexCounter reindex ipfs-file-based objects - ForceFilesReindexCounter int32 = 7 // + ForceFilesReindexCounter int32 = 9 // // ForceBundledObjectsReindexCounter reindex objects like anytypeProfile - ForceBundledObjectsReindexCounter int32 = 4 // reindex objects like anytypeProfile + ForceBundledObjectsReindexCounter int32 = 5 // reindex objects like anytypeProfile // ForceIdxRebuildCounter erases localstore indexes and reindex all type of objects // (no need to increase ForceThreadsObjectsReindexCounter & ForceFilesReindexCounter) - ForceIdxRebuildCounter int32 = 34 + ForceIdxRebuildCounter int32 = 35 // ForceFulltextIndexCounter performs fulltext indexing for all type of objects (useful when we change fulltext config) ForceFulltextIndexCounter int32 = 4 // ForceFilestoreKeysReindexCounter reindex filestore keys in all objects - ForceFilestoreKeysReindexCounter int32 = 1 + ForceFilestoreKeysReindexCounter int32 = 2 ) var log = logging.Logger("anytype-doc-indexer") @@ -118,13 +118,6 @@ type indexer struct { typeProvider typeprovider.ObjectTypeProvider } -type myHasher struct { -} - -func (m myHasher) Hash() string { - return "" -} - func (i *indexer) Init(a *app.App) (err error) { i.newAccount = a.MustComponent(config.CName).(*config.Config).NewAccount i.anytype = a.MustComponent(core.CName).(core.Service) @@ -132,7 +125,7 @@ func (i *indexer) Init(a *app.App) (err error) { i.relationService = a.MustComponent(relation.CName).(relation.Service) i.typeProvider = a.MustComponent(typeprovider.CName).(typeprovider.ObjectTypeProvider) i.source = a.MustComponent(source.CName).(source.Service) - i.btHash = myHasher{} + i.btHash = a.MustComponent("builtintemplate").(Hasher) i.doc = a.MustComponent(doc.CName).(doc.Service) i.quit = make(chan struct{}) i.archivedMap = make(map[string]struct{}, 100) @@ -276,7 +269,7 @@ func (i *indexer) reindexIfNeeded() error { } func (i *indexer) reindexOutdatedThreads() (toReindex, success int, err error) { - // TODO: [MR] check reindexing logic + //TODO: [MR] check reindexing logic //if i.threadService == nil { // return 0, 0, nil //} diff --git a/pkg/lib/core/smartblock/smartblock.go b/pkg/lib/core/smartblock/smartblock.go index 38479f740..a23ce51c1 100644 --- a/pkg/lib/core/smartblock/smartblock.go +++ b/pkg/lib/core/smartblock/smartblock.go @@ -1,8 +1,10 @@ package smartblock import ( + "encoding/binary" "errors" "fmt" + "github.com/textileio/go-threads/core/thread" "strings" "github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/addr" @@ -90,6 +92,27 @@ func SmartBlockTypeFromID(id string) (SmartBlockType, error) { return SmartBlockTypePage, ErrNoSuchSmartblock } +func PatchSmartBlockType(id string, sbt SmartBlockType) (string, error) { + tid, err := thread.Decode(id) + if err != nil { + return id, err + } + rawid := []byte(tid.KeyString()) + ver, n := binary.Uvarint(rawid) + variant, n2 := binary.Uvarint(rawid[n:]) + _, n3 := binary.Uvarint(rawid[n+n2:]) + finalN := n + n2 + n3 + buf := make([]byte, 3*binary.MaxVarintLen64+len(rawid)-finalN) + n = binary.PutUvarint(buf, ver) + n += binary.PutUvarint(buf[n:], variant) + n += binary.PutUvarint(buf[n:], uint64(sbt)) + copy(buf[n:], rawid[finalN:]) + if tid, err = thread.Cast(buf[:n+len(rawid)-finalN]); err != nil { + return id, err + } + return tid.String(), nil +} + // Panics in case of incorrect sb type! func (sbt SmartBlockType) ToProto() model.SmartBlockType { return model.SmartBlockType(sbt) diff --git a/util/builtintemplate/builtintemplate.go b/util/builtintemplate/builtintemplate.go index 8d8e0d90a..dca5742ed 100644 --- a/util/builtintemplate/builtintemplate.go +++ b/util/builtintemplate/builtintemplate.go @@ -22,7 +22,6 @@ import ( "github.com/anytypeio/go-anytype-middleware/pkg/lib/core/smartblock" "github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/addr" "github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model" - "github.com/anytypeio/go-anytype-middleware/pkg/lib/threads" "github.com/anytypeio/go-anytype-middleware/util/pbtypes" ) @@ -47,7 +46,7 @@ type builtinTemplate struct { func (b *builtinTemplate) Init(a *app.App) (err error) { b.source = a.MustComponent(source.CName).(source.Service) - b.makeGenHash(3) + b.makeGenHash(4) return } @@ -91,7 +90,7 @@ func (b *builtinTemplate) registerBuiltin(rd io.ReadCloser) (err error) { return } st := state.NewDocFromSnapshot("", snapshot, state.DoNotMigrateTypes).(*state.State) - id, err := threads.PatchSmartBlockType(st.RootId(), smartblock.SmartBlockTypeBundledTemplate) + id, err := smartblock.PatchSmartBlockType(st.RootId(), smartblock.SmartBlockTypeBundledTemplate) if err != nil { return } @@ -120,6 +119,7 @@ func (b *builtinTemplate) registerBuiltin(rd io.ReadCloser) (err error) { if err = b.validate(st.Copy()); err != nil { return } + log.With("id", id).Info("registering template") b.source.RegisterStaticSource(id, func() source.Source { return b.source.NewStaticSource(id, model.SmartBlockType_BundledTemplate, st.Copy(), nil) })