From 5807b75580c678d69af772a793d3a3828dfe4520 Mon Sep 17 00:00:00 2001 From: FleetAdmiralJakob Date: Tue, 26 Mar 2024 02:43:52 +0100 Subject: [PATCH] fix: Fixed the convex function and table so that they don't take too much space to exceed our spending limits. --- packages/city-data/convex/getCity.ts | 12 +++++++----- packages/city-data/convex/schema.ts | 3 +-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/city-data/convex/getCity.ts b/packages/city-data/convex/getCity.ts index f6386e9a..442b933e 100644 --- a/packages/city-data/convex/getCity.ts +++ b/packages/city-data/convex/getCity.ts @@ -92,19 +92,21 @@ export const findCityById = query({ }, }); +// This is not the fastest way of doing this stuff, +// FIRST: We should use an index for both of the coord fields. +// I did this first, but because of the big size these indexes took, our convex db storage limit exceeded. +// In the future, if we have more storage, we should use indexes for coord fields. +// SECOND: Instead of a bounding box search, we could use something like geohashes to find all nearby cities more efficiently and quickly. export const findNearestCityByCoord = mutation({ args: { coord: v.object({ lat: v.number(), lng: v.number() }) }, handler: async (ctx, args) => { // This is a simple query that finds cities within a 0.1-degree square; this optimization is to avoid scanning the whole table with havy calculations. const cities = await ctx.db .query("search") - .withIndex("by_lon", (q) => - q - .gt("coord.lon", args.coord.lng - 0.1) - .lt("coord.lon", args.coord.lng + 0.1), - ) .filter((q) => q.and( + q.gt(q.field("coord.lon"), args.coord.lng - 0.1), + q.lt(q.field("coord.lon"), args.coord.lng + 0.1), q.gt(q.field("coord.lat"), args.coord.lat - 0.1), q.lt(q.field("coord.lat"), args.coord.lat + 0.1), ), diff --git a/packages/city-data/convex/schema.ts b/packages/city-data/convex/schema.ts index 6adc06d8..137755ae 100644 --- a/packages/city-data/convex/schema.ts +++ b/packages/city-data/convex/schema.ts @@ -13,6 +13,5 @@ export default defineSchema({ .searchIndex("search_body", { searchField: "name", }) - .index("by_city_id", ["id"]) - .index("by_lon", ["coord.lon"]), + .index("by_city_id", ["id"]), });