GithubHelp home page GithubHelp logo

sopherapps / go-scdb Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 293 KB

A very simple and fast key-value pure-go store but persisting data to disk, with a "localStorage-like" API.

Home Page: https://pkg.go.dev/github.com/sopherapps/go-scdb/scdb

License: Other

Go 99.94% Makefile 0.06%
diskcache golang key-value-store localstorage

go-scdb's People

Contributors

tinitto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

go-scdb's Issues

Errors with Slice Out of bounds Error when Retrieving Value that is an empty string

When one tries to retrieve a key whose associated value is an empty string []byte("") twice one after another for the default configuration of the store, the code returns an error with an Slice Out of bounds error.

Expected Behavior

Ideally, the empty string value should be returned every time an attempt is made to get it.

Current Behavior

The first time just after setting the value, when a store.Get is called, it works fine.
On the second time (and any number of times after that), it errors with Out of bounds error: slice %d - %d out of bounds for %s}, where %d is the same number e.g. for a key like "foo", the error is `Out of bounds error: slice 20 - 20 out of bounds for maxLength 20 for data [0 0 0 20 0 0 0 3 102 111 111 0 0 0 0 0 0 0 0 0]

Possible Solution

I expect the cause to be that the data slice from the file naturally only goes up to the expiry section for that KeyValueEntry. Technically, the KeyValueEntry has no value section because an empty string cannot not be saved in the file. The reason for this deduction is that when you look at the error message, it is expecting a slice [20..20] yet the data is from 0 to 19.

If we try to cater for the special case where value is of length 0, the issue can be fixed.

Steps to Reproduce

package main

import (
	"fmt"
	"github.com/sopherapps/go-scdb/scdb"
	"log"
)

func main() {
    key := []byte("foo")
    value := []byte("")
    
    store, err := scdb.New("db", nil, nil, nil, nil, false)
    if err != nil {
        log.Fatalf("error opening store: %s", err)
     }
    defer func() {
         _ = store.Close()
    }()

    err := store.Set(key, value, nil)
    if err != nil {
         log.Fatalf("error inserting key value: %s", err)
    }

   for i := 1; i <= 3; i++ {
        got, err := store.Get(key)
	if err != nil {
	    log.Fatalf("error getting key value: %s", err)
	}
	// assert.Equal(t, value, got)
    }
}

Context (Environment)

I was trying to use scdb as a map to quickly check for the existence or absence of a key. The value didn't matter so I chose to use an empty string. I was actually using py_scdb but the error was in scdb, which is the underlying engine of py_scdb. I figured that go-scdb is also affected.

Detailed Description

Possible Implementation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.