Skip to content

added bolt store#26

Merged
connerohnesorge merged 5 commits into
mainfrom
bolt
May 20, 2025
Merged

added bolt store#26
connerohnesorge merged 5 commits into
mainfrom
bolt

Conversation

@connerohnesorge
Copy link
Copy Markdown
Owner

@connerohnesorge connerohnesorge commented May 19, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a BoltDB-backed persistent storage option for embeddings, enabling efficient, file-based storage and retrieval.
    • Added an example demonstrating how to use the BoltDB store with a semantic router for persistent embedding management.
  • Documentation

    • Added detailed documentation for the BoltDB store, including usage instructions, configuration options, concurrency notes, and example code.
    • Updated the documentation summary to include the new BoltDB store section.
    • Provided a README for the BoltDB store example.
  • Tests

    • Added comprehensive tests for the BoltDB store, covering basic operations, custom bucket names, and context cancellation handling.
  • Chores

    • Added Go module files for the new BoltDB store and its example.
    • Updated .gitignore to exclude Go workspace files.
    • Adjusted development environment scripts to support Go workspace initialization and removed formatting script.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 2025

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

📥 Commits

Reviewing files that changed from the base of the PR and between 384fa0c and 807b4a2.

⛔ Files ignored due to path filters (5)
  • examples/boltdb-store/go.sum is excluded by !**/*.sum
  • flake.lock is excluded by !**/*.lock
  • go.work is excluded by !**/*.work
  • go.work.sum is excluded by !**/*.sum
  • stores/bolt/go.sum is excluded by !**/*.sum
📒 Files selected for processing (11)
  • .gitignore (1 hunks)
  • doc/src/SUMMARY.md (1 hunks)
  • doc/src/stores/bolt.md (1 hunks)
  • examples/boltdb-store/README.md (1 hunks)
  • examples/boltdb-store/go.mod (1 hunks)
  • examples/boltdb-store/main.go (1 hunks)
  • flake.nix (2 hunks)
  • stores/bolt/bolt.go (1 hunks)
  • stores/bolt/bolt_test.go (1 hunks)
  • stores/bolt/doc.go (1 hunks)
  • stores/bolt/go.mod (1 hunks)
 ___________________________________________________________
< Patience, young padawan. The bugs will reveal themselves. >
 -----------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • ✅ Unit Test PR creation complete. (🔄 Check again to see options)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 2025

Note

Unit test generation is an Early Access feature. Expect some limitations and changes as we gather feedback and continue to improve it.


Generating unit tests... This may take up to 10 minutes.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (12)
doc/src/SUMMARY.md (1)

24-24: Add BoltDB to documentation summary
The new store is correctly linked under the "Stores" section. Consider reordering entries alphabetically for consistency across sections.

stores/bolt/doc.go (1)

8-24: Clarify example import statements
The example uses semanticrouter.NewRouter but only imports the Bolt store. Include imports for semanticrouter and log to make the snippet self-contained.

Apply this diff:

-// Example usage:
-//
-//	import (
-//	    "github.com/conneroisu/semanticrouter-go/stores/bolt"
-//	)
+// Example usage:
+//
+//	import (
+//	    "log"
+//	    bolt "github.com/conneroisu/semanticrouter-go/stores/bolt"
+//	    "github.com/conneroisu/semanticrouter-go"
+//	)
examples/boltdb-store/README.md (1)

32-33: Specify language for expected output code block
Markdown lint (MD040) suggests adding a language identifier. Change the fence to e.g. text for clarity.

-```
+```text
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

33-33: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)

doc/src/stores/bolt.md (1)

13-17: Consider adding version compatibility information

While the installation instructions are clear, it would be helpful to specify compatibility information such as minimum Go version required and any version constraints for the BoltDB dependency.

examples/boltdb-store/go.mod (1)

5-10: Consider version pinning for local module

The BoltDB store module is specified with version v0.0.0, which indicates an initial development version. For consistency with other dependencies, consider using a specific version once the module stabilizes.

examples/boltdb-store/main.go (2)

63-74: Consider making the model name configurable

The Ollama model name "mxbai-embed-large" is hard-coded with a comment suggesting it might need adjustment. Consider making this configurable through environment variables or command-line flags.

-			Model:  "mxbai-embed-large", // You may need to adjust the model name
+			Model:  getEnvWithDefault("OLLAMA_MODEL", "mxbai-embed-large"),

You would also need to add this helper function:

func getEnvWithDefault(key, defaultValue string) string {
    value := os.Getenv(key)
    if value == "" {
        return defaultValue
    }
    return value
}

83-93: Add context timeout for production reliability

The code uses a simple background context without timeout. For production applications, consider adding a timeout to prevent indefinite hanging if the matching service is unresponsive.

-	ctx := context.Background()
+	// Use a timeout to prevent indefinite hanging
+	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+	defer cancel()

Don't forget to add time to your imports:

import (
    "time"
    // other imports...
)
flake.nix (1)

2-2: Update project description

The description "Personal Website for Conner Ohnesorge" doesn't match the project's purpose as a semantic router with a BoltDB store. Consider updating it to accurately reflect the project.

-  description = "Personal Website for Conner Ohnesorge";
+  description = "Semantic Router with BoltDB Store Implementation";
stores/bolt/bolt_test.go (1)

1-99: Consider adding tests for additional edge cases

The current tests cover the primary functionality well, but consider adding tests for:

  • Empty embedding vectors
  • Very large embedding vectors (to test size limits)
  • Concurrent access to the store
func TestBoltStoreEdgeCases(t *testing.T) {
    // Setup code similar to TestBoltStore
    
    // Test with empty embedding
    emptyEmbedding := []float64{}
    emptyUtterance := semanticrouter.Utterance{
        Utterance: "empty embedding",
        Embed:     emptyEmbedding,
    }
    
    err = store.Set(ctx, emptyUtterance)
    assert.NoError(t, err)
    
    retrieved, err := store.Get(ctx, emptyUtterance.Utterance)
    assert.NoError(t, err)
    assert.Equal(t, emptyEmbedding, retrieved)
    
    // Test with large embedding
    largeEmbedding := make([]float64, 1024)
    for i := range largeEmbedding {
        largeEmbedding[i] = float64(i) * 0.01
    }
    
    largeUtterance := semanticrouter.Utterance{
        Utterance: "large embedding",
        Embed:     largeEmbedding,
    }
    
    err = store.Set(ctx, largeUtterance)
    assert.NoError(t, err)
    
    retrieved, err = store.Get(ctx, largeUtterance.Utterance)
    assert.NoError(t, err)
    assert.Equal(t, largeEmbedding, retrieved)
}

func TestBoltStoreConcurrency(t *testing.T) {
    // Setup code similar to TestBoltStore
    
    // Test concurrent access
    var wg sync.WaitGroup
    concurrencyLevel := 10
    
    for i := 0; i < concurrencyLevel; i++ {
        wg.Add(1)
        go func(idx int) {
            defer wg.Done()
            
            utterance := semanticrouter.Utterance{
                Utterance: fmt.Sprintf("concurrent test %d", idx),
                Embed:     []float64{float64(idx) * 0.1, float64(idx) * 0.2},
            }
            
            err := store.Set(ctx, utterance)
            assert.NoError(t, err)
            
            retrieved, err := store.Get(ctx, utterance.Utterance)
            assert.NoError(t, err)
            assert.Equal(t, utterance.Embed, retrieved)
        }(i)
    }
    
    wg.Wait()
}
stores/bolt/bolt.go (3)

77-100: Consider adding validation for embedding data

The Set method accepts and stores any embedding without validation. Consider adding validation to ensure the embedding is not nil and has reasonable dimensions before storing.

// Set stores an embedding with the given key.
func (s *Store) Set(ctx context.Context, keyValPair semanticrouter.Utterance) error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
	}

+	// Validate the embedding
+	if keyValPair.Embed == nil {
+		return fmt.Errorf("embedding cannot be nil")
+	}
+
+	// Optional: Check for reasonable dimensions
+	// if len(keyValPair.Embed) > MAX_DIMENSION {
+	//   return fmt.Errorf("embedding dimension exceeds maximum allowed: %d > %d", len(keyValPair.Embed), MAX_DIMENSION)
+	// }

	return s.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket(s.bucketName)
		if b == nil {
			return fmt.Errorf("bucket %s not found", string(s.bucketName))
		}

		// Marshal embedding to JSON for storage
		data, err := json.Marshal(keyValPair.Embed)
		if err != nil {
			return fmt.Errorf("failed to marshal embedding: %w", err)
		}

		// Store the embedding with the utterance as the key
		return b.Put([]byte(keyValPair.Utterance), data)
	})
}
🧰 Tools
🪛 golangci-lint (1.64.8)

78-78: undefined: semanticrouter

(typecheck)


132-135: Consider adding Delete and List methods for completeness

To provide a more complete API, consider adding methods to delete embeddings and list all stored keys.

// Delete removes an embedding for the given key.
func (s *Store) Delete(ctx context.Context, key string) error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
	}

	return s.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket(s.bucketName)
		if b == nil {
			return fmt.Errorf("bucket %s not found", string(s.bucketName))
		}
		return b.Delete([]byte(key))
	})
}

// List returns all keys stored in the bucket.
func (s *Store) List(ctx context.Context) ([]string, error) {
	select {
	case <-ctx.Done():
		return nil, ctx.Err()
	default:
	}

	var keys []string
	err := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(s.bucketName)
		if b == nil {
			return fmt.Errorf("bucket %s not found", string(s.bucketName))
		}

		return b.ForEach(func(k, v []byte) error {
			keys = append(keys, string(k))
			return nil
		})
	})

	if err != nil {
		return nil, err
	}

	return keys, nil
}

91-95: Consider binary encoding for performance optimization

JSON serialization works well for compatibility, but for performance-critical applications, consider using a more efficient binary encoding format like gob, msgpack, or protobuf, especially for large embedding vectors.

// For potential future optimization:
// Replace JSON with gob encoding
-		// Marshal embedding to JSON for storage
-		data, err := json.Marshal(keyValPair.Embed)
-		if err != nil {
-			return fmt.Errorf("failed to marshal embedding: %w", err)
-		}
+		// Marshal embedding to gob for storage
+		var buf bytes.Buffer
+		encoder := gob.NewEncoder(&buf)
+		if err := encoder.Encode(keyValPair.Embed); err != nil {
+			return fmt.Errorf("failed to encode embedding: %w", err)
+		}
+		data := buf.Bytes()

And similarly update the Get method's unmarshaling:

-		return json.Unmarshal(data, &embedding)
+		buf := bytes.NewBuffer(data)
+		decoder := gob.NewDecoder(buf)
+		return decoder.Decode(&embedding)

Remember to add the necessary import: "bytes" and "encoding/gob".

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 384fa0c and 807b4a2.

⛔ Files ignored due to path filters (5)
  • examples/boltdb-store/go.sum is excluded by !**/*.sum
  • flake.lock is excluded by !**/*.lock
  • go.work is excluded by !**/*.work
  • go.work.sum is excluded by !**/*.sum
  • stores/bolt/go.sum is excluded by !**/*.sum
