Merge lp:~zorba-coders/zorba/feature-uuid into lp:zorba

Proposed by Paul J. Lucas
Status: Merged
Approved by: Chris Hillery
Approved revision: 10990
Merged at revision: 11014
Proposed branch: lp:~zorba-coders/zorba/feature-uuid
Merge into: lp:zorba
Diff against target: 1159 lines (+458/-476)
20 files modified
CMakeCompiler.txt (+21/-0)
CMakeLists.txt (+1/-0)
ChangeLog (+1/-0)
doc/zorba/build_prerequisites.dox (+1/-0)
doc/zorba/build_ubuntu.dox (+1/-1)
include/zorba/config.h.cmake (+1/-0)
src/runtime/random/random_impl.cpp (+19/-11)
src/store/naive/simple_store.cpp (+8/-4)
src/store/naive/store.cpp (+0/-1)
src/unit_tests/CMakeLists.txt (+1/-0)
src/unit_tests/test_uuid.cpp (+42/-0)
src/unit_tests/unit_test_list.h (+1/-0)
src/unit_tests/unit_tests.cpp (+1/-0)
src/util/CMakeLists.txt (+1/-2)
src/util/uuid.cpp (+102/-0)
src/util/uuid.h (+257/-0)
src/util/uuid/sysdep.cpp (+0/-59)
src/util/uuid/sysdep.h (+0/-57)
src/util/uuid/uuid.cpp (+0/-297)
src/util/uuid/uuid.h (+0/-44)
To merge this branch: bzr merge lp:~zorba-coders/zorba/feature-uuid
Reviewer Review Type Date Requested Status
Chris Hillery Approve
Rodolfo Ochoa Approve
Matthias Brantner Approve
Paul J. Lucas Approve
Review via email: mp+118268@code.launchpad.net

Commit message

Replaced UUID with thin layer over native platform implementation.

Description of the change

Replaced UUID with thin layer over native platform implementation.

To post a comment you must log in.
lp:~zorba-coders/zorba/feature-uuid updated
10978. By Paul J. Lucas

Removed unnecessary #include.

Revision history for this message
Paul J. Lucas (paul-lucas) :
review: Approve
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

Chris: please look at the CMake stuff I changed.
Rodolfo: please look at the Windows-specific stuff (including the Windows-specific CMake stuff).

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :
Download full text (3.2 KiB)

Doesn't build on 32-bit Ubuntu 11.04 with g++ 4.5.2.

[ 68%] Building CXX object src/CMakeFiles/zorba_simplestore.dir/unit_tests/test_uuid.cpp.o
In file included from /home/mbrantner/zorba/sandbox/src/unit_tests/test_uuid.cpp:20:0:
/home/mbrantner/zorba/sandbox/src/util/uuid.h:35:11: error: ‘uint8_t’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:36:11: error: ‘value_type’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:37:11: error: ‘value_type’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:38:11: error: ‘value_type’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:39:11: error: ‘value_type’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:43:11: error: ‘pointer’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:44:11: error: ‘const_pointer’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:65:3: error: ‘value_type’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:80:3: error: ‘iterator’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:89:3: error: ‘const_iterator’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:98:3: error: ‘iterator’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h:107:3: error: ‘const_iterator’ does not name a type
/home/mbrantner/zorba/sandbox/src/util/uuid.h: In member function ‘zorba::uuid::size_type zorba::uuid::size() const’:
/home/mbrantner/zorba/sandbox/src/util/uuid.h:117:20: error: ‘data’ was not declared in this scope
/home/mbrantner/zorba/sandbox/src/util/uuid.h: In member function ‘void zorba::uuid::swap(zorba::uuid&)’:
/home/mbrantner/zorba/sandbox/src/util/uuid.h:126:29: error: ‘begin’ was not declared in this scope
/home/mbrantner/zorba/sandbox/src/util/uuid.h:126:36: error: ‘end’ was not declared in this scope
/home/mbrantner/zorba/sandbox/src/util/uuid.h:126:44: error: ‘struct zorba::uuid’ has no member named ‘begin’
/home/mbrantner/zorba/sandbox/src/util/uuid.h: In member function ‘zorba::uuid::version zorba::uuid::get_version() const’:
/home/mbrantner/zorba/sandbox/src/util/uuid.h:142:34: error: ‘data’ was not declared in this scope
/home/mbrantner/zorba/sandbox/src/util/uuid.h: In function ‘bool zorba::operator==(const zorba::uuid&, const zorba::uuid&)’:
/home/mbrantner/zorba/sandbox/src/util/uuid.h:166:25: error: ‘const struct zorba::uuid’ has no member named ‘begin’
/home/mbrantner/zorba/sandbox/src/util/uuid.h:166:37: error: ‘const struct zorba::uuid’ has no member named ‘end’
/home/mbrantner/zorba/sandbox/src/util/uuid.h:166:47: error: ‘const struct zorba::uuid’ has no member named ‘begin’
/home/mbrantner/zorba/sandbox/src/util/uuid.h: In function ‘bool zorba::operator<(const zorba::uuid&, const zorba::uuid&)’:
/home/mbrantner/zorba/sandbox/src/util/uuid.h:189:8: error: ‘const struct zorba::uuid’ has no member named ‘begin’
/home/mbrantner/zorba/sandbox/src/util/uuid.h:189:20: error: ‘const struct zorba::uuid’ has no member named ‘end’
/home/mbrantner/zorba/sandbox/src/util/uuid.h:189:30: error: ‘const struct zorba::uuid’ has no member named ‘begin’
/home/mbrantner/zorba/sandbox/src/util/uuid.h:189:...

Read more...

review: Needs Fixing
lp:~zorba-coders/zorba/feature-uuid updated
10979. By Paul J. Lucas

Now using stdint.h

10980. By Paul J. Lucas

Merge from trunk.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

It should be fixed.

Revision history for this message
Matthias Brantner (matthias-brantner) :
review: Approve
lp:~zorba-coders/zorba/feature-uuid updated
10981. By Paul J. Lucas

More comments.

Revision history for this message
Rodolfo Ochoa (rodolfo-ochoa) wrote :

From Windows compiling I'm having:

