Пример #1
0
    void Start()
    {
        Database.SetLogLevel(LogDomain.All, LogLevel.Verbose);

        // Get the database (and create it if it doesn't exist)
        database = new Database(databaseName, new DatabaseConfiguration()
        {
            Directory = Path.Combine(Application.persistentDataPath, "CouchBase")
        });

        // Create a new document (i.e. a record) in the database
        string id = null;

        using (MutableDocument mutableDoc = new MutableDocument("meta")) {
            mutableDoc.SetFloat("version", 2.0f)
            .SetString("type", "SDK");

            // Save it to the database
            database.Save(mutableDoc);
            id = mutableDoc.Id;
        }

        statusLabel.text += "\n- Saved meta doc";

        // Update a document
        using (Document doc = database.GetDocument("meta"))
            using (MutableDocument mutableDoc = doc.ToMutable()) {
                mutableDoc.SetFloat("version", 2.0f);
                database.Save(mutableDoc);

                using (Document docAgain = database.GetDocument("meta")) {
                    Debug.Log($"Document ID :: {docAgain.Id}");
                    Debug.Log($"Version {docAgain.GetFloat("version")}");
                    statusLabel.text += $"\n- Retrieved: {docAgain.Id}";
                }
            }

        // Create a query to fetch documents of type SDK
        // i.e. SELECT * FROM database WHERE type = "SDK"
        using (IWhere query = QueryBuilder.Select(SelectResult.All())
                              .From(DataSource.Database(database))
                              .Where(Expression.Property("version").EqualTo(Expression.Float(2.0f)))) {
            // Run the query
            IResultSet result = query.Execute();
            int        count  = result.AllResults().Count;
            Debug.Log($"Number of rows :: {count}");
            statusLabel.text += $"\n- Query result: {count}";
        }

#if UNITY_EDITOR
        Debug.LogWarning("CouchBase: In Unity Editor – Not replicating!");
        return;
#endif
        // Create replicator to push and pull changes to and from the cloud
        // NOTE: if Continuous is set to true, make sure to stop it and to clean up everything before exiting the application!
        URLEndpoint             targetEndpoint = new URLEndpoint(new Uri(syncUrl));
        ReplicatorConfiguration replConfig     = new ReplicatorConfiguration(database, targetEndpoint)
        {
            // Add authentication
            Authenticator  = new BasicAuthenticator(username, password),
            ReplicatorType = ReplicatorType.PushAndPull,
            Continuous     = false
        };

        // Create replicator
        replicator = new Replicator(replConfig);
        replicator.AddChangeListener((sender, args) => {
            if (args.Status.Error != null)
            {
                Debug.Log($"Error :: {args.Status.Error}");
            }
            else
            {
                if (replicator.Status.Activity != ReplicatorActivityLevel.Busy && replicator.Status.Activity != ReplicatorActivityLevel.Connecting)
                {
                    syncing = false;
                    Debug.Log($"Done syncing: {replicator.Status.Activity}");
                    doneSyncing = true;
                }
                else
                {
                    Debug.Log($"Progress: {args.Status.Progress.Completed} / {args.Status.Progress.Total}");
                }
            }
        });

        Debug.Log("Starting syncing...");
        statusLabel.text += "\n- Starting syncing...";
        replicator.Start();

        syncing = true;
        // Later, stop and dispose the replicator *before* closing/disposing the database
    }