我觉得这个问题很简单,但我很惊讶没有找到任何文档或信息。我只想获取或计算单个 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/