Пример #1
0
        public StoreResult UpdateDocument(ITransaction transaction, string collection, long rowId, JSONDocument update)
        {
            PersistenceManagerTransaction pmTransaction = transaction as PersistenceManagerTransaction;
            StorageResult <byte[]>        storageResult = _storageManager.UpdateDocument(pmTransaction,
                                                                                         _metadataIndices[collection].GetFileId(rowId), collection, rowId, SerializeDocument(update));

            if (storageResult.Status != StoreResult.Success)
            {
                return(storageResult.Status);
            }

            lock (_metadataIndices)
            {
                _metadataIndices[collection].Add(rowId, storageResult.FileId);
            }
            MetaDataIndexOperation operation = new MetaDataIndexOperation()
            {
                ActualDocumentSize = update.Size, FileId = storageResult.FileId, OperationType = OperationType.Update, RowId = _metadataIndices[collection].GetRowId(new DocumentKey(update.Key))
            };

            (pmTransaction.MetadataTransaction as MetaDataIndexTransaction).AddOperation(new DocumentKey(update.Key), operation);
            StoreResult metadataResult = _metadataIndices[collection].StoreKeyMetadata(pmTransaction.MetadataTransaction.InnerObject as ITransaction, update, rowId);

            if (metadataResult != StoreResult.Success) //undo data operation
            {
                //_storageManager.DeleteDocument(pmTransaction.DataTransaction, storageResult.FileId, _collectionName, rowId);
                return(metadataResult);
            }
            return(StoreResult.Success);
        }
Пример #2
0
        public override StorageResult <byte[]> UpdateDocument(ITransaction transaction, long fileId, string collection, long key, byte[] update)
        {
            lock (_oneOppLock)
            {
                PersistenceManagerTransaction pmTransaction = transaction as PersistenceManagerTransaction;

                if (!_collectionMetadataDictionary.ContainsKey(collection))
                {
                    throw new ArgumentException("Specified collection '" + collection + "' does not exist.");
                }

                string dbId = GetFullDbId(fileId);
                FileMetadata <long, byte[]> fileMetadata = _fileMetadataDictionary[dbId];
                if (fileMetadata == null)
                {
                    throw new Exception("unable to find database file.");
                }

                FileTransaction fileTransaction = pmTransaction.DataTransaction as FileTransaction;
                if (!fileTransaction.IsTransactionBegin(dbId))
                {
                    fileTransaction.Begin(dbId, fileMetadata.Provider.BeginTransaction(null, false));
                }

                StorageResult <byte[]> result = fileMetadata.Provider.UpdateDocument(fileTransaction.GetTransaction(dbId), collection, key,
                                                                                     update);

                result.FileId = fileId;
                DisplaySize();
                return(result);
            }
        }
Пример #3
0
        public override StorageResult <byte[]> UpdateDocument(ITransaction transaction, long fileId, string collection, long key, byte[] update)
        {
            lock (_oneOppLock)
            {
                PersistenceManagerTransaction pmTransaction = transaction as PersistenceManagerTransaction;

                if (!_collectionMetadataDictionary.ContainsKey(collection))
                {
                    throw new ArgumentException("Specified collection '" + collection + "' does not exist.");
                }

                string dbId = GetFullDbId(fileId);
                FileMetadata <long, byte[]> fileMetadata = _fileMetadataDictionary[dbId];

                if (fileMetadata == null)
                {
                    throw new Exception("Unable to find database file.");
                }

                if (pmTransaction != null)
                {
                    FileTransaction fileTransaction = pmTransaction.DataTransaction;
                    if (!fileTransaction.IsTransactionBegin(dbId))
                    {
                        fileTransaction.Begin(dbId, fileMetadata.Provider.BeginTransaction(null, false));
                    }

                    StorageResult <byte[]> result = fileMetadata.Provider.UpdateDocument(fileTransaction.GetTransaction(dbId), collection, key,
                                                                                         update);

                    if (result.Status == StoreResult.FailureDatabaseFull)
                    {
                        StorageResult <byte[]> deleteResult = DeleteDocument(transaction, fileId, collection, key);
                        if (deleteResult.Status == StoreResult.Failure)
                        {
                            throw new Exception("Error deleting document from store. key = " + key);
                        }

                        //todo: this decrement is not a proper fix as increments are on one level and decrements are on another level.
                        //if (_statsCollector != null)
                        //{
                        //    _statsCollector.DecrementStatsValue(StatisticsType.DocumentCount);
                        //}

                        StorageResult <byte[]> insertResult = StoreDocument(transaction, collection, key, update);
                        result.Status = insertResult.Status;
                        result.FileId = insertResult.FileId;

                        DisplaySize();
                        return(result);
                    }

                    result.FileId = fileId;
                    DisplaySize();
                    return(result);
                }
            }
            return(null);
        }
Пример #4
0
        public override StorageResult <byte[]> StoreDocument(ITransaction transaction, string collection, long key, byte[] value)
        {
            lock (_oneOppLock)
            {
                PersistenceManagerTransaction pmTransaction = transaction as PersistenceManagerTransaction;

                if (!_collectionMetadataDictionary.ContainsKey(collection))
                {
                    throw new ArgumentException("Specified collection '" + collection + "' does not exist.");
                }

                if (pmTransaction != null)
                {
                    KeyMetadata keyMetadata = new KeyMetadata(key, GenerateFileId(pmTransaction.MetadataTransaction));
                    string      dbId        = GetFullDbId(keyMetadata.FileId);
                    FileMetadata <long, byte[]> fileMetadata = _fileMetadataDictionary[dbId];

                    if (fileMetadata == null)
                    {
                        throw new Exception("unable to find database file.");
                    }

                    if (!pmTransaction.DataTransaction.IsTransactionBegin(dbId))
                    {
                        pmTransaction.DataTransaction.Begin(dbId, fileMetadata.Provider.BeginTransaction(null, false));
                    }

                    StorageResult <byte[]> result = fileMetadata.Provider.StoreDocument(pmTransaction.DataTransaction.GetTransaction(dbId), collection, keyMetadata.RowId,
                                                                                        value);

                    result.FileId = keyMetadata.FileId;

                    if (result.Status == StoreResult.FailureDatabaseFull)
                    {
                        StorageResult <byte[]> insertResult = StoreDocument(transaction, collection, key, value);
                        result.Status = insertResult.Status;
                        result.FileId = insertResult.FileId;
                    }

                    DisplaySize();
                    return(result);
                }
            }
            return(null);
        }
Пример #5
0
        public StorageResult <byte[]> GetDocument(long fileId, string collection, long key)
        {
            if (!_collectionMetadataDictionary.ContainsKey(collection))
            {
                throw new ArgumentException("Specified collection '" + collection + "' does not exist.");
            }

            if (fileId <= 0)
            {
                var result = new StorageResult <byte[]>();
                result.Status = StoreResult.FailureInvalidFileName;
                return(result);
            }
            string dbId = GetFullDbId(fileId);
            FileMetadata <long, byte[]> fileMetadata;

            _fileMetadataDictionary.TryGetValue(dbId, out fileMetadata);
            if (fileMetadata == null)
            {
                throw new Exception("Unable to find database file. Searching for file: " + dbId);
            }

            return(fileMetadata.Provider.GetDocument <long, byte[]>(collection, key));
        }
Пример #6
0
        public StoreResult StoreDocument(ITransaction transaction, string collection, long rowId, JSONDocument document)
        {
            var pmTransaction = transaction as PersistenceManagerTransaction;

            StorageResult <byte[]> storageResult = _storageManager.StoreDocument(pmTransaction, collection, rowId, SerializeDocument(document));

            if (storageResult.Status != StoreResult.Success)
            {
                return(storageResult.Status);
            }

            lock (_metadataIndices)
            {
                _metadataIndices[collection].Add(rowId, storageResult.FileId);
            }
            StoreResult metadataResult = _metadataIndices[collection].StoreKeyMetadata(pmTransaction.MetadataTransaction.InnerObject as ITransaction, document, rowId);

            if (metadataResult != StoreResult.Success) //undo data operation
            {
                _storageManager.DeleteDocument(pmTransaction, storageResult.FileId, collection, rowId);
                return(metadataResult);
            }
            return(StoreResult.Success);
        }
Пример #7
0
        public StoreResult RemoveDocument(ITransaction transaction, string collection, long rowId, JSONDocument document, IOperationContext context)
        {
            PersistenceManagerTransaction pmTransaction = transaction as PersistenceManagerTransaction;
            StorageResult <byte[]>        storageResult = _storageManager.DeleteDocument(pmTransaction, _metadataIndices[collection].GetFileId(rowId), collection, rowId);

            if (storageResult.Status != StoreResult.Success && storageResult.Status != StoreResult.SuccessDelete && storageResult.Status != StoreResult.SuccessKeyDoesNotExist)
            {
                return(storageResult.Status);
            }
            MetaDataIndexOperation operation = new MetaDataIndexOperation()
            {
                ActualDocumentSize = document.Size, FileId = storageResult.FileId, OperationType = OperationType.Remove, RowId = rowId
            };

            (pmTransaction.MetadataTransaction as MetaDataIndexTransaction).AddOperation(new DocumentKey(document.Key), operation);
            StoreResult metadataResult = _metadataIndices[collection].RemovekeyMetadata(pmTransaction.MetadataTransaction.InnerObject as ITransaction, document, rowId, context);

            if (metadataResult != StoreResult.Success) //undo data operation
            {
                _storageManager.StoreDocument(pmTransaction, collection, rowId, SerializeDocument(document));
                return(metadataResult);
            }
            return(StoreResult.SuccessDelete);
        }