【问题标题】:How do you get the size of a couchbase database你如何获得沙发数据库的大小
【发布时间】:2017-01-22 05:19:05
【问题描述】:

我觉得这个问题很简单,但令我惊讶的是根本没有找到任何文档或信息。我只是想获取或计算单个 couchbase 数据库的大小(以字节为单位)。

例如,我有一个数据库,用于存储经销商处汽车的所有信息。数据库中有多个文档。我想弄清楚如何计算数据库的非压缩总大小。这包括数据库中的所有内容(附件、文本、所有内容)

理想情况下,使用 Swift 3.0。但是,如果有人知道如何获取任何语言的数据库大小,我可以将语言移植到 swift。

   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?

   }

【问题讨论】:

  • 我认为这是关于 Couchbase Mobile,因为您使用的是 Swift?

标签: swift database size couchbase


【解决方案1】:

我希望 couchbase 提供更高级别的函数来获取数据库大小。但是,这不存在,您必须执行 file 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)
}

【讨论】:

    【解决方案2】:

    Couchbase Lite 数据库作为目录存储在文件系统中,因此您只需将目录中文件的大小相加即可。 (递归,虽然在实践中只有 2 级文件。)

    数据库目录没有直接访问器,但您可以从 CBLManager 的目录开始(使用其directory 属性),然后在数据库名称后附加.cblite2 文件扩展名。

    PS:如果您指定“Couchbase Lite”,您的问题会更容易识别。只是说“Couchbase”给人的印象是你在询问旗舰服务器数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-21
      • 2012-02-28
      • 2017-05-09
      • 1970-01-01
      • 2020-02-22
      • 2013-09-28
      • 2010-12-16
      相关资源
      最近更新 更多