Pre-built database

Starting with Prebuilt Database

In this section, you will learn how to bundle a pre-built Couchbase Lite database in an application. It can be a lot more efficient to bundle static or semi-static content database in your application and install it on the first launch. Even if some of the content changes on the server after you create the app, the app’s first pull replication will bring the database up to date. Here, you will use a pre-built database that contains only airport and hotel documents. The code below moves the pre-built database from the bundled location to the Application Support directory.

Open the file DbManager.java We will review the openUserDb(@Nonnull String username, @Nonnull char[] password) method.

This method first checks if a database file already exists for specific user. If it doesn’t exist it loads the database from the resources directory, unzips and copies it over to a folder created for ths user.

void openUserDb(@Nonnull String username, @Nonnull char[] password)
    throws IOException, CouchbaseLiteException, AuthenticationException, URISyntaxException {
    final File dbDir = new File(DB_DIR, username);

    final DatabaseConfiguration config = new DatabaseConfiguration();
    config.setDirectory(dbDir.getCanonicalPath());

    if (!new File(dbDir, DB_NAME + DB_SUFFIX).exists()) {
        Database.copy(unzipDb().getCanonicalFile(), DB_NAME, config);
    }

    database = new Database(DB_NAME, config);
    database.createIndex(
        FTS_INDEX_DESC,
        IndexBuilder.fullTextIndex(FullTextIndexItem.property(Hotel.PROP_DESCRIPTION)));
    LOGGER.log(Level.INFO, "user db: " + config.getDirectory());

    ...
}

Try it out

  • Log into the Travel Sample Mobile app as “demo” user and password as “password”

  • Click on "FLIGHTS`" button to make a flight reservation

  • In the “From” airport text field, enter “London”

  • Confirm that you see a list of flights in the drop down menu.

    java prebuilt db