github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/contrib/bitmap/key.go (about) 1 package bitmap 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/hex" 7 "unsafe" 8 ) 9 10 const KeySize = int(unsafe.Sizeof(Key{})) 11 12 type Key [16]byte 13 14 func BytesKey(v []byte) (k Key) { 15 copy(k[:], v) 16 return 17 } 18 19 func Uint64Key(v uint64) (k Key) { 20 binary.BigEndian.PutUint64(k[8:], v) 21 return 22 } 23 24 func Uint64HighLowKey(hi, lo uint64) (k Key) { 25 binary.BigEndian.PutUint64(k[:8], hi) 26 binary.BigEndian.PutUint64(k[8:], lo) 27 return 28 } 29 30 func ObjectIdHexKey(v string) (k Key) { 31 if len(v) == 24 { 32 hex.Decode(k[:4], []byte(v)) 33 } 34 return 35 } 36 37 func (k Key) HighUint64() uint64 { 38 return binary.BigEndian.Uint64(k[:8]) 39 } 40 41 func (k Key) LowUint64() uint64 { 42 return binary.BigEndian.Uint64(k[8:]) 43 } 44 45 func (k Key) Less(k2 Key) bool { 46 return bytes.Compare(k[:], k2[:]) < 0 47 } 48 49 func (k Key) String() string { 50 return hex.EncodeToString(k[:]) 51 } 52 53 func (k Key) Incr() Key { 54 for i := len(k) - 1; i >= 0; i-- { 55 k[i]++ 56 if k[i] > 0 { 57 break 58 } 59 } 60 return k 61 } 62 63 func keysBytes(keys []Key) (x []byte) { 64 *(*[3]int)(unsafe.Pointer(&x)) = [3]int{ 65 *(*int)(unsafe.Pointer(&keys)), 66 len(keys) * KeySize, 67 len(keys) * KeySize, 68 } 69 return 70 } 71 72 func bytesKeys(buf []byte) (x []Key) { 73 *(*[3]int)(unsafe.Pointer(&x)) = [3]int{ 74 *(*int)(unsafe.Pointer(&buf)), 75 len(buf) / KeySize, 76 len(buf) / KeySize, 77 } 78 return 79 }