📒 Files selected for processing (11)
  • .gitignore (1 hunks)
  • doc/src/SUMMARY.md (1 hunks)
  • doc/src/stores/bolt.md (1 hunks)
  • examples/boltdb-store/README.md (1 hunks)
  • examples/boltdb-store/go.mod (1 hunks)
  • examples/boltdb-store/main.go (1 hunks)
  • flake.nix (2 hunks)
  • stores/bolt/bolt.go (1 hunks)
  • stores/bolt/bolt_test.go (1 hunks)
  • stores/bolt/doc.go (1 hunks)
  • stores/bolt/go.mod (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
stores/bolt/bolt.go (1)
similarity.go (1)
  • Utterance (15-22)
🪛 markdownlint-cli2 (0.17.2)
examples/boltdb-store/README.md

33-33: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)

🪛 golangci-lint (1.64.8)
stores/bolt/bolt.go

78-78: undefined: semanticrouter

(typecheck)

🔇 Additional comments (18)
.gitignore (1)

6-7: Add go.work files to .gitignore
Including go.work and go.work.sum prevents workspace config files from being accidentally committed when using the new Go workspace setup.

stores/bolt/go.mod (1)

1-8: Define Go module and dependencies
Module path, Go version, and direct dependencies (bbolt for BoltDB and testify for tests) are correctly specified and align with the implementation.

