You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2009/10/27 06:30:14 UTC
svn commit: r830061 - in /lucene/lucy/trunk: core/Lucy/Object/Err.bp
core/Lucy/Object/Err.c core/Lucy/Store/FileHandle.bp
core/Lucy/Store/FileHandle.c core/Lucy/Test/Store/TestFileHandle.c
perl/lib/Lucy.pm perl/xs/Lucy/Object/Err.c
Author: marvin
Date: Tue Oct 27 05:30:13 2009
New Revision: 830061
URL: http://svn.apache.org/viewvc?rev=830061&view=rev
Log:
Commit LUCY-60. Add a thread-local (conceptually speaking) error variable
which low-level routines will write to. The implementation will be
binding-specific, since "thread-local" has to be different for each threading
model.
Modified:
lucene/lucy/trunk/core/Lucy/Object/Err.bp
lucene/lucy/trunk/core/Lucy/Object/Err.c
lucene/lucy/trunk/core/Lucy/Store/FileHandle.bp
lucene/lucy/trunk/core/Lucy/Store/FileHandle.c
lucene/lucy/trunk/core/Lucy/Test/Store/TestFileHandle.c
lucene/lucy/trunk/perl/lib/Lucy.pm
lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c
Modified: lucene/lucy/trunk/core/Lucy/Object/Err.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Err.bp?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Err.bp (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Err.bp Tue Oct 27 05:30:13 2009
@@ -10,8 +10,12 @@
* At present, it is only safe to catch exceptions which are specifically
* documented as catchable; most times when an Err is raised, Lucy leaks
* memory.
+ *
+ * The Err module also provides access to a per-thread Err shared variable via
+ * set_error() and get_error(). It may be used to store an Err object
+ * temporarily, so that calling code may choose how to handle a particular
+ * error condition.
*/
-
class Lucy::Object::Err extends Lucy::Object::Obj {
CharBuf *mess;
@@ -39,9 +43,24 @@
public CharBuf*
Get_Mess(Err *self);
+ /** Add information about the current stack frame onto <code>mess</code>.
+ */
+ void
+ Add_Frame(Err *self, const char *file, int line, const char *func);
+
public incremented Err*
Make(Err *self);
+ /** Set the value of "error", a per-thread Err shared variable.
+ */
+ public inert void
+ set_error(decremented Err *error);
+
+ /** Retrieve per-thread Err shared variable "error".
+ */
+ public inert Err*
+ get_error();
+
/** Print an error message to stderr with some C contextual information.
* Usually invoked via the WARN(pattern, ...) macro.
*/
@@ -55,6 +74,11 @@
throw_at(VTable *vtable, const char *file, int line, const char *func,
const char *pattern, ...);
+ /** Throw an existing exception after tacking on additional context data.
+ */
+ inert void
+ rethrow(Err *error, const char *file, int line, const char *func);
+
/** Raise an exception. Clean up the supplied message by decrementing its
* refcount.
*
@@ -106,6 +130,14 @@
#define LUCY_ERR_FUNC_MACRO NULL
#endif
+#define LUCY_ERR_ADD_FRAME(_error) \
+ Lucy_Err_Add_Frame(_error, __FILE__, __LINE__, \
+ LUCY_ERR_FUNC_MACRO)
+
+#define LUCY_RETHROW(_error) \
+ lucy_Err_rethrow((lucy_Err*)_error, __FILE__, __LINE__, \
+ LUCY_ERR_FUNC_MACRO)
+
/* Macro version of lucy_Err_throw_at which inserts contextual information
* automatically, provided that the compiler supports the necessary features.
*/
@@ -158,8 +190,10 @@
#ifdef LUCY_USE_SHORT_NAMES
#define THROW LUCY_THROW
+ #define RETHROW LUCY_RETHROW
#define WARN LUCY_WARN
#define MAKE_MESS LUCY_MAKE_MESS
+ #define ERR_ADD_FRAME LUCY_ERR_ADD_FRAME
#define ERR_FUNC_MACRO LUCY_ERR_FUNC_MACRO
#define ASSERT_IS_A LUCY_ASSERT_IS_A
#define ABSTRACT_CLASS_CHECK LUCY_ABSTRACT_CLASS_CHECK
Modified: lucene/lucy/trunk/core/Lucy/Object/Err.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Object/Err.c?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Object/Err.c (original)
+++ lucene/lucy/trunk/core/Lucy/Object/Err.c Tue Oct 27 05:30:13 2009
@@ -141,6 +141,25 @@
Err_get_mess(Err *self) { return self->mess; }
void
+Err_add_frame(Err *self, const char *file, int line, const char *func)
+{
+ if (func != NULL) {
+ CB_catf(self->mess, ",\n\t %s at %s line %i32\n", func, file,
+ (i32_t)line);
+ }
+ else {
+ CB_catf(self->mess, "\n\tat %s line %i32\n", file, (i32_t)line);
+ }
+}
+
+void
+Err_rethrow(Err *self, const char *file, int line, const char *func)
+{
+ Err_add_frame(self, file, line, func);
+ Err_do_throw(self);
+}
+
+void
lucy_Err_throw_at(VTable *vtable, const char *file, int line,
const char *func, const char *pattern, ...)
{
Modified: lucene/lucy/trunk/core/Lucy/Store/FileHandle.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Store/FileHandle.bp?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Store/FileHandle.bp (original)
+++ lucene/lucy/trunk/core/Lucy/Store/FileHandle.bp Tue Oct 27 05:30:13 2009
@@ -20,7 +20,6 @@
extends Lucy::Object::Obj {
CharBuf *path;
- CharBuf *error;
u32_t flags;
/** Abstract constructor.
@@ -30,7 +29,7 @@
* behaviors.
*/
inert FileHandle*
- init(FileHandle *self, const CharBuf *path = NULL, u32_t flags);
+ do_open(FileHandle *self, const CharBuf *path = NULL, u32_t flags);
/** Ensure that the FileWindow's buffer provides access to file data for
* <code>len</code> bytes starting at <code>offset</code>.
@@ -38,7 +37,7 @@
* @param window A FileWindow.
* @param offset File position to begin at.
* @param len Number of bytes to expose via the window.
- * @return true on success, false on failure (sets error)
+ * @return true on success, false on failure (sets Err_error)
*/
abstract bool_t
Window(FileHandle *self, FileWindow *window, i64_t offset, i64_t len);
@@ -46,7 +45,7 @@
/** Clean up the FileWindow, doing whatever is necessary to free its
* buffer and reset its internal variables.
*
- * @return true on success, false on failure (sets error)
+ * @return true on success, false on failure (sets Err_error)
*/
abstract bool_t
Release_Window(FileHandle *self, FileWindow *window);
@@ -56,7 +55,7 @@
* @param dest Supplied memory.
* @param offset File position to begin at.
* @param len Number of bytes to copy.
- * @return true on success, false on failure (sets error)
+ * @return true on success, false on failure (sets Err_error)
*/
abstract bool_t
Read(FileHandle *self, char *dest, i64_t offset, size_t len);
@@ -65,27 +64,30 @@
*
* @param data Content to write.
* @param len Number of bytes to write.
- * @return true on success, false on failure (sets error)
+ * @return true on success, false on failure (sets Err_error)
*/
abstract bool_t
Write(FileHandle *self, const void *data, size_t len);
- /** Return the current length of the file in bytes.
+ /** Return the current length of the file in bytes, or set Err_error and
+ * return -1 on failure.
*/
abstract i64_t
Length(FileHandle *self);
/** Advisory call alerting the FileHandle that it should prepare to occupy
* <code>len</code> bytes. The default implementation is a no-op.
+ *
+ * @return true on success, false on failure (sets Err_error).
*/
- void
+ bool_t
Grow(FileHandle *self, i64_t len);
/** Close the FileHandle, possibly releasing resources. Implementations
* should be be able to handle multiple invocations, returning success
* unless something unexpected happens.
*
- * @return true on success, false on failure (sets error)
+ * @return true on success, false on failure (sets Err_error)
*/
abstract bool_t
Close(FileHandle *self);
@@ -100,27 +102,6 @@
CharBuf*
Get_Path(FileHandle *self);
- /** Return the object's <code>error</code> attribute. Initially NULL.
- */
- CharBuf*
- Get_Error(FileHandle *self);
-
- /** Set the FileHandle's <code>error</code> attribute.
- */
- void
- Set_Error(FileHandle *self, CharBuf *error);
-
- /** Wrapper function which invokes VSetF_Error.
- */
- inert void
- setf_error(void *vself, const char *pattern, ...);
-
- /** Sets <code>error</code> member using arguments formatted for CharBuf's
- * VCatF().
- */
- void
- VSetF_Error(FileHandle *self, const char *pattern, va_list args);
-
/** Invokes Close(), but ignores whether it succeeds or fails.
*/
public void
Modified: lucene/lucy/trunk/core/Lucy/Store/FileHandle.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Store/FileHandle.c?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Store/FileHandle.c (original)
+++ lucene/lucy/trunk/core/Lucy/Store/FileHandle.c Tue Oct 27 05:30:13 2009
@@ -8,10 +8,9 @@
i32_t FH_object_count = 0;
FileHandle*
-FH_init(FileHandle *self, const CharBuf *path, u32_t flags)
+FH_do_open(FileHandle *self, const CharBuf *path, u32_t flags)
{
self->path = path ? CB_Clone(path) : CB_new(0);
- self->error = NULL;
self->flags = flags;
/* Track number of live FileHandles released into the wild. */
@@ -26,18 +25,18 @@
{
FH_Close(self);
DECREF(self->path);
- DECREF(self->error);
SUPER_DESTROY(self, FILEHANDLE);
/* Decrement count of FileHandle objects in existence. */
FH_object_count--;
}
-void
+bool_t
FH_grow(FileHandle *self, i64_t length)
{
UNUSED_VAR(self);
UNUSED_VAR(length);
+ return true;
}
void
@@ -48,32 +47,6 @@
CharBuf*
FH_get_path(FileHandle *self) { return self->path; }
-CharBuf*
-FH_get_error(FileHandle *self) { return self->error; }
-
-void
-FH_set_error(FileHandle *self, CharBuf *error)
-{
- DECREF(self->error);
- self->error = error ? CB_Clone(error) : NULL;
-}
-
-void
-FH_setf_error(void *vself, const char *pattern, ...)
-{
- va_list args;
- va_start(args, pattern);
- FH_VSetF_Error(vself, pattern, args);
- va_end(args);
-}
-
-void
-FH_vsetf_error(FileHandle *self, const char *pattern, va_list args)
-{
- DECREF(self->error);
- self->error = CB_new(50);
- CB_VCatF(self->error, pattern, args);
-}
/* Copyright 2009 The Apache Software Foundation
*
Modified: lucene/lucy/trunk/core/Lucy/Test/Store/TestFileHandle.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Store/TestFileHandle.c?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Store/TestFileHandle.c (original)
+++ lucene/lucy/trunk/core/Lucy/Test/Store/TestFileHandle.c Tue Oct 27 05:30:13 2009
@@ -27,27 +27,18 @@
}
VTable_Override(vtable, S_no_op_method, Lucy_FH_Close_OFFSET);
fh = (FileHandle*)VTable_Make_Obj(vtable);
- return FH_init(fh, NULL, 0);
+ return FH_do_open(fh, NULL, 0);
}
void
TestFH_run_tests()
{
- TestBatch *batch = Test_new_batch("TestFileHandle", 6, NULL);
+ TestBatch *batch = Test_new_batch("TestFileHandle", 2, NULL);
FileHandle *fh = S_new_filehandle();
ZombieCharBuf foo = ZCB_LITERAL("foo");
PLAN(batch);
- ASSERT_TRUE(batch, FH_Get_Error(fh) == NULL, "error starts off NULL");
- FH_Set_Error(fh, (CharBuf*)&foo);
- ASSERT_TRUE(batch, CB_Equals(FH_Get_Error(fh), (Obj*)&foo), "Set_Error");
- FH_setf_error(fh, "Oops.");
- ASSERT_TRUE(batch, CB_Equals_Str(FH_Get_Error(fh), "Oops.", 5),
- "setf_error");
- FH_Set_Error(fh, NULL);
- ASSERT_TRUE(batch, FH_Get_Error(fh) == NULL, "Set_Error to NULL");
-
ASSERT_TRUE(batch, CB_Equals_Str(FH_Get_Path(fh), "", 0), "Get_Path");
FH_Set_Path(fh, (CharBuf*)&foo);
ASSERT_TRUE(batch, CB_Equals(FH_Get_Path(fh), (Obj*)&foo), "Set_Path");
Modified: lucene/lucy/trunk/perl/lib/Lucy.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy.pm?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy.pm Tue Oct 27 05:30:13 2009
@@ -12,6 +12,8 @@
use Lucy::Autobinding;
+sub error {$Lucy::Object::Err::error}
+
{
package Lucy::Util::ToolSet;
use Carp qw( carp croak cluck confess );
@@ -34,6 +36,8 @@
{
package Lucy::Object::Err;
+ use Lucy::Util::ToolSet qw( blessed );
+
sub do_to_string { shift->to_string }
use Carp qw( longmess );
use overload
@@ -45,6 +49,18 @@
$err->cat_mess( longmess() );
die $err;
}
+
+ our $error;
+ sub set_error {
+ my $val = $_[1];
+ if ( defined $val ) {
+ confess("Not a Lucy::Object::Err")
+ unless ( blessed($val)
+ && $val->isa("Lucy::Object::Err") );
+ }
+ $error = $val;
+ }
+ sub get_error {$error}
}
{
Modified: lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c?rev=830061&r1=830060&r2=830061&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c (original)
+++ lucene/lucy/trunk/perl/xs/Lucy/Object/Err.c Tue Oct 27 05:30:13 2009
@@ -1,4 +1,22 @@
#include "xs/XSBind.h"
+#include "Lucy/Object/Host.h"
+
+lucy_Err*
+lucy_Err_get_error()
+{
+ lucy_Err *error
+ = (lucy_Err*)lucy_Host_callback_obj(LUCY_ERR, "get_error", 0);
+ LUCY_DECREF(error); /* Cancel out incref from callback. */
+ return error;
+}
+
+void
+lucy_Err_set_error(lucy_Err *error)
+{
+ lucy_Host_callback(LUCY_ERR, "set_error", 1,
+ LUCY_ARG_OBJ("error", error));
+ LUCY_DECREF(error);
+}
void
lucy_Err_do_throw(lucy_Err *err)