swift - 你如何获得 couchbase 数据库的大小

标签 swift database size couchbase

我觉得这个问题很简单,但我很惊讶没有找到任何文档或信息。我只想获取或计算单个 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 提供更高级别的函数来获取数据库大小。但是,那不存在,您必须执行 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)
}

关于swift - 你如何获得 couchbase 数据库的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41787621/

相关文章:

swift - 实时发光着色器混淆

mysql - surrogate & composite key的性能比较

c++ - 如果枚举不能放入整数类型会发生什么?

.net - 忽略 .net 中窗口字体大小的增加

swift - 如何从 UIPageViewController 打开 View 并返回到 UIPageViewController

ios - 如何以编程方式使用 SF Symbol?

swift - 如何快速更改 UIDatePickerView 中的月份名称?

database - SQLite 数据库中的身份列最大值

php - 我不能做外键,约束错误

gwt - 如何获取 <div> 的渲染大小