Permalink
Browse files

Drafted out the backend, ran into problems

You are in a maze of twisty little time/date formats, all
different.

I DO want to preserve the exact ntp timestamp rather than
translate to postgres's type(s).

That leads to a world of hurt. I need to stop and go look
at some other implementations - such as the round robin database
to see a good way to go.

Also got bit by featureitus - I'd like to use a geospacial
database - my vision is to be able to present bloat as
"height above ground" on a rotating globe with the time series(s).

My imagination shows the terminator line passing and behind it,
the bloated buffers gradulally becoming smaller and less noticable.
  • Loading branch information...
1 parent 08a7342 commit 38930c0c70fa5f2c088c7d9e28f93058e9a988ba Dave Taht committed Feb 9, 2011
Showing with 178 additions and 0 deletions.
  1. +77 −0 ntp.sql
  2. +72 −0 ntp_timestamp_type.c
  3. +29 −0 ntp_timestamp_type.sql
  4. BIN packet_structure.odt
View
77 ntp.sql
@@ -0,0 +1,77 @@
+-- NOTE: THIS IS NOT EVEN CLOSE TO WORKING CODE
+-- I'm still digging myself out of the morass of
+-- different data types, all different.
+
+-- constants - speed of light - speed of light in fiber
+
+-- watch out for this not working well in odbc
+
+-- postgres timestamps are not very precise
+-- and worse, can vary in resolution between versions
+-- that use floats and ints
+-- resolution also varies over time.
+
+-- and start in another epoch than ntp
+
+-- timestamp [ (p) ] [ without time zone ]
+-- 8 bytes both date and time
+-- 4713 BC 5874897 AD 1 microsecond / 14 digits
+
+-- so we will create a conversion routine
+-- between the two and keep the precise measure
+-- in the database
+
+-- and we either need an unsigned integer type
+-- to promote int4 to int8 seems inelegant
+-- perhaps we can do this this way, and
+-- then make sure we're careful about data entry
+
+create type uint (a int8);
+
+create type ntp_timestamp as (seconds uint, frac uint);
+
+-- also doing reports on intervals is no fun
+-- so we will also extract that info on the
+-- way in, rather the way out.
+
+-- calc RTT statically?
+--
+
+create table ntp_packet (
+ src inet,
+ dst inet,
+ src_port short,
+ dst_port short,
+ reference ntp_timestamp,
+ originate ntp_timestamp,
+ receive ntp_timestamp,
+ transmit ntp_timestamp,
+ t_reference timestamp,
+ t_originate timestamp,
+ t_receive timestamp,
+ t_transmit timestamp,
+
+ key_identifier int,
+ md
+}
+
+create trigger on ntp_packet when reference, originate, receive, transmit are modified
+
+-- Use postgis geography mapper
+
+-- Distance calculation using GEOGRAPHY (122.2km)
+ SELECT ST_Distance('LINESTRING(-122.33 47.606, 0.0 51.5)'::geography, 'POINT(-21.96 64.15)':: geography);
+INSERT INTO global_points (name, location) VALUES ('Town', ST_GeographyFromText('SRID=4326;POINT(-110 30)') );
+
+-- http://www.postgis.org/documentation/manual-1.5/
+
+--
+
+create table hosts(
+ inet src,
+ location GEOGRAPHY(POINTZ,4326),
+ latlong point,
+ varchar op,
+
+);
+
View
@@ -0,0 +1,72 @@
+// Should I just emulate the ntp timestamp entirely?
+
+
+
+typedef struct Ntp_Timestamp {
+ unsigned int x;
+ unsigned int y;
+} Ntp_Timestamp;
+
+PG_FUNCTION_INFO_V1(ntp_timestamp_in);
+
+Datum
+ntp_timestamp_in(PG_FUNCTION_ARGS)
+{
+ char *str = PG_GETARG_CSTRING(0);
+ unsigned int x,
+ y;
+ Ntp_Timestamp *result;
+
+ if (sscanf(str, " ( %lf , %lf )", &x, &y) != 2)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+ errmsg("invalid input syntax for ntp_timestamp: \"%s\"",
+ str)));
+
+ result = (Ntp_Timestamp *) palloc(sizeof(Ntp_Timestamp));
+ result->x = x;
+ result->y = y;
+ PG_RETURN_POINTER(result);
+}
+
+PG_FUNCTION_INFO_V1(ntp_timestamp_out);
+
+Datum
+ntp_timestamp_out(PG_FUNCTION_ARGS)
+{
+ Ntp_Timestamp *ntp_timestamp = (Ntp_Timestamp *) PG_GETARG_POINTER(0);
+ char *result;
+
+ result = (char *) palloc(100);
+ snprintf(result, 100, "(%g,%g)", ntp_timestamp->x, ntp_timestamp->y);
+ PG_RETURN_CSTRING(result);
+}
+
+PG_FUNCTION_INFO_V1(ntp_timestamp_recv);
+
+Datum
+ntp_timestamp_recv(PG_FUNCTION_ARGS)
+{
+ StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
+ Ntp_Timestamp *result;
+
+ result = (Ntp_Timestamp *) palloc(sizeof(Ntp_Timestamp));
+ result->x = pq_getmsgfloat8(buf);
+ result->y = pq_getmsgfloat8(buf);
+ PG_RETURN_POINTER(result);
+}
+
+PG_FUNCTION_INFO_V1(ntp_timestamp_send);
+
+Datum
+ntp_timestamp_send(PG_FUNCTION_ARGS)
+{
+ Ntp_Timestamp *ntp_timestamp = (Ntp_Timestamp *) PG_GETARG_POINTER(0);
+ StringInfoData buf;
+
+ pq_begintypsend(&buf);
+ pq_sendfloat8(&buf, ntp_timestamp->x);
+ pq_sendfloat8(&buf, ntp_timestamp->y);
+ PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+}
+
View
@@ -0,0 +1,29 @@
+CREATE FUNCTION complex_in(cstring)
+ RETURNS complex
+ AS 'filename'
+ LANGUAGE C IMMUTABLE STRICT;
+
+CREATE FUNCTION complex_out(complex)
+ RETURNS cstring
+ AS 'filename'
+ LANGUAGE C IMMUTABLE STRICT;
+
+CREATE FUNCTION complex_recv(internal)
+ RETURNS complex
+ AS 'filename'
+ LANGUAGE C IMMUTABLE STRICT;
+
+CREATE FUNCTION complex_send(complex)
+ RETURNS bytea
+ AS 'filename'
+ LANGUAGE C IMMUTABLE STRICT;
+
+CREATE TYPE complex (
+ internallength = 16,
+ input = complex_in,
+ output = complex_out,
+ receive = complex_recv,
+ send = complex_send,
+ alignment = double
+);
+
View
Binary file not shown.

0 comments on commit 38930c0

Please sign in to comment.