Created
August 9, 2024 22:37
-
-
Save electron0zero/5106ea0eda4c2f5098fb2bd70941fd12 to your computer and use it in GitHub Desktop.
Read some blooms in golang using "github.com/willf/bloom" and dump them into stdout, only tested for bloom created using "github.com/willf/bloom"
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"github.com/willf/bloom" | |
) | |
# same as viz.go but just dump the json into stdout: | |
func main() { | |
// Check if the file path is provided as an argument | |
if len(os.Args) < 2 { | |
fmt.Println("Usage: go run main.go <file-path>") | |
return | |
} | |
// Get the file path from the first argument | |
filePath := os.Args[1] | |
// Open the file that contains the Bloom filter data | |
file, err := os.Open(filePath) | |
if err != nil { | |
fmt.Println("Error opening file:", err) | |
return | |
} | |
defer file.Close() // Ensure the file is closed when done | |
// Create an empty BloomFilter instance | |
filter := &bloom.BloomFilter{} | |
// Read the data from the file into the BloomFilter | |
_, err = filter.ReadFrom(file) | |
if err != nil { | |
fmt.Println("Error reading Bloom filter from file:", err) | |
return | |
} | |
// Marshal the Bloom filter to JSON format | |
jsonData, err := json.MarshalIndent(filter, "", " ") | |
if err != nil { | |
fmt.Println("Error marshalling Bloom filter to JSON:", err) | |
return | |
} | |
// Print the JSON format of the Bloom filter to stdout | |
fmt.Println(string(jsonData)) | |
} | |
# run this code with: go run bloom.go mybloom.bin |
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"github.com/willf/bitset" | |
"github.com/willf/bloom" | |
) | |
type ExportedBloomFilter struct { | |
M uint `json:"m"` | |
K uint `json:"k"` | |
B *bitset.BitSet `json:"b"` // or any other type depending on how you want to represent the bitset | |
} | |
func NewExportedBloomFilter(bf *bloom.BloomFilter) (*ExportedBloomFilter, error) { | |
// Marshal the original BloomFilter to JSON | |
data, err := json.Marshal(bf) | |
if err != nil { | |
return nil, err | |
} | |
// Unmarshal the data into the new ExportedBloomFilter struct | |
var exportedBF ExportedBloomFilter | |
if err := json.Unmarshal(data, &exportedBF); err != nil { | |
return nil, err | |
} | |
return &exportedBF, nil | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("Usage: go run main.go <file-path>") | |
return | |
} | |
// Get the file path from the first argument | |
filePath := os.Args[1] | |
// Open the file that contains the Bloom filter data | |
file, err := os.Open(filePath) | |
if err != nil { | |
fmt.Println("Error opening file:", err) | |
return | |
} | |
defer file.Close() // Ensure the file is closed when done | |
// Create an empty BloomFilter instance | |
filter := &bloom.BloomFilter{} | |
// Read the data from the file into the BloomFilter | |
_, err = filter.ReadFrom(file) | |
if err != nil { | |
fmt.Println("Error reading Bloom filter from file:", err) | |
return | |
} | |
exportedBF, err := NewExportedBloomFilter(filter) | |
if err != nil { | |
fmt.Println("Error:", err) | |
return | |
} | |
// Print the attributes of the BloomFilter structure | |
fmt.Println("Bloom Filter Attributes:") | |
fmt.Printf("m (size in bits): %d\n", filter.Cap()) | |
fmt.Printf("k (number of hash functions): %d\n", filter.K()) | |
fmt.Printf("BitSet length: %d\n", filter.Cap()) | |
fmt.Printf("BitSet string: %s\n", exportedBF.B.String()) | |
} | |
# run this code with: go run viz.go mybloom.bin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment