00001
00002
00006 typedef struct Pair {
00007 int index;
00008 double value;
00009 } Pair;
00010
00015 struct serialized_svector_entry {
00016 int i;
00017 double v;
00018 };
00019
00023 struct serialized_fvector_entry {
00024 double v;
00025 };
00026
00027 #ifdef __POSTGRES_CALL
00028 #include "postgres.h"
00029
00035 size_t
00036 calculate_svector_total_size(int _size) {
00037 size_t size_of_pair = sizeof(struct serialized_svector_entry);
00038 return sizeof(int) + size_of_pair * _size;
00039 }
00040
00046 size_t
00047 calculate_fvector_total_size(int _size) {
00048 size_t size_of_pair = sizeof(struct serialized_fvector_entry);
00049 return sizeof(int) + size_of_pair * _size;
00050 }
00051
00060 size_t
00061 write_fvector_bin_psql_double(void *out, size_t max_size, int _size, double *ddata) {
00062 int i = 0;
00063 size_t total_size = calculate_fvector_total_size(_size);
00064 if(total_size > max_size) {
00065 elog(INFO, "total size: %d, max size: %d", total_size, max_size);
00066 elog(ERROR, "We blew up the buffer in [write_fvector_bin_psql_double]\n");
00067 return 0;
00068 }
00069 ((int *)out)[0] = _size;
00070 struct serialized_fvector_entry *_data = (struct serialized_fvector_entry*) &(((int *)out)[1]);
00071 memcpy(_data, ddata, total_size);
00072 return total_size;
00073 }
00074
00083 size_t
00084 write_svector_bin_psql_pair(void *out, size_t max_size, int _size, Pair *ddata) {
00085 int i = 0;
00086 size_t total_size = calculate_svector_total_size(_size);
00087 if(total_size > max_size) {
00088 elog(INFO, "total size: %d, max size: %d", total_size, max_size);
00089 elog(ERROR, "We blew up the buffer in [write_fvector_bin_psql_pair]\n");
00090 return 0;
00091 }
00092 ((int *)out)[0] = _size;
00093 struct serialized_svector_entry *_data = (struct serialized_svector_entry*) &(((int *)out)[1]);
00094
00095 memcpy(_data, ddata, total_size);
00096 return total_size;
00097 }
00098
00099 #endif