Commit 4631a207 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Correct the implementation of generate_tid.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@3 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent bbb45f89
#ifndef timestamp_h
#define timestamp_h
int generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN]);
int generate_tid (unsigned char ntid[ID_LEN], unsigned char ltid[ID_LEN]);
#endif
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include "neo_struct.h"
/* int */
/* inc_tid (u_int16_t tid[ID_LEN], int pos) */
/* { */
/* /\* printf ("inc_tid : pos %d, %d\n", pos, tid[pos]); *\/ */
/* tid[pos]++; */
/* /\* printf ("inc %d\n", tid[pos]); *\/ */
/* if (tid[pos] > 255) */
/* { */
/* tid[pos] = 0; */
/* if (pos >= 0) */
/* inc_tid (tid, pos - 1); */
/* else */
/* { */
/* printf ("no more tid left to generate\n"); */
/* return 0; */
/* } */
/* } */
/* return 1; */
/* } */
int
generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN])
generate_tid (unsigned char ntid[ID_LEN], unsigned char ltid[ID_LEN])
{
int i;
time_t epoch_time;
struct tm *gm_time;
struct timeval tv;
struct tm tm;
u_int32_t v;
double sconv = 0;
unsigned char new_tid[ID_LEN], last_tid[ID_LEN];
double sec;
/* generate time */
time (&epoch_time);
gm_time = (struct tm *) malloc (sizeof (struct tm));
gm_time = gmtime (&epoch_time);
if (gettimeofday (&tv, 0) < 0)
return 0;
if (! gmtime_r (&tv.tv_sec, &tm))
return 0;
/* create an integer array */
v =
((((gm_time->tm_year) * 12 + gm_time->tm_mon) * 31 +
gm_time->tm_mday - 1) * 24 + gm_time->tm_hour) * 60 + gm_time->tm_min;
ntid[0] = v / 16777216;
ntid[1] = (v % 16777216) / 65536;
ntid[2] = (v % 65536) / 256;
ntid[3] = v % 256;
gm_time->tm_sec /= sconv;
v = (u_int32_t) epoch_time % 60;
ntid[4] = v / 16777216;
ntid[5] = (v % 16777216) / 65536;
ntid[6] = (v % 65536) / 256;
ntid[7] = v % 256;
compare:
/* create the string array */
for (i = 0; i < ID_LEN; i++)
((((tm.tm_year) * 12 + tm.tm_mon) * 31 +
tm.tm_mday - 1) * 24 + tm.tm_hour) * 60 + tm.tm_min;
v = htonl (v);
memcpy (ntid, &v, 4);
sec = (tv.tv_sec % 60) + ((double) tv.tv_usec / 1000000);
sec /= ((double) 60) / ((double) (1 << 16)) / ((double) (1 << 16));
v = (u_int32_t) sec;
v = htonl (v);
memcpy (ntid + 4, &v, 4);
while (memcmp (ntid, ltid, ID_LEN) <= 0)
{
new_tid[i] = (unsigned char)ntid[i];
last_tid[i] = (unsigned char)ltid[i];
}
if (memcmp (new_tid, last_tid, ID_LEN) > 0)
return 1;
int i;
for (i = ID_LEN - 1; i > 3; i--)
{
......@@ -77,10 +44,12 @@ generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN])
else
{
ntid[i]++;
goto compare;
continue;
}
}
printf ("oups must finish compare function...\n");
return 0;
}
return 1;
}
......@@ -39,7 +39,7 @@ static int soc = -1;
static uuid_t master_id;
static char ID[UUID_LEN+1]; /* uuid is unparse into a 36 byte string */
static u_int64_t ltid; /* store last transaction id */
static u_int16_t last_generated_tid[ID_LEN]; /* last generated tid, use to check if tid increases */
static unsigned char last_generated_tid[ID_LEN]; /* last generated tid, use to check if tid increases */
static u_int16_t replication_nb = DEFAULT_REPLICATION_NB; /* number of replication for a transaction on storages nodes */
static u_int16_t last_storage_replication; /* id of the last storage node used for replication */
static struct storageInfo DBinfo; /* store informations about storage system */
......@@ -120,7 +120,7 @@ cleanup_handler (int sig)
/* must store last oid and tid */
fd = fopen (MASTER_DATA_FILE, "w");
fwrite (ascii, sizeof (int), 8, fd);
fwrite (last_generated_tid, sizeof (u_int16_t), 8, fd);
fwrite (last_generated_tid, sizeof (unsigned char), 8, fd);
fclose (fd);
/* send message of master close to all storages first */
......@@ -721,7 +721,7 @@ h_beginTrans (int conn, char *hbuf)
u_int64_t tid;
u_int32_t len = 0, data_len = 0, i;
char rflags[FLAG_LEN], *buf = NULL, *id = NULL;
u_int16_t s[ID_LEN];
unsigned char s[ID_LEN];
struct stringList storages;
/* header */
......@@ -1817,7 +1817,7 @@ main (int argc, char **argv)
{
fd = fopen (MASTER_DATA_FILE, "r");
fread (ascii, sizeof (int), 8, fd);
fread (last_generated_tid, sizeof (u_int16_t), 8, fd);
fread (last_generated_tid, sizeof (unsigned char), 8, fd);
fclose (fd);
}
else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment