- MinMax99: A Novel Approach to Spatial Database Design for Djowda
- Introduction
- Grid Table Design
- 3. Mathematical Basis
- 4. Data Structures
- 4. Data Structures
- 5. Algorithm Implementation
- 6. Querying and Optimization
- 7. Practical Examples
- Lobby System (Spatial Communication Layer)
- 8. Comparison with Existing Spatial Databases
- 9. Conclusion and Future Enhancements
The MinMax99 spatial database system is an innovative solution developed for the Djowda platform. Its design addresses the critical challenges of managing large-scale, location-based data in a cost-effective and scalable manner. By dividing the Earth's surface into a precise grid and leveraging a unique hierarchical structure, MinMax99 enables efficient querying and data retrieval for millions of stores and stakeholders. The MinMax99 algorithm is used by the Djowda Intelligent Cell (DIC), which you can read more about here.
-
Efficiency:
- Minimize computational overhead for querying spatial data.
- Enable ultra-fast lookups for nearby locations.
-
Scalability:
- Handle billions of grid cells while maintaining lightweight data structures.
- Support future growth as the Djowda platform expands.
-
Cost-Effectiveness:
- Reduce reliance on costly centralized databases by using local computations and flat data storage.
- Optimize data storage and querying for Firebase or similar systems.
-
Innovative Integration:
- Seamlessly integrate with WebRTC for peer-to-peer communication.
- Ensure compatibility with real-time systems and offline-first applications.
This document provides an in-depth explanation of the MinMax99 system, covering its mathematical basis, data structures, and algorithms, and comparing its strengths and weaknesses to traditional spatial database systems.
The foundation of the MinMax99 system is a grid-based representation of the Earth's surface, enabling precise spatial indexing and efficient data retrieval. This section explains the grid's construction, its hierarchical structure, and the rationale behind its design.
-
Earth's Surface Dimensions:
- Approximate equatorial circumference: 40,075 km (40,075,000 m).
- Approximate pole-to-pole distance: 20,000 km (20,000,000 m).
-
Grid Cell Resolution:
- Each cell represents an area of 500 m x 500 m, chosen to balance precision with scalability.
- Total area per cell: 250,000 m².
The MinMax99 grid divides the Earth's surface into a structured layout for efficient spatial indexing.
-
Columns:
(Earth's width / Cell size = 40,075,000 m / 500 m = 82,000) -
Rows:
(Earth's height / Cell size = 20,000,000 m / 500 m = 42,000)
Note: The values 82,000 for columns and 42,000 for rows are specifically chosen to simplify and facilitate future calculations within the MinMax99 system.
The total number of cells in the grid:
82,000 columns × 42,000 rows = 3,444,000,000 cells
- Cells are assigned unique IDs, starting from top-left to bottom-right in a column-major order:
- Cell (0): Top-left corner ((x = 0, y = 0)).
- Cell (3,444,000,000): Bottom-right corner ((x = 81,999, y = 41,999)).
| Row/Column | 0 | 1 | 2 | ... | 81,999 |
|---|---|---|---|---|---|
| 0 | Cell 0 | Cell 42,000 | Cell 84,000 | ... | Cell 3,441,999 |
| 1 | Cell 1 | Cell 42,001 | Cell 84,001 | ... | Cell 3,442,000 |
| 2 | Cell 2 | Cell 42,002 | Cell 84,002 | ... | Cell 3,442,001 |
| ... | ... | ... | ... | ... | ... |
| 41,999 | Cell 41,999 | Cell 83,999 | Cell 125,999 | ... | Cell 3,444,000 |
To optimize scalability and querying, the grid is divided into hierarchical levels:
-
Level 1 (Top-Level Nodes):
- Represents groups of 1 million cells each.
- Number of nodes at this level: ⌈3,444,000,000 / 1,000,000⌉ = 3,444
-
Level 2 (Intermediate Nodes):
-
Represents groups of 1,000 cells each within each Level 1 node.
-
Number of nodes per Level 1 group: 1,000.
-
-
Level 3 (Leaf Nodes):
- Represents the individual cells at the finest granularity.
-
Scalability:
- Supports billions of cells with minimal computational overhead.
-
Efficient Querying:
- Hierarchical structure allows rapid narrowing of search space:
- Top-Level Node → Intermediate Node → Individual Cell.
- Hierarchical structure allows rapid narrowing of search space:
-
Logical Grouping:
- Provides logical separation for data organization and storage.
-
Compatibility with Firebase:
- Hierarchical paths align with Firebase’s real-time database structure, ensuring fast data access.
The MinMax99 system relies on mathematical operations to map geo-coordinates to grid cells, assign unique identifiers, and determine spatial relationships efficiently.
Geo-coordinates (latitude and longitude) are converted into grid cell coordinates using the Earth's dimensions and the defined cell resolution.
The key constants used in these calculations are:
EARTH_WIDTH_METERS: Approximate equatorial circumference (40,075,000 m).EARTH_HEIGHT_METERS: Approximate pole-to-pole distance (20,000,000 m).CELL_SIZE_METERS: Cell resolution (500 m).NUM_COLUMNS: Calculated asEARTH_WIDTH_METERS / CELL_SIZE_METERS(82,000).NUM_ROWS: Calculated asEARTH_HEIGHT_METERS / CELL_SIZE_METERS(42,000).
- Convert longitude (`λ`) to the x-coordinate in meters:
x = (λ + 180) × (EARTH_WIDTH_METERS / 360)
- Convert latitude (`φ`) to the y-coordinate in meters:
y = (EARTH_HEIGHT_METERS / 2) - ln(tan(π / 4 + φ × π / 360)) × (EARTH_HEIGHT_METERS / 2π)
- Determine the column (`x_cell`):
x_cell = floor(x / CELL_SIZE_METERS)
- Determine the row (`y_cell`):
y_cell = floor(y / CELL_SIZE_METERS)
Each grid cell is assigned a unique numeric ID based on its column-major order:
Cell ID = x_cell × NUM_ROWS + y_cell
- Latitude:
12.971598, Longitude:77.594566(approx. Bengaluru, India). - Step 1: Convert geo-coordinates to meters:
x = 26,689,774 m, y = 9,458,465 m
- Step 2: Convert meters to cell coordinates:
x_cell = floor(26,689,774 / 500) = 53,379 y_cell = floor(9,458,465 / 500) = 18,916
- Step 3: Calculate Cell ID:
Cell ID = (53,379 × 42,000) + 18,916 = 2,241,938,716
The MinMax99 system identifies neighboring cells by offsetting the row ((y_{\text{cell}})) and column ((x_{\text{cell}})) indices of the central cell.
- Top Neighbor:
Cell ID_top = (x_cell × NUM_ROWS) + (y_cell - 1)
- Bottom Neighbor:
Cell ID_bottom = (x_cell × NUM_ROWS) + (y_cell + 1)
- Left Neighbor:
Cell ID_left = ((x_cell - 1) × NUM_ROWS) + y_cell
- Right Neighbor:
Cell ID_right = ((x_cell + 1) × NUM_ROWS) + y_cell
- Central Cell:
Cell ID =2,241,938,716(from previous calculation).
Coordinates:
x_cell = 53,379, y_cell = 18,916
- Neighboring Cells:
-
Top Neighbor:
Cell ID_top = (53,379 × 42,000) + (18,916 - 1) = 2,241,938,715 -
Bottom Neighbor:
Cell ID_bottom = (53,379 × 42,000) + (18,916 + 1) = 2,241,938,717 -
Left Neighbor:
Cell ID_left = ((53,379 - 1) × 42,000) + 18,916 = 2,241,896,716 -
Right Neighbor:
Cell ID_right = ((53,379 + 1) × 42,000) + 18,916 = 2,241,980,716
The distance between two cells is approximated using the differences in their row and column indices:
Distance (in meters) = sqrt((Δx × CELL_SIZE_METERS)² + (Δy × CELL_SIZE_METERS)²)
Where:
- ( \Delta x = |x_{\text{cell1}} - x_{\text{cell2}}| )
- ( \Delta y = |y_{\text{cell1}} - y_{\text{cell2}}| )
-
Cell 1: x_cell1 = 53,379, y_cell1 = 18,916
-
Cell 2: x_cell2 = 53,380, y_cell2 = 18,917
Step 1: Calculate differences in indices:
Δx = |53,379 - 53,380| = 1 Δy = |18,916 - 18,917| = 1
Step 2: Apply the formula:
Distance = sqrt((1 × 500)² + (1 × 500)²) = sqrt(250,000 + 250,000) = sqrt(500,000) ≈ 707.11 meters
The MinMax99 spatial database uses a combination of hierarchical and flat data structures to achieve efficient data storage and retrieval. This section explains the design and advantages of these structures.
The flat structure organizes data directly by cell numbers, minimizing lookup complexity and ensuring fast access.
Each cell number serves as a unique key, with associated data stored directly under it. For example:
store_stats/ <cell_number>/ <store_id>: { "isOpen": true, "updatedAt": "2024-10-26T14:00:00Z" } <store_id>: { "isOpen": false, "updatedAt": "2024-10-26T13:00:00Z" }
The MinMax99 spatial database combines hierarchical and flat data structures for efficient storage and retrieval. This section outlines the corrected structure based on the three-level hierarchy and flat storage strategy.
The flat structure organizes data directly by cell numbers, enabling fast lookups for frequently accessed items.
Each cell number serves as a unique key, with associated data stored directly under it. For example:
store_stats/ <cell_number>/ <store_id>: { "isOpen": true, "updatedAt": "2024-10-26T14:00:00Z" } <store_id>: { "isOpen": false, "updatedAt": "2024-10-26T13:00:00Z" }
-
Fast Access:
- Direct lookup of data by cell number avoids multi-level traversal.
-
Scalability:
- Handles millions of store entries efficiently.
-
Lightweight:
- Optimized for operations requiring real-time responses.
The hierarchical structure logically partitions the dataset into progressively smaller groups, ensuring scalability.
The hierarchy divides the grid as follows:
-
Level 1 (Top-Level Nodes):
- Represents groups of 1,000,000 cells, identified by ( \lfloor \text{cell_number} / 1,000,000 \rfloor ).
- Example: For
cell_number = 2,241,938,716, Level 1 =2241.
-
Level 2 (Intermediate Nodes):
- Represents groups of 1,000 cells within each top-level group, identified by ( \lfloor (\text{cell_number} / 1,000) \mod 1,000 \rfloor ).
- Example: For
cell_number = 2,241,938,716, Level 2 =938.
-
Level 3 (Leaf Nodes):
- Represents the individual cells, identified by ( \text{cell_number} \mod 1,000 ).
- Example: For
cell_number = 2,241,938,716, Level 3 =716.
For cell_number = 2,241,938,716, the hierarchical path is:
-
Fast Access:
- Direct lookup of data by cell number avoids multi-level traversal.
-
Scalability:
- Handles millions of store entries efficiently.
-
Lightweight:
- Optimized for operations requiring real-time responses.
store_data/ 2241/ 938/ 716/ <store_id>: { "name": "Store A", "inventory": { ... } }
-
Efficient Querying:
- Reduces search space by narrowing down from Level 1 to Level 3.
-
Logical Partitioning:
- Simplifies data management and avoids overloading flat structures.
-
Firebase Compatibility:
- Well-suited for Firebase’s hierarchical design, enabling seamless data retrieval.
MinMax99 strategically uses both structures:
- Flat Data:
- Best for high-frequency operations like store availability checks.
- Hierarchical Nodes:
- Ideal for scalable storage and operations involving multiple data points.
This section describes the core algorithms used in the MinMax99 system, including mapping geo-coordinates to cell numbers, constructing hierarchical database paths, and identifying neighboring cells.
This algorithm converts geo-coordinates (latitude and longitude) into a unique cell number based on the grid.
function geoToCellNumber(latitude, longitude):
EARTH_WIDTH_METERS = 40,075,000 // Earth's circumference in meters
EARTH_HEIGHT_METERS = 20,000,000 // Earth's height in meters
CELL_SIZE_METERS = 500 // Cell size in meters
NUM_ROWS = 42000 // Number of rows in the grid
// Convert longitude to x in meters
xMeters = (longitude + 180) × (EARTH_WIDTH_METERS / 360)
// Convert latitude to y in meters
yMeters = (EARTH_HEIGHT_METERS / 2) - ln(tan(π / 4 + latitude × π / 360)) × (EARTH_HEIGHT_METERS / (2π))
// Calculate cell coordinates
xCell = floor(xMeters / CELL_SIZE_METERS)
yCell = floor(yMeters / CELL_SIZE_METERS)
// Compute cell number (column-major order)
return (xCell × NUM_ROWS) + yCellExample Latitude: 12.971598, Longitude: 77.594566 (Bengaluru, India). Step 1: Convert geo-coordinates to meters:
x = 26,689,774 m, y = 9,458,465 m
Step 2: Convert meters to cell coordinates:
xCell = floor(26,689,774 / CELL_SIZE_METERS) = 53,379
yCell = floor(9,458,465 / CELL_SIZE_METERS) = 18,916
Step 3: Calculate Cell ID:
Cell ID = (53,379 × NUM_ROWS) + 18,916 = 2,241,938,716
5.2 Constructing Firebase Paths This algorithm generates a hierarchical path for a given cell number.
Pseudocode
function constructDbPath(cellNumber, nodeName):
LEVEL1 = floor(cellNumber / 1,000,000) // Top-level node
LEVEL2 = floor((cellNumber / 1,000) % 1,000) // Intermediate node
LEVEL3 = cellNumber % 1,000 // Leaf node
return LEVEL1 + "/" + LEVEL2 + "/" + LEVEL3 + "/" + nodeNameExample Cell Number: 2,241,938,716, Node Name: "store_data".
Firebase Path: 2241/938/716/store_data
5.3 Finding Neighboring Cells This algorithm identifies neighboring cells based on a given precision.
Pseudocode
function getNeighboringCells(cellNumber, precision):
CELL_SIZE_METERS = 500 // Grid cell size in meters
distance = floor(precision / CELL_SIZE_METERS) // Convert precision to cell units
// Convert cell number to x, y coordinates
xCell = floor(cellNumber / NUM_ROWS)
yCell = cellNumber % NUM_ROWS
neighbors = []
// Iterate over neighboring cell range
for dx in range(-distance, distance + 1):
for dy in range(-distance, distance + 1):
neighborX = xCell + dx
neighborY = yCell + dy
// Skip out-of-bounds neighbors
if neighborX < 0 or neighborX >= NUM_COLUMNS or neighborY < 0 or neighborY >= NUM_ROWS:
continue
// Calculate neighbor cell number
neighborCell = (neighborX × NUM_ROWS) + neighborY
neighbors.append(neighborCell)
return neighbors
Example Cell Number: 2,241,938,716 Precision (search extent): 1000m (results in a 5x5 cell area) Neighboring Cells:
[2,241,937,716, 2,241,939,716, 2,241,938,715, 2,241,938,717, ...]
This section explains algorithms for retrieving cells based on "layers." In this context, a layer signifies a concentric square region around a central cell, with each layer stepping outwards at 5-kilometer intervals. This method is distinct from finding all cells within a precise radial distance (as in section 5.3) and is useful for queries where stepped, square-shaped regions are more appropriate.
This algorithm finds all cell IDs within a specified number of layers from an initial cell.
function calculateCellsInLayers(initialCellID, layerNumber):
// Assumes constants: NUM_ROWS, NUM_COLUMNS, CELL_SIZE_METERS
// A "layer" here represents a 5000-meter step.
x_cell = floor(initialCellID / NUM_ROWS)
y_cell = initialCellID % NUM_ROWS
// Convert layerNumber to a range in cell units
// Each layer adds 5000m. Cell size is CELL_SIZE_METERS.
// So, number of cell units for the step is 5000 / CELL_SIZE_METERS
layer_step_in_cells = 5000 / CELL_SIZE_METERS
max_offset_in_cells = layerNumber * layer_step_in_cells
neighboringCellIDs = new List()
for dx_step from -max_offset_in_cells to max_offset_in_cells step layer_step_in_cells:
for dy_step from -max_offset_in_cells to max_offset_in_cells step layer_step_in_cells:
// Optional: Skip the initial cell itself if dx_step and dy_step are both 0
// if dx_step == 0 AND dy_step == 0:
// continue
newX = x_cell + dx_step
newY = y_cell + dy_step
if newX >= 0 AND newX < NUM_COLUMNS AND newY >= 0 AND newY < NUM_ROWS:
neighboringCellID = newX * NUM_ROWS + newY
neighboringCellIDs.add(neighboringCellID)
return neighboringCellIDsThis algorithm finds cell IDs that are specifically on the perimeter of a given layer number.
function calculatePerimeterCellsOfLayer(initialCellID, layerNumber):
// Assumes constants: NUM_ROWS, NUM_COLUMNS, CELL_SIZE_METERS
// A "layer" here represents a 5000-meter step.
x_cell = floor(initialCellID / NUM_ROWS)
y_cell = initialCellID % NUM_ROWS
layer_step_in_cells = 5000 / CELL_SIZE_METERS
max_offset_in_cells = layerNumber * layer_step_in_cells
layerCellIDs = new List()
// Iterate to find cells at the perimeter of the square defined by max_offset_in_cells
for dx_step from -max_offset_in_cells to max_offset_in_cells step layer_step_in_cells:
for dy_step from -max_offset_in_cells to max_offset_in_cells step layer_step_in_cells:
// Include only if it's on the border of the square layer
if abs(dx_step) == max_offset_in_cells OR abs(dy_step) == max_offset_in_cells:
newX = x_cell + dx_step
newY = y_cell + dy_step
if newX >= 0 AND newX < NUM_COLUMNS AND newY >= 0 AND newY < NUM_ROWS:
layerCellID = newX * NUM_ROWS + newY
layerCellIDs.add(layerCellID)
return layerCellIDsThis algorithm determines the relative position of a target cell with respect to a reference cell (e.g., North, South-East, Same Cell). This is useful for understanding spatial relationships between two points on the grid.
function determineRelativePosition(referenceCellID, targetCellID):
// Assumes constants: NUM_ROWS
// Convert referenceCellID to coordinates
x_ref = floor(referenceCellID / NUM_ROWS)
y_ref = referenceCellID % NUM_ROWS
// Convert targetCellID to coordinates
x_target = floor(targetCellID / NUM_ROWS)
y_target = targetCellID % NUM_ROWS
// Determine direction
// Note: Assuming (0,0) is top-left.
// Increasing y means moving South. Increasing x means moving East.
if x_ref == x_target AND y_ref == y_target:
return "Same Cell"
else if x_ref == x_target:
if y_target > y_ref:
return "South" // Or "Bottom" if preferred for pure cardinal
else:
return "North" // Or "Top"
else if y_ref == y_target:
if x_target > x_ref:
return "East" // Or "Right"
else:
return "West" // Or "Left"
else:
verticalDirection = ""
if y_target > y_ref:
verticalDirection = "South"
else:
verticalDirection = "North"
horizontalDirection = ""
if x_target > x_ref:
horizontalDirection = "East"
else:
horizontalDirection = "West"
return verticalDirection + "-" + horizontalDirection // e.g., "North-East"The function returns a string indicating the relative position:
- "Same Cell": If the reference and target cells are identical.
- Cardinal Directions: "North", "South", "East", "West" if the target cell is directly along one of these axes relative to the reference cell.
- Diagonal Directions: Combined strings like "North-East", "South-West", etc., for cells that are diagonally positioned.
Efficiency Direct Lookups: Single-cell queries are instantaneous due to numeric ID indexing. Regional Queries: Efficiently identify and query neighboring cells using the hierarchical structure. Scalability: Algorithms handle billions of cells with minimal computational overhead
The MinMax99 system ensures efficient spatial data retrieval through well-structured queries and strategic optimizations. This section explains how queries are performed and optimized for scalability and performance.
- Directly retrieves data for a specific cell number.
- Example:
- Firebase Path:
store_stats/<cell_number>/<store_id> - Query retrieves:
{ "isOpen": true, "updatedAt": "2024-10-26T14:00:00Z" }
- Firebase Path:
- Retrieve data from multiple cells in a region:
- Use
geoToCellNumberto determine the central cell. - Use
getNeighboringCellsto calculate neighboring cells based on precision. - Perform parallel queries for all relevant cells.
- Use
- Precision determines the range of neighboring cells to include:
- 500m x 500m precision (single cell): Query only the center cell.
- 1.5km x 1.5km precision (3x3 cells): Include the center cell and its direct neighbors.
- Larger regions: Expand the range to include additional layers of neighbors.
- Central Cell:
2,241,938,716 - Neighbors:
[2,241,937,716, 2,241,939,716, 2,241,938,715, 2,241,938,717, ...]
- Query all these cells to retrieve data for the region.
- Use hierarchical paths to narrow the search space:
- Level 1: Identify the relevant top-level node.
- Level 2: Refine to the intermediate node.
- Level 3: Access individual cell data.
- Leverage Firebase indexing for faster lookups:
- Index paths by
cell_numberorupdatedAtfields for real-time queries.
- Cache frequently accessed data (e.g., store statuses) locally to reduce redundant queries.
- Perform asynchronous queries for regional searches to improve response times:
- Example: Query 10 neighboring cells simultaneously using multi-threading.
- Use the flat structure for quick lookups like store availability checks.
- Employ hierarchical nodes for larger operations, such as fetching data for multiple regions.
- Update only relevant cells and users based on their spatial proximity to the change.
- Single-Cell Query Time: Near-instantaneous due to direct lookup by cell number.
- Regional Query Efficiency: Scales logarithmically with the size of the queried region.
- Resource Efficiency: Combines WebRTC for real-time updates and Firebase for infrequent but necessary database interactions.
This section provides step-by-step examples to demonstrate the functionality and efficiency of the MinMax99 system, from mapping geo-coordinates to querying data.
- A user’s location: Latitude =
12.971598, Longitude =77.594566.
- Convert Geo-Coordinates to Meters:
x = (77.594566 + 180) × (40,075,000 / 360) = 26,689,774 m y = (20,000,000 / 2) - ln(tan(π / 4 + (12.971598 × π / 360))) × (20,000,000 / (2π)) = 9,458,465 m
- Convert Meters to Cell Coordinates:
x_cell = floor(26,689,774 / 500) = 53,379 y_cell = floor(9,458,465 / 500) = 18,916
- Calculate the Cell Number:
Cell ID = (53,379 × 42,000) + 18,916 = 2,241,938,716
- Querying for the store status in cell
2,241,938,716.
-
Firebase Path:
store_stats/2,241,938,716/<store_id> -
Retrieved Data:
{ "isOpen": true, "updatedAt": "2024-10-26T14:00:00Z" }
- The user wants to find all nearby stores within a 1.5km x 1.5km precision (3x3 cells) region.
- Identify Neighboring Cells:
Center Cell ID = 2,241,938,716 Precision = 1.5km x 1.5km (3x3 cells) Neighbors = [2,241,937,715, 2,241,937,716, 2,241,937,717, 2,241,938,715, ...]
- Query Firebase:
Perform parallel queries for each cell in the list:
store_stats/<cell_number>/<store_id>
- Consolidate Results:
Combine responses into a unified list of stores, filtering based on criteria (e.g.,isOpen = true).
- Storing inventory data for a store in cell
2,241,938,716.
- Cell Number Hierarchy:
Level 1 = floor(2,241,938,716 / 1,000,000) = 2241 Level 2 = floor((2,241,938,716 / 1,000) % 1,000) = 938 Level 3 = 2,241,938,716 % 1,000 = 716
- Firebase Path:
store_data/2241/938/716/<store_id>
- Store Inventory:
{ "name": "Store A", "inventory": { "apples": { "price": 2.5, "quantity": 50 }, "bananas": { "price": 1.2, "quantity": 100 } }, "isOpen": true }
- Store 1 in
2,241,938,716. - Store 2 in
2,241,939,717.
- Extract Coordinates:
x1 = floor(2,241,938,716 / 42,000) = 53,379 y1 = 2,241,938,716 % 42,000 = 18,916
x2 = floor(2,241,939,717 / 42,000) = 53,379 y2 = 2,241,939,717 % 42,000 = 18,917
- Calculate Differences:
Δx = |53,379 - 53,379| = 0 Δy = |18,916 - 18,917| = 1
- Compute Distance:
Distance = sqrt((Δx × 500)² + (Δy × 500)²) = sqrt(0² + 500²) = 500 meters
The Lobby System is a key architectural feature of MinMax99 that enables efficient, scalable, and localized communication across the map grid.
- Each MinMax99 Cell: Represents a 500m × 500m square (250,000 m²).
- Lobby Block: A logical group of 20 × 20 cells, covering a 10km × 10km area.
- Client View: Each client (e.g., user or store app) subscribes to a 3 × 3 grid of lobbies (30km × 30km) centered on its current position.
- Note: The Lobby Grid uses Column-Major Order, aligned with the MinMax99 cell grid structure, to ensure consistent spatial indexing and traversal.
- Scopes MQTT communication by limiting message flow to local zones.
- Reduces network noise and improves real-time responsiveness.
- Facilitates discovery of nearby entities (stores, users, deliveries, etc.) within relevant proximity.
- Each lobby acts as a communication topic, e.g.,
lobby/123_456.
int lobbyX = cellX / 20;
int lobbyY = cellY / 20;
String topic = "lobby/" + lobbyX + "_" + lobbyY;Clients automatically subscribe to their current lobby and its 8 neighbors:
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int lx = lobbyX + dx;
int ly = lobbyY + dy;
subscribe("lobby/" + lx + "_" + ly);
}
}This setup keeps each client aware of activity in its local zone while maintaining scalability across a global grid.
The MinMax99 spatial database design offers a unique approach to managing large-scale location-based data. This section compares MinMax99 to traditional spatial databases, highlighting its strengths and weaknesses.
Common spatial database approaches include:
-
Quadtrees:
- A tree structure that recursively subdivides space into quadrants.
- Ideal for managing sparse data but can be inefficient for uniform datasets.
-
R-Trees:
- Organizes data into rectangles for efficient range queries.
- Commonly used in GIS systems for spatial indexing.
-
Geohashing:
- Encodes geo-coordinates into hash strings at varying precision levels.
- Efficient for proximity queries but less intuitive for hierarchical navigation.
-
Z-Order Curves:
- Maps multi-dimensional data into a one-dimensional space.
- Useful for range queries but computationally intensive.
-
Efficient Grid-Based Design:
- Simplifies spatial indexing by dividing the Earth's surface into uniform cells.
- Fixed resolution (500m x 500m) eliminates the complexity of dynamic subdivisions.
-
Hierarchical Structure:
- Combines flat data storage with logical groupings for scalability.
- Allows rapid narrowing of search space in large datasets.
-
Lightweight and Cost-Effective:
- Designed for platforms like Firebase, minimizing server costs.
- No need for specialized spatial indexing libraries or extensions.
-
Real-Time Integration:
- Built for seamless use with WebRTC for direct app-to-app communication.
-
Fixed Resolution:
- Lacks dynamic zoom-level adjustments for fine-grained precision.
-
Limited Geometric Queries:
- Not optimized for complex spatial operations like polygon intersections or nearest-neighbor in irregular shapes.
-
Dependence on Predefined Structures:
- Relies on column-major ordering, which may not align with all database use cases.
| Feature | MinMax99 | Quadtrees | R-Trees | Geohashing | Z-Order Curves |
|---|---|---|---|---|---|
| Precision | Fixed (500m x 500m) | Adjustable | Adjustable | Adjustable | Adjustable |
| Scalability | High, hierarchical grouping | Medium | High | High | High |
| Efficiency for Lookups | Very high (direct cell lookups) | Medium | High | High | Medium |
| Geometric Queries | Limited | High | Very High | Medium | Medium |
| Integration Cost | Low (Firebase/WebRTC-ready) | Medium | High | Low | Medium |
MinMax99 excels in scenarios requiring cost-effective, scalable, and lightweight spatial indexing. While it may lack advanced geometric operations, its efficiency for real-time querying and compatibility with modern app frameworks make it a powerful choice for location-based services.
The MinMax99 spatial database system offers an innovative and efficient solution for managing large-scale, location-based data. By combining a fixed-resolution grid with a hierarchical data structure, it achieves the following key goals:
- Scalability: Handles billions of entries with logical data partitioning.
- Efficiency: Provides rapid lookups and optimized queries using column-major ordering.
- Cost-Effectiveness: Reduces reliance on costly spatial database extensions while maintaining compatibility with Firebase and WebRTC.
While MinMax99 is well-suited for the current needs of the Djowda platform, its flexibility ensures that it can evolve to address future challenges.
To further optimize and enhance the MinMax99 system, the following advancements are planned:
- Introduce a finer-resolution grid for each 500m x 500m cell by dividing it into smaller squares (e.g., (500 \times 500) grid with 1m² precision).
- Each sub-cell will have its unique ID relative to the parent 500m x 500m cell.
Proposed Structure for 1m² Precision:
- Within a 500m x 500m cell, create a (500 \times 500) table:
- Sub-cell IDs range from
0,0to499,499.
- Sub-cell IDs range from
- Use a hierarchical path to reference both the parent cell and the sub-cell: store_data/<parent_cell>/<sub_cell_x>/<sub_cell_y>/<store_id>
- Allow dynamic zoom levels to switch between the 500m² grid and the finer 1m² sub-grid.
- Implement queries that adapt precision based on application requirements (e.g., zooming in for detailed mapping or proximity detection).
- Integrate AI models to predict high-traffic areas and cache frequently accessed cells.
- Enable real-time optimization of regional queries based on usage patterns.
- Extend the system to support advanced geometric operations, such as polygon-based searches or pathfinding within a grid.
- Implement mechanisms to distribute grid data across multiple nodes or regions for enhanced scalability and reliability.
| Version | Date | Description |
|---|---|---|
| v1.0 | 2024-11-19 | Initial version with 500m² precision and hierarchical structure. |
| v1.1 | Planned | Finer-resolution grid at 1m² precision with sub-cell ID system. |
| v2.0 | Planned | Support for AI-driven optimizations and advanced geometric queries. |
MinMax99 represents a significant step toward achieving scalable and efficient spatial data management for the Djowda platform. Its current implementation addresses key challenges in location-based systems, while future enhancements promise to extend its capabilities, ensuring it remains adaptable to emerging needs.