ostafen / clover

A lightweight document-oriented NoSQL database written in pure Golang.
MIT License
680 stars 55 forks source link

Inserts do not set up index correctly #6

Closed xxxserxxx closed 2 years ago

xxxserxxx commented 2 years ago

This issue was in the pre-badger code (<= dc5b9e3503ecba809eff2436d1ee69a4b171f36b). I'm putting it here for anyone else who wants to continue using the old storage. Note that if you're doing this, you should also cherry-pick the other indexing bug fix, e736362eb15e75437a1404458148678902a022d4.

The behavior is that Insert() of documents after the first would result in an incorrect index since it always started with an offset of 0 regardless of how many documents were already in the collection. Attempts to access document N>0 would always get the wrong document. This usually resulted in bad JSON formatting errors, either unexpected end of JSON or invalid syntax. The patch is:

diff --git a/storage.go b/storage.go
--- a/storage.go
+++ b/storage.go
@@ -260,7 +260,7 @@ func (s *storageImpl) Insert(collection
                return err
        }

-       pointers, err := appendDocs(&collectionFile{File: tempFile, size: 0}, docs)
+       pointers, err := appendDocs(&collectionFile{File: tempFile, size: coll.file.size}, docs)
        if err != nil {
                return err
        }

@ostafen, I'm submitting and then closing; hopefully others searching for a fix will find this.

ostafen commented 2 years ago

Thank you for pointing this out :+1: