zorba-coders team mailing list archive
-
zorba-coders team
-
Mailing list archive
-
Message #26946
[Merge] lp:~zorba-coders/zorba/feature-sleep_module into lp:zorba
Matthias Brantner has proposed merging lp:~zorba-coders/zorba/feature-sleep_module into lp:zorba.
Commit message:
initial version of sleep module
Requested reviews:
Matthias Brantner (matthias-brantner)
For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/feature-sleep_module/+merge/216475
initial version of sleep module
--
https://code.launchpad.net/~zorba-coders/zorba/feature-sleep_module/+merge/216475
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'modules/CMakeLists.txt'
--- modules/CMakeLists.txt 2014-01-03 17:31:44 +0000
+++ modules/CMakeLists.txt 2014-04-18 18:30:22 +0000
@@ -27,6 +27,7 @@
ADD_SUBDIRECTORY(schema)
ADD_SUBDIRECTORY(sctx)
ADD_SUBDIRECTORY(sequence)
+ADD_SUBDIRECTORY(sleep)
ADD_SUBDIRECTORY(store)
ADD_SUBDIRECTORY(structured-items)
ADD_SUBDIRECTORY(uri)
=== added directory 'modules/sleep'
=== added file 'modules/sleep/CMakeLists.txt'
--- modules/sleep/CMakeLists.txt 1970-01-01 00:00:00 +0000
+++ modules/sleep/CMakeLists.txt 2014-04-18 18:30:22 +0000
@@ -0,0 +1,21 @@
+# Copyright 2006-2014 The FLWOR Foundation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+DECLARE_ZORBA_MODULE(
+ FILE sleep.xq
+ URI "http://zorba.io/modules/sleep"
+ VERSION 1.0
+)
+
+# vim:set et sw=2 ts=2:
=== added file 'modules/sleep/sleep.xq'
--- modules/sleep/sleep.xq 1970-01-01 00:00:00 +0000
+++ modules/sleep/sleep.xq 2014-04-18 18:30:22 +0000
@@ -0,0 +1,41 @@
+xquery version "3.0";
+
+(:
+ : Copyright 2006-2013 The FLWOR Foundation.
+ :
+ : Licensed under the Apache License, Version 2.0 (the "License");
+ : you may not use this file except in compliance with the License.
+ : You may obtain a copy of the License at
+ :
+ : http://www.apache.org/licenses/LICENSE-2.0
+ :
+ : Unless required by applicable law or agreed to in writing, software
+ : distributed under the License is distributed on an "AS IS" BASIS,
+ : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ : See the License for the specific language governing permissions and
+ : limitations under the License.
+:)
+
+(:~
+ : This module provides a function to put the currently running query
+ : to sleep.
+ :
+ :)
+module namespace sleep = "http://zorba.io/modules/sleep";
+
+declare namespace an = "http://zorba.io/annotations";
+
+declare namespace ver = "http://zorba.io/options/versioning";
+declare option ver:module-version "1.0";
+
+(:~
+ : Puts the currently running query to sleep.
+ : This function is mainly useful in development e.g. to simulate the
+ : effects of long-running tasks wrt. the concurrent execution of
+ : requests.
+ :
+ : @param $millis the number of milliseconds to sleep
+ : @return the function is sequential and returns the empty sequence
+ :)
+declare %an:sequential function sleep:millis($millis as xs:integer)
+ as empty-sequence() external;
=== added directory 'modules/sleep/sleep.xq.src'
=== added file 'modules/sleep/sleep.xq.src/sleep.cpp'
--- modules/sleep/sleep.xq.src/sleep.cpp 1970-01-01 00:00:00 +0000
+++ modules/sleep/sleep.xq.src/sleep.cpp 2014-04-18 18:30:22 +0000
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2006-2014 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "sleep.h"
+
+#ifdef WIN32
+# include <windows.h>
+#else
+# include <time.h>
+#endif
+#include <stdlib.h>
+#include <limits>
+#include <zorba/item_factory.h>
+#include <zorba/empty_sequence.h>
+#include <zorba/user_exception.h>
+
+namespace zorba { namespace sleep {
+
+MillisFunction::~MillisFunction()
+{}
+
+zorba::ItemSequence_t
+MillisFunction::evaluate(
+ const Arguments_t& aArgs,
+ const zorba::StaticContext*,
+ const zorba::DynamicContext*) const
+{
+ // getIntegerValue is not available in Zorba
+ // => get the string, parse it, and check the limits
+ Item lRequestItem;
+ Iterator_t arg0_iter = aArgs[0]->getIterator();
+ arg0_iter->open();
+ arg0_iter->next(lRequestItem);
+ arg0_iter->close();
+
+ std::stringstream lStream;
+ unsigned int lMillis;
+
+ zorba::String lStringValue = lRequestItem.getStringValue();
+
+ bool lSuccess = lStream << lStringValue &&
+ lStream >> lMillis &&
+ lStream.eof();
+
+ if ( !lSuccess )
+ {
+ zorba::String lLocalName = "InvalidValue";
+ ItemFactory* lFactory = Zorba::getInstance(0)->getItemFactory();
+ zorba::Item lQName = lFactory->createQName( getURI(), "", lLocalName);
+
+ std::ostringstream lMsg;
+ lMsg << lStringValue.c_str() << ": exceeds limit ("
+ << std::numeric_limits<unsigned int>::min()
+ << " < x < "
+ << std::numeric_limits<unsigned int>::max() << ")";
+
+ throw USER_EXCEPTION(lQName, lMsg.str());
+ }
+
+#ifdef WIN32
+ Sleep(lMillis);
+#else
+ const int32_t lFactor = 1000000;
+ struct timespec lTs;
+ lTs.tv_sec = lMillis / 1000;
+ lTs.tv_nsec = (lMillis % 1000) * lFactor;
+ nanosleep(&lTs, 0);
+#endif
+
+ return zorba::ItemSequence_t(new zorba::EmptySequence());
+}
+
+zorba::ExternalFunction*
+SleepModule::getExternalFunction(const zorba::String& aLocalname)
+{
+ FuncMap_t::const_iterator lFind = theFunctions.find(aLocalname);
+ zorba::ExternalFunction*& lFunc = theFunctions[aLocalname];
+ if (lFind == theFunctions.end()) {
+ if (aLocalname.find("millis") != zorba::String::npos) {
+ lFunc = new MillisFunction(this);
+ }
+ }
+ return lFunc;
+}
+
+SleepModule::~SleepModule()
+{
+ for (FuncMap_t::const_iterator lIter = theFunctions.begin();
+ lIter != theFunctions.end(); ++lIter)
+ {
+ delete lIter->second;
+ }
+ theFunctions.clear();
+}
+
+void
+SleepModule::destroy()
+{
+ if (!dynamic_cast<SleepModule*>(this))
+ return;
+ delete this;
+}
+
+} /* namespace sleep */ } /* namespace zorba */
=== added file 'modules/sleep/sleep.xq.src/sleep.h'
--- modules/sleep/sleep.xq.src/sleep.h 1970-01-01 00:00:00 +0000
+++ modules/sleep/sleep.xq.src/sleep.h 2014-04-18 18:30:22 +0000
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2006-2014 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef ZORBA_MODULES_SLEEP_H
+#define ZORBA_MODULES_SLEEP_H
+
+#include <zorba/zorba.h>
+#include <zorba/external_module.h>
+#include <zorba/function.h>
+
+namespace zorba { namespace sleep {
+
+class SleepModule : public ExternalModule
+{
+public:
+ virtual ~SleepModule();
+
+ virtual void destroy();
+
+ virtual zorba::String
+ getURI() const { return "http://zorba.io/modules/sleep"; }
+
+ virtual zorba::ExternalFunction*
+ getExternalFunction(const zorba::String& aLocalname);
+
+protected:
+ struct ltstr
+ {
+ bool operator()(const String& s1, const String& s2) const
+ {
+ return s1.compare(s2) < 0;
+ }
+ };
+
+ typedef std::map<String, ExternalFunction*, ltstr> FuncMap_t;
+
+ FuncMap_t theFunctions;
+};
+
+
+class MillisFunction : public ContextualExternalFunction
+{
+public:
+ MillisFunction(const SleepModule* aModule)
+ : theModule(aModule)
+ {}
+
+ virtual ~MillisFunction();
+
+ virtual zorba::String
+ getLocalName() const { return "millis"; }
+
+ virtual zorba::String
+ getURI() const { return theModule->getURI(); }
+
+ virtual zorba::ItemSequence_t
+ evaluate(const Arguments_t&,
+ const zorba::StaticContext*,
+ const zorba::DynamicContext*) const;
+
+protected:
+ const SleepModule* theModule;
+};
+
+} /* namespace sleep */ } /* namespace zorba */
+
+#ifdef WIN32
+# define DLL_EXPORT __declspec(dllexport)
+#else
+# define DLL_EXPORT __attribute__ ((visibility("default")))
+#endif
+extern "C" DLL_EXPORT zorba::ExternalModule* createModule() {
+ return new zorba::sleep::SleepModule();
+}
+
+#endif
References