doc/src/stores/bolt.md (4)

1-12: Well-organized introduction and features section!

The BoltDB store documentation provides a clear introduction with a comprehensive list of features. The link to the BoltDB GitHub repository allows users to learn more about the underlying technology.


19-78: Comprehensive usage example with good error handling

The usage example is well-structured and covers all the necessary steps: creating the store, initializing the client, setting up the router, and matching utterances. The error handling is thorough, making this a robust example for users to follow.


81-94: Clear configuration options section

The documentation on configuring a custom bucket name is clear and includes a code example, which is helpful for users who need to customize the store.


96-99: Important concurrency limitations clearly documented

Explicitly mentioning BoltDB's single write transaction limitation and suggesting alternatives for high-concurrency scenarios is valuable information for users considering this store implementation.

examples/boltdb-store/go.mod (3)

12-12: Replace directive appropriate for development

The replace directive pointing to a local path is appropriate for development but should be removed when publishing the example for general use.


14-19: Indirect dependencies properly specified

All indirect dependencies are correctly listed with specific versions, which helps ensure reproducible builds.


3-3:

Details

✅ Verification successful

Verify Go version compatibility

The module specifies Go 1.24.0, which appears to be a future version compared to the current stable release. Ensure this is intentional and verify compatibility with current Go versions.


