GetLastAllocatedOid() private method

private GetLastAllocatedOid ( ) : ulong
return ulong
Esempio n. 1
0
        public IEnumerable <object> Enumerate(Type type)
        {
            if (type == typeof(object))
            {
                type = null;
            }
            else if (type != null)
            {
                AutoRegisterType(type);
            }
            ulong oid      = 0;
            ulong finalOid = _owner.GetLastAllocatedOid();
            long  prevProtectionCounter = 0;

            while (true)
            {
                _keyValueTrProtector.Start();
                if (oid == 0)
                {
                    prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                    _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                    if (!_keyValueTr.FindFirstKey())
                    {
                        break;
                    }
                }
                else
                {
                    if (_keyValueTrProtector.WasInterupted(prevProtectionCounter))
                    {
                        _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                        oid++;
                        var key    = BuildKeyFromOid(oid);
                        var result = _keyValueTr.Find(ByteBuffer.NewSync(key));
                        if (result == FindResult.Previous)
                        {
                            if (!_keyValueTr.FindNextKey())
                            {
                                result = FindResult.NotFound;
                            }
                        }
                        if (result == FindResult.NotFound)
                        {
                            oid--;
                            break;
                        }
                    }
                    else
                    {
                        if (!_keyValueTr.FindNextKey())
                        {
                            break;
                        }
                    }
                    prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                }
                oid = ReadOidFromCurrentKeyInTransaction();
                var o = GetObjFromObjCacheByOid(oid);
                if (o != null)
                {
                    if (type == null || type.IsInstanceOfType(o))
                    {
                        yield return(o);
                    }
                    continue;
                }
                TableInfo tableInfo;
                var       reader = ReadObjStart(oid, out tableInfo);
                if (type != null && !type.IsAssignableFrom(tableInfo.ClientType))
                {
                    continue;
                }
                var obj = ReadObjFinish(oid, tableInfo, reader);
                yield return(obj);
            }
            if (_dirtyObjSet == null)
            {
                yield break;
            }
            var dirtyObjsToEnum = _dirtyObjSet.Where(p => p.Key > oid && p.Key <= finalOid).ToList();

            dirtyObjsToEnum.Sort((p1, p2) =>
            {
                if (p1.Key < p2.Key)
                {
                    return(-1);
                }
                if (p1.Key > p2.Key)
                {
                    return(1);
                }
                return(0);
            });
            foreach (var dObjPair in dirtyObjsToEnum)
            {
                var obj = dObjPair.Value;
                if (type != null && !type.IsInstanceOfType(obj))
                {
                    continue;
                }
                yield return(obj);
            }
        }
Esempio n. 2
0
        public IEnumerable <object> Enumerate(Type type)
        {
            if (type == typeof(object))
            {
                type = null;
            }
            else if (type != null)
            {
                AutoRegisterType(type);
            }
            var   taken    = false;
            ulong oid      = 0;
            ulong finalOid = _owner.GetLastAllocatedOid();
            long  prevProtectionCounter = 0;

            try
            {
                while (true)
                {
                    if (!taken)
                    {
                        _keyValueTrProtector.Start(ref taken);
                    }
                    if (oid == 0)
                    {
                        prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                        _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                        if (!_keyValueTr.FindFirstKey())
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (_keyValueTrProtector.WasInterupted(prevProtectionCounter))
                        {
                            _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix);
                            oid++;
                            byte[] key = BuildKeyFromOid(oid);
                            if (_keyValueTr.FindKey(key, 0, key.Length, FindKeyStrategy.OnlyNext) == FindKeyResult.NotFound)
                            {
                                oid--;
                                break;
                            }
                        }
                        else
                        {
                            if (!_keyValueTr.FindNextKey())
                            {
                                break;
                            }
                        }
                    }
                    oid = ReadOidFromCurrentKeyInTransaction();
                    WeakReference weakObj;
                    if (_objCache.TryGetValue(oid, out weakObj))
                    {
                        var o = weakObj.Target;
                        if (o != null)
                        {
                            if (type == null || type.IsAssignableFrom(o.GetType()))
                            {
                                _keyValueTrProtector.Stop(ref taken);
                                yield return(o);

                                continue;
                            }
                            continue;
                        }
                    }
                    TableInfo             tableInfo;
                    KeyValueDBValueReader reader = ReadObjStart(oid, out tableInfo);
                    if (type != null && !type.IsAssignableFrom(tableInfo.ClientType))
                    {
                        continue;
                    }
                    object obj = ReadObjFinish(oid, tableInfo, reader);
                    _keyValueTrProtector.Stop(ref taken);
                    yield return(obj);
                }
            }
            finally
            {
                if (taken)
                {
                    _keyValueTrProtector.Stop();
                }
            }
            var dirtyObjsToEnum = _dirtyObjSet.Where(p => p.Key > oid && p.Key <= finalOid).ToList();

            dirtyObjsToEnum.Sort((p1, p2) =>
            {
                if (p1.Key < p2.Key)
                {
                    return(-1);
                }
                if (p1.Key > p2.Key)
                {
                    return(1);
                }
                return(0);
            });
            foreach (var dObjPair in dirtyObjsToEnum)
            {
                object obj = dObjPair.Value;
                if (type != null && !type.IsAssignableFrom(obj.GetType()))
                {
                    continue;
                }
                yield return(obj);
            }
        }