Example #1
0
        /// <summary>
        /// Invokes a store operation.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        /// <param name="grid">Grid.</param>
        /// <returns>Invocation result.</returns>
        /// <exception cref="IgniteException">Invalid operation type:  + opType</exception>
        public int Invoke(IBinaryStream stream, Ignite grid)
        {
            IBinaryReader reader = grid.Marshaller.StartUnmarshal(stream,
                                                                  _convertBinary ? BinaryMode.Deserialize : BinaryMode.ForceBinary);

            IBinaryRawReader rawReader = reader.GetRawReader();

            int opType = rawReader.ReadByte();

            // Setup cache session for this invocation.
            long sesId = rawReader.ReadLong();

            CacheStoreSession ses = grid.HandleRegistry.Get <CacheStoreSession>(sesId, true);

            ses.CacheName = rawReader.ReadString();

            _sesProxy.SetSession(ses);

            try
            {
                // Perform operation.
                switch (opType)
                {
                case OpLoadCache:
                {
                    var args = rawReader.ReadArray <object>();

                    stream.Seek(0, SeekOrigin.Begin);

                    int cnt = 0;
                    stream.WriteInt(cnt);     // Reserve space for count.

                    var writer = grid.Marshaller.StartMarshal(stream);

                    _store.LoadCache((k, v) =>
                        {
                            lock (writer) // User-defined store can be multithreaded.
                            {
                                writer.WithDetach(w =>
                                {
                                    w.WriteObject(k);
                                    w.WriteObject(v);
                                });

                                cnt++;
                            }
                        }, args);

                    stream.WriteInt(0, cnt);

                    grid.Marshaller.FinishMarshal(writer);

                    break;
                }

                case OpLoad:
                {
                    var val = _store.Load(rawReader.ReadObject <TK>());

                    stream.Seek(0, SeekOrigin.Begin);

                    var writer = grid.Marshaller.StartMarshal(stream);

                    writer.WriteObject(val);

                    grid.Marshaller.FinishMarshal(writer);

                    break;
                }

                case OpLoadAll:
                {
                    // We can't do both read and write lazily because stream is reused.
                    // Read keys non-lazily, write result lazily.
                    var keys = ReadAllKeys(rawReader);

                    var result = _store.LoadAll(keys);

                    stream.Seek(0, SeekOrigin.Begin);

                    int cnt = 0;
                    stream.WriteInt(cnt);     // Reserve space for count.

                    var writer = grid.Marshaller.StartMarshal(stream);

                    foreach (var entry in result)
                    {
                        var entry0 = entry;     // Copy modified closure.

                        writer.WithDetach(w =>
                            {
                                w.WriteObject(entry0.Key);
                                w.WriteObject(entry0.Value);
                            });

                        cnt++;
                    }

                    stream.WriteInt(0, cnt);

                    grid.Marshaller.FinishMarshal(writer);

                    break;
                }

                case OpPut:
                    _store.Write(rawReader.ReadObject <TK>(), rawReader.ReadObject <TV>());

                    break;

                case OpPutAll:
                    _store.WriteAll(ReadPairs(rawReader));

                    break;

                case OpRmv:
                    _store.Delete(rawReader.ReadObject <TK>());

                    break;

                case OpRmvAll:
                    _store.DeleteAll(ReadKeys(rawReader));

                    break;

                case OpSesEnd:
                    grid.HandleRegistry.Release(sesId);

                    _store.SessionEnd(rawReader.ReadBoolean());

                    break;

                default:
                    throw new IgniteException("Invalid operation type: " + opType);
                }

                return(0);
            }
            finally
            {
                _sesProxy.ClearSession();
            }
        }
Example #2
0
        /// <summary>
        /// Invokes a store operation.
        /// </summary>
        /// <param name="input">Input stream.</param>
        /// <param name="cb">Callback.</param>
        /// <param name="grid">Grid.</param>
        /// <returns>Invocation result.</returns>
        /// <exception cref="IgniteException">Invalid operation type:  + opType</exception>
        public int Invoke(IBinaryStream input, IUnmanagedTarget cb, Ignite grid)
        {
            IBinaryReader reader = grid.Marshaller.StartUnmarshal(input,
                                                                  _convertBinary ? BinaryMode.Deserialize : BinaryMode.ForceBinary);

            IBinaryRawReader rawReader = reader.GetRawReader();

            int opType = rawReader.ReadByte();

            // Setup cache sessoin for this invocation.
            long sesId = rawReader.ReadLong();

            CacheStoreSession ses = grid.HandleRegistry.Get <CacheStoreSession>(sesId, true);

            ses.CacheName = rawReader.ReadString();

            _sesProxy.SetSession(ses);

            try
            {
                // Perform operation.
                switch (opType)
                {
                case OpLoadCache:
                    _store.LoadCache((k, v) => WriteObjects(cb, grid, k, v), rawReader.ReadArray <object>());

                    break;

                case OpLoad:
                    object val = _store.Load(rawReader.ReadObject <object>());

                    if (val != null)
                    {
                        WriteObjects(cb, grid, val);
                    }

                    break;

                case OpLoadAll:
                    var keys = rawReader.ReadCollection();

                    var result = _store.LoadAll(keys);

                    foreach (DictionaryEntry entry in result)
                    {
                        WriteObjects(cb, grid, entry.Key, entry.Value);
                    }

                    break;

                case OpPut:
                    _store.Write(rawReader.ReadObject <object>(), rawReader.ReadObject <object>());

                    break;

                case OpPutAll:
                    var size = rawReader.ReadInt();

                    var dict = new Hashtable(size);

                    for (int i = 0; i < size; i++)
                    {
                        dict[rawReader.ReadObject <object>()] = rawReader.ReadObject <object>();
                    }

                    _store.WriteAll(dict);

                    break;

                case OpRmv:
                    _store.Delete(rawReader.ReadObject <object>());

                    break;

                case OpRmvAll:
                    _store.DeleteAll(rawReader.ReadCollection());

                    break;

                case OpSesEnd:
                    grid.HandleRegistry.Release(sesId);

                    _store.SessionEnd(rawReader.ReadBoolean());

                    break;

                default:
                    throw new IgniteException("Invalid operation type: " + opType);
                }

                return(0);
            }
            finally
            {
                _sesProxy.ClearSession();
            }
        }