Beispiel #1
0
        /// <summary>
        /// Visit key value pairs that are greater than or equal to the specified key. Visitation will continue as long as
        /// the visitor <seealso cref="KeyValueVisitor.visit(ReadableBuffer, ReadableBuffer) returns true"/>.
        /// </summary>
        /// <returns> {@code true} if an exact match was found, meaning that the first visited key/value pair was a perfect
        /// match for the specified key. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public boolean scan(SearchKey search, KeyValueVisitor visitor) throws java.io.IOException
        public virtual bool Scan(SearchKey search, KeyValueVisitor visitor)
        {
            BigEndianByteArrayBuffer searchKey = newBuffer(_keySize);
            BigEndianByteArrayBuffer key       = newBuffer(_keySize);
            BigEndianByteArrayBuffer value     = newBuffer(_valueSize);

            search.SearchKeyConflict(searchKey);
            int page = FindPage(searchKey, _pageCatalogue);

            if (page < 0 || (page >= _pageCatalogue.Length / (_keySize * 2)))
            {
                return(false);
            }
            using (PageCursor cursor = _file.io(page, PF_SHARED_READ_LOCK))
            {
                if (!cursor.Next())
                {
                    return(false);
                }
                // finds and reads the first key/value pair
                int offset = FindByteOffset(cursor, searchKey, key, value);
                try
                {
                    return(Arrays.Equals(searchKey.Buffer, key.Buffer));
                }
                finally
                {
                    VisitKeyValuePairs(_file.pageSize(), cursor, offset, visitor, false, key, value);
                }
            }
        }
Beispiel #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public boolean writeHeader(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value) throws java.io.IOException
        public virtual bool WriteHeader(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value)
        {
            bool result = _state.header(this, value.AllZeroes() || value.MinusOneAtTheEnd());

            DoWrite(key, value, State.Done);
            return(result);
        }
Beispiel #3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private boolean write(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value) throws java.io.IOException
        private bool Write(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value)
        {
            bool result = _metadata.visit(key, value);

            _writer.write(key.Buffer);
            _writer.write(value.Buffer);
            key.Clear();
            value.Clear();
            return(result);
        }
Beispiel #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static java.util.List<int> extract(EntryVisitor<WritableBuffer> producer) throws java.io.IOException
        private static IList <int> Extract(EntryVisitor <WritableBuffer> producer)
        {
            IList <int> result             = new List <int>();
            BigEndianByteArrayBuffer key   = new BigEndianByteArrayBuffer(4);
            BigEndianByteArrayBuffer value = new BigEndianByteArrayBuffer(4);

            while (producer.Visit(key, value))
            {
                result.Add(key.GetInt(0));
                result.Add(value.GetInt(0));
            }
            return(result);
        }
Beispiel #5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private boolean transfer(EntryVisitor<WritableBuffer> producer, EntryVisitor<ReadableBuffer> consumer) throws java.io.IOException
        private bool Transfer(EntryVisitor <WritableBuffer> producer, EntryVisitor <ReadableBuffer> consumer)
        {
            BigEndianByteArrayBuffer key   = new BigEndianByteArrayBuffer(KeySize);
            BigEndianByteArrayBuffer value = new BigEndianByteArrayBuffer(ValueSize);

            while (producer.Visit(key, value))
            {
                if (!consumer.Visit(key, value))
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void doWrite(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value, State expectedNextState) throws java.io.IOException
        private void DoWrite(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value, State expectedNextState)
        {
            this._keySize   = key.Size();
            this._valueSize = value.Size();
            Debug.Assert(key.AllZeroes(), "key should have been cleared by previous call");
            if (!Write(key, value))
            {
                if (_state != expectedNextState)
                {
                    _state = State.InError;
                    throw new System.InvalidOperationException("MetadataCollector stopped before " + expectedNextState + " reached.");
                }
            }
        }
Beispiel #7
0
        /// <param name="cursor"> the cursor for the page to search for the key in. </param>
        /// <param name="searchKey"> the key to search for. </param>
        /// <param name="key"> a buffer to write the key into. </param>
        /// <param name="value"> a buffer to write the value into. </param>
        /// <returns> the offset (in bytes within the given page) of the first entry with a key that is greater than or equal
        /// to the given key. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private int findByteOffset(org.neo4j.io.pagecache.PageCursor cursor, BigEndianByteArrayBuffer searchKey, BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value) throws java.io.IOException
        private int FindByteOffset(PageCursor cursor, BigEndianByteArrayBuffer searchKey, BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value)
        {
            int entrySize  = searchKey.Size() + value.Size();
            int last       = MaxPage(_file.pageSize(), entrySize, _totalEntries);
            int firstEntry = (cursor.CurrentPageId == 0) ? _headerEntries : 0;                 // skip header in first page
            int entryCount = _totalEntries % (_file.pageSize() / entrySize);

            // If the last page is full, 'entryCount' will be 0 at this point.
            if (cursor.CurrentPageId != last || entryCount == 0)
            {               // The current page is a full page (either because it has pages after it, or the last page is actually full).
                entryCount = _file.pageSize() / entrySize;
            }
            int entryOffset = FindEntryOffset(cursor, searchKey, key, value, firstEntry, entryCount - 1);

            return(entryOffset * entrySize);
        }
        // IMPLEMENTATION

        /// <summary>
        /// Create a collector for interpreting metadata from a file. </summary>
        private MetadataCollector Metadata(ReadableBuffer formatSpecifier, int pageSize, int keySize, int valueSize)
        {
            sbyte[] format = new sbyte[formatSpecifier.Size()];
            for (int i = 0; i < format.Length; i++)
            {
                format[i] = formatSpecifier.GetByte(i);
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final BigEndianByteArrayBuffer specifier = new BigEndianByteArrayBuffer(format);
            BigEndianByteArrayBuffer specifier = new BigEndianByteArrayBuffer(format);

//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: HeaderField<?>[] headerFields = headerFieldsForFormat(formatSpecifier);
            HeaderField <object>[] headerFields = HeaderFieldsForFormat(formatSpecifier);
            return(new MetadataCollectorAnonymousInnerClass(this, headerFields, specifier));
        }
Beispiel #9
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void writeData(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value) throws java.io.IOException
        public virtual void WriteData(BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value)
        {
            _state.data(this);
            Debug.Assert(key.Size() == _keySize);
            Debug.Assert(value.Size() == _valueSize);
            if (key.AllZeroes())
            {
                _state = State.InError;
                throw new System.ArgumentException("All-zero keys are not allowed.");
            }
            if (!Write(key, value))
            {
                _state = State.InError;
                throw new System.InvalidOperationException("MetadataCollector stopped on data field.");
            }
        }
Beispiel #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") static <Key> void applyUpdate(ReadableState<Key> store, java.util.concurrent.ConcurrentMap<Key, ChangeEntry> changes, Key key, ValueUpdate update, boolean reset, long version) throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal static void ApplyUpdate <Key>(ReadableState <Key> store, ConcurrentMap <Key, ChangeEntry> changes, Key key, ValueUpdate update, bool reset, long version)
        {
            ChangeEntry value = changes.get(key);

            if (value == null)
            {
                ChangeEntry newEntry = ChangeEntry.Of(new sbyte[store.KeyFormat().valueSize()], version);
                lock ( newEntry )
                {
                    value = changes.putIfAbsent(key, newEntry);
                    if (value == null)
                    {
                        BigEndianByteArrayBuffer buffer = new BigEndianByteArrayBuffer(newEntry.Data);
                        if (!reset)
                        {
                            PreviousValue lookup = new PreviousValue(newEntry.Data);
                            if (!store.Lookup(key, lookup))
                            {
                                buffer.Clear();
                            }
                        }
                        update.Update(buffer);
                        return;
                    }
                }
            }
            lock ( value )
            {
                BigEndianByteArrayBuffer target = new BigEndianByteArrayBuffer(value.Data);
                value.Version = version;
                if (reset)
                {
                    target.Clear();
                }
                update.Update(target);
            }
        }
Beispiel #11
0
        /// <param name="cursor"> the cursor for the page to search for the key in. </param>
        /// <param name="searchKey"> the key to search for. </param>
        /// <param name="key"> a buffer to write the key into. </param>
        /// <param name="value"> a buffer to write the value into. </param>
        /// <param name="min"> the offset (in number of entries within the page) of the first entry in the page. </param>
        /// <param name="max"> the offset (in number of entries within the page) of the last entry in the page. </param>
        /// <returns> the offset (in number of entries within the page) of the first entry with a key that is greater than or
        /// equal to the given key. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static int findEntryOffset(org.neo4j.io.pagecache.PageCursor cursor, BigEndianByteArrayBuffer searchKey, BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value, int min, int max) throws java.io.IOException
        internal static int FindEntryOffset(PageCursor cursor, BigEndianByteArrayBuffer searchKey, BigEndianByteArrayBuffer key, BigEndianByteArrayBuffer value, int min, int max)
        {
            int entrySize = key.Size() + value.Size();

            for (int mid; min <= max;)
            {
                mid = min + (max - min) / 2;
                ReadKeyValuePair(cursor, mid * entrySize, key, value);
                if (min == max)
                {
                    break;                              // break here instead of in the loop condition to ensure the right key is read
                }
                int cmp = compare(searchKey.Buffer, key.Buffer, 0);
                if (cmp > 0)                           // search key bigger than found key, continue after 'mid'
                {
                    min = mid + 1;
                }
                else                         // search key smaller (or equal to) than found key, continue before 'mid'
                {
                    max = mid;               // don't add, greater than are to be included...
                }
            }
            return(max);
        }
Beispiel #12
0
        /// <summary>
        /// Finds the page that would contain the given key from the <seealso cref="pageCatalogue page catalogue"/>.
        /// </summary>
        /// <param name="key"> The key to look for. </param>
        /// <returns> {@code -1} if the key is not contained in any page,
        /// otherwise the id of the page that would contain the key is returned. </returns>
        internal static int FindPage(BigEndianByteArrayBuffer key, sbyte[] catalogue)
        {
            int max = catalogue.Length / (key.Size() * 2) - 1;
            int min = 0;

            for (int mid; min <= max;)
            {
                mid = min + (max - min) / 2;
                // look at the low mark for the page
                int cmp = compare(key.Buffer, catalogue, mid * key.Size() * 2);
                if (cmp == 0)
                {
                    // this page starts with the key
                    // the previous page might also contain mid the key
                    max = mid;
                }
                if (cmp > 0)
                {
                    // look at the high mark for the page
                    cmp = compare(key.Buffer, catalogue, mid * key.Size() * 2 + key.Size());
                    if (cmp <= 0)
                    {
                        return(mid);                  // the key is within the range of this page
                    }
                    else                              // look at pages after 'mid'
                    {
                        min = mid + 1;
                    }
                }
                else                         // look at pages before 'mid'
                {
                    max = mid - 1;
                }
            }
            return(min);              // the first page after the value that was smaller than the key (mid + 1, you know...)
        }
Beispiel #13
0
 public override int CompareTo(Entry that)
 {
     return(BigEndianByteArrayBuffer.Compare(this.Key, that.Key, 0));
 }
Beispiel #14
0
 private static bool Visitable(BigEndianByteArrayBuffer key, bool acceptZeroKey)
 {
     return(acceptZeroKey || !key.AllZeroes());
 }
Beispiel #15
0
 private static void AssertCompare(sbyte[] lhs, Matcher <int> isAsExpected, sbyte[] rhs)
 {
     assertThat(BigEndianByteArrayBuffer.Compare(lhs, rhs, 0), isAsExpected);
 }