Ejemplo n.º 1
0
        Result <Pointer> Set(string key, StoredValue value)
        {
            switch (Type)
            {
            case MdType.Pointers:
                return(new InvalidOperation <Pointer>($"Cannot set values directly on pointers. Key {key}, value type {value.ValueType}"));

            case MdType.Values:
                using (var db = GetRocksDb())
                {
                    var formattedKey = GetKey(key);
                    var column       = db.GetColumnFamily(COLUMN_FAMILY);
                    db.Put(formattedKey, value.Json(), cf: column);
                    _keys.Add(formattedKey);
                }
                return(Result.OK(new Pointer
                {
                    MdLocator = this.MdLocator,
                    MdKey = key,
                    ValueType = value.ValueType
                }));

            default:
                return(new ArgumentOutOfRange <Pointer>(nameof(Type)));
            }
        }
Ejemplo n.º 2
0
        // It will return the direct pointer to the stored value
        // which makes it readily available for indexing at higher levels.
        Result <Pointer> Add(string key, StoredValue value)
        {
            if (IsFull)
            {
                return(new MdOutOfEntriesError <Pointer>($"Filled: {Count}/{MdMetadata.Capacity}"));
            }
            switch (Type)
            {
            case MdType.Pointers:
                if (Count == 0)
                {
                    return(ExpandLevel(key, value));
                }

                IMd target;

                using (var db = GetRocksDb())
                {
                    var column = db.GetColumnFamily(COLUMN_FAMILY);
                    var stored = db.Get(GetKey(Count), cf: column);
                    if (value == null)
                    {
                        return(new KeyNotFound <Pointer>($"Key: {key} in {_data.LocatorString}"));
                    }

                    var pointer = value.Parse <StoredValue>().Payload.Parse <Pointer>();
                    target = Locate(pointer.MdLocator);
                }

                if (target.IsFull)
                {
                    return(ExpandLevel(key, value));
                }
                return((target as LocalMd).Add(key, value));

            case MdType.Values:
                using (var db = GetRocksDb())
                {
                    var formattedKey = GetKey(key);
                    var column       = db.GetColumnFamily(COLUMN_FAMILY);
                    var stored       = db.Get(formattedKey, cf: column);

                    //if (stored != null)
                    //    return new ValueAlreadyExists<Pointer>($"Key: {key}.");

                    if (stored == null)
                    {
                        db.Put(formattedKey, value.Json(), cf: column);
                        db.Put($"{_data.LocatorString}_Count", $"{++Count}", cf: column);
                        _keys.Add(formattedKey);
                    }
                    else if (!_keys.Contains(formattedKey))
                    {
                        _keys.Add(formattedKey);
                    }
                }
                return(Result.OK(new Pointer
                {
                    MdLocator = this.MdLocator,
                    MdKey = key,
                    ValueType = value.ValueType
                }));

            default:
                return(new ArgumentOutOfRange <Pointer>(nameof(Type)));
            }
        }