-
Notifications
You must be signed in to change notification settings - Fork 7
fix(*): Method.Random[Key]
behavior
#269
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
base: main
Are you sure you want to change the base?
Changes from all commits
c165745
40162cc
617c62c
b5fa4a7
e57b55f
cd06794
f489b59
68f2e4b
dd7f4e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -488,60 +488,68 @@ export class MapProvider<StoredValue = unknown> extends JoshProvider<StoredValue | |
} | ||
|
||
public [Method.Random](payload: Payload.Random<StoredValue>): Payload.Random<StoredValue> { | ||
if (this.cache.size === 0) return { ...payload, data: [] }; | ||
const { count, unique } = payload; | ||
|
||
const { count, duplicates } = payload; | ||
|
||
if (this.cache.size < count) { | ||
if (unique && this.cache.size < count) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.InvalidCount, method: Method.Random })); | ||
|
||
return payload; | ||
} | ||
|
||
if (this.cache.size === 0) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.MissingData, method: Method.Random, context: { unique, count } })); | ||
|
||
return payload; | ||
} | ||
|
||
payload.data = []; | ||
|
||
const keys = Array.from(this.cache.keys()); | ||
|
||
if (duplicates) { | ||
while (payload.data.length < count) { | ||
const key = keys[Math.floor(Math.random() * keys.length)]; | ||
|
||
payload.data.push(this.cache.get(key)!); | ||
} | ||
} else { | ||
if (unique) { | ||
const randomKeys = new Set<string>(); | ||
|
||
while (randomKeys.size < count) randomKeys.add(keys[Math.floor(Math.random() * keys.length)]); | ||
|
||
for (const key of randomKeys) payload.data.push(this.cache.get(key)!); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to not need 2 loops here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could probably just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to get the value with the keys, so yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we could probably get the key and fetch the value in the same loop though |
||
} else { | ||
while (payload.data.length < count) { | ||
const key = keys[Math.floor(Math.random() * keys.length)]; | ||
|
||
payload.data.push(this.cache.get(key)!); | ||
} | ||
} | ||
|
||
return payload; | ||
} | ||
|
||
public [Method.RandomKey](payload: Payload.RandomKey): Payload.RandomKey { | ||
if (this.cache.size === 0) return { ...payload, data: [] }; | ||
const { count, unique } = payload; | ||
|
||
const { count, duplicates } = payload; | ||
|
||
if (this.cache.size < count) { | ||
if (unique && this.cache.size < count) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.InvalidCount, method: Method.RandomKey })); | ||
|
||
return payload; | ||
} | ||
|
||
if (this.cache.size === 0) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.MissingData, method: Method.RandomKey })); | ||
|
||
return payload; | ||
} | ||
|
||
payload.data = []; | ||
|
||
const keys = Array.from(this.cache.keys()); | ||
|
||
if (duplicates) { | ||
while (payload.data.length < count) payload.data.push(keys[Math.floor(Math.random() * keys.length)]); | ||
} else { | ||
if (unique) { | ||
const randomKeys = new Set<string>(); | ||
|
||
while (randomKeys.size < count) randomKeys.add(keys[Math.floor(Math.random() * keys.length)]); | ||
|
||
for (const key of randomKeys) payload.data.push(key); | ||
} else { | ||
while (payload.data.length < count) payload.data.push(keys[Math.floor(Math.random() * keys.length)]); | ||
} | ||
|
||
return payload; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -517,60 +517,70 @@ export class MariaProvider<StoredValue = unknown> extends JoshProvider<StoredVal | |
} | ||
|
||
public async [Method.Random](payload: Payload.Random<StoredValue>): Promise<Payload.Random<StoredValue>> { | ||
const { count, duplicates } = payload; | ||
const { count, unique } = payload; | ||
const size = await this.handler.size(); | ||
|
||
if (size === 0) return { ...payload, data: [] }; | ||
if (size < count) { | ||
if (unique && size < count) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.InvalidCount, method: Method.Random })); | ||
|
||
return payload; | ||
} | ||
|
||
if (size === 0) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.MissingData, method: Method.Random, context: { unique, count } })); | ||
|
||
return payload; | ||
} | ||
|
||
payload.data = []; | ||
|
||
const keys = await this.handler.keys(); | ||
|
||
if (duplicates) { | ||
while (payload.data.length < count) { | ||
const key = keys[Math.floor(Math.random() * size)]; | ||
|
||
payload.data.push((await this.handler.get(key))!); | ||
} | ||
} else { | ||
if (unique) { | ||
const randomKeys = new Set<string>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does mariadb have any way to query random that would be faster than this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it does have some kind of random query but its somehow worse than ours
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test this, it's possible while not the best it's better than the current solution There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean you can. My Maria doesn't work. |
||
|
||
while (randomKeys.size < count) randomKeys.add(keys[Math.floor(Math.random() * keys.length)]); | ||
|
||
for (const key of randomKeys) payload.data.push((await this.handler.get(key))!); | ||
} else { | ||
while (payload.data.length < count) { | ||
const key = keys[Math.floor(Math.random() * size)]; | ||
|
||
payload.data.push((await this.handler.get(key))!); | ||
} | ||
} | ||
|
||
return payload; | ||
} | ||
|
||
public async [Method.RandomKey](payload: Payload.RandomKey): Promise<Payload.RandomKey> { | ||
const { count, duplicates } = payload; | ||
const { count, unique } = payload; | ||
const size = await this.handler.size(); | ||
|
||
if (size === 0) return { ...payload, data: [] }; | ||
if (size < count) { | ||
if (unique && size < count) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.InvalidCount, method: Method.RandomKey })); | ||
|
||
return payload; | ||
} | ||
|
||
if (size === 0) { | ||
payload.errors.push(this.error({ identifier: CommonIdentifiers.MissingData, method: Method.RandomKey })); | ||
|
||
return payload; | ||
} | ||
|
||
payload.data = []; | ||
|
||
const keys = await this.handler.keys(); | ||
|
||
if (duplicates) { | ||
while (payload.data.length < count) payload.data.push(keys[Math.floor(Math.random() * size)]); | ||
} else { | ||
if (unique) { | ||
const randomKeys = new Set<string>(); | ||
|
||
while (randomKeys.size < count) randomKeys.add(keys[Math.floor(Math.random() * keys.length)]); | ||
|
||
for (const key of randomKeys) payload.data.push(key); | ||
} else { | ||
while (payload.data.length < count) payload.data.push(keys[Math.floor(Math.random() * size)]); | ||
} | ||
|
||
return payload; | ||
|
Uh oh!
There was an error while loading. Please reload this page.