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

Add GEOSEARCH command to sdk #676

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
172 changes: 172 additions & 0 deletions pkg/commands/geo_search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { keygen, newHttpClient } from "../test-utils.ts";

import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import { afterAll } from "https://deno.land/[email protected]/testing/bdd.ts";
import { GeoAddCommand } from "./geo_add.ts";
import { GeoSearchCommand } from "./geo_search.ts";

const client = newHttpClient();
const { newKey, cleanup } = keygen();
afterAll(cleanup);

Deno.test("should return distance successfully in meters", async () => {
const key = newKey();
await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoSearchCommand([
key,
{ type: "FROMLONLAT", coordinate: { lon: 15, lat: 37 } },
{ type: "BYRADIUS", radius: 200, radiusType: "KM" },
"ASC",
]).exec(client);

assertEquals(res, [{ member: "Catania" }, { member: "Palermo" }]);
});

Deno.test("should return members within the specified box", async () => {
const key = newKey();

await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoSearchCommand([
key,
{ type: "FROMLONLAT", coordinate: { lon: 14, lat: 37.5 } },
{ type: "BYBOX", rect: { width: 200, height: 200 }, rectType: "KM" },
"ASC",
]).exec(client);

assertEquals(res, [{ member: "Palermo" }, { member: "Catania" }]);
});

Deno.test("should return members with coordinates, distances, and hashes", async () => {
const key = newKey();

await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoSearchCommand([
key,
{ type: "FROMLONLAT", coordinate: { lon: 14, lat: 37.5 } },
{ type: "BYRADIUS", radius: 200, radiusType: "KM" },
"ASC",
{ withHash: true, withCoord: true, withDist: true },
]).exec(client);

assertEquals(res, [
{
member: "Palermo",
coord: { long: 13.361389338970184, lat: 38.1155563954963 },
dist: 88.526,
hash: "3479099956230698",
},
{
member: "Catania",
coord: { long: 15.087267458438873, lat: 37.50266842333162 },
dist: 95.9406,
hash: "3479447370796909",
},
]);
});

Deno.test("should return members with distances, and hashes", async () => {
const key = newKey();

await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoSearchCommand([
key,
{ type: "FROMLONLAT", coordinate: { lon: 14, lat: 37.5 } },
{ type: "BYRADIUS", radius: 200, radiusType: "KM" },
"ASC",
{ withHash: true, withDist: true },
]).exec(client);

assertEquals(res, [
{
member: "Palermo",
dist: 88.526,
hash: "3479099956230698",
},
{
member: "Catania",
dist: 95.9406,
hash: "3479447370796909",
},
]);
});

Deno.test("should return members with and coordinates", async () => {
const key = newKey();

await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoSearchCommand([
key,
{ type: "FROMLONLAT", coordinate: { lon: 14, lat: 37.5 } },
{ type: "BYRADIUS", radius: 200, radiusType: "KM" },
"ASC",
{ withCoord: true },
]).exec(client);

assertEquals(res, [
{
member: "Palermo",
coord: { long: 13.361389338970184, lat: 38.1155563954963 },
},
{
member: "Catania",
coord: { long: 15.087267458438873, lat: 37.50266842333162 },
},
]);
});

Deno.test("should return members with coordinates, and hashes", async () => {
const key = newKey();

await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoSearchCommand([
key,
{ type: "FROMLONLAT", coordinate: { lon: 14, lat: 37.5 } },
{ type: "BYRADIUS", radius: 200, radiusType: "KM" },
"ASC",
{ withHash: true, withCoord: true },
]).exec(client);

assertEquals(res, [
{
member: "Palermo",
hash: "3479099956230698",
coord: { long: 13.361389338970184, lat: 38.1155563954963 },
},
{
member: "Catania",
hash: "3479447370796909",
coord: { long: 15.087267458438873, lat: 37.50266842333162 },
},
]);
});
155 changes: 155 additions & 0 deletions pkg/commands/geo_search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { Command, CommandOptions } from "./command.ts";

type RadiusOptions = "M" | "KM" | "FT" | "MI";
type CenterPoint<TMemberType> =
| {
type: "FROMMEMBER" | "frommember";
member: TMemberType;
}
| {
type: "FROMLONLAT" | "fromlonlat";
coordinate: { lon: number; lat: number };
};

type Shape =
| { type: "BYRADIUS" | "byradius"; radius: number; radiusType: RadiusOptions }
| {
type: "BYBOX" | "bybox";
rect: { width: number; height: number };
rectType: RadiusOptions;
};

type GeoSearchCommandOptions = {
count?: { limit: number; any?: boolean };
withCoord?: boolean;
withDist?: boolean;
withHash?: boolean;
};

