Skip to content

Commit

Permalink
fix embedding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalalx committed Jul 22, 2024
1 parent 58e4fac commit caf9f43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
48 changes: 48 additions & 0 deletions locker/lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package locker

import (
"fmt"
"os"
"syscall"
"time"
)

const LOCK_FILE_PATH = "~/.kdb/.storage_lock"

type FileLock struct {
file *os.File
timeout time.Duration
}

func NewFileLock(timeout time.Duration) (*FileLock, error) {
file, err := os.OpenFile(LOCK_FILE_PATH, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return nil, err
}
return &FileLock{file: file, timeout: timeout}, nil
}

func (fl *FileLock) Lock() error {
start := time.Now()
for {
err := syscall.Flock(int(fl.file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err == nil {
return nil // Lock acquired successfully
}
if err != syscall.EWOULDBLOCK {
return fmt.Errorf("unexpected error while acquiring lock: %v", err)
}
if time.Since(start) > fl.timeout {
return fmt.Errorf("timeout while acquiring lock")
}
time.Sleep(50 * time.Millisecond) // Wait before trying again
}
}

func (fl *FileLock) Unlock() error {
return syscall.Flock(int(fl.file.Fd()), syscall.LOCK_UN)
}

func (fl *FileLock) Close() error {
return fl.file.Close()
}
11 changes: 6 additions & 5 deletions repos/embeddingDuckDbRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ func (repo *EmbeddingDuckDbRepo) connect() (*sql.DB, error) {
return nil, err
}

db.Exec("SET autoinstall_known_extensions=true;")
db.Exec("SET autoload_known_extensions=true;")
db.Exec("INSTALL vss")
db.Exec("LOAD vss")
db.Exec("SET hnsw_enable_experimental_persistence=true;")

return db, nil
}

func (repo *EmbeddingDuckDbRepo) Init(dims int) error {
batch := []string{
"SET autoinstall_known_extensions=true;",
"SET autoload_known_extensions=true;",
"INSTALL vss",
"LOAD vss",
"SET hnsw_enable_experimental_persistence=true;",
fmt.Sprintf(`CREATE TABLE IF NOT EXISTS embeddings (
id uuid DEFAULT gen_random_uuid(),
content TEXT NOT NULL,
Expand Down

0 comments on commit caf9f43

Please sign in to comment.