wedb_embed 0.1.13

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
pub use super::r#const::*;
use super::opt::{
  DistanceUnit, GeoHashArea, GeoHashBits, GeoHashNeighbors, GeoHashRadius, GeoHashRange, GeoPoint,
  GeoShape, GeoShapeType,
};
use crate::error::{Error, Result};

/// Global longitude range for WGS84 indexing [-180, 180].
/// 经度全局范围(WGS84 索引范围)
pub const GEO_LON_RANGE: GeoHashRange = GeoHashRange {
  min: GEO_LON_MIN,
  max: GEO_LON_MAX,
};

/// Global latitude range for WGS84 indexing [-85.05112878, 85.05112878].
/// 纬度全局范围(WGS84 索引范围)
pub const GEO_LAT_RANGE: GeoHashRange = GeoHashRange {
  min: GEO_LAT_MIN,
  max: GEO_LAT_MAX,
};

/// Standard Geohash longitude range.
/// 标准 Geohash 经度范围
pub const GEO_LON_RANGE_STANDARD: GeoHashRange = GeoHashRange {
  min: -180.0,
  max: 180.0,
};

/// Standard Geohash latitude range.
/// 标准 Geohash 纬度范围
pub const GEO_LAT_RANGE_STANDARD: GeoHashRange = GeoHashRange {
  min: -90.0,
  max: 90.0,
};

/// Validates whether longitude and latitude coordinates fall within valid bounds aligned with Kvrocks ValidateLongLat.
/// 校验经纬度坐标是否在合法范围内(对标 Kvrocks ValidateLongLat)
#[inline]
pub fn validate_long_lat(lon: f64, lat: f64) -> Result<()> {
  if lon.is_nan()
    || lat.is_nan()
    || lon.is_infinite()
    || lat.is_infinite()
    || !(GEO_LON_MIN..=GEO_LON_MAX).contains(&lon)
    || !(GEO_LAT_MIN..=GEO_LAT_MAX).contains(&lat)
  {
    let mut b_lon = zmij::Buffer::new();
    let mut b_lat = zmij::Buffer::new();
    let s_lon = b_lon.format(lon);
    let s_lat = b_lat.format(lat);
    let mut msg = String::with_capacity(36 + s_lon.len() + 1 + s_lat.len());
    msg.push_str("ERR invalid longitude,latitude pair ");
    msg.push_str(s_lon);
    msg.push(',');
    msg.push_str(s_lat);
    return Err(Error::invalid_data(msg));
  }
  Ok(())
}

/// Interleaves 32-bit longitude and latitude bits into a 64-bit integer aligned with Kvrocks Interleave64.
/// 64 位整数交替编织(经纬度比特交错,对标 Kvrocks Interleave64)
#[inline(always)]
pub const fn interleave64(xlo: u32, ylo: u32) -> u64 {
  const B: [u64; 5] = [
    0x5555555555555555,
    0x3333333333333333,
    0x0F0F0F0F0F0F0F0F,
    0x00FF00FF00FF00FF,
    0x0000FFFF0000FFFF,
  ];
  const S: [u32; 5] = [1, 2, 4, 8, 16];

  let mut x = xlo as u64;
  let mut y = ylo as u64;

  x = (x | (x << S[4])) & B[4];
  y = (y | (y << S[4])) & B[4];

  x = (x | (x << S[3])) & B[3];
  y = (y | (y << S[3])) & B[3];

  x = (x | (x << S[2])) & B[2];
  y = (y | (y << S[2])) & B[2];

  x = (x | (x << S[1])) & B[1];
  y = (y | (y << S[1])) & B[1];

  x = (x | (x << S[0])) & B[0];
  y = (y | (y << S[0])) & B[0];

  x | (y << 1)
}

/// Deinterleaves a 64-bit integer into separate longitude and latitude bits aligned with Kvrocks Deinterleave64.
/// 64 位整数反交替解织(分离纬度和经度比特,对标 Kvrocks Deinterleave64)
#[inline(always)]
pub const fn deinterleave64(interleaved: u64) -> (u32, u32) {
  const B: [u64; 6] = [
    0x5555555555555555,
    0x3333333333333333,
    0x0F0F0F0F0F0F0F0F,
    0x00FF00FF00FF00FF,
    0x0000FFFF0000FFFF,
    0x00000000FFFFFFFF,
  ];
  const S: [u32; 6] = [0, 1, 2, 4, 8, 16];

  let mut x = interleaved;
  let mut y = interleaved >> 1;

  x = (x | (x >> S[0])) & B[0];
  y = (y | (y >> S[0])) & B[0];

  x = (x | (x >> S[1])) & B[1];
  y = (y | (y >> S[1])) & B[1];

  x = (x | (x >> S[2])) & B[2];
  y = (y | (y >> S[2])) & B[2];

  x = (x | (x >> S[3])) & B[3];
  y = (y | (y >> S[3])) & B[3];

  x = (x | (x >> S[4])) & B[4];
  y = (y | (y >> S[4])) & B[4];

  x = (x | (x >> S[5])) & B[5];
  y = (y | (y >> S[5])) & B[5];

  (x as u32, y as u32)
}

