]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/utilities/spatialdb/utils.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / utilities / spatialdb / utils.h
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
2// This source code is licensed under both the GPLv2 (found in the
3// COPYING file in the root directory) and Apache 2.0 License
4// (found in the LICENSE.Apache file in the root directory).
7c673cae
FG
5
6#pragma once
7#include <string>
8#include <algorithm>
9
10#include "rocksdb/utilities/spatial_db.h"
11
12namespace rocksdb {
13namespace spatial {
14
15// indexing idea from http://msdn.microsoft.com/en-us/library/bb259689.aspx
16inline uint64_t GetTileFromCoord(double x, double start, double end,
17 uint32_t tile_bits) {
18 if (x < start) {
19 return 0;
20 }
21 uint64_t tiles = 1ull << tile_bits;
22 uint64_t r = static_cast<uint64_t>(((x - start) / (end - start)) * tiles);
23 return std::min(r, tiles - 1);
24}
25
26inline uint64_t GetQuadKeyFromTile(uint64_t tile_x, uint64_t tile_y,
27 uint32_t tile_bits) {
28 uint64_t quad_key = 0;
29 for (uint32_t i = 0; i < tile_bits; ++i) {
30 uint64_t mask = (1ull << i);
31 quad_key |= (tile_x & mask) << i;
32 quad_key |= (tile_y & mask) << (i + 1);
33 }
34 return quad_key;
35}
36
37inline BoundingBox<uint64_t> GetTileBoundingBox(
38 const SpatialIndexOptions& spatial_index, BoundingBox<double> bbox) {
39 return BoundingBox<uint64_t>(
40 GetTileFromCoord(bbox.min_x, spatial_index.bbox.min_x,
41 spatial_index.bbox.max_x, spatial_index.tile_bits),
42 GetTileFromCoord(bbox.min_y, spatial_index.bbox.min_y,
43 spatial_index.bbox.max_y, spatial_index.tile_bits),
44 GetTileFromCoord(bbox.max_x, spatial_index.bbox.min_x,
45 spatial_index.bbox.max_x, spatial_index.tile_bits),
46 GetTileFromCoord(bbox.max_y, spatial_index.bbox.min_y,
47 spatial_index.bbox.max_y, spatial_index.tile_bits));
48}
49
50// big endian can be compared using memcpy
51inline void PutFixed64BigEndian(std::string* dst, uint64_t value) {
52 char buf[sizeof(value)];
53 buf[0] = (value >> 56) & 0xff;
54 buf[1] = (value >> 48) & 0xff;
55 buf[2] = (value >> 40) & 0xff;
56 buf[3] = (value >> 32) & 0xff;
57 buf[4] = (value >> 24) & 0xff;
58 buf[5] = (value >> 16) & 0xff;
59 buf[6] = (value >> 8) & 0xff;
60 buf[7] = value & 0xff;
61 dst->append(buf, sizeof(buf));
62}
63
64// big endian can be compared using memcpy
65inline bool GetFixed64BigEndian(const Slice& input, uint64_t* value) {
66 if (input.size() < sizeof(uint64_t)) {
67 return false;
68 }
69 auto ptr = input.data();
70 *value = (static_cast<uint64_t>(static_cast<unsigned char>(ptr[0])) << 56) |
71 (static_cast<uint64_t>(static_cast<unsigned char>(ptr[1])) << 48) |
72 (static_cast<uint64_t>(static_cast<unsigned char>(ptr[2])) << 40) |
73 (static_cast<uint64_t>(static_cast<unsigned char>(ptr[3])) << 32) |
74 (static_cast<uint64_t>(static_cast<unsigned char>(ptr[4])) << 24) |
75 (static_cast<uint64_t>(static_cast<unsigned char>(ptr[5])) << 16) |
76 (static_cast<uint64_t>(static_cast<unsigned char>(ptr[6])) << 8) |
77 static_cast<uint64_t>(static_cast<unsigned char>(ptr[7]));
78 return true;
79}
80
81inline void PutDouble(std::string* dst, double d) {
82 dst->append(reinterpret_cast<char*>(&d), sizeof(double));
83}
84
85inline bool GetDouble(Slice* input, double* d) {
86 if (input->size() < sizeof(double)) {
87 return false;
88 }
89 memcpy(d, input->data(), sizeof(double));
90 input->remove_prefix(sizeof(double));
91 return true;
92}
93
94} // namespace spatial
95} // namespace rocksdb