com.couchbase.lite.Database Java Examples

The following examples show how to use com.couchbase.lite.Database. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From mini-hacks with MIT License 6 votes vote down vote up
private void setupViewAndQuery() {
    Database database = SyncManager.get().getDatabase();
    LiveQuery liveQuery = database.createAllDocumentsQuery().toLiveQuery();
    liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
        @Override
        public void changed(final LiveQuery.ChangeEvent event) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    docsCountView.setText(String.valueOf(event.getRows().getCount()));
                }
            });
        }
    });
    liveQuery.start();
}
 
Example #2
Source File: MainActivity.java    From mini-hacks with MIT License 6 votes vote down vote up
private void setupQuery() {
    Database database = null;
    try {
        database = manager.getExistingDatabase("todo");
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
    if (database != null) {
        LiveQuery liveQuery = database.createAllDocumentsQuery().toLiveQuery();
        liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
            @Override
            public void changed(final LiveQuery.ChangeEvent event) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        docCountLabel.setText(String.valueOf(event.getRows().getCount()));
                    }
                });
            }
        });
        liveQuery.start();
    }
}
 
Example #3
Source File: C4Log.java    From couchbase-lite-java with Apache License 2.0 6 votes vote down vote up
static void logCallback(String c4Domain, int c4Level, String message) {
    final LogLevel level = Log.getLogLevelForC4Level(c4Level);
    final LogDomain domain = Log.getLoggingDomainForC4Domain(c4Domain);

    final com.couchbase.lite.Log logger = Database.log;

    final ConsoleLogger console = logger.getConsole();
    console.log(level, domain, message);

    final Logger custom = logger.getCustom();
    if (custom != null) { custom.log(level, domain, message); }

    // This is necessary because there is no way to tell when the log level is set on a custom logger.
    // The only way to find out is to ask it.  As each new message comes in from Core,
    // we find the min level for the console and custom loggers and, if necessary, reset the callback level.
    final LogLevel newCallbackLevel = getCallbackLevel(level, custom);
    if (callbackLevel == newCallbackLevel) { return; }

    // This cannot be done synchronously because it will deadlock on the same mutex that is being held
    // for this callback
    CouchbaseLiteInternal.getExecutionService().getMainExecutor().execute(() -> setCoreCallbackLevel(level));
}
 
Example #4
Source File: RatingsAdapter.java    From mini-hacks with MIT License 5 votes vote down vote up
/**
 * Initialize parameters and starts listening on the Live Query for updates from the database.
 * @param context Android context in which the application is running
 * @param query LiveQuery to use in the adapter to populate the Recycler View
 */
public RatingsAdapter(Context context, final LiveQuery query, Database database) {
    this.context = context;
    this.query = query;

    /** Use the database change listener instead of live query listener because we want
     * to listen for conflicts as well
     */
    database.addChangeListener(new Database.ChangeListener() {
        @Override
        public void changed(final Database.ChangeEvent event) {
            ((Activity) RatingsAdapter.this.context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    enumerator = query.getRows();
                    notifyDataSetChanged();
                }
            });
        }
    });
    query.start();
    ((Activity) RatingsAdapter.this.context).runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                enumerator = query.run();
            } catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }
            notifyDataSetChanged();
        }
    });
}
 
Example #5
Source File: PerfTest.java    From couchbase-lite-android with Apache License 2.0 5 votes vote down vote up
protected void eraseDB() {
    closeDB();
    try {
        Database.delete(dbName, new File(dbConfig.getDirectory()));
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
    openDB();
}
 
Example #6
Source File: PerfTest.java    From couchbase-lite-android with Apache License 2.0 5 votes vote down vote up
protected Database closeDB(Database _db) {
    if (_db != null) {
        try {
            _db.close();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
    }
    return null;
}
 
Example #7
Source File: PerfTest.java    From couchbase-lite-android with Apache License 2.0 5 votes vote down vote up
protected void openDB() {
    try {
        db = new Database(dbName, dbConfig);
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: MainActivity.java    From couchbase-lite-android with Apache License 2.0 5 votes vote down vote up
void databaseOperationSDCard() {
    try {
        Manager mgr = new Manager(new SDCardContext(getBaseContext()), null);
        DatabaseOptions opt = new DatabaseOptions();
        opt.setCreate(true);
        Database db = mgr.openDatabase(DB_NAME, opt);

        Map<String, Object> props;
        Document doc = db.getExistingDocument(DOC_ID);
        if (doc == null) {
            // new doc
            doc = db.getDocument(DOC_ID);
            props = new HashMap<>();
            props.put("Database Version", "1.4.1");
            props.put("update", 1);
            doc.putProperties(props);
        } else {
            // update
            props = new HashMap<>(doc.getProperties());
            props.put("update", (Integer) props.get("update") + 1);
            doc.putProperties(props);
        }

        Log.i(TAG, "Num of docs: " + db.getDocumentCount());
        Log.i(TAG, "Doc content: " + db.getDocument(DOC_ID).getProperties());

        db.close();
    } catch (Exception e) {
        Log.e(TAG, "" + e.getMessage());
        e.printStackTrace();
    }
}
 
Example #9
Source File: MainActivity.java    From couchbase-lite-android with Apache License 2.0 5 votes vote down vote up
void databaseOperation() {
    try {
        Manager mgr = new Manager(new AndroidContext(getBaseContext()), null);
        DatabaseOptions opt = new DatabaseOptions();
        opt.setCreate(true);
        Database db = mgr.openDatabase(DB_NAME, opt);

        Map<String, Object> props;
        Document doc = db.getExistingDocument(DOC_ID);
        if (doc == null) {
            // new doc
            doc = db.getDocument(DOC_ID);
            props = new HashMap<>();
            props.put("Database Version", "1.4.1");
            props.put("update", 1);
            doc.putProperties(props);
        } else {
            // update
            props = new HashMap<>(doc.getProperties());
            props.put("update", (Integer) props.get("update") + 1);
            doc.putProperties(props);
        }

        Log.i(TAG, "Num of docs: " + db.getDocumentCount());
        Log.i(TAG, "Doc content: " + db.getDocument(DOC_ID).getProperties());

        db.close();
    } catch (Exception e) {
        Log.e(TAG, "" + e.getMessage());
        e.printStackTrace();
    }
}
 
Example #10
Source File: ReactCBLite.java    From react-native-couchbase-lite with MIT License 5 votes vote down vote up
private void initWithCredentials(Credentials credentials, Callback callback) {
    this.allowedCredentials = credentials;

    try {
        View.setCompiler(new JavaScriptViewCompiler());
        Database.setFilterCompiler(new JavaScriptReplicationFilterCompiler());

        AndroidContext context = new AndroidContext(this.context);

        manager = new Manager(context, Manager.DEFAULT_OPTIONS);

        this.startListener();

        String url;
        if (credentials != null) {
            url = String.format(
                    Locale.ENGLISH,
                    "http://%s:%s@localhost:%d/",
                    credentials.getLogin(),
                    credentials.getPassword(),
                    listener.getListenPort()
            );
        } else {
            url = String.format(
                    Locale.ENGLISH,
                    "http://localhost:%d/",
                    listener.getListenPort()
            );
        }

        callback.invoke(url, null);

    } catch (final Exception e) {
        Log.e(TAG, "Couchbase init failed", e);
        callback.invoke(null, e.getMessage());
    }
}
 
Example #11
Source File: Log.java    From couchbase-lite-java with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public static void initLogging() {
    C4Log.forceCallbackLevel(Database.log.getConsole().getLevel());
    setC4LogLevel(LogDomain.ALL_DOMAINS, LogLevel.DEBUG);
}
 
Example #12
Source File: C4Log.java    From couchbase-lite-java with Apache License 2.0 4 votes vote down vote up
public static void setCallbackLevel(@NonNull LogLevel consoleLevel) {
    setCoreCallbackLevel(getCallbackLevel(consoleLevel, Database.log.getCustom()));
}
 
Example #13
Source File: SyncManager.java    From mini-hacks with MIT License 4 votes vote down vote up
public void setDatabase(Database database) {
    this.database = database;
}
 
Example #14
Source File: SyncManager.java    From mini-hacks with MIT License 4 votes vote down vote up
public Database getDatabase() {
    return database;
}
 
Example #15
Source File: SyncManager.java    From mini-hacks with MIT License 4 votes vote down vote up
public void setDatabase(Database database) {
    this.database = database;
}
 
Example #16
Source File: SyncManager.java    From mini-hacks with MIT License 4 votes vote down vote up
public Database getDatabase() {
    return database;
}
 
Example #17
Source File: PlacesAdapter.java    From mini-hacks with MIT License 4 votes vote down vote up
public PlacesAdapter(Context context, List<Place> dataSet, Database database) {
    this.context = context;
    this.dataSet = dataSet;
    this.database = database;
}