Error 88 error C2440: '=' : cannot convert from 'u_long (__stdcall *)(u_long)' to 'uint32_t' C:\dev\feature-uuid\src\runtime\random\random_impl.cpp 141 1 zorba_simplestore
Error 89 error C2440: '=' : cannot convert from 'u_long (__stdcall *)(u_long)' to 'uint32_t' C:\dev\feature-uuid\src\runtime\random\random_impl.cpp 141 1 zorba_simplestore
Error 90 error C3861: 'ntohl': identifier not found C:\dev\feature-uuid\src\runtime\random\random_impl.cpp 139 1 zorba_simplestore
Error 178 error C3861: 'ntohl': identifier not found C:\dev\feature-uuid\src\util\uuid.cpp 74 1 zorba_simplestore
Error 179 error C2440: 'initializing' : cannot convert from 'u_long (__stdcall *)(u_long)' to 'const uint32_t' C:\dev\feature-uuid\src\util\uuid.cpp 76 1 zorba_simplestore
Error 180 error C3861: 'ntohs': identifier not found C:\dev\feature-uuid\src\util\uuid.cpp 77 1 zorba_simplestore
Error 181 error C2440: 'initializing' : cannot convert from 'u_short (__stdcall *)(u_short)' to 'const uint16_t' C:\dev\feature-uuid\src\util\uuid.cpp 77 1 zorba_simplestore
Error 182 error C3861: 'ntohs': identifier not found C:\dev\feature-uuid\src\util\uuid.cpp 78 1 zorba_simplestore
Error 183 error C2440: 'initializing' : cannot convert from 'u_short (__stdcall *)(u_short)' to 'const uint16_t' C:\dev\feature-uuid\src\util\uuid.cpp 78 1 zorba_simplestore

review: Needs Fixing
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

Rodolfo: I don't understand why there are errors. I actually test compiled this under Windows using Windows 7 and Visual Studio 2010.

According to <http://msdn.microsoft.com/en-us/library/windows/desktop/ms740075(v=vs.85).aspx>, ntohs is declared in Winsock2.h which I include.

Any ideas?

lp:~zorba-coders/zorba/feature-uuid updated
10982. By Paul J. Lucas

Removed uuid.cpp dependency on ntohs() and ntohl().

10983. By Paul J. Lucas

Removed all ntohl() -- not really necessary.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

Actually, I just removed all the problematic code so it should compile on Windows. Please try again.

lp:~zorba-coders/zorba/feature-uuid updated
10984. By Paul J. Lucas

get_version() now properly returns "unknown" when unknown.

Revision history for this message
Rodolfo Ochoa (rodolfo-ochoa) wrote :

Yep, that works!

review: Approve
lp:~zorba-coders/zorba/feature-uuid updated
10985. By Paul J. Lucas

Added comment.

Revision history for this message
Chris Hillery (ceejatec) wrote :

My only concern is that for non-Apple, non-Windows platforms, you add -luuid to the link line, but there's no CMake stuff to search for a uuid library or to ensure that such an option will compile/link successfully. Is it a guarantee that it will always be available, at least on Linux?

review: Needs Information
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

> My only concern is that for non-Apple, non-Windows platforms, you add -luuid
> to the link line, but there's no CMake stuff to search for a uuid library or
> to ensure that such an option will compile/link successfully. Is it a
> guarantee that it will always be available, at least on Linux?

Good catch. I don't suppose you could add the relevant CMake stuff to find libuuid?

lp:~zorba-coders/zorba/feature-uuid updated
10986. By Paul J. Lucas

Merge from trunk.

Revision history for this message
Chris Hillery (ceejatec) wrote :

> Good catch. I don't suppose you could add the relevant CMake stuff to find
> libuuid?

1. Are the things you compile/link against on Windows and Mac guaranteed to exist? (ie, do I only need to find libuuid on Linux platforms)

2. What should happen if I *don't* find it? Can the UUID code be made optional at compile time? Would that mean we need a ZORBA_WITH_UUID or something like that? Or, do we have a hard requirement on libuuid and the build should fail without it?

It does appear that on Ubuntu, at least, libuuid comes from the package "libuuid1", which is part of the "minimal" Task and is depended on by some pretty low-level packages such as util-linux and e2fsprogs. It's not clear to me whether it's at all possible to have a functional Linux/Ubuntu system without libuuid existing. So maybe it's OK to assume it will always be there.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

> 1. Are the things you compile/link against on Windows and Mac guaranteed to
> exist? (ie, do I only need to find libuuid on Linux platforms)

Yes.

> 2. What should happen if I *don't* find it? Can the UUID code be made optional
> at compile time? Would that mean we need a ZORBA_WITH_UUID or something like
> that? Or, do we have a hard requirement on libuuid and the build should fail
> without it?

I think it has to be the latter, but Matthias might know.

Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

> > 1. Are the things you compile/link against on Windows and Mac guaranteed to
> > exist? (ie, do I only need to find libuuid on Linux platforms)
>
> Yes.
>
> > 2. What should happen if I *don't* find it? Can the UUID code be made
> optional
> > at compile time? Would that mean we need a ZORBA_WITH_UUID or something like
> > that? Or, do we have a hard requirement on libuuid and the build should fail
> > without it?
>
> I think it has to be the latter, but Matthias might know.
I think it should be a hard requirement. UUID functionality comes with Windows and Mac. On Linux, the library should always be available. Also, as far as I understood, we already depended on libuuid before.

Revision history for this message
Chris Hillery (ceejatec) wrote :

> Also, as far as I understood, we already depended on libuuid before.

As far as I can tell, no, we didn't. But, if we should have a hard requirement, so be it. As I said, it looks like it's pretty much standard on Linux.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

> As far as I can tell, no, we didn't. But, if we should have a hard
> requirement, so be it. As I said, it looks like it's pretty much standard on
> Linux.

Chris: Can you therefore please add the CMake magic to test for libuuid when not on Mac/Windows? Thanks.

lp:~zorba-coders/zorba/feature-uuid updated
10987. By Chris Hillery

Make missing uuid.h on Linux a fatal error; check that -luuid works.

Revision history for this message
Chris Hillery (ceejatec) wrote :

Turns out this is less standard than I thought. On my standard Ubuntu installation, I cannot compile this branch without installing the "uuid-dev" Ubuntu package, which is where uuid/uuid.h comes from.

I have added a bit of CMake stuff to check that -luuid works if ZORBA_HAVE_UUID_H is true. I also added a clause to make CMake configuration fail is ZORBA_HAVE_UUID_H is false, and you're not on Apple or Win32.

However, I'm a bit uneasy about adding a new hard requirement like this. At the very least, if we decide that this way of doing things is correct, we need to update the Linux build documentation to list the UUID development headers as a build requirement, and mention the uuid-dev on the Ubuntu build page. I won't have time to do that this week due to NoSQL Now, so Paul, please take a crack at it (assuming we decide that it is going to be a new requirement).

review: Needs Fixing
lp:~zorba-coders/zorba/feature-uuid updated
10988. By Paul J. Lucas

Merge from trunk.

10989. By Paul J. Lucas

Added mention of libUUID to documentation.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

I added a mention to the build documentation. Approve now?

lp:~zorba-coders/zorba/feature-uuid updated
10990. By Paul J. Lucas

Merge from trunk.

Revision history for this message
Chris Hillery (ceejatec) wrote :

I still don't love the additional dependency, but I think it probably makes sense not to roll our own UUID libs, so I'll approve this. I wonder if the RQ machine will have it?...

