Last active
December 20, 2017 14:45
-
-
Save BrianLeishman/2cac04db270d14661a653c88f2050d6d to your computer and use it in GitHub Desktop.
BID for MySQL (9-byte, time based ID generator)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017, Brian Leishman | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* * Redistributions of source code must retain the above copyright notice, this | |
* list of conditions and the following disclaimer. | |
* * Redistributions in binary form must reproduce the above copyright notice, | |
* this list of conditions and the following disclaimer in the documentation | |
* and/or other materials provided with the distribution. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* USAGE INSTRUCTIONS: | |
* | |
* make sure libmysqlclient-dev is installed: | |
* apt-get install libmysqlclient-dev | |
* | |
* Replace "/usr/lib/mysql/plugin" with your MySQL plugins directory (can be found by running "select @@plugin_dir;") | |
* gcc -I/usr/include/mysql -o bid.so -shared bid.c -fPIC && cp bid.so /usr/lib/mysql/plugin/bid.so | |
* | |
* Then, on the server: | |
* create function`bid`returns string soname'bid.so'; | |
* | |
* And use/test like: | |
* select `bid`(); | |
* | |
* Also, make sure to store in a "BINARY(9)" column type | |
* | |
* Yeet! | |
* | |
*/ | |
#ifdef STANDARD | |
/* STANDARD is defined, don't use any mysql functions */ | |
#include <string.h> | |
#ifdef __WIN__ | |
typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */ | |
typedef __int64 longlong; | |
#else | |
typedef unsigned long long ulonglong; | |
typedef long long longlong; | |
#endif /*__WIN__*/ | |
#else | |
#include <my_global.h> | |
#include <my_sys.h> | |
#if defined(MYSQL_SERVER) | |
#include <m_string.h> | |
#else | |
/* when compiled as standalone */ | |
#include <string.h> | |
#endif | |
#endif | |
#include <mysql.h> | |
#include <ctype.h> | |
#include <stdint.h> | |
#include <time.h> | |
#ifdef HAVE_DLOPEN | |
int randint(int n) { | |
//https://stackoverflow.com/a/822361/728236 | |
if ((n - 1) == RAND_MAX) { | |
return rand(); | |
} else { | |
// Chop off all of the values that would cause skew... | |
long end = RAND_MAX / n; // truncate skew | |
assert(end > 0L); | |
end *= n; | |
// ... and ignore results from rand() that fall above that limit. | |
// (Worst case the loop condition should succeed 50% of the time, | |
// so we can expect to bail out of this loop pretty quickly.) | |
int r; | |
while ((r = rand()) >= end); | |
return r % n; | |
} | |
} | |
my_bool bid_init(UDF_INIT *initid, UDF_ARGS *args, char *message); | |
void bid_deinit(UDF_INIT *initid); | |
char *bid(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))); | |
my_bool bid_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { | |
initid->maybe_null = 0; | |
return 0; | |
} | |
char *bid(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))) { | |
struct timespec ts; | |
unsigned char *bytes = malloc(sizeof (char) * (9)); | |
timespec_get(&ts, TIME_UTC); | |
uint64_t Timestamp = ts.tv_sec * 1000000000 + ts.tv_nsec; | |
bytes[0] = (Timestamp >> 56) & 0xFF; | |
bytes[1] = (Timestamp >> 48) & 0xFF; | |
bytes[2] = (Timestamp >> 40) & 0xFF; | |
bytes[3] = (Timestamp >> 32) & 0xFF; | |
bytes[4] = (Timestamp >> 24) & 0xFF; | |
bytes[5] = (Timestamp >> 16) & 0xFF; | |
bytes[6] = (Timestamp >> 8) & 0xFF; | |
bytes[7] = Timestamp & 0xFF; | |
bytes[8] = randint(256) & 0xFF; | |
*length = 9; | |
return bytes; | |
} | |
#endif /* HAVE_DLOPEN */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment