Skip to content

[bug] windows 11, when use merge, error: The process cannot access the file because it is being used by another process. #158

@xujiajun

Description

@xujiajun

Describe the bug
windows 11, when use merge, error:
./tmp/database/3.dat: The process cannot access the file because it is being used by another process.

Give sample code if you can.

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/xujiajun/nutsdb"
)

func main() {
	opt := nutsdb.DefaultOptions
	opt.Dir = "./tmp/database"
	opt.SegmentSize = 1024 * 20
	//opt.RWMode = nutsdb.MMap
	//opt.SyncEnable = false
	opt.EntryIdxMode = nutsdb.HintKeyAndRAMIdxMode

	db, err := nutsdb.Open(opt)
	if err != nil {
		log.Fatalln("Open err:", err)
	}
	defer db.Close()

	bucket1 := "bucket001"
	go func() {
		for {
			_, err := GetBucketAll(db, bucket1)
			if err != nil {
				log.Printf("%s GetBucketAll err:%v", bucket1, err)
			}
			time.Sleep(100 * time.Millisecond)
		}
	}()
	//

	go func() {
		for {
			//log.Printf("%s SetDeviceSensor start....", bucket1)
			err := SetData(db, bucket1, []byte("is_online"), []byte("true"))
			if err != nil {
				log.Printf("%s SetDeviceSensor err:%v", bucket1, err)
			}
			//log.Printf("%s SetDeviceSensor end....", bucket1)
		}
	}()

	go func() {
		for {
			time.Sleep(10 * time.Second)
			if err := db.Merge(); err != nil {
				log.Println("Merge err:", err)
				//os.Exit(0)
			}
			fmt.Println("merge ok")
		}
	}()

	select {}
}

func GetBucketAll(db *nutsdb.DB, bucket string) (map[string][]byte, error) {
	data := make(map[string][]byte)
	if err := db.View(func(tx *nutsdb.Tx) error {
		entries, err := tx.GetAll(bucket)
		if err != nil {
			return err
		}
		for _, entry := range entries {
			data[string(entry.Key)] = entry.Value
		}
		return nil
	}); err != nil {
		return nil, err
	}
	return data, nil
}

func SetData(db *nutsdb.DB, table string, key, value []byte) error {
	if err := db.Update(func(tx *nutsdb.Tx) error {
		return tx.Put(table, key, value, 0)
	}); err != nil {
		return err
	}
	return nil
}

Expected behavior
no error

What actually happens
Windows 11:

please complete the following information :

  • OS: windows 11
  • NutsDB Version 0.8.0

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions