2

I feel that the question is simple yet I was surprised to not find any documentation nor information on it at all. I simply want to get or compute the size in bytes of a single couchbase database.

For example, I have a database that stores all the information of cars at a dealership. There are multiple documents within the database. I would like to figure out how to compute the non-compressed total size of the database. This include everything in the database (attachments, text, everything)

Ideally, using Swift 3.0. However, I can port over language to swift if anyone knows how to get the database size in any language.

   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?

   }
Sean Mayes
  • 158
  • 10

2 Answers2

1

I was hoping for a higher level function provided by couchbase for getting the database size. However, that does not exist and you have to do file io. Modifying a solution from How To Get Directory Size With Swift On OS X, I created the function below (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)
}
Community
  • 1
  • 1
Sean Mayes
  • 158
  • 10
0

A Couchbase Lite database is stored as a directory in the filesystem, so all you have to do is add up the sizes of the files in the directory. (Recursively, although in practice there are only 2 levels of files.)

There's no direct accessor for the directory of a database, but you can start from the directory of the CBLManager (using its directory property) and then append the name of the database with a .cblite2 filename extension.

PS: It makes your questions easier to identify if you specify "Couchbase Lite". Just saying "Couchbase" gives the impression you're asking about the flagship server database.

Jens Alfke
  • 1,946
  • 12
  • 15