ostafen / clover

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

Runtime error by using of function db.Insert #150

Closed giesan closed 6 months ago

giesan commented 6 months ago

Hello everyone,

I am currently using version v2.0.0-alpha.3 and get the following error message when using the db.Insert function

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7c3e2e]

goroutine 1 [running]:
github.com/ostafen/clover/v2/document.(*Document).Has(...)
    /home/andy/go/pkg/mod/github.com/ostafen/clover/v2@v2.0.0-alpha.3/document/document.go:91
github.com/ostafen/clover/v2.(*DB).Insert(0xc0001246c0, {0x88f12f, 0x5}, {0xc000158600, 0x4, 0xc000046508?})
    /home/andy/go/pkg/mod/github.com/ostafen/clover/v2@v2.0.0-alpha.3/db.go:131 +0xae
main.main()
    /home/andy/repos/learning/golang/nosql/clover/main.go:30 +0x288
exit status 2

Here is my code I tried with.

package main

import (
    c "github.com/ostafen/clover/v2"
    d "github.com/ostafen/clover/v2/document"
)

func main() {

    db, _ := c.Open(".")
    defer db.Close()

    db.CreateCollection("todos")

    // Create todos
    todo1 := d.NewDocument()
    todo1.Set("title", "delectus aut autem")
    todo1.Set("completed", false)
    todo1.Set("userId", 1)

    todo2 := d.NewDocument()
    todo2.Set("title", "quis ut nam facilis et officia qui")
    todo2.Set("completed", false)
    todo2.Set("userId", 2)

    var docs = make([]*d.Document, 2)
    docs = append(docs, todo1)
    docs = append(docs, todo2)

    db.Insert("todos", docs...)
}

Am I doing something wrong? Best regards andy

ostafen commented 6 months ago

This snippet results in a slice with 4 documents, with the first 2 being nil. Try replacing:

var docs = make([]*d.Document, 2)

with:

var docs = make([]*d.Document, 0, 2)
giesan commented 6 months ago

Hello Stefano, thank you for your reply. I have now also found the solution. Sorry, that was my mistake. I close this issue. Many thanks and best regards andy