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

Changed some variable names #42

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions memoria.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,49 @@ func cleanUp(file *os.File, onCleanUpError error) error {
}
return fmt.Errorf("%s ..Files Cleaned!", onCleanUpError)
}

// Implementing Concurrent Bulk Write Operations using Go Routines

type WriteResult struct {
Key string
Error error
}

func (m *Memoria) BulkWrite(pairs map[string][]byte) []WriteResult {
var wg sync.WaitGroup
results := make([]WriteResult, 0, len(pairs)) //To store results of each write op and also I've kept its size equal to no. of pairs
var mu sync.Mutex

mu.Lock()

//Creating channel for goroutines
resultChan := make(chan WriteResult, len(pairs)) //Hence the Buffer size in channel is no. of pairs

//Implementing goroutines
for key, value := range pairs {
wg.Add(1)
go func(key string, value []byte) {
defer wg.Done()

err := m.Write(key, value)

// Capture the result and send it to the result channel
resultChan <- WriteResult{
Key: key,
Error: err,
}
}(key, value)
}
go func() {
wg.Wait()
close(resultChan) //To close the channel once all the goroutines are completed
}()

for result := range resultChan {
results = append(results, result)
}
mu.Unlock()

return results

}
75 changes: 75 additions & 0 deletions test/memoria_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"bytes"
"os"
"strings"
"testing"

memoria "github.com/IMGIITRoorkee/Memoria_Simple"
Expand Down Expand Up @@ -79,3 +80,77 @@ func TestMemoriaWriteRead(t *testing.T) {
})
}
}

func TestMemoriaWriteReadString(t *testing.T) {

// Create temporary directory for tests
tempDir, err := os.MkdirTemp("", "memoria-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)

m := memoria.New(memoria.Options{
Basedir: tempDir,
MaxCacheSize: 1024,
})

tests := []struct {
name string
key string
value string
wantErr bool
}{
{
name: "Simple write and read (String Wrapper)",
key: "test1",
value: string("Hello World!"),
wantErr: false,
},
{
name: "Empty key",
key: "",
value: string("test"),
wantErr: true,
},
{
name: "Empty value",
key: "test2",
value: string(""),
wantErr: false,
},
{
name: "Large value",
key: "test3",
value: strings.Repeat("a", 1000),
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

// Test WriteString
err := m.WriteString(tt.key, tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
return
}

if tt.wantErr {
return
}

// Test ReadString
got, err := m.ReadString(tt.key)
if err != nil {
t.Errorf("Read() error = %v", err)
return
}

if got != tt.value {
t.Errorf("Read() got = %v, want %v", got, tt.value)
}
})
}
}
Loading