[Ngms-svn] SVN-Commit: [98] [NMTree] implements TDescriptionElement.update, set

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 1月 28日 (木) 11:00:39 JST


Revision: 98
          http://sourceforge.jp/projects/ngms/svn/view?view=rev&revision=98
Author:   osiire
Date:     2010-01-28 11:00:39 +0900 (Thu, 28 Jan 2010)

Log Message:
-----------
[NMTree] implements TDescriptionElement.update, set
[NMShell] fix command parser bugs.
[NMShell] ngms shell now can be waked in any current directory.

Modified Paths:
--------------
    trunk/source/NMShell/src/info/ngms/nmshell/Main.scala
    trunk/source/NMShell/src/info/ngms/nmshell/NMShellCommandParser.scala
    trunk/source/NMShell/src/info/ngms/nmshell/NMShellConfigFile.scala
    trunk/source/NMShell/test/CommandParserTest.scala
    trunk/source/NMTree/src/info/ngms/nmtree/AbstractDescriptionElement.scala
    trunk/source/NMTree/src/info/ngms/nmtree/NMDescription.scala
    trunk/source/NMTree/src/info/ngms/nmtree/NMFileSystemTree.scala
    trunk/source/NMTree/src/info/ngms/nmtree/NMTree.scala
    trunk/source/NMTree/test/NMTreeBehavior.scala
    trunk/source/bin/ngms
    trunk/source/bin/ngms.bat

Modified: trunk/source/NMShell/src/info/ngms/nmshell/Main.scala
===================================================================
--- trunk/source/NMShell/src/info/ngms/nmshell/Main.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMShell/src/info/ngms/nmshell/Main.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -16,10 +16,11 @@
     }
 
     def main(args : Array[String]) : Unit = {
-        parseArgs(args)
+        val binaryPath = args(0)
+        parseArgs(args.drop(0))
         NMTree.init( NMShellEnvironment )
         val root = new NMPath(NMPath.root)
-        val config : NMShellConfigFile = new NMShellConfigFile(NMShellConfigFile.defaultPath)
+        val config : NMShellConfigFile = new NMShellConfigFile(binaryPath)
         config.rootType match {
             case NMTreeImplementations.RowFileSystem => {
                 val fs = new info.ngms.nmtree.NMFileSystemTree( config.rootPath, root )

Modified: trunk/source/NMShell/src/info/ngms/nmshell/NMShellCommandParser.scala
===================================================================
--- trunk/source/NMShell/src/info/ngms/nmshell/NMShellCommandParser.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMShell/src/info/ngms/nmshell/NMShellCommandParser.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -104,8 +104,9 @@
     }
     def number = "[1-9]+[0-9]*".r
 
-    def command = ((command_name <~ spaces) ~ repsep(cmd_string, spaces)) ^^ {
-        case name ~ args => new Command(name, args)
+    def command = command_name ~ opt(spaces ~> repsep(cmd_string, spaces)) ^^ {
+        case name ~ Some(args) => new Command(name, args)
+        case name ~ None => new Command(name, Nil)
     }
 
     def command_name = "[^ :<>|&`\"]+".r

Modified: trunk/source/NMShell/src/info/ngms/nmshell/NMShellConfigFile.scala
===================================================================
--- trunk/source/NMShell/src/info/ngms/nmshell/NMShellConfigFile.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMShell/src/info/ngms/nmshell/NMShellConfigFile.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -5,16 +5,19 @@
 package info.ngms.nmshell
 
 object NMShellConfigFile {
-    val defaultPath = "share/ngms/ngms.conf"
-    val userPath = "~/.ngms/ngms.conf"
+    val defaultConfigPath = "share/ngms/ngms.conf"
+    val userConfigPath = "~/.ngms/ngms.conf"
+    val defaultRootPath = "share/ngms/data/trunk"
+    val initialUser = "root"
 }
 
 /**
  * ngms.confファイルを表現するクラス
  */
-class NMShellConfigFile ( path : String ) {
+class NMShellConfigFile ( val binaryPath : String ) {
     // 未実装
-    val rootPath = "./share/ngms/data"
-    val initialUser = "root"
+    val configPath = binaryPath + "../" + NMShellConfigFile.defaultConfigPath
+    val rootPath = binaryPath + "../" + NMShellConfigFile.defaultRootPath
+    val initialUser = NMShellConfigFile.initialUser
     val rootType = info.ngms.nmtree.NMTreeImplementations.RowFileSystem
 }
\ No newline at end of file

Modified: trunk/source/NMShell/test/CommandParserTest.scala
===================================================================
--- trunk/source/NMShell/test/CommandParserTest.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMShell/test/CommandParserTest.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -35,6 +35,18 @@
         }
     }
 
+    test("args2") {
+        val input = "ls"
+        val line = NMShellCommandParser.parse(NMShellCommandParser.line, input).get
+        line match {
+            case Exit => assert(false)
+            case Statements(sts) => {
+                assert(sts.length == 1)
+                assert(sts.head.cmds.head.name === "ls")
+            }
+        }
+    }
+
     test("pipe") {
         val input = " foo -la | bar -args "
         val line = NMShellCommandParser.parse(NMShellCommandParser.line, input).get

Modified: trunk/source/NMTree/src/info/ngms/nmtree/AbstractDescriptionElement.scala
===================================================================
--- trunk/source/NMTree/src/info/ngms/nmtree/AbstractDescriptionElement.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMTree/src/info/ngms/nmtree/AbstractDescriptionElement.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -81,15 +81,23 @@
     }
 
     def update(tag : String, value : String, comment : String ) : Unit = {
-        val desc = header(DescriptionElement.normalizeTag(tag))
-        desc.value = value
-        desc.comment = comment
+        val ntag = DescriptionElement.normalizeTag(tag)
+        header.update(ntag, new NMDescription(ntag, value, comment))
      }
 
-    def set( dscr : NMDescription ) : Unit = { }
+    def set( dscr : NMDescription ) : Unit = {
+        val ntag = DescriptionElement.normalizeTag(dscr.tag)
+        header.update(ntag, dscr)
+    }
 
-    def update( f : List[NMDescription] => List[NMDescription] ) : Unit = { }
+    def replace( ds : List[NMDescription] ) : Unit = {
+        header.clear()
+        ds.foreach(set)
+    }
 
+    def update( f : List[NMDescription] => List[NMDescription] ) : Unit = {
+        replace(f(descriptions))
+    }
 }
 
 object DescriptionElement {
@@ -109,7 +117,7 @@
         tag.toLowerCase // tags in Map should be lower case.
 
     def normalizeValue(tag : String, value : String) : NMDescription =
-        new NMDescription(tag, value, "")
+        NMDescription.make(tag, value)
 
     def normalize(tag : String, value : String) =
         normalizeTag(tag) -> normalizeValue(tag, value)

Modified: trunk/source/NMTree/src/info/ngms/nmtree/NMDescription.scala
===================================================================
--- trunk/source/NMTree/src/info/ngms/nmtree/NMDescription.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMTree/src/info/ngms/nmtree/NMDescription.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -10,13 +10,19 @@
  * @author ogasa****@itpl*****
  */
  class NMDescription ( val tag : String, var value : String, var comment : String)
+ {
+    override def toString() : String = {
+        tag + ":" + value + NMDescription.commentDelimiter + comment
+    }
+ }
 
  /**
  * タグとそれに関連付いた値を保持する構造。コメント付き。
  * @version $Id$
  * @author ogasa****@itpl*****
  */
- object NMDescription {
+ object NMDescription
+ {
     /**
      * コメントの直前につけるマークの定義
      */
@@ -34,6 +40,6 @@
             new NMDescription(tag, parts(0).trim, "")
         else
             new NMDescription(tag, parts(0).trim, parts(1).trim)
-    }
- }
+     }
+}
 

Modified: trunk/source/NMTree/src/info/ngms/nmtree/NMFileSystemTree.scala
===================================================================
--- trunk/source/NMTree/src/info/ngms/nmtree/NMFileSystemTree.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMTree/src/info/ngms/nmtree/NMFileSystemTree.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -12,7 +12,10 @@
 import java.util.Date
 import scala.collection.mutable.LinkedHashMap
 
