Giter Club home page Giter Club logo

bbloom's People

Contributors

andreasbriese avatar jtorvald avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bbloom's Issues

Use of unsafe is unsound

Casting to a uintptr, saving the uintptr, and then casting it back is unsound.

bbloom/bbloom.go

Lines 80 to 88 in e2d15f3

func NewWithBoolset(bs *[]byte, locs uint64) (bloomfilter Bloom) {
bloomfilter = New(float64(len(*bs)<<3), float64(locs))
ptr := uintptr(unsafe.Pointer(&bloomfilter.bitset[0]))
for _, b := range *bs {
*(*uint8)(unsafe.Pointer(ptr)) = b
ptr++
}
return bloomfilter
}

bbloom/bbloom.go

Lines 232 to 246 in e2d15f3

func (bl Bloom) JSONMarshal() []byte {
bloomImEx := bloomJSONImExport{}
bloomImEx.SetLocs = uint64(bl.setLocs)
bloomImEx.FilterSet = make([]byte, len(bl.bitset)<<3)
ptr := uintptr(unsafe.Pointer(&bl.bitset[0]))
for i := range bloomImEx.FilterSet {
bloomImEx.FilterSet[i] = *(*byte)(unsafe.Pointer(ptr))
ptr++
}
data, err := json.Marshal(bloomImEx)
if err != nil {
log.Fatal("json.Marshal failed: ", err)
}
return data
}

Union and Intersection features

Hi Andreas,

congrats for your work on bbloom, I am integrating it at the core of my new project.
I was wondering if you were planning to add methods to calculate union and intersection of 2 bloom filters or if you could put me in the right direction to learn how to do it.

Let me know, best,
Romain.

Right way to integrate it into my project

This may be a stupid question, but I'd very much appreciate your help. I want to implement a page view counter that will be pushed to a database every X hours. Before it gets pushed, then I intend to store it in memory and make sure that the same device (checked using IP + UA) doesn't count for multiple views (unless 1000 other people have visited the page, or it has been X hours since last visit)

I have the following code:

package main

import (
	"fmt"
	"sync"
)

type page struct {
	mu     *sync.Mutex
	values map[string]int64
}

var pageCounters = page{
	values: make(map[string]int64),
}

func (p *page) Delete(key string) {
	p.mu.Lock()
	defer p.mu.Unlock()
	delete(pageCounters.values, key)
}

func (p *page) Get(key string) int64 {
	p.mu.Lock()
	defer p.mu.Unlock()
	return p.values[key]
}

func (p *page) Incr(key string) int64 {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.values[key]++
	return p.values[key]
}

func main() {
	_ = pageCounters.Incr("/some/url/here")
	_ = pageCounters.Incr("/another/url")
	_ = pageCounters.Incr("/some/url/here")
	
	fmt.Println(pageCounters.Get("/some/url/here"))
	fmt.Println(pageCounters.Get("/another/url"))
	fmt.Println(pageCounters)
	pageCounters.Delete("/another/url")
	fmt.Println(pageCounters)

}

The plan now is to update the page struct to include a bloomfilter field, I'm thinking it would be done like this:

type page struct {
	mu     *sync.Mutex
	values map[string]int64
	bloom  bbloom.Bloom
}

I'm now wondering how I should update the Incr func so that:

  • if a bloom filter doesn't exist, then it will create one containing 1000 items
  • each item will be a combination of user IP address + User Agent (curious how you would recommend storing it?)
  • when the bloom filters items have been filled up, then the bloom filter should either be deleted and replaced by a new bloom filter, or have the previous items be overridden (I assume this is impossible)

I'm thinking it would look something like this:

func (p *page) Incr(key, ipUA string) int64 {
	p.mu.Lock()
	defer p.mu.Unlock()
	if (p.values[key] == 0) {
		p.bloom = bbloom.New(1000.0, 7.0) // probably use wrong values here
	}
	added := p.bloom.AddIfNotHasTS([]byte(ipUA))
	if added == true {
		p.values[key]++
	}
	return p.values[key]
}

Right now then I don't really know what will happen if the bloomfilter is full, and I'm not sure how to check for this and how to replace it. I guess if (p.values[key] == 0) could become if (p.values[key] == 0 || p.bloom.Size == 999)?

I also wonder, would there be any benefit to store the bloom as JSON? Keep in mind that each website page will have its own filter and they'll only be deleted after X hours (meaning there could potentially be many thousands of page structs initialized at any given moment)

I hope you don't mind me abusing your repo & time with this question

Missing License

I want to use this in one of my projects but have to make sure the license is compatible

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.