/// Encodes data into binary format.
/// Geohash 范围编码(对标 Kvrocks GeohashEncode)
#[inline]
pub fn geohash_encode(
  long_range: &GeoHashRange,
  lat_range: &GeoHashRange,
  longitude: f64,
  latitude: f64,
  step: u8,
) -> Option<GeoHashBits> {
  if step == 0 || step > 32 || long_range.is_zero() || lat_range.is_zero() {
    return None;
  }
  if longitude > GEO_LON_MAX
    || longitude < GEO_LON_MIN
    || latitude > GEO_LAT_MAX
    || latitude < GEO_LAT_MIN
  {
    return None;
  }
  if latitude < lat_range.min
    || latitude > lat_range.max
    || longitude < long_range.min
    || longitude > long_range.max
  {
    return None;
  }

  let lat_offset = (latitude - lat_range.min) / (lat_range.max - lat_range.min);
  let long_offset = (longitude - long_range.min) / (long_range.max - long_range.min);

  let max_val = (1u64 << step) as f64;
  let lat_val = (lat_offset * max_val) as u32;
  let long_val = (long_offset * max_val) as u32;

  let bits = interleave64(lat_val, long_val);
  Some(GeoHashBits { bits, step })
}

/// Decodes data from binary format.
/// Geohash 范围解码(对标 Kvrocks GeohashDecode)
#[inline]
pub fn geohash_decode(
  long_range: &GeoHashRange,
  lat_range: &GeoHashRange,
  hash: GeoHashBits,
) -> GeoHashArea {
  if hash.is_zero() || lat_range.is_zero() || long_range.is_zero() {
    return GeoHashArea::default();
  }

  let (ilato, ilono) = deinterleave64(hash.bits);
  let lat_scale = lat_range.max - lat_range.min;
  let long_scale = long_range.max - long_range.min;
  let max_val = (1u64 << hash.step) as f64;

  let lat_min = lat_range.min + (ilato as f64 / max_val) * lat_scale;
  let lat_max = lat_range.min + ((ilato + 1) as f64 / max_val) * lat_scale;
  let lon_min = long_range.min + (ilono as f64 / max_val) * long_scale;
  let lon_max = long_range.min + ((ilono + 1) as f64 / max_val) * long_scale;

  GeoHashArea {
    hash,
    longitude: GeoHashRange {
      min: lon_min,
      max: lon_max,
    },
    latitude: GeoHashRange {
      min: lat_min,
      max: lat_max,
    },
  }
}

/// Decodes data from binary format.
/// 将解码区域转换为中心经纬度坐标(对标 Kvrocks GeohashDecodeAreaToLongLat)
#[inline]
pub fn geohash_decode_area_to_long_lat(area: &GeoHashArea) -> (f64, f64) {
  let lon = ((area.longitude.min + area.longitude.max) * 0.5).clamp(GEO_LON_MIN, GEO_LON_MAX);
  let lat = ((area.latitude.min + area.latitude.max) * 0.5).clamp(GEO_LAT_MIN, GEO_LAT_MAX);
  (lon, lat)
}

/// Aligns a Geohash to 52 bits aligned with Kvrocks GeoHashHelper::Align52Bits.
/// 52 位对齐(对标 Kvrocks GeoHashHelper::Align52Bits)
#[inline(always)]
pub const fn align_52bits(hash: GeoHashBits) -> u64 {
  let shift = 52 - hash.step * 2;
  hash.bits << shift
}

/// Encodes data into binary format.
/// 将经纬度编码为 52 位的 Geohash 整数值(对标 Kvrocks Geo::Add 编码)
#[inline]
pub fn encode_geohash(lon: f64, lat: f64) -> u64 {
  geohash_encode(&GEO_LON_RANGE, &GEO_LAT_RANGE, lon, lat, GEO_STEP_MAX)
    .map(align_52bits)
    .unwrap_or(0)
}

