Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to print the right order #7

Open
gcclinux opened this issue Feb 1, 2023 · 0 comments
Open

Unable to print the right order #7

gcclinux opened this issue Feb 1, 2023 · 0 comments

Comments

@gcclinux
Copy link

gcclinux commented Feb 1, 2023

`package pkg

import (
"fmt"
"strconv"
"time"

"github.com/deroproject/graviton"

)

func RunGraviton() {

store, _ := graviton.NewDiskStore("db/graviton.db")
ss, _ := store.LoadSnapshot(0)
root, _ := ss.GetTree("root")

// Create a list of date and store in database

today := time.Now()
keys := 0
for x := 0; x < 10; x++ {
	date := today.AddDate(0, 0, x)
	save := fmt.Sprintf("%02d-%02d-%04d", date.Day(), date.Month(), date.Year())
	root.Put([]byte(strconv.Itoa(keys)), []byte(save))
	keys++
}
graviton.Commit(root)

// Get first value stored in database
first := root.Cursor()
k, v, _ := first.First()
fmt.Printf("FIRST: key=%s, value=%s\n", k, v)

// Get last value stored in database
last := root.Cursor()
for k, v, err := last.Last(); err == nil; k, v, err = last.Next() {
	fmt.Printf("LAST : key=%s, value=%s\n", k, v)
}

// Get individual value based on key = "2" example
var key string = "2"
value, _ := root.Get([]byte(key))
fmt.Printf("SELECT: key=%s, value=%s\n", key, string(value))

fmt.Println()
fmt.Println("LAST 5:")
// Get last 5 values stored in database
five := root.Cursor()
count := 5
for k, v, err := five.Last(); err == nil && count != 0; k, v, err = five.Prev() {
	fmt.Printf("LAST: key=%s, value=%s\n", k, v)
	count--
}
fmt.Println()

// Get all values stored in database
a := root.Cursor()
fmt.Println("ALL:")
for k, v, err := a.First(); err == nil; k, v, err = a.Next() {
	fmt.Printf("key=%s, value=%s\n", k, v)
}

fmt.Println()

// Count len of db entries..
c := root.Cursor()
lenght := 0

for _, _, err := c.First(); err == nil; _, _, err = c.Next() {
	lenght++
}

fmt.Printf("Len or total records: %d\n", lenght)

}
`

Result:

`$ go run main.go

GRAVITON TEST
FIRST: key=1, value=02-02-2023
LAST : key=7, value=08-02-2023
SELECT: key=2, value=03-02-2023

LAST 5:
LAST: key=7, value=08-02-2023
LAST: key=2, value=03-02-2023
LAST: key=8, value=09-02-2023
LAST: key=9, value=10-02-2023
LAST: key=4, value=05-02-2023

ALL:
key=1, value=02-02-2023
key=0, value=01-02-2023
key=3, value=04-02-2023
key=5, value=06-02-2023
key=6, value=07-02-2023
key=4, value=05-02-2023
key=9, value=10-02-2023
key=8, value=09-02-2023
key=2, value=03-02-2023
key=7, value=08-02-2023

Len or total records: 10
`

Can you help fix this, LAST 5 is incorrect, ALL is not in order, FIRST key also is not the first one.

I really appreciate any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant