/**
         * Replaces documents in the collection with given documents. This method is used to modify documents inside a collection by replacing existing documents with given documents. The field used to perform the replacement is the document unique identifier (_id).
         */
        public int replaceDocuments(JArray documents, bool markDirty)
        {
            // get the shared manager
            JSONStoreSQLLite store = JSONStoreSQLLite.sharedManager();

            if (store == null)
            {
                //JSONStoreLoggerError(@"Error: JSON_STORE_DATABASE_NOT_OPEN, code: %d", rc);
                throw new JSONStoreException(JSONStoreConstants.JSON_STORE_DATABASE_NOT_OPEN);
            }

            int    numReplaced = 0;
            JArray failures    = new JArray();

            lock (JSONStore.lockThis)
            {
                if (!JSONStore.transactionInProgress)
                {
                    //Start transaction
                    store.startTransaction();
                }

                foreach (JToken document in documents)
                {
                    bool worked = store.replace(document, collectionName, markDirty);

                    if (worked)
                    {
                        //It worked, increment the number of docs replaced
                        numReplaced++;
                    }
                    else
                    {
                        //If we can't store all the data, we rollback and go
                        //to the error callback
                        numReplaced = JSONStoreConstants.JSON_STORE_PERSISTENT_STORE_FAILURE;
                        failures.Add(document);
                        break;
                    }
                }

                if (numReplaced < 0)
                {
                    if (!JSONStore.transactionInProgress)
                    {
                        store.rollbackTransaction();
                    }

                    throw new JSONStoreException(JSONStoreConstants.JSON_STORE_REPLACE_DOCUMENTS_FAILURE, failures);
                }

                // things worked, commit
                if (!JSONStore.transactionInProgress)
                {
                    store.commitTransaction();
                }

                return(numReplaced);
            }
        }
        /**
         * Stores data as documents in the collection.
         */
        public int addData(JArray jsonArray, bool markDirty, JSONStoreAddOptions options)
        {
            // get the shared manager
            JSONStoreSQLLite store = JSONStoreSQLLite.sharedManager();

            if (store == null)
            {
                //JSONStoreLoggerError(@"Error: JSON_STORE_DATABASE_NOT_OPEN, code: %d", rc);
                throw new JSONStoreException(JSONStoreConstants.JSON_STORE_DATABASE_NOT_OPEN);
            }

            int numWorked = 0;

            lock (JSONStore.lockThis)
            {
                //Start transaction if no user transaction is already open
                if (!JSONStore.transactionInProgress)
                {
                    store.startTransaction();
                }

                foreach (JObject data in jsonArray)
                {
                    // loop through all the data in the array and attempt to store
                    if (store.storeObject(data, collectionName, markDirty, options.additionalSearchFields))
                    {
                        numWorked++;
                    }
                    else
                    {
                        //If we can't store all the data, we rollback and go
                        //to the error callback
                        if (!JSONStore.transactionInProgress)
                        {
                            store.rollbackTransaction();
                        }
                        throw new JSONStoreException(JSONStoreConstants.JSON_STORE_PERSISTENT_STORE_FAILURE);
                    }
                }

                // things worked, commit
                if (!JSONStore.transactionInProgress)
                {
                    store.commitTransaction();
                }
                return(numWorked);
            }
        }
Ejemplo n.º 3
0
        /**
         * Rollback user transaction
         */
        public static bool rollbackTransaction()
        {
            lock (lockThis)
            {
                bool worked = true;

                // get the shared manager
                JSONStoreSQLLite store = JSONStoreSQLLite.sharedManager();

                if (store == null)
                {
                    //JSONStoreLoggerError(@"Error: JSON_STORE_DATABASE_NOT_OPEN, code: %d", rc);
                    // problem getting the shared manager for this username
                    throw new JSONStoreException(JSONStoreConstants.JSON_STORE_DATABASE_NOT_OPEN);
                }

                if (!transactionInProgress)
                {
                    worked = false;

                    //JSONStoreLoggerError(@"Error: JSON_STORE_NO_TRANSACTION_IN_PROGRESS, code: %d", rc);
                    throw new JSONStoreException(JSONStoreConstants.JSON_STORE_NO_TRANSACTION_IN_PROGRESS);
                }
                else
                {
                    worked = store.rollbackTransaction();

                    if (!worked)
                    {
                        transactionInProgress = false;

                        //JSONStoreLoggerError(@"Error: JSON_STORE_TRANSACTION_FAILURE, code: %d", rc);
                        throw new JSONStoreException(JSONStoreConstants.JSON_STORE_TRANSACTION_FAILURE);
                    }
                    transactionInProgress = false;
                }

                //JSONStoreLoggerAnalytics(username != nil ? username : @"", @"", @"commitTransaction", startTime, rc);

                return(worked);
            }
        }