-
Notifications
You must be signed in to change notification settings - Fork 2
feat(tests): add new example test cases #3
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ package shard | |||||
| import ( | ||||||
| "crypto/sha512" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "io/ioutil" | ||||||
| "os" | ||||||
| "path" | ||||||
|
|
@@ -14,7 +15,7 @@ func ExampleWriter() { | |||||
| fmt.Println(err) | ||||||
| return | ||||||
| } | ||||||
| defer os.RemoveAll(name) //nolint:wsl | ||||||
| defer func() { _ = os.RemoveAll(name) }() //nolint:wsl | ||||||
|
|
||||||
| w := NewWriter(5, &PrefixSum64Hash{sha512.New()}, NewOSFileWriterFactory(path.Join(name, "test-"))) | ||||||
| records := []string{ | ||||||
|
|
@@ -35,7 +36,7 @@ func ExampleWriter() { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| w.Close() | ||||||
| _ = w.Close() // Changed this line | ||||||
|
|
||||||
| for i := 0; i < 5; i++ { | ||||||
| filename := fmt.Sprintf("test-%05d-of-00005", i) | ||||||
|
|
@@ -49,3 +50,53 @@ func ExampleWriter() { | |||||
| // test-00003-of-00005: | ||||||
| // test-00004-of-00005:test1test2test3test1 | ||||||
| } | ||||||
|
|
||||||
| func ExamplePrefixSum64Hash_Sum64() { | ||||||
| hasher := &PrefixSum64Hash{sha512.New()} | ||||||
| hasher.Write([]byte("hello")) | ||||||
| fmt.Println(hasher.Sum64()) | ||||||
| // Output: 11200964803485168504 | ||||||
| } | ||||||
|
|
||||||
| func ExampleNewOSFileWriterFactory() { | ||||||
| tempDir, err := ioutil.TempDir("", "examplefactory") | ||||||
| if err != nil { | ||||||
| fmt.Println("Failed to create temp dir:", err) | ||||||
| return | ||||||
| } | ||||||
| defer func() { _ = os.RemoveAll(tempDir) }() | ||||||
|
|
||||||
| factoryPrefix := "testfactory-" | ||||||
| factory := NewOSFileWriterFactory(path.Join(tempDir, factoryPrefix)) | ||||||
|
|
||||||
| numFiles := 3 | ||||||
| for i := 0; i < numFiles; i++ { | ||||||
| writer := factory(i, numFiles) | ||||||
| _, err := fmt.Fprintf(writer, "data for file %d", i) | ||||||
| if err != nil { | ||||||
| fmt.Printf("Error writing to file %d: %v\n", i, err) | ||||||
| } | ||||||
| if closer, ok := writer.(io.Closer); ok { | ||||||
| err := closer.Close() | ||||||
| if err != nil { | ||||||
| fmt.Printf("Error closing file %d: %v\n", i, err) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| for i := 0; i < numFiles; i++ { | ||||||
| fileName := fmt.Sprintf("%s%05d-of-%05d", factoryPrefix, i, numFiles) | ||||||
| filePath := path.Join(tempDir, fileName) | ||||||
| content, err := ioutil.ReadFile(filePath) | ||||||
|
||||||
| content, err := ioutil.ReadFile(filePath) | |
| content, err := os.ReadFile(filePath) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package sstable | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| // "io" // Will add if ReadEntry or other functions require it directly for type matching. | ||
| // For now, bytes.Reader and the CursorToOffset itself will handle reader interfaces. | ||
| ) | ||
|
|
||
| func ExampleCursorToOffset() { | ||
| // 1. Prepare entries | ||
| entry1 := Entry{Key: []byte("key1"), Value: []byte("value1")} | ||
| entry2 := Entry{Key: []byte("key2"), Value: []byte("value22")} // Different length value | ||
|
|
||
| // 2. Marshal entries into a buffer | ||
| var buf bytes.Buffer | ||
| data1, err := entry1.MarshalBinary() | ||
| if err != nil { | ||
| fmt.Println("Error marshalling entry1:", err) | ||
| return | ||
| } | ||
| buf.Write(data1) | ||
|
|
||
| data2, err := entry2.MarshalBinary() | ||
| if err != nil { | ||
| fmt.Println("Error marshalling entry2:", err) | ||
| return | ||
| } | ||
| buf.Write(data2) | ||
|
|
||
| serializedData := buf.Bytes() | ||
|
|
||
| // 3. Calculate endOffset (total size of serialized data) | ||
| // This could also be entry1.Size() + entry2.Size() | ||
| endOffset := uint64(len(serializedData)) | ||
|
|
||
| // 4. Create a bytes.NewReader | ||
| reader := bytes.NewReader(serializedData) | ||
|
|
||
| // 5. Create CursorToOffset instance | ||
| // Reader can be io.Reader or io.ReaderAt. bytes.NewReader implements both. | ||
| // For this example, let's ensure it's treated as a general io.Reader | ||
| // by the cursor, though CursorToOffset will detect it as io.ReaderAt if not type asserted. | ||
| // The implementation of CursorToOffset.Entry() prefers io.ReaderAt if available. | ||
| cursor := &CursorToOffset{ | ||
| reader: reader, // bytes.NewReader is also an io.ReaderAt | ||
| offset: 0, | ||
| endOffset: endOffset, | ||
| entry: nil, // Starts with no entry loaded | ||
| } | ||
|
|
||
| // 6. Loop through entries | ||
| for !cursor.Done() { | ||
| currentEntry := cursor.Entry() | ||
| if currentEntry == nil { | ||
| // This might happen if ReadEntry/ReadEntryAt fails, | ||
| // or if Done() condition is met but loop condition was already checked. | ||
| // Or if endOffset is 0. | ||
| // Given the Done() logic, if Entry() returns nil, Done() should usually be true. | ||
| // Let's assume valid entries for example purposes. | ||
| fmt.Println("Error: current entry is nil, but not done.") | ||
| break | ||
| } | ||
| fmt.Printf("Key: %s, Value: %s\n", string(currentEntry.Key), string(currentEntry.Value)) | ||
| cursor.Next() | ||
| } | ||
|
|
||
| // Output: | ||
| // Key: key1, Value: value1 | ||
| // Key: key2, Value: value22 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package sstable | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| ) | ||
|
|
||
| func ExampleNewRecordIOReader() { | ||
| // 1. Prepare entries | ||
| entry1 := Entry{Key: []byte("keyA"), Value: []byte("valueA")} | ||
| entry2 := Entry{Key: []byte("keyB"), Value: []byte("valueBB")} // Different length value | ||
|
|
||
| // 2. Marshal entries into a buffer | ||
| var dataBuf bytes.Buffer | ||
| data1Bytes, err := entry1.MarshalBinary() | ||
| if err != nil { | ||
| fmt.Println("Error marshalling entry1:", err) | ||
| return | ||
| } | ||
| dataBuf.Write(data1Bytes) | ||
|
|
||
| data2Bytes, err := entry2.MarshalBinary() | ||
| if err != nil { | ||
| fmt.Println("Error marshalling entry2:", err) | ||
| return | ||
| } | ||
| dataBuf.Write(data2Bytes) | ||
|
|
||
| serializedEntries := dataBuf.Bytes() | ||
|
|
||
| // 3. Create a bytes.NewReader (which implements io.Reader and io.ReaderAt) | ||
| reader := bytes.NewReader(serializedEntries) | ||
|
|
||
| // 4. Calculate the total size of the serialized data | ||
| totalSize := uint64(len(serializedEntries)) | ||
|
|
||
| // 5. Call NewRecordIOReader | ||
| // NewRecordIOReader takes an io.Reader, but CursorToOffset (which it returns) | ||
| // will try to use it as io.ReaderAt if possible. bytes.NewReader supports this. | ||
| cursor := NewRecordIOReader(reader, totalSize) | ||
|
|
||
| // 6. Loop through entries using the cursor | ||
| for !cursor.Done() { | ||
| currentEntry := cursor.Entry() | ||
| if currentEntry == nil { | ||
| // Should not happen in this example if data is valid and size is correct | ||
| fmt.Println("Error: current entry is nil, but not done.") | ||
| break | ||
| } | ||
| fmt.Printf("Key: %s, Value: %s\n", string(currentEntry.Key), string(currentEntry.Value)) | ||
| cursor.Next() | ||
| } | ||
|
|
||
| // Output: | ||
| // Key: keyA, Value: valueA | ||
| // Key: keyB, Value: valueBB | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ioutilpackage is deprecated; consider usingos.MkdirTempto create a temporary directory in newer Go versions.