Skip to content

Commit

Permalink
improve fetcher code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Apr 2, 2023
1 parent 697a554 commit 5063e33
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions db/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type DocumentFetcher struct {
kvEnd bool
isReadingDocument bool

// Since deleted documents are stored under a different instance type than active documents,
// we use a parallel fetcher to be able to return the documents in the expected order.
// That being lexicographically ordered dockeys.
deletedDocFetcher *DocumentFetcher
}

Expand All @@ -75,6 +78,26 @@ func (df *DocumentFetcher) Init(
return client.NewErrUninitializeProperty("DocumentFetcher", "Schema")
}

err := df.init(col, fields, reverse)
if err != nil {
return err
}

if showDeleted {
if df.deletedDocFetcher == nil {
df.deletedDocFetcher = new(DocumentFetcher)
}
return df.deletedDocFetcher.init(col, fields, reverse)
}

return nil
}

func (df *DocumentFetcher) init(
col *client.CollectionDescription,
fields []*client.FieldDescription,
reverse bool,
) error {
df.col = col
df.fields = fields
df.reverse = reverse
Expand All @@ -99,38 +122,6 @@ func (df *DocumentFetcher) Init(
for _, field := range col.Schema.Fields {
df.schemaFields[uint32(field.ID)] = field
}
if showDeleted {
ddf := df.deletedDocFetcher
if ddf == nil {
ddf = new(DocumentFetcher)
}

ddf.col = col
ddf.fields = fields
ddf.reverse = reverse
ddf.initialized = true
ddf.isReadingDocument = false
ddf.doc = new(encodedDocument)

if ddf.kvResultsIter != nil {
if err := ddf.kvResultsIter.Close(); err != nil {
return err
}
}
ddf.kvResultsIter = nil
if ddf.kvIter != nil {
if err := ddf.kvIter.Close(); err != nil {
return err
}
}
ddf.kvIter = nil

ddf.schemaFields = make(map[uint32]client.FieldDescription)
for _, field := range col.Schema.Fields {
ddf.schemaFields[uint32(field.ID)] = field
}
df.deletedDocFetcher = ddf
}
return nil
}

Expand Down Expand Up @@ -427,12 +418,22 @@ func (df *DocumentFetcher) FetchNextDoc(
ddf := df.deletedDocFetcher
if ddf != nil {
if !ddf.kvEnd {
if df.kvEnd || ddf.kv.Key.DocKey < df.kv.Key.DocKey {
encdoc, err = ddf.FetchNext(ctx)
if err != nil {
return nil, core.Doc{}, err
if df.reverse {
if df.kvEnd || ddf.kv.Key.DocKey > df.kv.Key.DocKey {
encdoc, err = ddf.FetchNext(ctx)
if err != nil {
return nil, core.Doc{}, err
}
status = client.Deleted
}
} else {
if df.kvEnd || ddf.kv.Key.DocKey < df.kv.Key.DocKey {
encdoc, err = ddf.FetchNext(ctx)
if err != nil {
return nil, core.Doc{}, err
}
status = client.Deleted
}
status = client.Deleted
}
}
}
Expand Down

0 comments on commit 5063e33

Please sign in to comment.