关于couchbase lite和同步网关

时间:2016-06-30 05:06:22

标签: couchbase-lite

我可以将同步gateway连接到couchbase server。我考虑将couchbase lite用作Windows应用程序的本地database或独立database

需要澄清:

  1. couchbase lite(来自应用程序的本地数据库)如何与同步网关连接?它是由Couchbase lite提供的API完成的吗?
  2. 是否有任何人拥有简单的java代码,这将有助于获得如何与db连接以及如何使用java代码处理数据库等事情?

2 个答案:

答案 0 :(得分:0)

private URL createSyncURL(boolean isEncrypted){
    URL syncURL = null;
    String host = "https://10.0.2.2";  //sync gateway ip
    String port = "4984";              //sync gateway port
    String dbName = "couchbaseevents";
    try {
        syncURL = new URL(host + ":" + port + "/" + dbName);
    } catch (MalformedURLException me) {
        me.printStackTrace();
    }
    return syncURL;
}

private void startReplications() throws CouchbaseLiteException {
    Replication pull = this.getDatabaseInstance().createPullReplication(this.createSyncURL(false));
    Replication push = this.getDatabaseInstance().createPushReplication(this.createSyncURL(false));
    pull.setContinuous(true);
    push.setContinuous(true);
    pull.start();
    push.start();
}

当你调用startReplications方法时,它将在内部调用createSyncURL,它将couchbase lite db连接到同步网关

答案 1 :(得分:0)

Couchbase Lite是一个嵌入式数据库。它支持许多平台,包括移动和桌面。

Couchbase Lite是一个功能齐全的完整数据库,可以单独使用。这意味着您的应用必须包含Couchbase Lite库代码。

使用Sync Gateway时,Couchbase Lite会在后台使用REST调用执行复制。您实际上可以使用REST调用直接自己使用Sync Gateway。

Couchbase Mobile developer portal上有代码示例。这是Java中的一个片段(注意:在Android上,使用AndroidContext而不是JavaContext):

// Get the database (and create it if it doesn’t exist).
Manager manager = new Manager(new JavaContext(), Manager.DEFAULT_OPTIONS);
Database database = manager.getDatabase("mydb");

// Create a new document (i.e. a record) in the database.
Document document = database.createDocument();
Map properties = new HashMap();
properties.put("firstName", "John");
document.putProperties(properties);

// Update a document.
document.update(new Document.DocumentUpdater() {
    @Override
    public boolean update(UnsavedRevision newRevision) {
        Map properties = newRevision.getUserProperties();
        properties.put("firstName", "Johnny");
        newRevision.setUserProperties(properties);
        return true;
    }
});

// Delete a document.
document.delete();

// Create replicators to push & pull changes to & from the cloud.
URL url = new URL("https://www.my.com/mydb/");
Replication push = database.createPushReplication(url);
Replication pull = database.createPullReplication(url);
push.setContinuous(true);
pull.setContinuous(true);

// Add authentication.
Authenticator authenticator = AuthenticatorFactory.createBasicAuthenticator(name, password);
push.setAuthenticator(authenticator);
pull.setAuthenticator(authenticator);

// Listen to database change events (there are also change
// events for documents, replications, and queries).
database.addChangeListener(this);

// Start replicators
push.start();
pull.start();