/// Decodes data from binary format.
/// 将 52 位 Geohash 解码回 (lon, lat)(对标 Kvrocks Geo::decodeGeoHash)
#[inline]
pub fn decode_geohash(hash: u64) -> (f64, f64) {
  let area = geohash_decode(
    &GEO_LON_RANGE,
    &GEO_LAT_RANGE,
    GeoHashBits {
      bits: hash,
      step: GEO_STEP_MAX,
    },
  );
  geohash_decode_area_to_long_lat(&area)
}

/// Encodes data into binary format.
/// 将经纬度编码为 11 字节 Base32 字符数组(零堆分配,与 Redis/Kvrocks 规范 100% 兼容)
#[inline]
pub fn encode_geohash_bytes(lon: f64, lat: f64) -> [u8; 11] {
  let encoded = geohash_encode(
    &GEO_LON_RANGE_STANDARD,
    &GEO_LAT_RANGE_STANDARD,
    lon,
    lat,
    26,
  )
  .unwrap_or_default();

  let mut buf = [0u8; 11];
  for (i, byte) in buf.iter_mut().enumerate() {
    let idx = if i == 10 {
      0
    } else {
      ((encoded.bits >> (52 - (i + 1) * 5)) & 0x1f) as usize
    };
    *byte = BASE32_ALPHABET[idx];
  }
  buf
}

/// Encodes data into binary format.
/// 将经纬度编码为 11 位 Base32 字符串(与 Redis/Kvrocks 规范 100% 兼容)
#[inline]
pub fn encode_geohash_string(lon: f64, lat: f64) -> String {
  let buf = encode_geohash_bytes(lon, lat);
  str::from_utf8(&buf).unwrap_or("").to_string()
}

/// Encodes a 52-bit Geohash into an 11-character Base32 string aligned with Kvrocks Geo::EncodeGeoHash.
/// 将 52 位 Geohash 转换为 11 位 Base32 字符串(对标 Kvrocks Geo::EncodeGeoHash)
#[inline]
pub fn geohash_to_base32(hash: u64) -> String {
  let (lon, lat) = decode_geohash(hash);
  encode_geohash_string(lon, lat)
}

#[inline]
pub fn coords_to_base32(lon: f64, lat: f64) -> Result<String> {
  validate_long_lat(lon, lat)?;
  Ok(encode_geohash_string(lon, lat))
}

/// Decodes data from binary format.
/// 将 Base32 Geohash 字符串解码为坐标 (lon, lat)(O(1) 查找表加速)
pub fn base32_to_coords(s: &str) -> Result<(f64, f64)> {
  let s_bytes = s.as_bytes();
  if s_bytes.is_empty() || s_bytes.len() > 11 {
    return Err(Error::invalid_data("ERR invalid geohash string length"));
  }
  let mut bits: u64 = 0;
  for (i, &ch) in s_bytes.iter().enumerate() {
    let val = BASE32_DECODE_TABLE[ch as usize];
    if val == 0xFF {
      return Err(Error::invalid_data("ERR invalid character in geohash"));
    }
    if i < 10 {
      bits |= (val as u64) << (52 - (i + 1) * 5);
    } else {
      bits |= ((val & 0x18) as u64) >> 3;
    }
  }
  let area = geohash_decode(
    &GEO_LON_RANGE_STANDARD,
    &GEO_LAT_RANGE_STANDARD,
    GeoHashBits { bits, step: 26 },
  );
  Ok(geohash_decode_area_to_long_lat(&area))
}

/// Shifts Geohash block along the X axis aligned with Kvrocks GeohashMoveX.
/// X 轴移动 Geohash 块(对标 Kvrocks GeohashMoveX)
#[inline]
pub fn geohash_move_x(hash: &mut GeoHashBits, d: i8) {
  if d == 0 || hash.step == 0 || hash.step > 26 {
    return;
  }
  let mut x = hash.bits & 0xaaaaaaaaaaaaaaaa;
  let y = hash.bits & 0x5555555555555555;
  let shift = 64 - hash.step * 2;
  let zz = 0x5555555555555555 >> shift;

  if d > 0 {
    x = x.wrapping_add(zz + 1);
  } else {
    x = (x | zz).wrapping_sub(zz + 1);
  }
  x &= 0xaaaaaaaaaaaaaaaa >> shift;
  hash.bits = x | y;
}

