From 6d5f70b6f435d3ddcbf06e3a675d3acfb8baa703 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Tue, 8 Nov 2016 10:25:35 +0100 Subject: [PATCH] Add a global DECODE_REV function in JS. --- arangod/V8Server/v8-vocbase.cpp | 49 +++++++++++++++++++++++++++++++++ lib/Basics/HybridLogicalClock.h | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/arangod/V8Server/v8-vocbase.cpp b/arangod/V8Server/v8-vocbase.cpp index 3e93ad715a..0ce210b89d 100644 --- a/arangod/V8Server/v8-vocbase.cpp +++ b/arangod/V8Server/v8-vocbase.cpp @@ -41,6 +41,7 @@ #include "Aql/QueryCache.h" #include "Aql/QueryList.h" #include "Aql/QueryRegistry.h" +#include "Basics/HybridLogicalClock.h" #include "Basics/MutexLocker.h" #include "Basics/ScopeGuard.h" #include "Basics/StaticStrings.h" @@ -2690,6 +2691,10 @@ void TRI_V8ReloadRouting(v8::Isolate* isolate) { } +//////////////////////////////////////////////////////////////////////////////// +/// @brief check if we are in the enterprise edition +//////////////////////////////////////////////////////////////////////////////// + static void JS_IsEnterprise(v8::FunctionCallbackInfo const& args) { TRI_V8_TRY_CATCH_BEGIN(isolate); v8::HandleScope scope(isolate); @@ -2701,6 +2706,45 @@ static void JS_IsEnterprise(v8::FunctionCallbackInfo const& args) { TRI_V8_TRY_CATCH_END } +//////////////////////////////////////////////////////////////////////////////// +/// @brief decode a _rev time stamp +//////////////////////////////////////////////////////////////////////////////// + +static void JS_DecodeRev(v8::FunctionCallbackInfo const& args) { + TRI_V8_TRY_CATCH_BEGIN(isolate); + v8::HandleScope scope(isolate); + + if (args.Length() != 1 || !args[0]->IsString()) { + TRI_V8_THROW_EXCEPTION_USAGE("DECODE_REV()"); + } + + std::string rev = TRI_ObjectToString(args[0]); + uint64_t revInt = HybridLogicalClock::decodeTimeStamp(rev); + uint64_t timeMilli = HybridLogicalClock::extractTime(revInt); + uint64_t count = HybridLogicalClock::extractCount(revInt); + + time_t timeSeconds = timeMilli / 1000; + uint64_t millis = timeMilli % 1000; + struct tm date; + gmtime_r(&timeSeconds, &date); + char buffer[32]; + strftime(buffer, 32, "%Y-%m-%dT%H:%M:%S.000Z", &date); + buffer[20] = (millis / 100) + '0'; + buffer[21] = ((millis / 10) % 10) + '0'; + buffer[22] = (millis % 10) + '0'; + buffer[23] = 0; + + v8::Handle result = v8::Object::New(isolate); + result->Set(TRI_V8_ASCII_STRING("date"), + TRI_V8_ASCII_STRING(buffer)); + result->Set(TRI_V8_ASCII_STRING("count"), + v8::Number::New(isolate, count)); + + TRI_V8_RETURN(result); + + TRI_V8_TRY_CATCH_END +} + //////////////////////////////////////////////////////////////////////////////// /// @brief creates a TRI_vocbase_t global context //////////////////////////////////////////////////////////////////////////////// @@ -2892,6 +2936,11 @@ void TRI_InitV8VocBridge(v8::Isolate* isolate, v8::Handle context, TRI_AddGlobalFunctionVocbase(isolate, context, TRI_V8_ASCII_STRING("SYS_IS_ENTERPRISE"), JS_IsEnterprise); + + TRI_AddGlobalFunctionVocbase(isolate, context, + TRI_V8_ASCII_STRING("DECODE_REV"), + JS_DecodeRev); + // ............................................................................. // create global variables // ............................................................................. diff --git a/lib/Basics/HybridLogicalClock.h b/lib/Basics/HybridLogicalClock.h index b11f93dff0..34bd90903f 100644 --- a/lib/Basics/HybridLogicalClock.h +++ b/lib/Basics/HybridLogicalClock.h @@ -154,7 +154,6 @@ class HybridLogicalClock { return ms; } - private: // helper to compute the offset between epoch and 1970 uint64_t computeOffset1970(); @@ -166,6 +165,7 @@ class HybridLogicalClock { return (time << 20) + count; } + private: static char encodeTable[65]; static signed char decodeTable[256];