예제 #1
0
        public StorageResult <TValue> GetDocument <TKey, TValue>(string collection, TKey key)
        {
            StorageResult <TValue> result = new StorageResult <TValue>();

            if (string.IsNullOrEmpty(collection))
            {
                throw new ArgumentException("Collection name can not be null or empty.");
            }

            if (!CollectionExists(collection))
            {
                throw new ArgumentException("Specified collection not found in " + GetFileInfo() + " Collection = " + collection);
            }
            ReadTransaction transaction = null;

            try
            {
                if (_readerTransaction != null)
                {
                    if (_readerTransaction.ShouldRenew)
                    {
                        _readerTransaction.WaitUntillFree();
                    }
                }

                lock (_readerTransactionLock)
                {
                    if (_readerTransaction == null || !_readerTransaction.Running)
                    {
                        _readerTransaction = new ReadTransaction(BeginTransaction(null, true));
                    }

                    transaction = _readerTransaction;
                    transaction.Enter();
                }

                ValidateTransaction(transaction.Transaction);
                LightningTransaction lmdbTransaction = (LightningTransaction)transaction.Transaction.InnerObject;

                byte[] keyBytes   = _environment.ConverterStore.GetToBytes <TKey>().Convert(_collectionTable[collection].Collection, key);
                byte[] valueBytes = lmdbTransaction.Get(_collectionTable[collection].Collection, keyBytes);
                result.Document = _environment.ConverterStore.GetFromBytes <TValue>().Convert(_collectionTable[collection].Collection, valueBytes);
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Exit();
                }
            }

            result.Status = StoreResult.Success;
            return(result);
        }
예제 #2
0
        public IDataReader <TKey, TValue> GetAllDocuments <TKey, TValue>(string collection)
        {
            if (string.IsNullOrEmpty(collection))
            {
                throw new ArgumentException("Collection name can not be null or empty.");
            }

            if (!CollectionExists(collection))
            {
                throw new ArgumentException("Specified collection not found in " + GetFileInfo() + " Collection = " + collection);
            }

            ReadTransaction transaction = null;

            try
            {
                if (_readerTransaction != null)
                {
                    if (_readerTransaction.ShouldRenew)
                    {
                        _readerTransaction.WaitUntillFree();
                    }
                }

                lock (_readerTransactionLock)
                {
                    if (_readerTransaction == null || !_readerTransaction.Running)
                    {
                        _readerTransaction = new ReadTransaction(BeginTransaction(null, true));
                    }

                    transaction = _readerTransaction;
                    transaction.Enter();
                }
                ValidateTransaction(transaction.Transaction);
                LightningTransaction lmdbTransaction = (LightningTransaction)transaction.Transaction.InnerObject;

                LightningCursor cursor = lmdbTransaction.CreateCursor(_collectionTable[collection].Collection);
                LMDBDataReader <TKey, TValue> reader = new LMDBDataReader <TKey, TValue>(cursor.GetEnumerator(), _environment, _collectionTable[collection].Collection);
                return(reader);
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Exit();
                }
            }
        }