/// Shifts Geohash block along the Y axis aligned with Kvrocks GeohashMoveY.
/// Y 轴移动 Geohash 块(对标 Kvrocks GeohashMoveY)
#[inline]
pub fn geohash_move_y(hash: &mut GeoHashBits, d: i8) {
  if d == 0 || hash.step == 0 || hash.step > 26 {
    return;
  }
  let x = hash.bits & 0xaaaaaaaaaaaaaaaa;
  let mut y = hash.bits & 0x5555555555555555;
  let shift = 64 - hash.step * 2;
  let zz = 0xaaaaaaaaaaaaaaaa >> shift;

  if d > 0 {
    y = y.wrapping_add(zz + 1);
  } else {
    y = (y | zz).wrapping_sub(zz + 1);
  }
  y &= 0x5555555555555555 >> shift;
  hash.bits = x | y;
}

/// Calculates Geohash values for all 8 neighboring grid cells aligned with Kvrocks GeohashNeighbors.
/// 获取 8 邻居区域的 Geohash(对标 Kvrocks GeohashNeighbors)
pub fn geohash_neighbors(hash: &GeoHashBits) -> GeoHashNeighbors {
  let mut n = GeoHashNeighbors {
    north: *hash,
    east: *hash,
    west: *hash,
    south: *hash,
    north_east: *hash,
    south_east: *hash,
    north_west: *hash,
    south_west: *hash,
  };

  geohash_move_x(&mut n.east, 1);
  geohash_move_x(&mut n.west, -1);
  geohash_move_y(&mut n.south, -1);
  geohash_move_y(&mut n.north, 1);

  geohash_move_x(&mut n.north_west, -1);
  geohash_move_y(&mut n.north_west, 1);

  geohash_move_x(&mut n.north_east, 1);
  geohash_move_y(&mut n.north_east, 1);

  geohash_move_x(&mut n.south_east, 1);
  geohash_move_y(&mut n.south_east, -1);

  geohash_move_x(&mut n.south_west, -1);
  geohash_move_y(&mut n.south_west, -1);

  n
}

/// Estimates Geohash precision step count based on search radius aligned with Kvrocks GeoHashHelper::EstimateStepsByRadius.
/// 根据半径估算 Geohash 搜索步长(精度比特数,对标 Kvrocks GeoHashHelper::EstimateStepsByRadius)
#[inline]
pub fn estimate_steps_by_radius(mut range_meters: f64, lat: f64) -> u8 {
  if range_meters <= 0.0 {
    return 26;
  }
  let mut step = 1i32;
  while range_meters < MERCATOR_MAX {
    range_meters *= 2.0;
    step += 1;
  }
  step -= 2;

  if !(-66.0..=66.0).contains(&lat) {
    step -= 1;
    if !(-80.0..=80.0).contains(&lat) {
      step -= 1;
    }
  }

  step.clamp(1, 26) as u8
}

/// Computes bounding box for a spatial search shape aligned with Kvrocks GeoHashHelper::BoundingBox.
/// 计算搜索形状的外接边界盒(对标 Kvrocks GeoHashHelper::BoundingBox)
pub fn bounding_box(geo_shape: &mut GeoShape) {
  let longitude = geo_shape.center_lon;
  let latitude = geo_shape.center_lat;
  let height = geo_shape.conversion
    * if geo_shape.shape_type == GeoShapeType::Circular {
      geo_shape.radius
    } else {
      geo_shape.height * 0.5
    };
  let width = geo_shape.conversion
    * if geo_shape.shape_type == GeoShapeType::Circular {
      geo_shape.radius
    } else {
      geo_shape.width * 0.5
    };

  let lat_delta = (height / EARTH_RADIUS_METERS) / D_R;
  let lat_top_rad = (latitude + lat_delta) * D_R;
  let lat_bottom_rad = (latitude - lat_delta) * D_R;

  let long_delta_top = (width / EARTH_RADIUS_METERS / lat_top_rad.cos()) / D_R;
  let long_delta_bottom = (width / EARTH_RADIUS_METERS / lat_bottom_rad.cos()) / D_R;

  let is_south = latitude < 0.0;
  let long_delta = if is_south {
    long_delta_bottom
  } else {
    long_delta_top
  };

  geo_shape.bounds[0] = longitude - long_delta;
  geo_shape.bounds[1] = latitude - lat_delta;
  geo_shape.bounds[2] = longitude + long_delta;
  geo_shape.bounds[3] = latitude + lat_delta;
}

