FAQ
Author: marvin
Date: Mon Nov 23 20:03:40 2009
New Revision: 883479

URL: http://svn.apache.org/viewvc?rev=883479&view=rev
Log:
Commit LUCY-69, adding RAMFolder.

Added:
lucene/lucy/trunk/core/Lucy/Store/RAMFolder.bp (with props)
lucene/lucy/trunk/core/Lucy/Store/RAMFolder.c (with props)
lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.bp (with props)
lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.c (with props)
lucene/lucy/trunk/perl/lib/Lucy/Store/RAMFolder.pm (with props)
lucene/lucy/trunk/perl/t/core/104-ram_folder.t (with props)
Modified:
lucene/lucy/trunk/perl/lib/Lucy/Test.pm

Added: lucene/lucy/trunk/core/Lucy/Store/RAMFolder.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Store/RAMFolder.bp?rev=883479&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Store/RAMFolder.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Store/RAMFolder.bp Mon Nov 23 20:03:40 2009
@@ -0,0 +1,68 @@
+parcel Lucy;
+
+/** In-memory Folder implementation.
+ *
+ * RAMFolder is an entirely in-memory implementation of
+ * Lucy::Store::Folder, primarily used for testing and development.
+ */
+
+class Lucy::Store::RAMFolder extends Lucy::Store::Folder {
+
+ inert incremented RAMFolder*
+ new(const CharBuf *path = NULL);
+
+ /**
+ * @param path Relative path, used for subfolders.
+ */
+ public inert RAMFolder*
+ init(RAMFolder *self, const CharBuf *path = NULL);
+
+ public void
+ Initialize(RAMFolder *self);
+
+ public bool_t
+ Check(RAMFolder *self);
+
+ public void
+ Close(RAMFolder *self);
+
+ incremented FileHandle*
+ Local_Open_FileHandle(RAMFolder *self, const CharBuf *name, u32_t flags);
+
+ bool_t
+ Local_MkDir(RAMFolder *self, const CharBuf *name);
+
+ bool_t
+ Local_Exists(RAMFolder *self, const CharBuf *name);
+
+ bool_t
+ Local_Is_Directory(RAMFolder *self, const CharBuf *name);
+
+ Folder*
+ Local_Find_Folder(RAMFolder *self, const CharBuf *name);
+
+ bool_t
+ Local_Delete(RAMFolder *self, const CharBuf *name);
+
+ public bool_t
+ Rename(RAMFolder *self, const CharBuf* from, const CharBuf *to);
+
+ public bool_t
+ Hard_Link(RAMFolder *self, const CharBuf *from, const CharBuf *to);
+}
+
+/* Copyright 2009 The Apache Software 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Store/RAMFolder.bp
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Store/RAMFolder.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Store/RAMFolder.c?rev=883479&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Store/RAMFolder.c (added)
+++ lucene/lucy/trunk/core/Lucy/Store/RAMFolder.c Mon Nov 23 20:03:40 2009
@@ -0,0 +1,346 @@
+#define C_LUCY_RAMFOLDER
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Store/RAMFolder.h"
+#include "Lucy/Store/InStream.h"
+#include "Lucy/Store/OutStream.h"
+#include "Lucy/Store/RAMFile.h"
+#include "Lucy/Store/RAMFileHandle.h"
+#include "Lucy/Util/IndexFileNames.h"
+
+/* Return the concatenation of the Folder's path and the supplied path. */
+static CharBuf*
+S_fullpath(RAMFolder *self, const CharBuf *path);
+
+RAMFolder*
+RAMFolder_new(const CharBuf *path)
+{
+ RAMFolder *self = (RAMFolder*)VTable_Make_Obj(RAMFOLDER);
+ return RAMFolder_init(self, path);
+}
+
+RAMFolder*
+RAMFolder_init(RAMFolder *self, const CharBuf *path)
+{
+ Folder_init((Folder*)self, path);
+ return self;
+}
+
+void
+RAMFolder_initialize(RAMFolder *self)
+{
+ UNUSED_VAR(self);
+}
+
+bool_t
+RAMFolder_check(RAMFolder *self)
+{
+ UNUSED_VAR(self);
+ return true;
+}
+
+bool_t
+RAMFolder_local_mkdir(RAMFolder *self, const CharBuf *name)
+{
+ if (Hash_Fetch(self->entries, (Obj*)name)) {
+ Err_set_error(Err_new(CB_newf(
+ "Can't MkDir, '%o' already exists", name)));
+ return false;
+ }
+ else {
+ CharBuf *fullpath = S_fullpath(self, name);
+ Hash_Store(self->entries, (Obj*)name,
+ (Obj*)RAMFolder_new(fullpath));
+ DECREF(fullpath);
+ return true;
+ }
+}
+
+FileHandle*
+RAMFolder_local_open_filehandle(RAMFolder *self, const CharBuf *name,
+ u32_t flags)
+{
+ RAMFileHandle *fh;
+ CharBuf *fullpath = S_fullpath(self, name);
+ RAMFile *file = (RAMFile*)Hash_Fetch(self->entries, (Obj*)name);
+ bool_t can_create = (flags & (FH_WRITE_ONLY | FH_CREATE))
+ == (FH_WRITE_ONLY | FH_CREATE)
+ ? true : false;
+
+ /* Make sure the filepath isn't a directory, and that it either exists
+ * or we have permission to create it. */
+ if (file) {
+ if (!Obj_Is_A(file, RAMFILE)) {
+ Err_set_error(Err_new(CB_newf("Not a file: '%o'", fullpath)));
+ DECREF(fullpath);
+ return NULL;
+ }
+ }
+ else if (!can_create) {
+ Err_set_error(Err_new(CB_newf("File not found: '%o'", fullpath)));
+ DECREF(fullpath);
+ return NULL;
+ }
+
+ /* Open the file and store it if it was just created. */
+ fh = RAMFH_open(fullpath, flags, file);
+ if (fh) {
+ if (!file) {
+ file = RAMFH_Get_File(fh);
+ Hash_Store(self->entries, (Obj*)name, INCREF(file));
+ }
+ }
+ else {
+ Err *error = Err_get_error();
+ ERR_ADD_FRAME(error);
+ }
+
+ DECREF(fullpath);
+
+ return (FileHandle*)fh;
+}
+
+bool_t
+RAMFolder_local_exists(RAMFolder *self, const CharBuf *name)
+{
+ return !!Hash_Fetch(self->entries, (Obj*)name);
+}
+
+bool_t
+RAMFolder_local_is_directory(RAMFolder *self, const CharBuf *name)
+{
+ Obj *entry = Hash_Fetch(self->entries, (Obj*)name);
+ if (entry && Obj_Is_A(entry, FOLDER)) { return true; }
+ return false;
+}
+
+#define OP_RENAME 1
+#define OP_HARD_LINK 2
+
+static bool_t
+S_rename_or_hard_link(RAMFolder *self, const CharBuf* from, const CharBuf *to,
+ Folder *from_folder, Folder *to_folder,
+ ZombieCharBuf *from_name, ZombieCharBuf *to_name,
+ int op)
+{
+ Obj *elem = NULL;
+ RAMFolder *inner_from_folder = NULL;
+ RAMFolder *inner_to_folder = NULL;
+ UNUSED_VAR(self);
+
+ /* Make sure the source and destination folders exist. */
+ if (!from_folder) {
+ Err_set_error(Err_new(CB_newf("File not found: '%o'", from)));
+ return false;
+ }
+ if (!to_folder) {
+ Err_set_error(Err_new(CB_newf(
+ "Invalid file path (can't find dir): '%o'", to)));
+ return false;
+ }
+
+ /* Extract RAMFolders from compound reader wrappers, if necessary. */
+ inner_from_folder = (RAMFolder*)from_folder;
+ inner_to_folder = (RAMFolder*)to_folder;
+ /*
+ inner_from_folder = Obj_Is_A(from_folder, COMPOUNDFILEREADER)
+ ? (RAMFolder*)CFReader_Get_Real_Folder(from_folder)
+ : (RAMFolder*)from_folder;
+ inner_to_folder = Obj_Is_A(to_folder, COMPOUNDFILEREADER)
+ ? (RAMFolder*)CFReader_Get_Real_Folder(to_folder)
+ : (RAMFolder*)to_folder;
+ */
+ if (!Obj_Is_A(inner_from_folder, RAMFOLDER)) {
+ Err_set_error(Err_new(CB_newf("Not a RAMFolder, but a '%o'",
+ Obj_Get_Class_Name(inner_from_folder))));
+ return false;
+ }
+ if (!Obj_Is_A(inner_to_folder, RAMFOLDER)) {
+ Err_set_error(Err_new(CB_newf("Not a RAMFolder, but a '%o'",
+ Obj_Get_Class_Name(inner_to_folder))));
+ return false;
+ }
+
+ /* Find the original element. */
+ elem = Hash_Fetch(inner_from_folder->entries, (Obj*)from_name);
+ if (!elem) {
+ if (0) { }
+ /*
+ if ( Obj_Is_A(from_folder, COMPOUNDFILEREADER)
+ && CFReader_Local_Exists(from_folder, (CharBuf*)from_name)
+ ) {
+ Err_set_error(Err_new(CB_newf("Source file '%o' is virtual",
+ from)));
+ }
+ */
+ else {
+ Err_set_error(Err_new(CB_newf("File not found: '%o'", from)));
+ }
+ return false;
+ }
+
+ /* Execute the rename/hard-link. */
+ if (op == OP_RENAME) {
+ Obj *existing = Hash_Fetch(inner_to_folder->entries, (Obj*)to_name);
+ if (existing) {
+ bool_t conflict = false;
+
+ /* Return success fast if file is copied on top of itself. */
+ if ( inner_from_folder == inner_to_folder
+ && ZCB_Equals(from_name, (Obj*)to_name)
+ ) {
+ return true;
+ }
+
+ /* Don't allow clobbering of different entry type. */
+ if (Obj_Is_A(elem, RAMFILE)) {
+ if (!Obj_Is_A(existing, RAMFILE)) {
+ conflict = true;
+ }
+ }
+ else if (Obj_Is_A(elem, FOLDER)) {
+ if (!Obj_Is_A(existing, FOLDER)) {
+ conflict = true;
+ }
+ }
+ if (conflict) {
+ Err_set_error(Err_new(CB_newf("Can't clobber a %o with a %o",
+ Obj_Get_Class_Name(existing), Obj_Get_Class_Name(elem))));
+ return false;
+ }
+ }
+
+ /* Perform the store first, then the delete. Inform Folder objects
+ * about the relocation. */
+ Hash_Store(inner_to_folder->entries, (Obj*)to_name, INCREF(elem));
+ DECREF(Hash_Delete(inner_from_folder->entries, (Obj*)from_name));
+ if (Obj_Is_A(elem, FOLDER)) {
+ CharBuf *newpath = S_fullpath(inner_to_folder, (CharBuf*)to_name);
+ Folder_Set_Path(elem, newpath);
+ DECREF(newpath);
+ }
+ }
+ else if (op == OP_HARD_LINK) {
+ if (!Obj_Is_A(elem, RAMFILE)) {
+ Err_set_error(Err_new(CB_newf("'%o' isn't a file, it's a %o",
+ from, Obj_Get_Class_Name(elem))));
+ return false;
+ }
+ else {
+ Obj *existing
+ = Hash_Fetch(inner_to_folder->entries, (Obj*)to_name);
+ if (existing) {
+ Err_set_error(Err_new(CB_newf("'%o' already exists", to)));
+ return false;
+ }
+ else {
+ Hash_Store(inner_to_folder->entries, (Obj*)to_name,
+ INCREF(elem));
+ }
+ }
+ }
+ else {
+ THROW(ERR, "Unexpected op: %i32", (i32_t)op);
+ }
+
+ return true;
+}
+
+bool_t
+RAMFolder_rename(RAMFolder *self, const CharBuf* from, const CharBuf *to)
+{
+ Folder *from_folder = RAMFolder_Enclosing_Folder(self, from);
+ Folder *to_folder = RAMFolder_Enclosing_Folder(self, to);
+ ZombieCharBuf from_zbc = ZCB_BLANK;
+ ZombieCharBuf to_zbc = ZCB_BLANK;
+ ZombieCharBuf *from_name = IxFileNames_local_part(from, &from_zbc);
+ ZombieCharBuf *to_name = IxFileNames_local_part(to, &to_zbc);
+ bool_t result = S_rename_or_hard_link(self, from, to,
+ from_folder, to_folder, from_name, to_name, OP_RENAME);
+ if (!result) { ERR_ADD_FRAME(Err_get_error()); }
+ return result;
+}
+
+bool_t
+RAMFolder_hard_link(RAMFolder *self, const CharBuf *from, const CharBuf *to)
+{
+ Folder *from_folder = RAMFolder_Enclosing_Folder(self, from);
+ Folder *to_folder = RAMFolder_Enclosing_Folder(self, to);
+ ZombieCharBuf from_zbc = ZCB_BLANK;
+ ZombieCharBuf to_zbc = ZCB_BLANK;
+ ZombieCharBuf *from_name = IxFileNames_local_part(from, &from_zbc);
+ ZombieCharBuf *to_name = IxFileNames_local_part(to, &to_zbc);
+ bool_t result = S_rename_or_hard_link(self, from, to,
+ from_folder, to_folder, from_name, to_name, OP_HARD_LINK);
+ if (!result) { ERR_ADD_FRAME(Err_get_error()); }
+ return result;
+}
+
+bool_t
+RAMFolder_local_delete(RAMFolder *self, const CharBuf *name)
+{
+ Obj *entry = Hash_Fetch(self->entries, (Obj*)name);
+ if (entry) {
+ if (Obj_Is_A(entry, RAMFILE)) {
+ ;
+ }
+ else if (Obj_Is_A(entry, FOLDER)) {
+ RAMFolder *inner_folder = (RAMFolder*)CERTIFY(entry, RAMFOLDER);
+ if (Hash_Get_Size(inner_folder->entries)) {
+ /* Can't delete non-empty dir. */
+ return false;
+ }
+ }
+ else {
+ return false;
+ }
+ DECREF(Hash_Delete(self->entries, (Obj*)name));
+ return true;
+ }
+ else {
+ return false;
+ }
+}
+
+Folder*
+RAMFolder_local_find_folder(RAMFolder *self, const CharBuf *path)
+{
+ Folder *local_folder = (Folder*)Hash_Fetch(self->entries, (Obj*)path);
+ if (local_folder && Obj_Is_A(local_folder, FOLDER)) {
+ return local_folder;
+ }
+ return NULL;
+}
+
+void
+RAMFolder_close(RAMFolder *self)
+{
+ UNUSED_VAR(self);
+}
+
+static CharBuf*
+S_fullpath(RAMFolder *self, const CharBuf *path)
+{
+ if (CB_Get_Size(self->path)) {
+ return CB_newf("%o/%o", self->path, path);
+ }
+ else {
+ return CB_Clone(path);
+ }
+}
+
+/* Copyright 2009 The Apache Software 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Store/RAMFolder.c
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.bp?rev=883479&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.bp Mon Nov 23 20:03:40 2009
@@ -0,0 +1,22 @@
+parcel Lucy;
+
+inert class Lucy::Test::Store::TestRAMFolder {
+ inert void
+ run_tests();
+}
+
+/* Copyright 2009 The Apache Software 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.bp
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.c?rev=883479&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.c (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.c Mon Nov 23 20:03:40 2009
@@ -0,0 +1,484 @@
+#define C_LUCY_RAMFOLDER
+#define C_LUCY_CHARBUF
+#include "Lucy/Util/ToolSet.h"
+
+#define CHAZ_USE_SHORT_NAMES
+#include "Charmonizer/Test.h"
+
+#include "Lucy/Test/Store/TestRAMFolder.h"
+#include "Lucy/Store/RAMFolder.h"
+#include "Lucy/Store/RAMFileHandle.h"
+
+static CharBuf foo = ZCB_LITERAL("foo");
+static CharBuf bar = ZCB_LITERAL("bar");
+static CharBuf baz = ZCB_LITERAL("baz");
+static CharBuf boffo = ZCB_LITERAL("boffo");
+static CharBuf banana = ZCB_LITERAL("banana");
+static CharBuf foo_bar = ZCB_LITERAL("foo/bar");
+static CharBuf foo_bar_baz = ZCB_LITERAL("foo/bar/baz");
+static CharBuf foo_bar_boffo = ZCB_LITERAL("foo/bar/boffo");
+static CharBuf foo_boffo = ZCB_LITERAL("foo/boffo");
+static CharBuf foo_foo = ZCB_LITERAL("foo/foo");
+static CharBuf nope = ZCB_LITERAL("nope");
+static CharBuf nope_nyet = ZCB_LITERAL("nope/nyet");
+
+static void
+test_Initialize_and_Check(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ Folder_Initialize(folder);
+ PASS(batch, "Initialized concludes without incident");
+ ASSERT_TRUE(batch, Folder_Check(folder), "Check succeeds");
+ DECREF(folder);
+}
+
+static void
+test_Local_Exists(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ FileHandle *fh = Folder_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ Folder_Local_MkDir(folder, &foo);
+
+ ASSERT_TRUE(batch, Folder_Local_Exists(folder, &boffo),
+ "Local_Exists() returns true for file");
+ ASSERT_TRUE(batch, Folder_Local_Exists(folder, &foo),
+ "Local_Exists() returns true for dir");
+ ASSERT_FALSE(batch, Folder_Local_Exists(folder, &bar),
+ "Local_Exists() returns false for non-existent entry");
+
+ DECREF(folder);
+}
+
+static void
+test_Local_Is_Directory(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ FileHandle *fh = Folder_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ Folder_Local_MkDir(folder, &foo);
+
+ ASSERT_FALSE(batch, Folder_Local_Is_Directory(folder, &boffo),
+ "Local_Is_Directory() returns false for file");
+ ASSERT_TRUE(batch, Folder_Local_Is_Directory(folder, &foo),
+ "Local_Is_Directory() returns true for dir");
+ ASSERT_FALSE(batch, Folder_Local_Is_Directory(folder, &bar),
+ "Local_Is_Directory() returns false for non-existent entry");
+
+ DECREF(folder);
+}
+
+static void
+test_Local_Find_Folder(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ RAMFolder *local;
+ FileHandle *fh;
+
+ Folder_MkDir(folder, &foo);
+ Folder_MkDir(folder, &foo_bar);
+ fh = Folder_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ fh = Folder_Open_FileHandle(folder, &foo_boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, &nope);
+ ASSERT_TRUE(batch, local == NULL, "Non-existent entry yields NULL");
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, (CharBuf*)&EMPTY);
+ ASSERT_TRUE(batch, local == NULL, "Empty string yields NULL");
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, &foo_bar);
+ ASSERT_TRUE(batch, local == NULL, "nested folder yields NULL");
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, &foo_boffo);
+ ASSERT_TRUE(batch, local == NULL, "nested file yields NULL");
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, &boffo);
+ ASSERT_TRUE(batch, local == NULL, "local file yields NULL");
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, &bar);
+ ASSERT_TRUE(batch, local == NULL, "name of nested folder yields NULL");
+
+ local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, &foo);
+ ASSERT_TRUE(batch,
+ local
+ && Obj_Is_A(local, RAMFOLDER)
+ && CB_Equals_Str(RAMFolder_Get_Path(local), "foo", 3),
+ "Find local directory");
+
+ DECREF(folder);
+}
+
+static void
+test_Local_MkDir(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ bool_t result;
+
+ result = Folder_Local_MkDir(folder, &foo);
+ ASSERT_TRUE(batch, result, "Local_MkDir succeeds and returns true");
+
+ Err_set_error(NULL);
+ result = Folder_Local_MkDir(folder, &foo);
+ ASSERT_FALSE(batch, result,
+ "Local_MkDir returns false when a dir already exists");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Local_MkDir sets Err_error when a dir already exists");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo),
+ "Existing dir untouched after failed Local_MkDir");
+
+ {
+ FileHandle *fh = Folder_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ Err_set_error(NULL);
+ result = Folder_Local_MkDir(folder, &foo);
+ ASSERT_FALSE(batch, result,
+ "Local_MkDir returns false when a file already exists");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Local_MkDir sets Err_error when a file already exists");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &boffo) &&
+ !Folder_Local_Is_Directory(folder, &boffo),
+ "Existing file untouched after failed Local_MkDir");
+ }
+
+ DECREF(folder);
+}
+
+static void
+test_Local_Open_FileHandle(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ FileHandle *fh;
+
+ fh = Folder_Local_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ ASSERT_TRUE(batch, fh && Obj_Is_A(fh, RAMFILEHANDLE),
+ "opened FileHandle");
+ DECREF(fh);
+
+ fh = Folder_Local_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ ASSERT_TRUE(batch, fh && Obj_Is_A(fh, RAMFILEHANDLE),
+ "opened FileHandle for append");
+ DECREF(fh);
+
+ Err_set_error(NULL);
+ fh = Folder_Local_Open_FileHandle(folder, &boffo,
+ FH_CREATE | FH_WRITE_ONLY | FH_EXCLUSIVE);
+ ASSERT_TRUE(batch, fh == NULL, "FH_EXLUSIVE flag prevents open");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "failure due to FH_EXLUSIVE flag sets Err_error");
+
+ fh = Folder_Local_Open_FileHandle(folder, &boffo, FH_READ_ONLY);
+ ASSERT_TRUE(batch, fh && Obj_Is_A(fh, RAMFILEHANDLE),
+ "opened FileHandle for reading");
+ DECREF(fh);
+
+ Err_set_error(NULL);
+ fh = Folder_Local_Open_FileHandle(folder, &nope, FH_READ_ONLY);
+ ASSERT_TRUE(batch, fh == NULL,
+ "Can't open non-existent file for reading");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Opening non-existent file for reading sets Err_error");
+
+ DECREF(folder);
+}
+
+static void
+test_Local_Delete(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ FileHandle *fh;
+
+ fh = Folder_Open_FileHandle(folder, &boffo, FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ ASSERT_TRUE(batch, Folder_Local_Delete(folder, &boffo),
+ "Local_Delete on file succeeds");
+
+ Folder_Local_MkDir(folder, &foo);
+ fh = Folder_Open_FileHandle(folder, &foo_boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+
+ Err_set_error(NULL);
+ ASSERT_FALSE(batch, Folder_Local_Delete(folder, &foo),
+ "Local_Delete on non-empty dir fails");
+
+ Folder_Delete(folder, &foo_boffo);
+ ASSERT_TRUE(batch, Folder_Local_Delete(folder, &foo),
+ "Local_Delete on empty dir succeeds");
+
+ DECREF(folder);
+}
+
+static void
+test_Rename(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ FileHandle *fh;
+ bool_t result;
+
+ Folder_MkDir(folder, &foo);
+ Folder_MkDir(folder, &foo_bar);
+ fh = Folder_Open_FileHandle(folder, &boffo, FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+
+ /* Move files. */
+
+ result = Folder_Rename(folder, &boffo, &banana);
+ ASSERT_TRUE(batch, result, "Rename succeeds and returns true");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &banana),
+ "File exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &boffo),
+ "File no longer exists at old path");
+
+ result = Folder_Rename(folder, &banana, &foo_bar_boffo);
+ ASSERT_TRUE(batch, result, "Rename to file in nested dir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar_boffo),
+ "File exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &banana),
+ "File no longer exists at old path");
+
+ result = Folder_Rename(folder, &foo_bar_boffo, &boffo);
+ ASSERT_TRUE(batch, result, "Rename from file in nested dir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &boffo),
+ "File exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &foo_bar_boffo),
+ "File no longer exists at old path");
+
+ fh = Folder_Open_FileHandle(folder, &foo_boffo,
+ FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ result = Folder_Rename(folder, &boffo, &foo_boffo);
+ ASSERT_TRUE(batch, result, "Clobber");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &boffo),
+ "File no longer exists at old path");
+
+ /* Move Dirs. */
+
+ Folder_MkDir(folder, &baz);
+ result = Folder_Rename(folder, &baz, &boffo);
+ ASSERT_TRUE(batch, result, "Rename dir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &boffo),
+ "Folder exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &baz),
+ "Folder no longer exists at old path");
+
+ result = Folder_Rename(folder, &boffo, &foo_foo);
+ ASSERT_TRUE(batch, result, "Rename dir into nested subdir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_foo),
+ "Folder exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &boffo),
+ "Folder no longer exists at old path");
+
+ result = Folder_Rename(folder, &foo_foo, &foo_bar_baz);
+ ASSERT_TRUE(batch, result, "Rename dir from nested subdir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar_baz),
+ "Folder exists at new path");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &foo_foo),
+ "Folder no longer exists at old path");
+
+ /* Test failed clobbers. */
+
+ Err_set_error(NULL);
+ result = Folder_Rename(folder, &foo_boffo, &foo_bar);
+ ASSERT_FALSE(batch, result, "Rename file clobbering dir fails");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Failed rename sets Err_error");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File still exists at old path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar),
+ "Dir still exists after failed clobber");
+
+ Err_set_error(NULL);
+ result = Folder_Rename(folder, &foo_bar, &foo_boffo);
+ ASSERT_FALSE(batch, result, "Rename dir clobbering file fails");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Failed rename sets Err_error");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar),
+ "Dir still exists at old path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File still exists after failed clobber");
+
+ /* Test that "renaming" succeeds where to and from are the same. */
+
+ result = Folder_Rename(folder, &foo_boffo, &foo_boffo);
+ ASSERT_TRUE(batch, result, "Renaming file to itself succeeds");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File still exists");
+
+ result = Folder_Rename(folder, &foo_bar, &foo_bar);
+ ASSERT_TRUE(batch, result, "Renaming dir to itself succeeds");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar),
+ "Dir still exists");
+
+ /* Invalid filepaths. */
+
+ Err_set_error(NULL);
+ result = Folder_Rename(folder, &foo_boffo, &nope_nyet);
+ ASSERT_FALSE(batch, result, "Rename into non-existent subdir fails");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Renaming into non-existent subdir sets Err_error");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "Entry still exists at old path");
+
+ Err_set_error(NULL);
+ result = Folder_Rename(folder, &nope_nyet, &boffo);
+ ASSERT_FALSE(batch, result, "Rename non-existent file fails");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Renaming non-existent source file sets Err_error");
+
+ DECREF(folder);
+}
+
+static void
+test_Hard_Link(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ FileHandle *fh;
+ bool_t result;
+
+ Folder_MkDir(folder, &foo);
+ Folder_MkDir(folder, &foo_bar);
+ fh = Folder_Open_FileHandle(folder, &boffo, FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+
+ /* Link files. */
+
+ result = Folder_Hard_Link(folder, &boffo, &banana);
+ ASSERT_TRUE(batch, result, "Hard_Link succeeds and returns true");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &banana),
+ "File exists at new path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &boffo),
+ "File still exists at old path");
+ Folder_Delete(folder, &boffo);
+
+ result = Folder_Hard_Link(folder, &banana, &foo_bar_boffo);
+ ASSERT_TRUE(batch, result, "Hard_Link to target within nested dir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar_boffo),
+ "File exists at new path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &banana),
+ "File still exists at old path");
+ Folder_Delete(folder, &banana);
+
+ result = Folder_Hard_Link(folder, &foo_bar_boffo, &foo_boffo);
+ ASSERT_TRUE(batch, result, "Hard_Link from file in nested dir");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File exists at new path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_bar_boffo),
+ "File still exists at old path");
+ Folder_Delete(folder, &foo_bar_boffo);
+
+ /* Invalid clobbers. */
+
+ fh = Folder_Open_FileHandle(folder, &boffo, FH_CREATE | FH_WRITE_ONLY);
+ DECREF(fh);
+ result = Folder_Hard_Link(folder, &foo_boffo, &boffo);
+ ASSERT_FALSE(batch, result, "Clobber of file fails");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &boffo),
+ "File still exists at new path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File still exists at old path");
+ Folder_Delete(folder, &boffo);
+
+ Folder_MkDir(folder, &baz);
+ result = Folder_Hard_Link(folder, &foo_boffo, &baz);
+ ASSERT_FALSE(batch, result, "Clobber of dir fails");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &baz),
+ "Dir still exists at new path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File still exists at old path");
+ Folder_Delete(folder, &baz);
+
+ /* Invalid Hard_Link of dir. */
+
+ Folder_MkDir(folder, &baz);
+ result = Folder_Hard_Link(folder, &baz, &banana);
+ ASSERT_FALSE(batch, result, "Hard_Link dir fails");
+ ASSERT_FALSE(batch, Folder_Exists(folder, &banana),
+ "Nothing at new path");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &baz),
+ "Folder still exists at old path");
+ Folder_Delete(folder, &baz);
+
+ /* Test that linking to yourself fails. */
+
+ result = Folder_Hard_Link(folder, &foo_boffo, &foo_boffo);
+ ASSERT_FALSE(batch, result, "Hard_Link file to itself fails");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "File still exists");
+
+ /* Invalid filepaths. */
+
+ Err_set_error(NULL);
+ result = Folder_Rename(folder, &foo_boffo, &nope_nyet);
+ ASSERT_FALSE(batch, result, "Hard_Link into non-existent subdir fails");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Hard_Link into non-existent subdir sets Err_error");
+ ASSERT_TRUE(batch, Folder_Exists(folder, &foo_boffo),
+ "Entry still exists at old path");
+
+ Err_set_error(NULL);
+ result = Folder_Rename(folder, &nope_nyet, &boffo);
+ ASSERT_FALSE(batch, result, "Hard_Link non-existent source file fails");
+ ASSERT_TRUE(batch, Err_get_error() != NULL,
+ "Hard_Link non-existent source file sets Err_error");
+
+ DECREF(folder);
+}
+
+static void
+test_Close(TestBatch *batch)
+{
+ RAMFolder *folder = RAMFolder_new(NULL);
+ RAMFolder_Close(folder);
+ PASS(batch, "Close() concludes without incident");
+ RAMFolder_Close(folder);
+ RAMFolder_Close(folder);
+ PASS(batch, "Calling Close() multiple times is safe");
+ DECREF(folder);
+}
+
+void
+TestRAMFolder_run_tests()
+{
+ TestBatch *batch = Test_new_batch("TestRAMFolder", 97, NULL);
+
+ PLAN(batch);
+ test_Initialize_and_Check(batch);
+ test_Local_Exists(batch);
+ test_Local_Is_Directory(batch);
+ test_Local_Find_Folder(batch);
+ test_Local_MkDir(batch);
+ test_Local_Open_FileHandle(batch);
+ test_Local_Delete(batch);
+ test_Rename(batch);
+ test_Hard_Link(batch);
+ test_Close(batch);
+
+ batch->destroy(batch);
+}
+
+/* Copyright 2009 The Apache Software 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Store/TestRAMFolder.c
------------------------------------------------------------------------------
svn:eol-style = native

Added: lucene/lucy/trunk/perl/lib/Lucy/Store/RAMFolder.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Store/RAMFolder.pm?rev=883479&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Store/RAMFolder.pm (added)
+++ lucene/lucy/trunk/perl/lib/Lucy/Store/RAMFolder.pm Mon Nov 23 20:03:40 2009
@@ -0,0 +1,50 @@
+use Lucy;
+
+1;
+
+__END__
+
+__BINDING__
+
+my $synopsis = <<'END_SYNOPSIS';
+ my $folder = Lucy::Store::RAMFolder->new;
+
+ # or sometimes...
+ my $folder = Lucy::Store::RAMFolder->new(
+ path => $relative_path,
+ );
+END_SYNOPSIS
+
+my $constructor = <<'END_CONSTRUCTOR';
+ my $folder = Lucy::Store::RAMFolder->new(
+ path => $relative_path, # default: empty string
+ );
+END_CONSTRUCTOR
+
+Boilerplater::Binding::Perl::Class->register(
+ parcel => "Lucy",
+ class_name => "Lucy::Store::RAMFolder",
+ bind_constructors => ["new"],
+ make_pod => {
+ synopsis => $synopsis,
+ constructor => { sample => $constructor },
+ }
+);
+
+__COPYRIGHT__
+
+/* Copyright 2009 The Apache Software 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.
+ */
+

