Exemplo n.º 1
0
        public bool MoveNext()
        {
            if (_currentKey == null)
            {
                _currentKey = _start;
                // todo: check
                _iterator = _db.NewIterator();
                _iterator.Seek(_start);
                var firstKey = _iterator.Key();
                if (KeyConfig.ByteCompare(firstKey, _start) < 0)
                {
                    return(false);
                }
            }
            else
            {
                _nextFunction(_iterator);
                _currentKey = _iterator.Key();
            }

            if (!_iterator.Valid())
            {
                return(false);
            }

            var key = _iterator.Key();

            if (KeyConfig.ByteCompare(key, _end) > 0)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        public IEnumerable <Triple> O(TripleObject o, Triple c)
        {
            if (c == null)
            {
                return(O(o));
            }

            var oh     = KeySegments.GetNameOKeyObject(_name, o);
            var startS = KeyConfig.ConcatBytes(oh, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(oh, KeyConfig.ByteOne);

            var(_, _, oKey) = new KeySegments(_name, c).GetKeys();
            var continuation = KeyConfig.ConcatBytes(oKey, KeyConfig.ByteOne);

            if (KeyConfig.ByteCompare(continuation, startS) < 0)
            {
                throw new InvalidOperationException("Invalid continuation token. Before range");
            }
            else if (KeyConfig.ByteCompare(continuation, endS) > 0)
            {
                return(Enumerable.Empty <Triple>());
            }

            return(new RocksEnumerable(_db, continuation, endS, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 3
0
        public IEnumerable <IGrouping <string, TripleObject> > GetSubjectGroupings(string s)
        {
            var sh     = KeySegments.GetNameSKeySubject(_name, s);
            var startS = KeyConfig.ConcatBytes(sh, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(sh, KeyConfig.ByteOne);

            return(new RocksSubjectGrouping(_db, _name, s, startS, endS));
        }
Exemplo n.º 4
0
        public IEnumerable <Triple> SP(string s, string p)
        {
            var sh     = KeySegments.GetNameSKeySubjectPredicate(_name, s, p);
            var startS = KeyConfig.ConcatBytes(sh, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(sh, KeyConfig.ByteOne);

            return(new RocksEnumerable(_db, startS, endS, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 5
0
        public IEnumerable <Triple> OS(TripleObject o, string s)
        {
            var oh     = KeySegments.GetNameOKeyObjectSubject(_name, o, s);
            var startS = KeyConfig.ConcatBytes(oh, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(oh, KeyConfig.ByteOne);

            return(new RocksEnumerable(_db, startS, endS, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 6
0
        public IEnumerable <Triple> PO(string p, TripleObject o)
        {
            var ph     = KeySegments.GetNamePKeyPredicateObject(_name, p, o);
            var startS = KeyConfig.ConcatBytes(ph, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(ph, KeyConfig.ByteOne);

            return(new RocksEnumerable(_db, startS, endS, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 7
0
        public static byte[] GetNamePKeyPredicate(string name, string predicate)
        {
            var nameBytes = KeyConfig.GetBytes(name);
            var z         = KeyConfig.ByteZero;
            var pBytes    = KeyConfig.GetBytes(predicate);

            return(KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteP, z, pBytes));
        }
Exemplo n.º 8
0
        public static byte[] GetNameSKeySubject(string name, string subject)
        {
            var nameBytes = KeyConfig.GetBytes(name);
            var z         = KeyConfig.ByteZero;
            var sBytes    = KeyConfig.GetBytes(subject);

            return(KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteS, z, sBytes));
        }
Exemplo n.º 9
0
        public IEnumerable <Triple> GetTriples()
        {
            var nameBytes = KeySegments.GetNameSKey(_name);
            var start     = KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteZero);
            var end       = KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteOne);

            return(new RocksEnumerable(_db, start, end, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 10
0
        public static byte[] GetNameOKeyObject(string name, TripleObject o)
        {
            var nameBytes = KeyConfig.GetBytes(name);
            var z         = KeyConfig.ByteZero;
            var oBytes    = KeyConfig.GetBytes(o.ToValue());
            var isIdBytes = o.IsID ? KeyConfig.ByteTrue : KeyConfig.ByteFalse;

            return(KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteO, z, isIdBytes, z, oBytes));
        }
Exemplo n.º 11
0
 public KeySegments(string name, string s, string p, TripleObject o)
 {
     _name = KeyConfig.GetBytes(name);
     S     = KeyConfig.GetBytes(s);
     P     = KeyConfig.GetBytes(p);
     O     = KeyConfig.GetBytes(o.ToValue());
     IsId  = o.IsID ? KeyConfig.ByteTrue : KeyConfig.ByteFalse;
     Index = BitConverter.GetBytes(o.Index);
 }
Exemplo n.º 12
0
        public static byte[] ToBytes(this TripleObject o)
        {
            var oBytes    = KeyConfig.GetBytes(o.ToValue());
            var isIdBytes = o.IsID ? KeyConfig.ByteTrue : KeyConfig.ByteFalse;
            var typeBytes = KeyConfig.GetBytes((int)o.TokenType);

            // index is not converted
            return(KeyConfig.ConcatBytes(isIdBytes, typeBytes, oBytes));
        }
Exemplo n.º 13
0
        public static byte[] GetNameSKeySubjectPredicateIndex(string name, string subject, string predicate, int index)
        {
            var nameBytes  = KeyConfig.GetBytes(name);
            var z          = KeyConfig.ByteZero;
            var sBytes     = KeyConfig.GetBytes(subject);
            var pBytes     = KeyConfig.GetBytes(predicate);
            var indexBytes = BitConverter.GetBytes(index);

            return(KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteS, z, sBytes, z, pBytes, z, indexBytes));
        }
Exemplo n.º 14
0
        public bool Exists(string s, string p, TripleObject o)
        {
            var keySegments = new KeySegments(_name, s, p, o);
            var oPrefix     = keySegments.GetOPrefix();
            var start       = KeyConfig.ConcatBytes(oPrefix, KeyConfig.ByteZero);
            var end         = KeyConfig.ConcatBytes(oPrefix, KeyConfig.ByteOne);
            var oEnumerable = new RocksEnumerable(_db, start, end, (it) => it.Next());

            return(oEnumerable.Any());
        }
Exemplo n.º 15
0
        public (byte[], byte[], byte[]) GetKeys()
        {
            if (_sKey == null)
            {
                var z = KeyConfig.ByteZero;
                _sKey = KeyConfig.ConcatBytes(_name, KeyConfig.ByteS, z, S, z, P, z, Index, z, IsId, z, O);
                _pKey = KeyConfig.ConcatBytes(_name, KeyConfig.ByteP, z, P, z, IsId, z, O, z, Index, z, S);
                _oKey = KeyConfig.ConcatBytes(_name, KeyConfig.ByteO, z, IsId, z, O, z, S, z, P, z, Index);
            }

            return(_sKey, _pKey, _oKey);
        }
Exemplo n.º 16
0
        public IEnumerable <string> P()
        {
            var pPrefix    = KeySegments.GetNamePPredicate(Name);
            var startP     = KeyConfig.ConcatBytes(pPrefix, KeyConfig.ByteZero);
            var endP       = KeyConfig.ConcatBytes(pPrefix, KeyConfig.ByteOne);
            var predicates = new RocksEnumerable(_db, startP, endP, (it) => {
                var key     = it.Key();
                var splits  = KeyConfig.Split(key);
                var nextKey = KeyConfig.ConcatBytes(splits[0], KeyConfig.ByteZero, splits[1], KeyConfig.ByteOne);
                return(it.Seek(nextKey));
            }).Select(x => x.Predicate);

            return(predicates);
        }
Exemplo n.º 17
0
        IEnumerator <IGrouping <string, TripleObject> > IEnumerable <IGrouping <string, TripleObject> > .GetEnumerator()
        {
            var predicates = new RocksEnumerable(_db, _start, _end, (it) =>
            {
                var key     = it.Key();
                var splits  = KeyConfig.Split(key);
                var nextKey = KeyConfig.ConcatBytes(splits[0], KeyConfig.ByteZero, splits[1], KeyConfig.ByteZero, splits[2], KeyConfig.ByteOne);
                return(it.Seek(nextKey));
            }).Select(x => x.Predicate);

            foreach (var p in predicates)
            {
                yield return(new RocksPredicateGrouping(_db, _name, _s, p));
            }
        }
Exemplo n.º 18
0
        public IEnumerator <TripleObject> GetEnumerator()
        {
            var sBytes = Hash($"{_name}.S");
            var sh     = Hash(_s);
            var ph     = Hash(_p);
            var start  = KeyConfig.ConcatBytes(sBytes, KeyConfig.ByteZero, sh, KeyConfig.ByteZero, ph, KeyConfig.ByteZero);
            var end    = KeyConfig.ConcatBytes(sBytes, KeyConfig.ByteZero, sh, KeyConfig.ByteZero, ph, KeyConfig.ByteOne);

            var baseKey = $"{_name}.S.{Hash(_s)}.{Hash(_p)}";
            IEnumerable <Triple> idEnum = new RocksEnumerable(_db, start, end, (iterator) =>
            {
                return(iterator.Next());
            });

            foreach (var item in idEnum)
            {
                yield return(item.Object);
            }
        }
Exemplo n.º 19
0
        public IEnumerable <IGrouping <string, IGrouping <string, TripleObject> > > GetGroupings()
        {
            var nameBytes = KeySegments.GetNameSKey(Name);
            var start     = KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteZero);
            var end       = KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteOne);
            var subjects  = new RocksEnumerable(_db, start, end, (it) => {
                var key     = it.Key();
                var splits  = KeyConfig.Split(key);
                var nextKey = KeyConfig.ConcatBytes(splits[0], KeyConfig.ByteZero, splits[1], KeyConfig.ByteOne);
                return(it.Seek(nextKey));
            }).Select(x => x.Subject);

            foreach (var s in subjects)
            {
                var sh     = KeySegments.GetNameSKeySubject(Name, s);
                var startS = KeyConfig.ConcatBytes(sh, KeyConfig.ByteZero);
                var endS   = KeyConfig.ConcatBytes(sh, KeyConfig.ByteOne);

                yield return(new RocksSubjectGrouping(_db, Name, s, startS, endS));
            }
        }
Exemplo n.º 20
0
        public IEnumerable <Triple> SP(string s, string p, Triple c)
        {
            var sh     = KeySegments.GetNameSKeySubjectPredicate(_name, s, p);
            var startS = KeyConfig.ConcatBytes(sh, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(sh, KeyConfig.ByteOne);

            // todo: optimize
            var(sKey, _, _) = new KeySegments(_name, c).GetKeys();
            var continuation = KeyConfig.ConcatBytes(sKey, KeyConfig.ByteOne);

            if (KeyConfig.ByteCompare(continuation, startS) < 0)
            {
                throw new InvalidOperationException("Invalid continuation token. Before range");
            }
            else if (KeyConfig.ByteCompare(continuation, endS) > 0)
            {
                return(Enumerable.Empty <Triple>());
            }

            return(new RocksEnumerable(_db, continuation, endS, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 21
0
        public IEnumerable <Triple> P(string p, Triple c)
        {
            if (c == null)
            {
                return(P(p));
            }
            var ph     = KeySegments.GetNamePKeyPredicate(_name, p);
            var startS = KeyConfig.ConcatBytes(ph, KeyConfig.ByteZero);
            var endS   = KeyConfig.ConcatBytes(ph, KeyConfig.ByteOne);

            var(_, pKey, _) = new KeySegments(_name, c).GetKeys();
            var continuation = KeyConfig.ConcatBytes(pKey, KeyConfig.ByteOne);

            if (KeyConfig.ByteCompare(continuation, startS) < 0)
            {
                throw new InvalidOperationException("Invalid continuation token. Before range");
            }
            else if (KeyConfig.ByteCompare(continuation, endS) > 0)
            {
                return(Enumerable.Empty <Triple>());
            }

            return(new RocksEnumerable(_db, continuation, endS, (Iterator it) => { return it.Next(); }));
        }
Exemplo n.º 22
0
 public static byte[] GetNameSKey(string name)
 {
     return(KeyConfig.ConcatBytes(KeyConfig.GetBytes(name), KeyConfig.ByteS));
 }
Exemplo n.º 23
0
        public byte[] GetOPrefix()
        {
            var z = KeyConfig.ByteZero;

            return(KeyConfig.ConcatBytes(_name, KeyConfig.ByteO, z, IsId, z, O, z, S, z, P));
        }
Exemplo n.º 24
0
        public static byte[] GetNamePPredicate(string name)
        {
            var nameBytes = KeyConfig.GetBytes(name);

            return(KeyConfig.ConcatBytes(nameBytes, KeyConfig.ByteP));
        }
Exemplo n.º 25
0
 private byte[] Hash(string input)
 {
     return(KeyConfig.GetBytes(input));
 }