/// Computes 9-neighbor Geohash search areas covering a shape aligned with Kvrocks GeoHashHelper::GetAreasByShapeWGS84.
/// 获取形状覆盖的 9 邻居 Geohash 检索区域(对标 Kvrocks GeoHashHelper::GetAreasByShapeWGS84)
pub fn get_areas_by_shape_wgs84(geo_shape: &mut GeoShape) -> GeoHashRadius {
  bounding_box(geo_shape);
  let min_lon = geo_shape.bounds[0];
  let min_lat = geo_shape.bounds[1];
  let max_lon = geo_shape.bounds[2];
  let max_lat = geo_shape.bounds[3];

  let longitude = geo_shape.center_lon;
  let latitude = geo_shape.center_lat;
  let radius_meters = geo_shape.conversion
    * if geo_shape.shape_type == GeoShapeType::Circular {
      geo_shape.radius
    } else {
      (geo_shape.width * 0.5).hypot(geo_shape.height * 0.5)
    };

  let mut steps = estimate_steps_by_radius(radius_meters, latitude);

  let mut hash =
    geohash_encode(&GEO_LON_RANGE, &GEO_LAT_RANGE, longitude, latitude, steps).unwrap_or_default();
  let mut neighbors = geohash_neighbors(&hash);
  let mut area = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, hash);

  let decrease_step = {
    let north = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, neighbors.north);
    haversine_distance(longitude, latitude, longitude, north.latitude.max) < radius_meters
      || {
        let south = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, neighbors.south);
        haversine_distance(longitude, latitude, longitude, south.latitude.min) < radius_meters
      }
      || {
        let east = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, neighbors.east);
        haversine_distance(longitude, latitude, east.longitude.max, latitude) < radius_meters
      }
      || {
        let west = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, neighbors.west);
        haversine_distance(longitude, latitude, west.longitude.min, latitude) < radius_meters
      }
  };

  if steps > 1 && decrease_step {
    steps -= 1;
    hash = geohash_encode(&GEO_LON_RANGE, &GEO_LAT_RANGE, longitude, latitude, steps)
      .unwrap_or_default();
    neighbors = geohash_neighbors(&hash);
    area = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, hash);
  }

  if steps >= 2 {
    if area.latitude.min < min_lat {
      neighbors.south = GeoHashBits::default();
      neighbors.south_west = GeoHashBits::default();
      neighbors.south_east = GeoHashBits::default();
    }
    if area.latitude.max > max_lat {
      neighbors.north = GeoHashBits::default();
      neighbors.north_east = GeoHashBits::default();
      neighbors.north_west = GeoHashBits::default();
    }
    if area.longitude.min < min_lon {
      neighbors.west = GeoHashBits::default();
      neighbors.south_west = GeoHashBits::default();
      neighbors.north_west = GeoHashBits::default();
    }
    if area.longitude.max > max_lon {
      neighbors.east = GeoHashBits::default();
      neighbors.south_east = GeoHashBits::default();
      neighbors.north_east = GeoHashBits::default();
    }
  }

  GeoHashRadius {
    hash,
    area,
    neighbors,
  }
}

/// Computes 52-bit ZSet score range [min, max) for a Geohash box aligned with Kvrocks Geo::scoresOfGeoHashBox.
/// 计算 Geohash 块对应的 52 位 ZSet 分值区间 [min, max)(对标 Kvrocks Geo::scoresOfGeoHashBox)
#[inline(always)]
pub const fn scores_of_geohash_box(hash: GeoHashBits) -> (u64, u64) {
  let min = align_52bits(hash);
  let next = GeoHashBits {
    bits: hash.bits.wrapping_add(1),
    step: hash.step,
  };
  let max = align_52bits(next);
  (min, max)
}

/// Calculates Haversine great-circle distance in meters between two coordinates aligned with Kvrocks GeoHashHelper::GetDistance.
/// 计算两个经纬度坐标之间的球面 Haversine 距离(米)(对标 Kvrocks GeoHashHelper::GetDistance)
#[inline]
pub fn haversine_distance(lon1: f64, lat1: f64, lon2: f64, lat2: f64) -> f64 {
  if (lon1 - lon2).abs() < f64::EPSILON && (lat1 - lat2).abs() < f64::EPSILON {
    return 0.0;
  }
  let lat1r = lat1 * D_R;
  let lon1r = lon1 * D_R;
  let lat2r = lat2 * D_R;
  let lon2r = lon2 * D_R;

  let u = ((lat2r - lat1r) * 0.5).sin();
  let v = ((lon2r - lon1r) * 0.5).sin();

  let a = (u * u + lat1r.cos() * lat2r.cos() * v * v).clamp(0.0, 1.0);
  let c = 2.0 * a.sqrt().asin();

  EARTH_RADIUS_METERS * c
}

