2017-01-22 7 views
1

私は質問がシンプルだと感じていますが、私はドキュメントや情報が一切見つからないことに驚いていました。私は単純に単一のcouchbaseデータベースのサイズを取得または計算したいだけです。あなたはどのようにcouchbaseデータベースのサイズを取得しますか

たとえば、ディーラーに車のすべての情報を格納するデータベースがあります。データベースには複数の文書があります。私はデータベースの圧縮されていない合計サイズを計算する方法を理解したいと思います。これには、データベース内のすべてのもの(添付ファイル、テキスト、すべて)が含まれます。

理想的には、Swift 3.0を使用します。しかし、どの言語でデータベースサイズを取得するかを知っていれば、言語を移植することができます。

func openDatabase() -> CBLDatabase 
    { 
     var db : CBLDatabase? 
     let db_name = "dealership" 
     let options = CBLDatabaseOptions() 
     options.create = true 

     do 
     { 
      // This will create a database if one does not exist. Otherwise, it will open it. 
      try db = CBLManager.sharedInstance().openDatabaseNamed(db_name, with: options) 

      // I would love if this was a thing! Since it is not, I would like to write a function to get the database size. 
      let db_size = db.getSize() // <-- This is my question. How to compute the database size. 
     } 
     catch let error as NSError 
     { 
      NSLog("Some error %@", error) 
     } 

     return db 
    } 

    /** Gets the size of the database in MB */ 
    func getSize() -> Int 
    { 
     // What goes here? 

    } 
+0

Swiftを使用しているので、これはCouchbase Mobileに関するものとしますか? –

答えて

0

AのCouchbase Liteデータベースは、あなたがしなければならないすべては、ディレクトリ内のファイルのサイズを合計で、ファイルシステム内のディレクトリとして格納されます。 (再帰的には実際には2つのレベルのファイルしかありませんが)

データベースのディレクトリに直接アクセサはありませんが、CBLManagerのディレクトリ(directoryプロパティを使用)から開始して、ファイル名の拡張子は.cblite2です。

PS:「Couchbase ライト」と指定すると、質問を簡単に識別できます。ちょうど "Couchbase"と言うと、あなたはフラッグシップサーバデータベースについて質問しているという印象を与えます。

0

私はデータベースサイズを得るためにcouchbaseによって提供されるより高いレベルの機能を望んでいました。しかし、それは存在せず、ファイルioを実行する必要があります。 How To Get Directory Size With Swift On OS Xから解決策を修正し、私は以下の関数を作成しました(Swift 3.0)。

func getDatabaseSize(_ db_name : String) -> UInt64 
{ 
    // Build the couchbase database url 
    let url_str = CBLManager.sharedInstance().directory + "/" + db_name + ".cblite2" 
    let url = NSURL(fileURLWithPath: url_str) as URL 

    var size = 0 
    var is_dir: ObjCBool = false 

    // Verify that the file exist and is a directory 
    if (FileManager.default.fileExists(atPath: url.path, isDirectory: &is_dir) && is_dir.boolValue) 
    { 
     FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.fileSizeKey], options: [])?.forEach 
     { 
      size += (try? ($0 as? URL)?.resourceValues(forKeys: [.fileSizeKey]))??.fileSize ?? 0 
     } 
    } 

    return UInt64(size) 
} 
関連する問題