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

feat: add bf.insert #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 31 additions & 3 deletions src/bloom-filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BaseFilter from './base-filter'
import { InsertOptions, StringOrNumber } from './options'

/**
* Bloom filter
Expand Down Expand Up @@ -62,10 +63,37 @@ export default class BloomFilter extends BaseFilter {
}

/**
* Get filter info
* @return Array of string
*/
* Get filter info
* @return Array of string
*/
public info(): Promise<string[]> {
return this.client.call('BF.INFO', this.name)
}

/**
* BF.INSERT is a sugarcoated combination of BF.RESERVE and BF.ADD.
* It creates a new filter if the key does not exist using the relevant arguments (see BF.RESERVE).
* Next, all ITEMS are inserted.
* @param items Array of items
* @param options InsertOptions
* @return Array of integers, each is either 1 or 0 depending on whether the corresponding item was newly added or may have already existed
*/
public insert(
items: any[],
{ errorRate, capacity, expansionRate, upsert = true }: InsertOptions = {}
): Promise<number[]> {
const cmd: StringOrNumber[] = ['BF.INSERT', this.name]
if (errorRate) cmd.push('ERROR', errorRate)

if (capacity) cmd.push('CAPACITY', capacity)

if (expansionRate)
if (expansionRate < 0) cmd.push('NONSCALING')
else cmd.push('EXPANSION', expansionRate)

if (!upsert) cmd.push('NOCREATE')

cmd.push('ITEMS', ...items)
return this.client.call(...cmd)
}
}
9 changes: 9 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ export default class Options implements OptionsInterface {
Object.assign(this, options)
}
}

export interface InsertOptions {
errorRate?: number
capacity?: number
expansionRate?: number
upsert?: boolean
}

export type StringOrNumber = string | number