/// Converts distance in meters to target unit value.
/// 根据单位转换米数为目标单位数值
#[inline]
pub fn convert_meters_to_unit(meters: f64, unit: &str) -> f64 {
  if let Some(u) = DistanceUnit::parse(unit) {
    u.from_meters(meters)
  } else {
    meters
  }
}

/// Converts distance from source unit value to meters.
/// 根据目标单位转换距离为米数
#[inline]
pub fn convert_unit_to_meters(dist: f64, unit: &str) -> f64 {
  if let Some(u) = DistanceUnit::parse(unit) {
    u.to_meters(dist)
  } else {
    dist
  }
}

#[inline]
pub fn geohash_encode_wgs84(longitude: f64, latitude: f64, step: u8) -> Option<GeoHashBits> {
  geohash_encode(&GEO_LON_RANGE, &GEO_LAT_RANGE, longitude, latitude, step)
}

#[inline]
pub fn geohash_decode_wgs84(hash: GeoHashBits) -> (f64, f64) {
  let area = geohash_decode(&GEO_LON_RANGE, &GEO_LAT_RANGE, hash);
  geohash_decode_area_to_long_lat(&area)
}

#[inline]
pub fn score_to_coord(score: f64) -> (f64, f64) {
  geohash_decode_wgs84(GeoHashBits {
    bits: score as u64,
    step: GEO_STEP_MAX,
  })
}

impl GeoShape {
  pub fn new_circular(lon: f64, lat: f64, radius_meters: f64) -> Self {
    let mut shape = Self {
      shape_type: GeoShapeType::Circular,
      center_lon: lon,
      center_lat: lat,
      radius: radius_meters,
      width: 0.0,
      height: 0.0,
      conversion: 1.0,
      bounds: [0.0; 4],
    };
    bounding_box(&mut shape);
    shape
  }

  pub fn new_circular_with_unit(lon: f64, lat: f64, radius: f64, unit: DistanceUnit) -> Self {
    let mut shape = Self {
      shape_type: GeoShapeType::Circular,
      center_lon: lon,
      center_lat: lat,
      radius,
      width: 0.0,
      height: 0.0,
      conversion: unit.conversion_factor(),
      bounds: [0.0; 4],
    };
    bounding_box(&mut shape);
    shape
  }

  pub fn new_rectangular(lon: f64, lat: f64, width_meters: f64, height_meters: f64) -> Self {
    let mut shape = Self {
      shape_type: GeoShapeType::Rectangular,
      center_lon: lon,
      center_lat: lat,
      radius: 0.0,
      width: width_meters,
      height: height_meters,
      conversion: 1.0,
      bounds: [0.0; 4],
    };
    bounding_box(&mut shape);
    shape
  }

  pub fn new_rectangular_with_unit(
    lon: f64,
    lat: f64,
    width: f64,
    height: f64,
    unit: DistanceUnit,
  ) -> Self {
    let mut shape = Self {
      shape_type: GeoShapeType::Rectangular,
      center_lon: lon,
      center_lat: lat,
      radius: 0.0,
      width,
      height,
      conversion: unit.conversion_factor(),
      bounds: [0.0; 4],
    };
    bounding_box(&mut shape);
    shape
  }

  /// Checks whether coordinate point falls within current shape aligned with Kvrocks appendIfWithinShape.
  /// 检查经纬度坐标点是否在当前形状范围内(对标 Kvrocks appendIfWithinShape)
  #[inline]
  pub fn contains_point(&self, lon: f64, lat: f64) -> bool {
    match self.shape_type {
      GeoShapeType::Circular => {
        let d_meters = haversine_distance(self.center_lon, self.center_lat, lon, lat);
        d_meters <= self.radius * self.conversion
      }
      GeoShapeType::Rectangular => {
        lon >= self.bounds[0]
          && lon <= self.bounds[2]
          && lat >= self.bounds[1]
          && lat <= self.bounds[3]
      }
      GeoShapeType::None => false,
    }
  }

  /// Checks whether GeoPoint falls within current shape aligned with RediSearch/Geo.
  /// 检查 GeoPoint 是否在当前形状范围内(对标 RediSearch/Geo 规范)
  #[inline]
  pub fn point_in_radius(&self, pt: &GeoPoint) -> bool {
    self.contains_point(pt.longitude, pt.latitude)
  }
}