🏁 Script executed:

#!/bin/bash
# Check current Go version in the environment and compare with specified version
go version
echo "Module requires Go 1.24.0, which may not be widely available yet"

# Check if any Go files use features specific to future Go versions
rg -t go "//go:build" --glob "!vendor/**"

Length of output: 225


Go 1.24 compatibility verified

The environment is already running Go v1.24.1 and there are no indications of future-only features in the codebase. Specifying go 1.24.0 in go.mod is therefore acceptable.

examples/boltdb-store/main.go (4)

16-25: Well-defined semantic routes with descriptive examples

The WeatherRoutes variable provides a good set of example utterances that clearly represent weather-related queries.


27-36: Consistent route definition pattern

The TravelRoutes follows the same pattern as WeatherRoutes, with appropriate examples for travel-related queries, maintaining consistency in the code structure.


38-42: Clean main function with proper error handling

The main function follows the common Go pattern of delegating to a run function and handling fatal errors appropriately.


44-56: Good store initialization with proper cleanup

The code correctly creates a BoltDB store, handles errors, and ensures proper cleanup with deferred functions. The comment explaining that in a real application you would keep the database file is helpful.

flake.nix (1)

37-51: Comprehensive workspace initialization script

The initWorkspace script effectively sets up a Go workspace with all relevant modules, which is excellent for local development across multiple packages. This approach will make it easier to work on and test the BoltDB store alongside other components.

stores/bolt/bolt_test.go (2)

15-66: Well-structured test covering core functionality

The test thoroughly verifies basic store operations including initializing a store, storing/retrieving embeddings, handling non-existent keys, and custom bucket configuration.


68-99: Good coverage of context cancellation handling

The cancellation test properly verifies that both Set and Get operations respect context cancellation and return appropriate errors.

stores/bolt/bolt.go (2)

15-135: Well-implemented BoltDB store with good error handling

The implementation provides a solid foundation for persistent embedding storage with proper context handling, error management, and a clean API.

🧰 Tools
🪛 golangci-lint (1.64.8)

78-78: undefined: semanticrouter

(typecheck)


78-78: False positive in static analysis: semanticrouter is correctly imported

The static analysis hint reports "undefined: semanticrouter" on line 78, but this is a false positive. The semanticrouter package is correctly imported on line 11 as github.com/conneroisu/semanticrouter-go.

🧰 Tools
🪛 golangci-lint (1.64.8)

78-78: undefined: semanticrouter

(typecheck)

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 2025

Creating a PR to put the unit tests in...

The changes have been created in this pull request: View PR

@connerohnesorge connerohnesorge merged commit 072c3ab into main May 20, 2025
7 checks passed
@connerohnesorge connerohnesorge deleted the bolt branch May 20, 2025 17:23
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

Successfully merging this pull request may close these issues.

1 participant