review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job feature-uuid-2012-08-31T01-04-21.504Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CMakeCompiler.txt'
--- CMakeCompiler.txt 2012-08-30 13:45:43 +0000
+++ CMakeCompiler.txt 2012-08-30 23:54:18 +0000
@@ -241,3 +241,24 @@
241 ENDIF (NOT CMAKE_C_FLAGS MATCHES "-fvisibility")241 ENDIF (NOT CMAKE_C_FLAGS MATCHES "-fvisibility")
242 ENDIF ( ZORBA_GNUCOMPILER_MAJOR_VERSION GREATER 3 )242 ENDIF ( ZORBA_GNUCOMPILER_MAJOR_VERSION GREATER 3 )
243ENDIF(CMAKE_COMPILER_IS_GNUCC)243ENDIF(CMAKE_COMPILER_IS_GNUCC)
244
245IF (APPLE)
246 # Needed for CFUUID*() functions in util/uuid.c.
247 SET(CMAKE_SHARED_LINKER_FLAGS "-framework CoreFoundation ${CMAKE_SHARED_LINKER_FLAGS}")
248ELSEIF (WIN32)
249 # Needed for UuidCreateSequential() function in util/uuid.c.
250 SET(requiredlibs ${requiredlibs} "Rpcrt4")
251ELSEIF (ZORBA_HAVE_UUID_H)
252 # Needed for uuid_generate() function in util/uuid.c.
253 SET(CMAKE_SHARED_LINKER_FLAGS "-luuid ${CMAKE_SHARED_LINKER_FLAGS}")
254 # Double-check that we can link against the required library.
255 CHECK_LIBRARY_EXISTS(uuid uuid_generate "" LIBUUID_EXISTS)
256 IF (NOT LIBUUID_EXISTS)
257 MESSAGE (FATAL_ERROR "Could not find libuuid - required on Linux!")
258 ENDIF (NOT LIBUUID_EXISTS)
259ELSE (APPLE)
260 # We're not Apple or Win32, and we didn't find uuid.h - can't compile
261 MESSAGE (FATAL_ERROR "Could not find uuid/uuid.h header, which is "
262 "required on Linux. On Ubuntu, the 'uuid-dev' package is required; "
263 "look for similar packages on other platforms.")
264ENDIF (APPLE)
244265
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2012-08-30 13:45:43 +0000
+++ CMakeLists.txt 2012-08-30 23:54:18 +0000
@@ -101,6 +101,7 @@
101 CHECK_INCLUDE_FILES ("execinfo.h" ZORBA_HAVE_EXECINFO_H)101 CHECK_INCLUDE_FILES ("execinfo.h" ZORBA_HAVE_EXECINFO_H)
102ENDIF (NOT APPLE OR ${CMAKE_SYSTEM_VERSION} VERSION_GREATER "10.4")102ENDIF (NOT APPLE OR ${CMAKE_SYSTEM_VERSION} VERSION_GREATER "10.4")
103CHECK_INCLUDE_FILE_CXX ("FlexLexer.h" ZORBA_HAVE_FLEXLEXER_H)103CHECK_INCLUDE_FILE_CXX ("FlexLexer.h" ZORBA_HAVE_FLEXLEXER_H)
104CHECK_INCLUDE_FILES ("uuid/uuid.h" ZORBA_HAVE_UUID_H)
104105
105CHECK_FUNCTION_EXISTS (strtof ZORBA_HAVE_STRTOF_FUNCTION)106CHECK_FUNCTION_EXISTS (strtof ZORBA_HAVE_STRTOF_FUNCTION)
106CHECK_FUNCTION_EXISTS (strtoll ZORBA_HAVE_STRTOLL_FUNCTION)107CHECK_FUNCTION_EXISTS (strtoll ZORBA_HAVE_STRTOLL_FUNCTION)
107108
=== modified file 'ChangeLog'
--- ChangeLog 2012-08-30 15:14:16 +0000
+++ ChangeLog 2012-08-30 23:54:18 +0000
@@ -16,6 +16,7 @@
16 * Fixed bugs #899364 and 899363 (throw XQST0103 in case of non-distinct window16 * Fixed bugs #899364 and 899363 (throw XQST0103 in case of non-distinct window
17 variables)17 variables)
18 * Fixed bug #899366 (enforce the type declaration of a window variable)18 * Fixed bug #899366 (enforce the type declaration of a window variable)
19 * Now using native UUID libraries.
19 * Fixed bug #1024892 (index declaration references udf declared after the index)20 * Fixed bug #1024892 (index declaration references udf declared after the index)
20 * Fixed bug #1038410 (Memory leaks in parser, trace iterator, and general index)21 * Fixed bug #1038410 (Memory leaks in parser, trace iterator, and general index)
21 * Fixed bug #1039488 (inserting more than one pair at once in a JSON object)22 * Fixed bug #1039488 (inserting more than one pair at once in a JSON object)
2223
=== modified file 'doc/zorba/build_prerequisites.dox'
--- doc/zorba/build_prerequisites.dox 2012-05-02 10:40:42 +0000
+++ doc/zorba/build_prerequisites.dox 2012-08-30 23:54:18 +0000
@@ -33,6 +33,7 @@
3333
34- Libxml2 2.2.16 or later (http://xmlsoft.org/)34- Libxml2 2.2.16 or later (http://xmlsoft.org/)
35- Iconv 1.12 (used by Libxml2; http://www.gnu.org/software/libiconv/)35- Iconv 1.12 (used by Libxml2; http://www.gnu.org/software/libiconv/)
36- libUUID (non-Mac, non-Windows only; http://linux.die.net/man/3/libuuid)
3637
37\subsection recommended_core_zorba Recommended for Core Zorba38\subsection recommended_core_zorba Recommended for Core Zorba
3839
3940
=== modified file 'doc/zorba/build_ubuntu.dox'
--- doc/zorba/build_ubuntu.dox 2012-08-30 13:45:43 +0000
+++ doc/zorba/build_ubuntu.dox 2012-08-30 23:54:18 +0000
@@ -24,7 +24,7 @@
24\subsection ubuntu_libs Core Zorba Library Dependencies24\subsection ubuntu_libs Core Zorba Library Dependencies
25Required libraries for core Zorba:25Required libraries for core Zorba:
26\code26\code
27sudo apt-get install libxml2-dev27sudo apt-get install libxml2-dev uuid-dev
28\endcode28\endcode
2929
30Recommended libraries for core Zorba:30Recommended libraries for core Zorba:
3131
=== modified file 'include/zorba/config.h.cmake'
--- include/zorba/config.h.cmake 2012-08-30 13:45:43 +0000
+++ include/zorba/config.h.cmake 2012-08-30 23:54:18 +0000
@@ -52,6 +52,7 @@
52#cmakedefine ZORBA_HAVE_SYS_TYPES_H52#cmakedefine ZORBA_HAVE_SYS_TYPES_H
53#cmakedefine ZORBA_HAVE_USTRING_H53#cmakedefine ZORBA_HAVE_USTRING_H
54#cmakedefine ZORBA_HAVE_UTYPES_H54#cmakedefine ZORBA_HAVE_UTYPES_H
55#cmakedefine ZORBA_HAVE_UUID_H
5556
56// Platform functions57// Platform functions
57#cmakedefine ZORBA_HAVE_CLOCKGETTIME_FUNCTION58#cmakedefine ZORBA_HAVE_CLOCKGETTIME_FUNCTION
5859
=== modified file 'src/runtime/random/random_impl.cpp'
--- src/runtime/random/random_impl.cpp 2012-08-30 13:45:43 +0000
+++ src/runtime/random/random_impl.cpp 2012-08-30 23:54:18 +0000
@@ -15,6 +15,8 @@
15 */15 */
16#include "stdafx.h"16#include "stdafx.h"
1717
18#include <sstream>
19
18#include "system/globalenv.h"20#include "system/globalenv.h"
1921
20#include "runtime/random/random.h"22#include "runtime/random/random.h"
@@ -27,7 +29,7 @@
27#include <zorba/util/time.h>29#include <zorba/util/time.h>
28#include <limits>30#include <limits>
2931
30#include "util/uuid/uuid.h"32#include "util/uuid.h"
3133
32namespace zorba {34namespace zorba {
3335
@@ -104,6 +106,7 @@
104 store::Item_t num;106 store::Item_t num;
105 unsigned int int_seed;107 unsigned int int_seed;
106 long walltime_millis;108 long walltime_millis;
109 uint32_t time_low;
107 zstring ltmp;110 zstring ltmp;
108111
109 RandomIteratorState* state;112 RandomIteratorState* state;
@@ -125,11 +128,16 @@
125 time::get_current_walltime( lCurrWallTime );128 time::get_current_walltime( lCurrWallTime );
126 walltime_millis = time::get_walltime_in_millis( lCurrWallTime );129 walltime_millis = time::get_walltime_in_millis( lCurrWallTime );
127130
128 uuid_t u;131 uuid u;
129 memset(&u, 0, sizeof(uuid_t));132 uuid::create(&u);
130 uuid_create(&u);133 //
131134 // Note that the time_low field as extracted here is always big-endian even
132 walltime_millis += u.time_low;135 // on a little-endian CPU, but it doesn't really matter since it's only
136 // being used as a fancy random-number generator seed.
137 //
138 time_low =
139 (u.data[0] << 24) | (u.data[1] << 16) | (u.data[2] << 8) | u.data[3];
140 walltime_millis += time_low;
133141
134 int_seed = walltime_millis % std::numeric_limits<unsigned int>::max();142 int_seed = walltime_millis % std::numeric_limits<unsigned int>::max();
135143
@@ -151,16 +159,16 @@
151UuidIterator::nextImpl(store::Item_t& result, PlanState& planState) const159UuidIterator::nextImpl(store::Item_t& result, PlanState& planState) const
152{160{
153 store::Item_t item;161 store::Item_t item;
154 uuid_t u;162 uuid u;
155 zstring uuidStr;163 zstring uuidStr;
164 std::ostringstream oss;
156165
157 PlanIteratorState* state;166 PlanIteratorState* state;
158 DEFAULT_STACK_INIT(PlanIteratorState, state, planState);167 DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
159168
160 memset(&u, 0, sizeof(uuid_t));169 uuid::create(&u);
161170 oss << u;
162 uuid_create(&u);171 uuidStr = oss.str();
163 uuidStr = uuidToString(u);
164172
165 GENV_ITEMFACTORY->createString(result, uuidStr);173 GENV_ITEMFACTORY->createString(result, uuidStr);
166 STACK_PUSH(true, state);174 STACK_PUSH(true, state);
167175
=== modified file 'src/store/naive/simple_store.cpp'
--- src/store/naive/simple_store.cpp 2012-08-30 13:45:43 +0000
+++ src/store/naive/simple_store.cpp 2012-08-30 23:54:18 +0000
@@ -15,6 +15,8 @@
15 */15 */
16#include "stdafx.h"16#include "stdafx.h"
1717
18#include <sstream>
19
18#include "simple_store.h"20#include "simple_store.h"
19#include "store_defs.h"21#include "store_defs.h"
2022
@@ -30,7 +32,7 @@
30#include "diagnostics/diagnostic.h"32#include "diagnostics/diagnostic.h"
31#include <zorba/diagnostic_list.h>33#include <zorba/diagnostic_list.h>
3234
33#include "util/uuid/uuid.h"35#include "util/uuid.h"
34#include "zorbautils/string_util.h"36#include "zorbautils/string_util.h"
3537
36#ifdef ZORBA_WITH_JSON38#ifdef ZORBA_WITH_JSON
@@ -288,9 +290,11 @@
288 return theItemFactory->createAnyURI(result, id);290 return theItemFactory->createAnyURI(result, id);
289 }291 }
290292
291 uuid_t uuid;293 uuid u;
292 uuid_create(&uuid);294 uuid::create(&u);
293 zstring uuidStr = uuidToURI(uuid);295 std::ostringstream oss;
296 oss << "urn:uuid:" << u;
297 zstring uuidStr = oss.str();
294298
295 assignReference(xmlNode, uuidStr);299 assignReference(xmlNode, uuidStr);
296300
297301
=== modified file 'src/store/naive/store.cpp'
--- src/store/naive/store.cpp 2012-08-30 13:45:43 +0000
+++ src/store/naive/store.cpp 2012-08-30 23:54:18 +0000
@@ -58,7 +58,6 @@
58#include "tree_id_generator.h"58#include "tree_id_generator.h"
5959
60#include "util/cxx_util.h"60#include "util/cxx_util.h"
61#include "util/uuid/uuid.h"
62#include "zorbautils/string_util.h"61#include "zorbautils/string_util.h"
6362
64#ifndef ZORBA_NO_FULL_TEXT63#ifndef ZORBA_NO_FULL_TEXT
6564
=== modified file 'src/unit_tests/CMakeLists.txt'
--- src/unit_tests/CMakeLists.txt 2012-08-30 13:45:43 +0000
+++ src/unit_tests/CMakeLists.txt 2012-08-30 23:54:18 +0000
@@ -20,6 +20,7 @@
20 test_json_parser.cpp20 test_json_parser.cpp
21 test_string.cpp21 test_string.cpp
22 test_uri.cpp22 test_uri.cpp
23 test_uuid.cpp
23 unit_tests.cpp24 unit_tests.cpp
24 test_uri.cpp25 test_uri.cpp
25 test_json_parser.cpp26 test_json_parser.cpp
2627
=== added file 'src/unit_tests/test_uuid.cpp'
--- src/unit_tests/test_uuid.cpp 1970-01-01 00:00:00 +0000
+++ src/unit_tests/test_uuid.cpp 2012-08-30 23:54:18 +0000
@@ -0,0 +1,42 @@
1/*
2 * Copyright 2006-2010 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "stdafx.h"
18#include <iostream>
19
20#include "util/uuid.h"
21
22using namespace std;
23using namespace zorba;
24
25///////////////////////////////////////////////////////////////////////////////
26
27///////////////////////////////////////////////////////////////////////////////
28
29namespace zorba {
30namespace UnitTests {
31
32int test_uuid( int argc, char *argv[] ) {
33 uuid u;
34 uuid::create( &u );
35 cout << u << endl;
36 return 0;
37}
38
39} // namespace UnitTests
40} // namespace zorba
41
42/* vim:set et sw=2 ts=2: */
043
=== modified file 'src/unit_tests/unit_test_list.h'
--- src/unit_tests/unit_test_list.h 2012-08-30 13:45:43 +0000
+++ src/unit_tests/unit_test_list.h 2012-08-30 23:54:18 +0000
@@ -45,6 +45,7 @@
45#ifndef ZORBA_HAVE_UNIQUE_PTR45#ifndef ZORBA_HAVE_UNIQUE_PTR
46 int test_unique_ptr( int, char*[] );46 int test_unique_ptr( int, char*[] );
47#endif /* ZORBA_HAVE_UNIQUE_PTR */47#endif /* ZORBA_HAVE_UNIQUE_PTR */
48 int test_uuid( int, char*[] );
48#ifndef ZORBA_HAVE_UNORDERED_MAP49#ifndef ZORBA_HAVE_UNORDERED_MAP
49 int test_unordered_map( int, char*[] );50 int test_unordered_map( int, char*[] );
50#endif /* ZORBA_HAVE_UNORDERED_MAP */51#endif /* ZORBA_HAVE_UNORDERED_MAP */
5152
=== modified file 'src/unit_tests/unit_tests.cpp'
--- src/unit_tests/unit_tests.cpp 2012-08-30 13:45:43 +0000
+++ src/unit_tests/unit_tests.cpp 2012-08-30 23:54:18 +0000
@@ -54,6 +54,7 @@
54#ifndef ZORBA_HAVE_UNIQUE_PTR54#ifndef ZORBA_HAVE_UNIQUE_PTR
55 libunittests["unique_ptr"] = test_unique_ptr;55 libunittests["unique_ptr"] = test_unique_ptr;
56#endif /* ZORBA_HAVE_UNIQUE_PTR */56#endif /* ZORBA_HAVE_UNIQUE_PTR */
57 libunittests["uuid"] = test_uuid;
57#ifndef ZORBA_HAVE_UNORDERED_MAP58#ifndef ZORBA_HAVE_UNORDERED_MAP
58 libunittests["unordered_map"] = test_unordered_map;59 libunittests["unordered_map"] = test_unordered_map;
59#endif /* ZORBA_HAVE_UNORDERED_MAP */60#endif /* ZORBA_HAVE_UNORDERED_MAP */
6061
=== modified file 'src/util/CMakeLists.txt'
--- src/util/CMakeLists.txt 2012-08-30 13:45:43 +0000
+++ src/util/CMakeLists.txt 2012-08-30 23:54:18 +0000
@@ -29,11 +29,10 @@
29 unicode_categories.cpp29 unicode_categories.cpp
30 uri_util.cpp30 uri_util.cpp
31 utf8_util.cpp31 utf8_util.cpp
32 uuid.cpp
32 xml_util.cpp33 xml_util.cpp
33 fx/fxcharheap.cpp34 fx/fxcharheap.cpp
34 string/empty_rep_base.cpp35 string/empty_rep_base.cpp
35 uuid/sysdep.cpp
36 uuid/uuid.cpp
37 http_util.h36 http_util.h
38 http_util.cpp)37 http_util.cpp)
3938
4039
=== removed directory 'src/util/uuid'
=== added file 'src/util/uuid.cpp'
--- src/util/uuid.cpp 1970-01-01 00:00:00 +0000
+++ src/util/uuid.cpp 2012-08-30 23:54:18 +0000
@@ -0,0 +1,102 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <zorba/config.h>
18
19#include <cstdio> /* for sprintf(3) */
20
21#if defined( __APPLE__ )
22# include <cstring> /* for memcpy(3) */
23# include <CoreFoundation/CoreFoundation.h>
24#elif defined( ZORBA_HAVE_UUID_H )
25# include <uuid/uuid.h>
26#elif defined( _WIN32 )
27# include <Rpc.h>
28#else
29# error "Unsupported operating system for generating UUIDs"
30#endif
31
32#include "uuid.h"
33
34using namespace std;
35
36namespace zorba {
37
38///////////////////////////////////////////////////////////////////////////////
39
40void uuid::create( uuid *result ) {
41#if defined( __APPLE__ )
42 CFUUIDRef uuid_ref = CFUUIDCreate( NULL );
43 CFUUIDBytes uuid_bytes = CFUUIDGetUUIDBytes( uuid_ref );
44 CFRelease( uuid_ref );
45 ::memcpy( result->data, &uuid_bytes, sizeof result->data );
46#elif defined( ZORBA_HAVE_UUID_H )
47 uuid_generate( result->data );
48#elif defined( _WIN32 )
49 UuidCreateSequential( (UUID*)result->data );
50#endif /* _WIN32 */
51}
52
53uuid::variant uuid::get_variant() const {
54 value_type const octet8 = data[8];
55 if ( (octet8 & 0x80u) == 0x00u )
56 return ncs;
57 if ( (octet8 & 0xC0u) == 0x80u )
58 return rfc4122;
59 if ( (octet8 & 0xE0u) == 0xC0u )
60 return microsoft;
61 return future;
62}
63
64uuid::version uuid::get_version() const {
65 value_type const temp = data[6] & 0xF0u;
66 switch ( temp ) {
67 case time_based:
68 case dce_security:
69 case name_based_md5:
70 case random_number_based:
71 case name_based_sha1:
72 return static_cast<version>( temp );
73 default:
74 return unknown;
75 }
76}
77
78///////////////////////////////////////////////////////////////////////////////
79
80ostream& operator<<( ostream &os, uuid const &u ) {
81 char buf[37];
82 sprintf( // verbose & tedious, but fast
83 buf,
84 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
85 u.data[ 0], u.data[ 1], u.data[ 2], u.data[ 3],
86 u.data[ 4], u.data[ 5], u.data[ 6], u.data[ 7],
87 u.data[ 8], u.data[ 9], u.data[10], u.data[11],
88 u.data[12], u.data[13], u.data[14], u.data[15]
89 );
90 return os << buf;
91}
92
93///////////////////////////////////////////////////////////////////////////////
94
95} // namespace zorba
96
97/*
98 * Local variables:
99 * mode: c++
100 * End:
101 */
102/* vim:set et sw=2 ts=2: */
0103
=== added file 'src/util/uuid.h'
--- src/util/uuid.h 1970-01-01 00:00:00 +0000
+++ src/util/uuid.h 2012-08-30 23:54:18 +0000
@@ -0,0 +1,257 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#ifndef ZORBA_UUID_H
19#define ZORBA_UUID_H
20
21#include <algorithm>
22#include <iostream>
23
24#include "zorbamisc/config/stdint.h"
25
26namespace zorba {
27
28///////////////////////////////////////////////////////////////////////////////
29
30/**
31 * A %uuid contains the raw bytes for a UUID. Note that this is intentially a
32 * \c struct with no constructors, no destructor, and no user-defined
33 * assignment operators so that it remains a POD.
34 *
35 * See also:
36 * - RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace.
37 * - http://en.wikipedia.org/wiki/Universally_unique_identifier
38 *
39 * The UUID data is stored as an array of 16 8-bit bytes comprising the 128
40 * bits that is a UUID. The data is intentionally \e not stored as shown in
41 * "Appendix A - Sample Implementation" of RFC 4122 (the \c struct with
42 * individial fields such as \c time_low, \c time_mid, etc.) since C++ does not
43 * guarantee that the \c struct fields will be laid out in memory as shown in
44 * section "4.1.2. Layout and Byte Order" due to possible padding.
45 */
46struct uuid {
47 typedef uint8_t value_type;
48 typedef value_type& reference;
49 typedef value_type const& const_reference;
50 typedef value_type* pointer;
51 typedef value_type const* const_pointer;
52 typedef std::size_t size_type;
53 typedef std::ptrdiff_t difference_type;
54
55 typedef pointer iterator;
56 typedef const_pointer const_iterator;
57
58 enum variant {
59 ncs, ///< NCS backward compatibility
60 rfc4122, ///< RFC 4122
61 microsoft, ///< Microsoft compatibility
62 future ///< Reserved for future use
63 };
64
65 enum version {
66 unknown,
67 time_based = 0x10,
68 dce_security = 0x20,
69 name_based_md5 = 0x30,
70 random_number_based = 0x40,
71 name_based_sha1 = 0x50
72 };
73
74 /**
75 * The raw UUID data.
76 */
77 value_type data[16];
78
79 /**
80 * Creates a UUID. The variant and version of the UUID created is
81 * platform-dependent.
82 *
83 * @param result A pointer to the result.
84 */
85 static void create( uuid *result );
86
87 /**
88 * Creates an iterator to the beginning of the data.
89 *
90 * @return Returns said iterator.
91 */
92 iterator begin() {
93 return data;
94 }
95
96 /**
97 * Creates a const_iterator to the beginning of the data.
98 *
99 * @return Returns said iterator.
100 */
101 const_iterator begin() const {
102 return data;
103 }
104
105 /**
106 * Creates an iterator to one past the end of the data.
107 *
108 * @return Returns said iterator.
109 */
110 iterator end() {
111 return data + size();
112 }
113
114 /**
115 * Creates a const_iterator to one past the end of the data.
116 *
117 * @return Returns said iterator.
118 */
119 const_iterator end() const {
120 return data + size();
121 }
122
123 /**
124 * Gets the size of the UUID data.
125 *
126 * @return Always returns 16.
127 */
128 size_type size() const {
129 return sizeof( data );
130 }
131
132 /**
133 * Swaps this UUID's data with that of another.
134 *
135 * @param that The other UUID to swap data with.
136 */
137 void swap( uuid &that ) {
138 std::swap_ranges( begin(), end(), that.begin() );
139 }
140
141 /**
142 * Gets the variant of this UUID.
143 *
144 * @return Returns said variant.
145 */
146 variant get_variant() const;
147
148 /**
149 * Gets the version of this UUID.
150 *
151 * @return Returns said version.
152 */
153 version get_version() const;
154};
155
156////////// Functions //////////////////////////////////////////////////////////
157
158/**
159 * Swaps two UUIDs' data.
160 *
161 * @param u1 The first UUID.
162 * @param u2 The second UUID.
163 */
164inline void swap( uuid &u1, uuid &u2 ) {
165 u1.swap( u2 );
166}
167
168/**
169 * Compares two UUIDs for equality.
170 *
171 * @param u1 The first UUID.
172 * @param u2 The second UUID.
173 * @return Returns \c true only if the two UUIDs are equal.
174 */
175inline bool operator==( uuid const &u1, uuid const &u2 ) {
176 return std::equal( u1.begin(), u1.end(), u2.begin() );
177}
178
179/**
180 * Compares two UUIDs for inequality.
181 *
182 * @param u1 The first UUID.
183 * @param u2 The second UUID.
184 * @return Returns \c true only if the two UUIDs are not equal.
185 */
186inline bool operator!=( uuid const &u1, uuid const &u2 ) {
187 return !(u1 == u2);
188}
189
190/**
191 * Compares two UUIDs for less-than.
192 *
193 * @param u1 The first UUID.
194 * @param u2 The second UUID.
195 * @return Returns \c true only if the first UUID is less than the second.
196 */
197inline bool operator<( uuid const &u1, uuid const &u2 ) {
198 return std::lexicographical_compare(
199 u1.begin(), u1.end(), u2.begin(), u2.end()
200 );
201}
202
203/**
204 * Compares two UUIDs for less-than-or-equal-to.
205 *
206 * @param u1 The first UUID.
207 * @param u2 The second UUID.
208 * @return Returns \c true only if the first UUID is less than or equal to the
209 * second.
210 */
211inline bool operator<=( uuid const &u1, uuid const &u2 ) {
212 return !(u2 < u1);
213}
214
215/**
216 * Compares two UUIDs for greater-than.
217 *
218 * @param u1 The first UUID.
219 * @param u2 The second UUID.
220 * @return Returns \c true only if the first UUID is greater than the second.
221 */
222inline bool operator>( uuid const &u1, uuid const &u2 ) {
223 return u2 < u1;
224}
225
226/**
227 * Compares two UUIDs for greater-than-or-equal-to.
228 *
229 * @param u1 The first UUID.
230 * @param u2 The second UUID.
231 * @return Returns \c true only if the first UUID is greater than or equal to
232 * the second.
233 */
234inline bool operator>=( uuid const &u1, uuid const &u2 ) {
235 return !(u1 < u2);
236}
237
238/**
239 * Emits the given UUID to the given ostream in canonical UUID format.
240 *
241 * @param os The ostream to emit to.
242 * @param u The UUID to emit.
243 * @return Returns \a os.
244 */
245std::ostream& operator<<( std::ostream &os, uuid const &u );
246
247///////////////////////////////////////////////////////////////////////////////
248
249} // namespace zorba
250
251#endif /* ZORBA_UUID_H */
252/*
253 * Local variables:
254 * mode: c++
255 * End:
256 */
257/* vim:set et sw=2 ts=2: */
0258
=== removed file 'src/util/uuid/sysdep.cpp'
--- src/util/uuid/sysdep.cpp 2012-08-30 13:45:43 +0000
+++ src/util/uuid/sysdep.cpp 1970-01-01 00:00:00 +0000
@@ -1,59 +0,0 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "stdafx.h"
17
18#include "sysdep.h"
19
20namespace zorba {
21/* system dependent call to get the current system time. Returned as
22 100ns ticks since UUID epoch, but resolution may be less than
23 100ns. */
24#ifdef WIN32
25
26void get_system_time(xs_unsignedLong *uuid_time)
27{
28 ULARGE_INTEGER time;
29
30 /* NT keeps time in FILETIME format which is 100ns ticks since
31 Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
32 The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
33 + 18 years and 5 leap days, meaning P6653D*/
34 GetSystemTimeAsFileTime((FILETIME *)&time);
35 time.QuadPart += (xs_unsignedLong) (1000*1000*10) // seconds
36 * (xs_unsignedLong) (60 * 60 * 24) // days
37 * (xs_unsignedLong) (17+30+31+365*18+5); // 6653 of days
38 *uuid_time = time.QuadPart;
39}
40
41#else
42
43void get_system_time(xs_unsignedLong *uuid_time)
44{
45 struct timeval tp;
46
47 gettimeofday(&tp, (struct timezone *)0);
48
49 /* Offset between UUID formatted times and Unix formatted times.
50 UUID UTC base time is October 15, 1582.
51 Unix base time is January 1, 1970, meaning P141427D*/
52 *uuid_time = ((xs_unsignedLong)tp.tv_sec * 10000000)
53 + ((xs_unsignedLong)tp.tv_usec * 10)
54 + I64(0x01B21DD213814000);
55}
56
57#endif
58}/*namespace zorba*/
59/* vim:set et sw=2 ts=2: */
600
=== removed file 'src/util/uuid/sysdep.h'
--- src/util/uuid/sysdep.h 2012-08-30 13:45:43 +0000
+++ src/util/uuid/sysdep.h 1970-01-01 00:00:00 +0000
@@ -1,57 +0,0 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ZORBA_UTIL_UUID_SYSDEP_H
18#define ZORBA_UTIL_UUID_SYSDEP_H
19
20#include "zorbatypes/schema_types.h"
21
22#ifdef WIN32
23 #include <windows.h>
24#else
25 #include <sys/types.h>
26 #include <sys/time.h>
27#endif
28
29/* Set the following to a calls to get and release a global lock */
30#define LOCK
31#define UNLOCK
32
33// /* Set this to what your compiler uses for 64-bit data type */
34#ifdef WIN32
35 #define unsigned64_t unsigned __int64
36 #define I64(C) C
37#else
38 #define unsigned64_t unsigned long long
39 #define I64(C) C##LL
40#endif
41
42namespace zorba {
43
44/* set the following to the number of 100ns ticks of the actual
45resolution of your system's clock */
46#define UUIDS_PER_TICK 1024
47
48typedef struct {
49 char nodeID[6];
50} uuid_node_t;
51
52void get_system_time(xs_unsignedLong *uuid_time);
53
54}/*namespace zorba*/
55
56#endif /* ZORBA_UTIL_UUID_SYSDEP_H */
57/* vim:set et sw=2 ts=2: */
580
=== removed file 'src/util/uuid/uuid.cpp'
--- src/util/uuid/uuid.cpp 2012-08-30 13:45:43 +0000
+++ src/util/uuid/uuid.cpp 1970-01-01 00:00:00 +0000
@@ -1,297 +0,0 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "stdafx.h"
17
18#include <string.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22#include "util/uuid/sysdep.h"
23#include "util/uuid/uuid.h"
24#include "util/fs_util.h"
25
26namespace zorba {
27
28/* various forward declarations */
29static int read_state(xs_short *clockseq, xs_unsignedLong *timestamp,
30 uuid_node_t *node);
31static void write_state(xs_short clockseq, xs_unsignedLong timestamp,
32 uuid_node_t node);
33static void format_uuid_v1(uuid_t *uuid, xs_short clockseq,
34 xs_unsignedLong timestamp, uuid_node_t node);
35static void get_current_time(xs_unsignedLong *timestamp);
36static xs_short true_random(void);
37
38int uuid_create(uuid_t *uuid)
39{
40 xs_unsignedLong timestamp, last_time;
41 xs_short clockseq;
42 uuid_node_t node;
43 memset(&node, 0, sizeof(uuid_node_t));
44 uuid_node_t last_node;
45 memset(&last_node, 0, sizeof(uuid_node_t));
46 int f;
47
48 //acquire system-wide lock so we're alone
49 LOCK;
50 //get time, node ID, saved state from non-volatile storage
51 get_current_time(&timestamp);
52// get_ieee_node_identifier(&node);
53 f = read_state(&clockseq, &last_time, &last_node);
54
55 //if no NV state, or if clock went backwards, or node ID
56 //changed (e.g., new network card) change clockseq
57 if (!f || memcmp(&node, &last_node, sizeof node))
58 clockseq = true_random();
59 else if (timestamp < last_time)
60 clockseq++;
61
62 //save the state for next time
63 write_state(clockseq, timestamp, node);
64
65 UNLOCK;
66
67 //stuff fields into the UUID
68 format_uuid_v1(uuid, clockseq, timestamp, node);
69 return 1;
70}
71
72/* format_uuid_v1 -- make a UUID from the timestamp, clockseq,
73 and node ID */
74void format_uuid_v1(uuid_t* uuid, xs_short clock_seq,
75 xs_unsignedLong timestamp, uuid_node_t node)
76{
77 //Construct a version 1 uuid with the information we've gathered plus a few constants.
78 uuid->time_low = (xs_unsignedInt)(timestamp & 0xFFFFFFFF);
79 uuid->time_mid = (xs_unsignedShort)((timestamp >> 32) & 0xFFFF);
80 uuid->time_hi_and_version =(xs_unsignedShort)((timestamp >> 48) & 0x0FFF);
81 uuid->time_hi_and_version |= (1 << 12);
82 uuid->clock_seq_low = clock_seq & 0xFF;
83 uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
84 uuid->clock_seq_hi_and_reserved |= 0x80;
85 memcpy(&uuid->node, &node, sizeof uuid->node);
86}
87
88/* data type for UUID generator persistent state */
89typedef struct {
90 xs_unsignedLong ts; // saved timestamp
91 uuid_node_t node; // saved node ID
92 xs_short cs; // saved clock sequence
93} uuid_state;
94
95static uuid_state st;
96
97zstring get_tmp_state_name()
98{
99 zstring lTmpFileName;
100#ifdef ZORBA_WITH_FILE_ACCESS
101 fs::get_temp_file<zstring>(&lTmpFileName);
102 lTmpFileName = lTmpFileName.substr(0, lTmpFileName.find_last_of(fs::dir_separator) + 1);
103#endif
104 return lTmpFileName += "state";
105}
106
107//read_state -- read UUID generator state from non-volatile store
108int read_state(xs_short *clockseq, xs_unsignedLong *timestamp,
109 uuid_node_t *node)
110{
111 static int inited = 0;
112 FILE *fp = 0;
113
114 //only need to read state once per boot
115 if (!inited) {
116#ifdef WIN32
117 errno_t err = fopen_s( &fp, get_tmp_state_name().c_str(), "rb");
118 if ( err != 0)
119 return 0;
120#else
121 fp = fopen(get_tmp_state_name().c_str(), "rb");
122 if (fp == NULL)
123 return 0;
124#endif
125
126 size_t lReadLength = fread(&st, sizeof st, 1, fp);
127 fclose(fp);
128 if (lReadLength == 0)
129 return 0;
130 inited = 1;
131 }
132 *clockseq = st.cs;
133 *timestamp = st.ts;
134 *node = st.node;
135 return 1;
136}
137
138// write_state -- save UUID generator state back to non-volatile storage
139void write_state(xs_short clockseq, xs_unsignedLong timestamp,
140 uuid_node_t node)
141{
142 volatile static bool inited = false;
143 static xs_unsignedLong next_save;
144 FILE* fp = 0;
145
146 if (!inited) {
147 next_save = timestamp;
148 inited = 1;
149 }
150
151 //always save state to volatile shared state
152 st.cs = clockseq;
153 st.ts = timestamp;
154 st.node = node;
155 if (timestamp >= next_save) {
156#ifdef WIN32
157 errno_t err = fopen_s( &fp, get_tmp_state_name().c_str(), "wb");
158 if ( err != 0)
159 return ;
160#else
161 fp = fopen(get_tmp_state_name().c_str(), "wb");
162 if (fp == NULL)
163 return ;
164#endif
165 fwrite(&st, sizeof st, 1, fp);
166 fclose(fp);
167 //schedule next save for 10 seconds from now
168 next_save = timestamp + (10 * 10 * 1000 * 1000);
169 }
170}
171
172// get-current_time -- get time as 60-bit 100ns ticks since UUID epoch.
173// Compensate for the fact that real clock resolution is less than 100ns.
174void get_current_time(xs_unsignedLong *timestamp)
175{
176 static int inited = 0;
177 static xs_unsignedLong time_last;
178 static xs_short uuids_this_tick;
179 xs_unsignedLong time_now;
180
181 if (!inited) {
182 get_system_time(&time_now);
183 uuids_this_tick = UUIDS_PER_TICK;
184 inited = 1;
185 }
186
187 for ( ; ; ) {
188 get_system_time(&time_now);
189
190 //if clock reading changed since last UUID generated
191 if (time_last != time_now) {
192 //reset count of uuids gen'd with this clock reading
193 uuids_this_tick = 0;
194 time_last = time_now;
195 break;
196 }
197 if (uuids_this_tick < UUIDS_PER_TICK) {
198 uuids_this_tick++;
199 break;
200 }
201 //going too fast for our clock; spin
202 }
203 // add the count of uuids to low order bits of the clock reading
204 *timestamp = time_now + uuids_this_tick;
205}
206
207// true_random -- this does not generate a crypto-quality random number.
208static xs_short true_random(void)
209{
210 static int inited = 0;
211 xs_unsignedLong time_now;
212
213 if (!inited) {
214 get_system_time(&time_now);
215 time_now = time_now / UUIDS_PER_TICK;
216 srand((unsigned int)
217 (((time_now >> 32) ^ time_now) & 0xffffffff));
218 inited = 1;
219 }
220
221 return rand();
222}
223
224uuid_t NameSpace_DNS = { // 6ba7b810-9dad-11d1-80b4-00c04fd430c8
225 0x6ba7b810,
226 0x9dad,
227 0x11d1,
228 0x80,
229 0xb4,
230 {
231 0x00, (unsigned char)0xc0,
232 0x4f, (unsigned char)0xd4,
233 0x30, (unsigned char)0xc8
234 }
235};
236
237/* uuidToString -- transform a UUID to a string*/
238zstring uuidToString(const uuid_t& u)
239{
240 char lBuffer[174];
241 sprintf(lBuffer, "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-9300a64ac3cd", u.time_low, u.time_mid,
242 u.time_hi_and_version, u.clock_seq_hi_and_reserved,
243 u.clock_seq_low);
244
245 zstring lResult = lBuffer;
246 return lResult;
247}
248
249/* uuidToString -- transform a UUID to a uri*/
250zstring uuidToURI(const uuid_t& u)
251{
252 char lBuffer[200];
253 sprintf(lBuffer, "urn:uuid:%8.8x-%4.4x-%4.4x-%2.2x%2.2x-9300a64ac3cd", u.time_low, u.time_mid,
254 u.time_hi_and_version, u.clock_seq_hi_and_reserved,
255 u.clock_seq_low);
256
257 zstring lResult = lBuffer;
258 return lResult;
259}
260
261/*
262//This appendix lists the name space IDs for some potentially
263//interesting namespaces, as initialized C structures and in the
264//string representation defined above.
265
266// Name string is a URL
267uuid_t NameSpace_URL = { // 6ba7b811-9dad-11d1-80b4-00c04fd430c8
268 0x6ba7b811,
269 0x9dad,
270 0x11d1,
271 0x80,
272 0xb4,
273 {0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
274};
275
276// Name string is an ISO OID
277uuid_t NameSpace_OID = { // 6ba7b812-9dad-11d1-80b4-00c04fd430c8
278 0x6ba7b812,
279 0x9dad,
280 0x11d1,
281 0x80,
282 0xb4,
283 {0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
284};
285
286// Name string is an X.500 DN (in DER or a text output format)
287uuid_t NameSpace_X500 = { // 6ba7b814-9dad-11d1-80b4-00c04fd430c8
288 0x6ba7b814,
289 0x9dad,
290 0x11d1,
291 0x80,
292 0xb4,
293 {0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
294};
295*/
296}
297/* vim:set et sw=2 ts=2: */
2980
=== removed file 'src/util/uuid/uuid.h'
--- src/util/uuid/uuid.h 2012-08-30 13:45:43 +0000
+++ src/util/uuid/uuid.h 1970-01-01 00:00:00 +0000
@@ -1,44 +0,0 @@
1/*
2 * Copyright 2006-2008 The FLWOR Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ZORBA_UTIL_UUID_H
18#define ZORBA_UTIL_UUID_H
19
20#undef uuid_t
21
22namespace zorba {
23
24typedef struct {
25 xs_unsignedInt time_low;
26 xs_unsignedShort time_mid;
27 xs_unsignedShort time_hi_and_version;
28 xs_unsignedByte clock_seq_hi_and_reserved;
29 xs_unsignedByte clock_seq_low;
30 xs_byte node[6];
31} uuid_t;
32
33/* uuid_create -- generate a UUID */
34int uuid_create(uuid_t * uuid);
35
36/* uuidToString -- transform a UUID to a string */
37zstring uuidToString(const uuid_t& uuid);
38
39/* uuidToURI -- transform a UUID to a URI-formatted string */
40zstring uuidToURI(const uuid_t& uuid);
41
42} // namespace zorba
43#endif /* ZORBA_UTIL_UUID_H */
44/* vim:set et sw=2 ts=2: */

Subscribers

People subscribed via source and target branches