Propchange: lucene/lucy/trunk/perl/lib/Lucy/Store/RAMFolder.pm
------------------------------------------------------------------------------
svn:eol-style = native

Modified: lucene/lucy/trunk/perl/lib/Lucy/Test.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Test.pm?rev=883479&r1=883478&r2=883479&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Test.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Test.pm Mon Nov 23 20:03:40 2009
@@ -52,6 +52,9 @@
else if (strEQ(package, "TestRAMFileHandle")) {
lucy_TestRAMFH_run_tests();
}
+ else if (strEQ(package, "TestRAMFolder")) {
+ lucy_TestRAMFolder_run_tests();
+ }
/* Lucy::Util */
else if (strEQ(package, "TestIndexFileNames")) {
lucy_TestIxFileNames_run_tests();

Added: lucene/lucy/trunk/perl/t/core/104-ram_folder.t
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/t/core/104-ram_folder.t?rev=883479&view=auto
==============================================================================
--- lucene/lucy/trunk/perl/t/core/104-ram_folder.t (added)
+++ lucene/lucy/trunk/perl/t/core/104-ram_folder.t Mon Nov 23 20:03:40 2009
@@ -0,0 +1,6 @@
+use strict;
+use warnings;
+
+use Lucy::Test;
+Lucy::Test::run_tests("TestRAMFolder");
+

Propchange: lucene/lucy/trunk/perl/t/core/104-ram_folder.t
------------------------------------------------------------------------------
svn:eol-style = native

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouplucy-commits @
categorieslucene
postedNov 23, '09 at 8:04p
activeNov 23, '09 at 8:04p
posts1
users1
websitelucene.apache.org

1 user in discussion

Marvin: 1 post

People

Translate

site design / logo © 2018 Grokbase