type GeoSearchOptions<TOptions> = TOptions extends {
withHash: true;
withCoord: true;
withDist: true;
} ? { hash: string; coord: { long: number; lat: number }; dist: number }
: TOptions extends { withHash: true; withCoord: true }
? { hash: string; coord: { long: number; lat: number } }
: TOptions extends { withHash: true; withDist: true }
? { hash: string; dist: number }
: TOptions extends { withCoord: true; withDist: true }
? { coord: { long: number; lat: number }; dist: number }
: TOptions extends { withCoord: true }
? { coord: { long: number; lat: number } }
: TOptions extends { withDist: true } ? { dist: number }
: TOptions extends { withHash: true } ? { hash: string }
// deno-lint-ignore ban-types
: {};

type GeoSearchResponse<TOptions, TMemberType> = ({
member: TMemberType;
} & GeoSearchOptions<TOptions>)[];

/**
* @see https://redis.io/commands/geosearch
*/
export class GeoSearchCommand<
TMemberType = string,
TOptions extends GeoSearchCommandOptions = GeoSearchCommandOptions,
> extends Command<any[] | any[][], GeoSearchResponse<TOptions, TMemberType>> {
constructor(
[key, centerPoint, shape, order, opts]: [
key: string,
centerPoint: CenterPoint<TMemberType>,
shape: Shape,
order: "ASC" | "DESC" | "asc" | "desc",
opts?: TOptions,
],
commandOptions?: CommandOptions<
any[] | any[][],
GeoSearchResponse<TOptions, TMemberType>
>,
) {
const command: unknown[] = ["GEOSEARCH", key];

if (
centerPoint.type === "FROMMEMBER" || centerPoint.type === "frommember"
) {
command.push(centerPoint.type, centerPoint.member);
}
if (
centerPoint.type === "FROMLONLAT" || centerPoint.type === "fromlonlat"
) {
command.push(
centerPoint.type,
centerPoint.coordinate.lon,
centerPoint.coordinate.lat,
);
}

if (shape.type === "BYRADIUS" || shape.type === "byradius") {
command.push(shape.type, shape.radius, shape.radiusType);
}
if (shape.type === "BYBOX" || shape.type === "bybox") {
command.push(
shape.type,
shape.rect.width,
shape.rect.height,
shape.rectType,
);
}
command.push(order);

if (opts?.count) {
command.push(opts.count.limit, ...(opts.count.any ? ["ANY"] : []));
}

const transform = (result: string[] | string[][]) => {
if (!opts?.withCoord && !opts?.withDist && !opts?.withHash) {
return result.map((member) => {
try {
return { member: JSON.parse(member as string) };
} catch {
return { member };
}
});
} else {
return result.map((members) => {
let counter = 1;
const obj = {} as any;

try {
obj.member = JSON.parse(members[0] as string);
} catch {
obj.member = members[0];
}

if (opts.withDist) {
obj.dist = parseFloat(members[counter++]);
}
if (opts.withHash) {
obj.hash = members[counter++].toString();
}
if (opts.withCoord) {
obj.coord = {
long: parseFloat(members[counter][0]),
lat: parseFloat(members[counter][1]),
};
}
return obj;
});
}
};

super(
[
...command,
...(opts?.withCoord ? ["WITHCOORD"] : []),
...(opts?.withDist ? ["WITHDIST"] : []),
...(opts?.withHash ? ["WITHHASH"] : []),
],
{
...commandOptions,
deserialize: transform,
},
);
}
}
1 change: 1 addition & 0 deletions pkg/commands/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from "./flushall.ts";
export * from "./flushdb.ts";
export * from "./geo_add.ts";
export * from "./geo_dist.ts";
export * from "./geo_search.ts";
export * from "./get.ts";
export * from "./getbit.ts";
export * from "./getdel.ts";
Expand Down
7 changes: 7 additions & 0 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
FlushDBCommand,
GeoAddCommand,
GeoDistCommand,
GeoSearchCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -1166,6 +1167,12 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
geodist: (...args: CommandArgs<typeof GeoDistCommand>) =>
new GeoDistCommand(args, this.commandOptions).exec(this.client),

/**
* @see https://redis.io/commands/geosearch
*/
geosearch: (...args: CommandArgs<typeof GeoSearchCommand>) =>
new GeoSearchCommand(args, this.commandOptions).exec(this.client),

/**
* @see https://redis.io/commands/json.get
*/
Expand Down
7 changes: 7 additions & 0 deletions pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FlushDBCommand,
GeoAddCommand,
GeoDistCommand,
GeoSearchCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -255,6 +256,12 @@ export class Redis {
geodist: (...args: CommandArgs<typeof GeoDistCommand>) =>
new GeoDistCommand(args, this.opts).exec(this.client),

/**
* @see https://redis.io/commands/geosearch
*/
geosearch: (...args: CommandArgs<typeof GeoSearchCommand>) =>
new GeoSearchCommand(args, this.opts).exec(this.client),

/**
* @see https://redis.io/commands/json.get
*/
Expand Down
Loading