<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>couchdbfs.rb.old</filename>
    </added>
    <added>
      <filename>couchdbfs_test.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -2,17 +2,19 @@ require 'fusefs'
 require 'rubygems'
 require 'couchrest'
 
-DEBUG = true
+DEBUG = false #true
 
-class FileCouch &lt; CouchRest::Model
-  use_database CouchRest.database!('http://localhost:5984/couchfs')
-	def FileCouch.usedb
-  	use_database CouchRest.database!('http://localhost:5984/couchfs')
-	end
-	view_by :name
-	view_by :path,
-					:map =&gt; &quot;          function(doc) {
-            if (doc['couchrest-type'] == 'FileCouch' &amp;&amp; doc['path']) {
+class CouchdbDir
+  def initialize()
+  	@db = CouchRest.database!(&quot;http://127.0.0.1:5984/couchfs&quot;)
+		create_view()
+		create_root()
+  end
+
+	def create_view
+      @view = {}
+			@view['path'] = {'map' =&gt; &quot;function(doc) {
+            if (doc['path']) {
 						if(doc['path'] == \&quot;/\&quot;)
 						{
               emit(doc['path'], null);
@@ -23,10 +25,10 @@ class FileCouch &lt; CouchRest::Model
             }
 				}
           }&quot;
+			}
 
-	view_by :base_path,
-					:map =&gt; &quot;          function(doc) {
-            if (doc['couchrest-type'] == 'FileCouch' &amp;&amp; doc['base_path']) {
+			@view['base_path'] = {'map' =&gt; &quot;function(doc) {
+            if (doc['base_path']) {
 						if(doc['base_path'] == \&quot;/\&quot;)
 						{
               emit(doc['base_path'], null);
@@ -37,124 +39,116 @@ class FileCouch &lt; CouchRest::Model
             }
 				}
           }&quot;
-	view_by :type
-end
-
+			}
+      @db.save({
+        &quot;_id&quot; =&gt; &quot;_design/doc&quot;,
+        :views =&gt; @view
+      })
+	end
 
-class CouchdbDir
-  def initialize()
-		@db = FileCouch.usedb()
-		@cache = {}
-  end
+	#create the root dir
+	def create_root
+		if @db.view(&quot;doc/path&quot;, :key =&gt; &quot;/&quot;)['rows'].nitems == 0
+	    ret = {&quot;path&quot; =&gt; &quot;/&quot;, &quot;name&quot; =&gt; &quot;/&quot;, &quot;files&quot; =&gt; []}
+			@db.save(ret)
+		end
+	end
 
   def contents(path)
 		p &quot;call:contents?(#{path})&quot; if DEBUG
-		@cache = {}
-		files = FileCouch.by_path :key =&gt; path
+		files = @db.view(&quot;doc/base_path&quot;, :key =&gt; path)
+		p files if DEBUG
 		ret = []
-		files.each do |fil|
-			ret += fil[&quot;files&quot;].map {|t| t.sub(/^[D|F]/, &quot;&quot;) }
+		files['rows'].each do |fil|
+			ret &lt;&lt; @db.get(fil['id'])['name']
 		end
-		@cache[path] = files
 		return ret
   end
   def directory?(path)
-		p &quot;call:directory?(#{path})&quot; if DEBUG
-		name = File.basename(path)
-		path_only_b = path.sub(/#{name}$/, &quot;&quot;)
-		path_only = path_only_b != &quot;/&quot; ? path_only_b.sub(/\/$/, &quot;&quot;) : path_only_b
-		if @cache[path_only]
-			files = @cache[path_only] 
+		dir = @db.view(&quot;doc/path&quot;, :key =&gt; path)['rows']
+		if dir.nitems &gt; 0
+			return !@db.get(dir[0]['id']).key?(&quot;size&quot;)
 		else
-			files = FileCouch.by_path :key =&gt; path_only
+			return false
 		end
-		trovato = false
-    files.each do |fil|
-			if fil[&quot;files&quot;].include?(&quot;D#{name}&quot;)
-				trovato = true
-			end
-		end
-		return trovato
   end
 
   def file?(path)
-		p &quot;call:file?(#{path})&quot; if DEBUG
-		name = File.basename(path)
-		path_only_b = path.sub(/#{name}$/, &quot;&quot;)
-		path_only = path_only_b != &quot;/&quot; ? path_only_b.sub(/\/$/, &quot;&quot;) : path_only_b
-		if @cache[path_only] != nil
-			files = @cache[path_only]
+		dir = @db.view(&quot;doc/path&quot;, :key =&gt; path)['rows']
+		if dir.nitems &gt; 0
+			return @db.get(dir[0]['id']).key?(&quot;size&quot;)
 		else
-			files = FileCouch.by_path :key =&gt; path_only
-		end
-		trovato = false
-    files.each do |fil|
-			if fil[&quot;files&quot;].include?(&quot;F#{name}&quot;)
-				trovato = true
-			end
+			return false
 		end
-		return trovato
   end
 
   def touch(path)
     puts &quot;#{path} has been pushed like a button!&quot;
   end
   def read_file(path)
-    ret = FileCouch.by_path :key =&gt; path
-		ret.each do |fil|
+    ret = @db.view(&quot;doc/path&quot;, :key =&gt; path)
+		ret['rows'].each do |fil|
 			return @db.fetch_attachment(fil[&quot;_id&quot;], fil[&quot;name&quot;])
 		end
   end
   def size(path)
 		p &quot;call:size(#{path})&quot;
-		ret = FileCouch.by_path :key =&gt; path 
-		ret.each do |fil|
+		ret = @db.view(&quot;doc/path&quot;, :key =&gt; path)
+		ret['rows'].each do |fil|
 			return fil[&quot;size&quot;] ? fil[&quot;size&quot;] : 10
 		end
   end
 
   # File writing
   def can_write?(path)
-    true
+		return @db.view(&quot;doc/path&quot;, :key =&gt; path)['rows'].nitems == 0
   end
-  def write_to(path,body)
+	def write_to(path, body)
+		name = File.basename(path)
+		path_only = path.sub(/#{name}$/, &quot;&quot;)
+    file = {&quot;path&quot; =&gt; path, &quot;base_path&quot; =&gt; path_only, &quot;_attachments&quot; =&gt; { name =&gt; {&quot;data&quot; =&gt; body}}, &quot;name&quot; =&gt; name, &quot;size&quot; =&gt; body.length, &quot;type&quot; =&gt; &quot;f&quot;}
+		@db.save(file)
+	end
+  def write_to_old(path,body)
 		name = File.basename(path)
 		path_only = path.sub(/#{name}$/, &quot;&quot;)
 		body ||= &quot;&quot;
-		ret = FileCouch.by_path :key =&gt; path
-		ret.each do |r|
+		ret = @db.view(&quot;doc/path&quot;, :key =&gt; path)
+		ret['rows'].each do |r|
 			r.destroy
 		end
-    ret = FileCouch.new({&quot;path&quot; =&gt; path, &quot;base_path&quot; =&gt; path_only, &quot;_attachments&quot; =&gt; { name =&gt; {&quot;data&quot; =&gt; body}}, &quot;name&quot; =&gt; name, &quot;size&quot; =&gt; body.length})
-		ret.save	
+    ret = {&quot;type&quot; =&gt; &quot;FileCouch&quot;, &quot;path&quot; =&gt; path, &quot;base_path&quot; =&gt; path_only, &quot;_attachments&quot; =&gt; { name =&gt; {&quot;data&quot; =&gt; body}}, &quot;name&quot; =&gt; name, &quot;size&quot; =&gt; body.length}
+		#ret.save	
+		@db.save(ret)
 		ret3 = {}
 		if path_only != &quot;/&quot;
-	    ret2 = FileCouch.by_path :key =&gt; path_only.sub(/\/$/, &quot;&quot;)
+	    ret2 = @db.view(&quot;doc/path&quot;, :key =&gt; path_only.sub(/\/$/, &quot;&quot;))
 		else
-	    ret2 = FileCouch.by_path :key =&gt; path_only
+	    ret2 = @db.view(&quot;doc/path&quot;, :key =&gt; path_only)
 		end
 		ret3 = nil
-		if ret2.nitems == 0
+		if ret2['rows'].nitems == 0
 			name2 = File.basename(path_only)
 			path_only2 = path_only.sub(/\/#{name2}$/, &quot;&quot;)
 			if path_only2 == &quot;&quot;
 				path_only2 = &quot;/&quot;
 			end
-    	ret3 = FileCouch.new({&quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []})
+    	ret3 = {&quot;type&quot; =&gt; &quot;FileCouch&quot;, &quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []}
 		else
-			ret2.each do |r|
+			ret2['rows'].each do |r|
 				if r[&quot;files&quot;].nitems &lt; 1000
 					ret3 = r
 				end
 			end
 			if ret3 == nil
 				#new
-    		ret3 = FileCouch.new({&quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []})
+    		ret3 = {&quot;type&quot; =&gt; &quot;FileCouch&quot;, &quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []}
 			end
 		end
     ret3[&quot;files&quot;] ||= []
 		ret3[&quot;files&quot;] &lt;&lt; &quot;F#{name}&quot;
-		ret3.save
+		@db.save(ret3)
+		#ret3.save
   end
 
   # Delete a file
@@ -166,11 +160,11 @@ class CouchdbDir
 		path_only_b = path.sub(/#{name}$/, &quot;&quot;)
 		path_only = path_only_b != &quot;/&quot; ? path_only_b.sub(/\/$/, &quot;&quot;) : path_only_b
     
-		ret = FileCouch.by_path :key =&gt; path
+		ret = @db.view(&quot;doc/path&quot;, :key =&gt; path)
 		ret.each do |fil|
 			fil.destroy
 		end
-		ret = FileCouch.by_path :key =&gt; path_only
+		ret = @db.view(&quot;doc/path&quot;, :key =&gt; path_only)
 		ret.each do |dir|
 			if dir[&quot;files&quot;].include?(&quot;D#{name}&quot;)
 				dir[&quot;files&quot;].delete(&quot;D#{name}&quot;)
@@ -180,18 +174,25 @@ class CouchdbDir
 
 
   def can_mkdir?(path)
-		true
+		return @db.view(&quot;doc/path&quot;, :key =&gt; path)['rows'].nitems == 0
   end
   def mkdir(path)
 		name = File.basename(path)
 		path_only = path.sub(/#{name}$/, &quot;&quot;)
-    ret = FileCouch.new({&quot;path&quot; =&gt; path, &quot;base_path&quot; =&gt; path_only, &quot;name&quot; =&gt; name, &quot;files&quot; =&gt; []})
-		ret.save	
+    dir = {&quot;path&quot; =&gt; path, &quot;base_path&quot; =&gt; path_only, &quot;name&quot; =&gt; name}
+		@db.save(dir)
+	end
+  def mkdir_old(path)
+		name = File.basename(path)
+		path_only = path.sub(/#{name}$/, &quot;&quot;)
+    ret = {&quot;type&quot; =&gt; &quot;FileCouch&quot;, &quot;path&quot; =&gt; path, &quot;base_path&quot; =&gt; path_only, &quot;name&quot; =&gt; name, &quot;files&quot; =&gt; []}
+		#ret.save
+		@db.save(ret)
 		ret3 = {}
 		if path_only != &quot;/&quot;
-	    ret2 = FileCouch.by_path :key =&gt; path_only.sub(/\/$/, &quot;&quot;)
+	    ret2 = @db.view(&quot;doc/path&quot;, :key =&gt; path_only.sub(/\/$/, &quot;&quot;))
 		else
-	    ret2 = FileCouch.by_path :key =&gt; path_only
+	    ret2 = @db.view(&quot;doc/path&quot;, :key =&gt; path_only)
 		end
 		ret3 = nil
 		if ret2.nitems == 0
@@ -200,7 +201,7 @@ class CouchdbDir
 			if path_only2 == &quot;&quot;
 				path_only2 = &quot;/&quot;
 			end
-    	ret3 = FileCouch.new({&quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []})
+    	ret3 = {&quot;type&quot; =&gt; &quot;FileCouch&quot;, &quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []}
 		else
 			ret2.each do |r|
 				if r[&quot;files&quot;].nitems &lt; 100
@@ -209,12 +210,13 @@ class CouchdbDir
 			end
 			if ret3 == nil
 				#new
-    		ret3 = FileCouch.new({&quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []})
+    		ret3 = {&quot;type&quot; =&gt; &quot;FileCouch&quot;, &quot;path&quot; =&gt; path_only, &quot;base_path&quot; =&gt; path_only2, &quot;name&quot; =&gt; name2, &quot;files&quot; =&gt; []}
 			end
 		end
     ret3[&quot;files&quot;] ||= []
 		ret3[&quot;files&quot;] &lt;&lt; &quot;D#{name}&quot;
-		ret3.save
+		@db.save(ret3)
+		#ret3.save
   end
 
   # rmdir
@@ -222,33 +224,36 @@ class CouchdbDir
 		true
   end
   def rmdir(path)
-    ret = FileCouch.by_path :key =&gt; path
+    ret = @db.view(&quot;doc/path&quot;, :key =&gt; path)
 		ret.each do |fil|
 			fil.destroy
 		end
   end
 
+	def delete_db
+		@db.delete!
+	end
 end
 
-if (File.basename($0) == File.basename(__FILE__))
-  if (ARGV.size &lt; 1)
-    puts &quot;Usage: #{$0} &lt;directory&gt; &lt;options&gt;&quot;
-    exit
-  end
-
-  dirname, yamlfile = ARGV.shift, ARGV.shift
-
-  unless File.directory?(dirname)
-    puts &quot;Usage: #{dirname} is not a directory.&quot;
-    exit
-  end
-
-  root = CouchdbDir.new()
-
-  # Set the root FuseFS
-  FuseFS.set_root(root)
-
-  FuseFS.mount_under(dirname, *ARGV)
-
-  FuseFS.run # This doesn't return until we're unmounted.
-end
+#if (File.basename($0) == File.basename(__FILE__))
+#  if (ARGV.size &lt; 1)
+#    puts &quot;Usage: #{$0} &lt;directory&gt; &lt;options&gt;&quot;
+#    exit
+#  end
+#
+#  dirname, yamlfile = ARGV.shift, ARGV.shift
+#
+#  unless File.directory?(dirname)
+#    puts &quot;Usage: #{dirname} is not a directory.&quot;
+#    exit
+#  end
+#
+#  root = CouchdbDir.new()
+#
+#  # Set the root FuseFS
+#  FuseFS.set_root(root)
+#
+#  FuseFS.mount_under(dirname, *ARGV)
+#
+#  FuseFS.run # This doesn't return until we're unmounted.
+#end</diff>
      <filename>couchdbfs.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>f0f3d9e185d2ac35062be0001f855d7cd22ebae8</id>
    </parent>
  </parents>
  <author>
    <name>nik</name>
    <email>sacchi.nicola@gmail.com</email>
  </author>
  <url>http://github.com/niky81/couchdbfs/commit/c45a4f1e40f7899f5262f71c8d73f5043ba98bda</url>
  <id>c45a4f1e40f7899f5262f71c8d73f5043ba98bda</id>
  <committed-date>2009-03-01T03:46:56-08:00</committed-date>
  <authored-date>2009-03-01T03:46:56-08:00</authored-date>
  <message>not working yet</message>
  <tree>0220bbdc6281cc3fef5d7f70dfda5058a68a8b7d</tree>
  <committer>
    <name>nik</name>
    <email>sacchi.nicola@gmail.com</email>
  </committer>
</commit>