-
+/**
+ *  NMTreeImplementationの実装。
+ *  OS上のファイルシステムを扱い、それをNMTreeImplementationとして利用できるようラップする。
+ */
 class NMFileSystemTree( realRoot : String, mnt : NMPath ) {
     var mountPoint : NMPath = mnt
 
@@ -24,21 +27,18 @@
         new NMPath( path )
     }
 
-    trait Identifier {
-        val id = 0L // 未実装
-    }
+    val crlf : Array[Byte] = Array(0x0d, 0x0a)
 
     def createStream( path : NMPath ) : NMTree.TStreamElement = {
         val file = new File( path )
         file.createNewFile()
         new {
-            val input : InputStream = new FileInputStream( file )
-            val output : OutputStream = {
-                new FileOutputStream( file )
-            }
-            def close : Unit =
+            lazy val input : InputStream = new FileInputStream( file )
+            lazy val output : OutputStream = new FileOutputStream( file )
+            def close : Unit = {
                 input.close
                 output.close
+            }
         }
     }
 
@@ -62,7 +62,7 @@
                     for (line <- h) {
                         line match {
                             case DescriptionElement(tag, value) =>
-                                map + (DescriptionElement.normalizeTag(tag) -> DescriptionElement.normalizeValue(tag, value))
+                                map + DescriptionElement.normalize(tag, value)
                             case _ =>
                                 ()
                         }
@@ -78,6 +78,33 @@
                     val header = lheader
                     val body = lbody
                     def close : Unit = ()
+                    private def writeLine( out : FileOutputStream, line : String ) : Unit = {
+                        for(c <- line) { out.write(c) }
+                        out.write(crlf)
+                    }
+                    var smode = false
+                    def delaySync( mode : Boolean ) : Unit = smode = mode
+                    def sync : Unit = {
+                        val output = new FileOutputStream( file )
+                        for( (_, dscr) <- header ) {
+                            writeLine(output, dscr.toString)
+                        }
+                        body.foreach(writeLine(output, _))
+                        output.close
+                    }
+                    override def set( dscr : NMDescription ) : Unit = {
+                        super.set( dscr )
+                        if( !smode ) {
+                            sync
+                        }
+                    }
+                    override def update( f : List[NMDescription] => List[NMDescription] ) : Unit = {
+                        super.update(f)
+                        if( !smode ) {
+                            sync
+                        }
+                    }
+
                 }
             } catch {
                 case e: java.nio.BufferUnderflowException => // load binary file

Modified: trunk/source/NMTree/src/info/ngms/nmtree/NMTree.scala
===================================================================
--- trunk/source/NMTree/src/info/ngms/nmtree/NMTree.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMTree/src/info/ngms/nmtree/NMTree.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -196,6 +196,18 @@
         def findWith[A](tag : String) (default : A) (f : NMDescription => A) : A
 
         /*
+         *  set及びupdateメソッドにてエレメントへの永続化を行うかどうかのフラグ。*  
+         *  @param mode trueの場合には、set及びupdateメソッドにてこのインスタンスが保持しているDescription情報をエレメントへの永続化する処理が省略されます。
+         *  falseの場合には、set及びupdateメソッドにてこのインスタンスが保持しているDescription情報がエレメントへの永続化されます。
+         */
+        def delaySync( mode : Boolean ) : Unit
+
+        /*
+         *  このインスタンスが保持しているDescription情報をエレメントへ永続化します。
+         */
+        def sync : Unit
+
+        /*
          * 指定されたタグと値をエレメントに書き込む。
          * @param tag キーと成るタグ。
          * @param value タグに対応する値。

Modified: trunk/source/NMTree/test/NMTreeBehavior.scala
===================================================================
--- trunk/source/NMTree/test/NMTreeBehavior.scala	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/NMTree/test/NMTreeBehavior.scala	2010-01-28 02:00:39 UTC (rev 98)
@@ -13,9 +13,29 @@
         assert( path.exists === true )
         assert( path.isDir === true )
     }
-    test("ディレクトリの削除") (pending)
-    test("ディレクトリの移動") (pending)
+    test("ディレクトリの削除") {
+        val path = new NMPath("/rdir")
+        NMTree.createDir( path )
+        assert( path.exists === true )
+        assert( path.isDir === true )
+        NMTree.localCommit(List(Delete(path, false)))
+        assert( path.exists === false )
+    }
+    test("ディレクトリの移動") {
+        val d1 = new NMPath("/dir1")
+        val d2 = new NMPath("/dir2")
+        val abc = new NMPath("/dir1/abc")
+        NMTree.createDir( d1 )
+        NMTree.createDir( d2 )
+        val a = NMTree.createStream(abc)
+        val abc2 = new NMPath("/dir2/abc")
+        NMTree.localCommit(List(Move(abc, abc2)))
+        assert( abc.exists === false )
+        assert( abc2.exists === true )
+    }
+
     test("Tree実装をまたがるディレクトリの移動") (pending)
+
     test("ファイルの作成及び削除") {
         val path = new NMPath("/willdelete")
         val stream = NMTree.createStream(path)
@@ -45,10 +65,39 @@
         assert ( src.exists === true )
         assert ( dst.exists === true )
     }
-    test("ファイルへの書き込み") (pending)
-    test("ファイルからの読み込み") (pending)
-    test("Descriptionファイルへの書き込み") (pending)
-    test("Descriptionファイルからの読み込み") (pending)
+    test("ファイルへの書き込み") {
+        val src = new NMPath("/writable")
+        val stream = NMTree.createStream(src)
+        stream.output.write(100)
+        stream.output.flush
+        assert( stream.input.read === 100 )
+        stream.close
+    }
+    test("ファイルからの読み込み") {
+        val w = new NMPath("/readable")
+        val ws = NMTree.createStream(w)
+        ws.output.write(122)
+        ws.output.flush
+        ws.close
+        val src = new NMPath("/readable")
+        val stream = NMTree.createStream(src)
+        assert( stream.input.read === 122 )
+        stream.close
+    }
+    test("Descriptionファイルへの読み書き") {
+        val w = new NMPath("/wd")
+        val ws = NMTree.createDescription(w)
+        ws.set(new NMDescription("key", "value", "comment"))
+        ws.close
+        val r = new NMPath("/wd")
+        val rs = NMTree.createDescription(r)
+        rs.findVal("key") match {
+          case Some(v) => assert( v === "value" )
+          case _ => assert( false )
+        }
+        rs.close
+    }
+
     test("Tableファイルへの書き込み") (pending)
     test("Tableファイルからの読み込み") (pending)
     test("XMLファイルへの書き込み") (pending)

Modified: trunk/source/bin/ngms
===================================================================
--- trunk/source/bin/ngms	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/bin/ngms	2010-01-28 02:00:39 UTC (rev 98)
@@ -1,3 +1,5 @@
 #!/bin/sh
-libs=`dirname $0`/../share/ngms
-scala -cp ${libs}/ngms.jar:${libs}extends/ info.ngms.nmshell.Main $*
+here=`dirname $0`
+libs=${here}/../share/ngms
+scala -cp ${libs}/ngms.jar:${libs}/extends/ info.ngms.nmshell.Main ${here} $*
+

Modified: trunk/source/bin/ngms.bat
===================================================================
--- trunk/source/bin/ngms.bat	2010-01-27 11:27:06 UTC (rev 97)
+++ trunk/source/bin/ngms.bat	2010-01-28 02:00:39 UTC (rev 98)
@@ -1,2 +1,2 @@
 @echo off
-scala -cp %~dp0..\share\ngms\ngms.jar;%~dp0..\share\ngms\extends\ info.ngms.nmshell.Main %*
+scala -cp %~dp0..\share\ngms\ngms.jar;%~dp0..\share\ngms\extends\ info.ngms.nmshell.Main %~dp0 %*




Ngms-svn メーリングリストの案内
Back to archive index