com.couchbase.lite.View Java Examples

The following examples show how to use com.couchbase.lite.View. 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: PerfTestCouchbase.java    From android-database-performance with Apache License 2.0 6 votes vote down vote up
@Override
protected void doIndexedStringEntityQueries() throws Exception {
    // set up index on string property
    View indexedStringView = database.getView("indexedStringView");
    indexedStringView.setMap(new Mapper() {
        @Override
        public void map(Map<String, Object> document, Emitter emitter) {
            String indexedString = (String) document.get("indexedString");
            // only need an index of strings mapped to the document, so provide no value
            emitter.emit(indexedString, null);
        }
    }, "1");
    log("Set up view.");

    for (int i = 0; i < RUNS; i++) {
        log("----Run " + (i + 1) + " of " + RUNS);
        indexedStringEntityQueriesRun(indexedStringView, getBatchSize());
    }
}
 
Example #2
Source File: SyncManager.java    From mini-hacks with MIT License 6 votes vote down vote up
private void registerViews() {
    View citiesView = database.getView(CITIES_VIEW);
    citiesView.setMapReduce(new Mapper() {
        @Override
        public void map(Map<String, Object> document, Emitter emitter) {
            if (document.get("type") != null && document.get("type").equals("city")) {
                List<Object> key = new ArrayList<Object>();
                key.add(document.get("city"));
                emitter.emit(key, null);
            }
        }
    }, new Reducer() {
        @Override
        public Object reduce(List<Object> keys, List<Object> values, boolean rereduce) {
            return new Integer(values.size());
        }
    }, "9");
}
 
Example #3
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 #4
Source File: MainActivity.java    From mini-hacks with MIT License 5 votes vote down vote up
public void deletePlace(android.view.View view) {
    Log.d("", "delete me");
    adapter.dataSet.remove(2);
    try {
        database.getExistingDocument(adapter.dataSet.get(2).getId()).delete();
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
    adapter.notifyItemRemoved(2);
}
 
Example #5
Source File: MainActivity.java    From mini-hacks with MIT License 5 votes vote down vote up
private void registerViews() {
    View placesView = database.getView(PLACES_VIEW);
    placesView.setMap(new Mapper() {
        @Override
        public void map(Map<String, Object> document, Emitter emitter) {
            emitter.emit(document.get("_id"), document);
        }
    }, "1");
}
 
Example #6
Source File: PerfTestCouchbase.java    From android-database-performance with Apache License 2.0 4 votes vote down vote up
private void indexedStringEntityQueriesRun(View indexedStringView, int count)
        throws CouchbaseLiteException {
    // create entities
    String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
    database.beginTransaction();
    for (int i = 0; i < count; i++) {
        Document entity = database.getDocument(String.valueOf(i));
        Map<String, Object> properties = new HashMap<>();
        properties.put("indexedString", fixedRandomStrings[i]);
        entity.putProperties(properties);
    }
    database.endTransaction(true);
    log("Built and inserted entities.");

    // query for entities by indexed string at random
    int[] randomIndices = StringGenerator.getFixedRandomIndices(getQueryCount(), count - 1);

    // clear the document cache to force loading properties from the database
    database.clearDocumentCache();

    startClock();
    for (int i = 0; i < getQueryCount(); i++) {
        int nextIndex = randomIndices[i];
        List<Object> keyToQuery = new ArrayList<>(1);
        keyToQuery.add(fixedRandomStrings[nextIndex]);

        Query query = indexedStringView.createQuery();
        query.setKeys(keyToQuery);
        QueryEnumerator result = query.run();
        while (result.hasNext()) {
            QueryRow row = result.next();
            //noinspection unused
            Document document = row.getDocument();
        }
    }
    stopClock(Benchmark.Type.QUERY_INDEXED);

    // delete all entities
    deleteAll();
    log("Deleted all entities.");
}