Exemplo n.º 1
0
        public unsafe ICellAccessor UseGenericCell(Trinity.Storage.LocalMemoryStorage storage, long CellId)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;
            var    err = storage.GetLockedCellInfo(CellId, out size, out type, out cellPtr, out entryIndex);

            if (err != TrinityErrorCode.E_SUCCESS)
            {
                throw new CellNotFoundException("Cannot access the cell.");
            }
            switch ((CellType)type)
            {
            case CellType.Movie:
                return(Movie_Accessor.New(CellId, cellPtr, entryIndex, CellAccessOptions.ThrowExceptionOnCellNotFound));

            case CellType.Celebrity:
                return(Celebrity_Accessor.New(CellId, cellPtr, entryIndex, CellAccessOptions.ThrowExceptionOnCellNotFound));

            default:
                storage.ReleaseCellLock(CellId, entryIndex);
                throw new CellTypeNotMatchException("Cannot determine cell type.");
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public unsafe ICell LoadGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;
            var    err = storage.GetLockedCellInfo(cellId, out size, out type, out cellPtr, out entryIndex);

            if (err != TrinityErrorCode.E_SUCCESS)
            {
                throw new CellNotFoundException("Cannot access the cell.");
            }
            try
            {
                var accessor = UseGenericCell(cellId, cellPtr, entryIndex, type);
                var cell     = accessor.Deserialize();
                accessor.Dispose();
                return(cell);
            }
            catch (Exception ex)
            {
                storage.ReleaseCellLock(cellId, entryIndex);
                ExceptionDispatchInfo.Capture(ex).Throw();
                throw;
            }
        }
Exemplo n.º 3
0
        public unsafe ICell LoadGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;

            var err = storage.GetLockedCellInfo(cellId, out size, out type, out cellPtr, out entryIndex);

            if (err != TrinityErrorCode.E_SUCCESS)
            {
                throw new CellNotFoundException("Cannot access the cell.");
            }

            switch ((CellType)type)
            {
            /*FOREACH*/
            case CellType.t_cell_name:
                var t_cell_name_accessor = new t_cell_name_Accessor(cellPtr);
                var t_cell_name_cell     = (t_cell_name)t_cell_name_accessor;
                storage.ReleaseCellLock(cellId, entryIndex);
                t_cell_name_cell.CellID = cellId;
                return(t_cell_name_cell);

                break;

            /*END*/
            default:
                throw new CellTypeNotMatchException("Cannot determine cell type.");
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public unsafe ICellAccessor UseGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;

            var err = storage.GetLockedCellInfo(cellId, out size, out type, out cellPtr, out entryIndex);

            if (err != TrinityErrorCode.E_SUCCESS)
            {
                throw new CellNotFoundException("Cannot access the cell.");
            }

            switch ((CellType)type)
            {
            /*FOREACH*/
            case CellType.t_cell_name:
                return(t_cell_name_Accessor._get()._Setup(cellId, cellPtr, entryIndex, CellAccessOptions.ThrowExceptionOnCellNotFound));

            /*END*/
            default:
                storage.ReleaseCellLock(cellId, entryIndex);
                throw new CellTypeNotMatchException("Cannot determine cell type.");
            }
        }
Exemplo n.º 5
0
 public unsafe static node Loadnode(this Trinity.Storage.LocalMemoryStorage storage, long CellID)
 {
     using (var cell = new node_Accessor(CellID, ActionOnCellNotFound.ThrowException))
     {
         node ret = cell;
         ret.CellID = CellID;
         return(ret);
     }
 }
Exemplo n.º 6
0
 /// <inheritdoc/>
 public void SaveGenericCell(Trinity.Storage.LocalMemoryStorage storage, CellAccessOptions writeAheadLogOptions, long cellId, ICell cell)
 {
     switch ((CellType)cell.CellType)
     {
     case CellType.TripleStore:
         storage.SaveTripleStore(writeAheadLogOptions, cellId, (TripleStore)cell);
         break;
     }
 }
Exemplo n.º 7
0
 public unsafe static SSSPCell LoadSSSPCell(this Trinity.Storage.LocalMemoryStorage storage, long CellID)
 {
     using (var cell = new SSSPCell_Accessor(CellID, ActionOnCellNotFound.ThrowException))
     {
         SSSPCell ret = cell;
         ret.CellID = CellID;
         return(ret);
     }
 }
Exemplo n.º 8
0
 /// <inheritdoc/>
 public void SaveGenericCell(Trinity.Storage.LocalMemoryStorage storage, CellAccessOptions writeAheadLogOptions, ICell cell)
 {
     switch ((CellType)cell.CellType)
     {
     case CellType.Graph:
         storage.SaveGraph(writeAheadLogOptions, (Graph)cell);
         break;
     }
 }
Exemplo n.º 9
0
 public void SaveGenericCell(Trinity.Storage.LocalMemoryStorage storage, CellAccessOptions writeAheadLogOptions, long cellId, ICell cell)
 {
     switch ((CellType)cell.CellType)
     {
     /*FOREACH*/
     case CellType.t_cell_name:
         storage.Savet_cell_name(writeAheadLogOptions, cellId, (t_cell_name)cell);
         break;
         /*END*/
     }
 }
Exemplo n.º 10
0
 public void SaveGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId, ICell cell)
 {
     switch ((CellType)cell.CellType)
     {
     /*FOREACH*/
     case CellType.t_cell_name:
         storage.Savet_cell_name(cellId, (t_cell_name)cell);
         break;
         /*END*/
     }
 }
Exemplo n.º 11
0
        /// <inheritdoc/>
        public unsafe ICellAccessor UseGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId, CellAccessOptions options, string cellType)
        {
            switch (cellType)
            {
            case "TripleStore": return(TripleStore_Accessor._get()._Lock(cellId, options));

            default:
                Throw.invalid_cell_type();
                return(null);
            }
        }
Exemplo n.º 12
0
 public unsafe static bool IsSSSPCell(this Trinity.Storage.LocalMemoryStorage storage, long CellID)
 {
     //int idx;
     //int size;
     //byte* ptr = storage.GetLockedUnsafeCellLocation(CellID, out size, out idx);
     //if (ptr == null)
     //    throw new Exception(string.Format("Cell #{0} doesn't exist.", CellID));
     //bool ret = *ptr == (byte)CellType.SSSPCell;
     //storage.ReleaseCellLock(CellID, idx);
     //return ret;
     return(storage.GetCellType(CellID) == (ushort)CellType.SSSPCell);
 }
Exemplo n.º 13
0
 public unsafe static CellType GetCellType(this Trinity.Storage.LocalMemoryStorage storage, long CellID)
 {
     //int idx;
     //int size;
     //byte* ptr = storage.GetLockedUnsafeCellLocation(CellID, out size, out idx);
     //if (ptr == null)
     //    throw new Exception(string.Format("Cell #{0} doesn't exist.", CellID));
     //CellType ret = (CellType)(*ptr);
     //storage.ReleaseCellLock(CellID, idx);
     //return ret;
     return((CellType)storage.GetCellType(CellID));
 }
Exemplo n.º 14
0
        /// <summary>
        /// Allocate a generic cell accessor on the specified cell.
        /// If <c><see cref="Trinity.TrinityConfig.ReadOnly"/> == false</c>,
        /// on calling this method, it attempts to acquire the lock of the cell,
        /// and blocks until it gets the lock.
        /// </summary>
        /// <param name="storage">A <see cref="Trinity.Storage.LocalMemoryStorage"/> instance.</param>
        /// <param name="CellId">The id of the specified cell.</param>
        /// <param name="options">Specifies write-ahead logging behavior. Valid values are CellAccessOptions.StrongLogAhead(default) and CellAccessOptions.WeakLogAhead. Other values are ignored.</param>
        /// <returns>A <see cref="t_Namespace.GenericCellAccessor"/> instance.</returns>
        public unsafe ICellAccessor UseGenericCell(Trinity.Storage.LocalMemoryStorage storage, long CellId, CellAccessOptions options)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;

            var err = storage.GetLockedCellInfo(CellId, out size, out type, out cellPtr, out entryIndex);

            switch (err)
            {
            case TrinityErrorCode.E_SUCCESS:
                break;

            case TrinityErrorCode.E_CELL_NOT_FOUND:
            {
                if ((options & CellAccessOptions.ThrowExceptionOnCellNotFound) != 0)
                {
                    Throw.cell_not_found(CellId);
                }
                else if ((options & CellAccessOptions.CreateNewOnCellNotFound) != 0)
                {
                    throw new ArgumentException("CellAccessOptions.CreateNewOnCellNotFound is not valid for this method. Cannot determine new cell type.", "options");
                }
                else if ((options & CellAccessOptions.ReturnNullOnCellNotFound) != 0)
                {
                    return(null);
                }
                else
                {
                    Throw.cell_not_found(CellId);
                }
                break;
            }

            default:
                throw new CellNotFoundException("Cannot access the cell.");
            }

            switch ((CellType)type)
            {
            /*FOREACH*/
            case CellType.t_cell_name:
                return(t_cell_name_Accessor.New(CellId, cellPtr, entryIndex, options));

            /*END*/
            default:
                storage.ReleaseCellLock(CellId, entryIndex);
                throw new CellTypeNotMatchException("Cannot determine cell type.");
            }
            ;
        }
Exemplo n.º 15
0
        public void SaveGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId, ICell cell)
        {
            switch ((CellType)cell.CellType)
            {
            case CellType.Movie:
                storage.SaveMovie(cellId, (Movie)cell);
                break;

            case CellType.Celebrity:
                storage.SaveCelebrity(cellId, (Celebrity)cell);
                break;
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Allocate a generic cell accessor on the specified cell.
        /// If <c><see cref="Trinity.TrinityConfig.ReadOnly"/> == false</c>,
        /// on calling this method, it attempts to acquire the lock of the cell,
        /// and blocks until it gets the lock.
        /// </summary>
        /// <param name="storage">A <see cref="Trinity.Storage.LocalMemoryStorage"/> instance.</param>
        /// <param name="CellId">The id of the specified cell.</param>
        /// <param name="options">Cell access options.</param>
        /// <param name="cellType">Specifies the type of cell to be created.</param>
        /// <returns>A <see cref="t_Namespace.GenericCellAccessor"/> instance.</returns>
        public unsafe ICellAccessor UseGenericCell(Trinity.Storage.LocalMemoryStorage storage, long CellId, CellAccessOptions options, string cellType)
        {
            switch (cellType)
            {
            /*FOREACH*/
            case "t_cell_name": return(t_cell_name_Accessor.New(CellId, options));

            /*END*/
            default:
                Throw.invalid_cell_type();
                return(null);   // should not reach here
            }
        }
Exemplo n.º 17
0
        public void SaveGenericCell(Trinity.Storage.LocalMemoryStorage storage, CellAccessOptions writeAheadLogOptions, long cellId, ICell cell)
        {
            switch ((CellType)cell.CellType)
            {
            case CellType.Movie:
                storage.SaveMovie(writeAheadLogOptions, cellId, (Movie)cell);
                break;

            case CellType.Celebrity:
                storage.SaveCelebrity(writeAheadLogOptions, cellId, (Celebrity)cell);
                break;
            }
        }
Exemplo n.º 18
0
        /// <inheritdoc/>
        public unsafe ICellAccessor UseGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId, CellAccessOptions options)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;

            var err = storage.GetLockedCellInfo(cellId, out size, out type, out cellPtr, out entryIndex);

            switch (err)
            {
            case TrinityErrorCode.E_SUCCESS:
                break;

            case TrinityErrorCode.E_CELL_NOT_FOUND:
            {
                if ((options & CellAccessOptions.ThrowExceptionOnCellNotFound) != 0)
                {
                    Throw.cell_not_found(cellId);
                }
                else if ((options & CellAccessOptions.CreateNewOnCellNotFound) != 0)
                {
                    throw new ArgumentException("CellAccessOptions.CreateNewOnCellNotFound is not valid for this method. Cannot determine new cell type.", "options");
                }
                else if ((options & CellAccessOptions.ReturnNullOnCellNotFound) != 0)
                {
                    return(null);
                }
                else
                {
                    Throw.cell_not_found(cellId);
                }
                break;
            }

            default:
                throw new CellNotFoundException("Cannot access the cell.");
            }

            try
            {
                return(UseGenericCell(cellId, cellPtr, entryIndex, type, options));
            }
            catch (Exception ex)
            {
                storage.ReleaseCellLock(cellId, entryIndex);
                ExceptionDispatchInfo.Capture(ex).Throw();
                // should never reach here
                throw;
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Perform actions to each corresponding cell in Trinity local storage.
        /// </summary>
        /// <param name="dispose">true indicates that current memory trunk will be disposed once it is processed; otherwise, the memory is kept in-memory.</param>
        /// <param name="parallel">true indicates the actions are performed in parallel; otherwise, the actions are performed sequentially.</param>
        /// <param name="actions">A list of CellAccessorActions.</param>
        public static unsafe void ForEach(this Trinity.Storage.LocalMemoryStorage storage, bool dispose, bool parallel, params CellAccessorAction[] actions)
        {
            Dictionary <ushort, Action <long, long, int> > actionMap = new Dictionary <ushort, Action <long, long, int> >();

            foreach (var action in actions)
            {
                actionMap[(ushort)action.CellType] = action._action;
            }

            if (parallel)
            {
                Parallel.ForEach <MemoryTrunk>(storage.memory_trunks, mem_trunk =>
                {
                    mem_trunk.ForEach(
                        new CellAction <long, int, int, ushort>(
                            (byte *ptr, long id, int size, int idx, ushort cellType) =>
                    {
                        if (actionMap.ContainsKey(cellType))
                        {
                            actionMap[cellType](id, (long)ptr, idx);
                        }
                    }));
                    if (dispose)
                    {
                        mem_trunk.Save();
                        mem_trunk.Dispose();
                    }
                });
            }
            else
            {
                for (int i = 0; i < storage.memory_trunks.Length; i++)
                {
                    storage.memory_trunks[i].ForEach(
                        new CellAction <long, int, int, ushort>(
                            (byte *ptr, long id, int size, int idx, ushort cellType) =>
                    {
                        if (actionMap.ContainsKey(cellType))
                        {
                            actionMap[cellType](id, (long)ptr, idx);
                        }
                    }));
                    if (dispose)
                    {
                        storage.memory_trunks[i].Save();
                        storage.memory_trunks[i].Dispose();
                    }
                }
            }
        }
Exemplo n.º 20
0
        public unsafe static bool SaveSSSPCell(this Trinity.Storage.LocalMemoryStorage storage, long CellID, int distance = default(int), long parent = default(long), List <long> neighbors = null)
        {
            byte *targetPtr = null;

            targetPtr += 4;
            targetPtr += 8;

            if (neighbors != null)
            {
                targetPtr += neighbors.Count * 8 + sizeof(int);
            }
            else
            {
                targetPtr += sizeof(int);
            }


            byte[] tmpcell = new byte[(int)(targetPtr)];
            fixed(byte *tmpcellptr = tmpcell)
            {
                targetPtr          = tmpcellptr;
                *(int *)targetPtr  = distance;
                targetPtr         += 4;
                *(long *)targetPtr = parent;
                targetPtr         += 8;

                if (neighbors != null)
                {
                    *(int *)targetPtr = neighbors.Count * 8;
                    targetPtr        += sizeof(int);
                    for (int iterator_0 = 0; iterator_0 < neighbors.Count; ++iterator_0)
                    {
                        *(long *)targetPtr = neighbors[iterator_0];
                        targetPtr         += 8;
                    }
                }
                else
                {
                    *(int *)targetPtr = 0;
                    targetPtr        += sizeof(int);
                }
            }

            return(storage.SaveCell(CellID, tmpcell, (ushort)CellType.SSSPCell));
        }
Exemplo n.º 21
0
        /// <summary>
        /// Perform actions to each corresponding cell in Trinity local storage.
        /// </summary>
        /// <param name="parallel">true indicates the actions are performed in parallel; otherwise, the actions are performed sequentially.</param>
        /// <param name="actions">A list of CellAccessorActions.</param>
        public static unsafe void ForEach(this Trinity.Storage.LocalMemoryStorage storage, bool parallel, params CellAccessorAction[] actions)
        {
            Dictionary <ushort, Action <long, long, int> > actionMap = new Dictionary <ushort, Action <long, long, int> >();

            foreach (var action in actions)
            {
                actionMap[(ushort)action.CellType] = action._action;
            }
            storage.ForEach(
                new CellAction <long, int, int, ushort>(
                    (byte *ptr, long id, int size, int idx, ushort cellType) =>
            {
                if (actionMap.ContainsKey(cellType))
                {
                    actionMap[cellType](id, (long)ptr, idx);
                }
            }), parallel);
        }
Exemplo n.º 22
0
        public unsafe static void Transform(this Trinity.Storage.LocalMemoryStorage storage, params CellConverter[] converters)
        {
            Dictionary <ushort, CellTransformAction <long, int, ushort> > converterMap = new Dictionary <ushort, CellTransformAction <long, int, ushort> >();

            foreach (var converter in converters)
            {
                converterMap[(ushort)converter.SourceCellType] = converter._action;
            }
            storage.TransformCells((byte *ptr, long id, int count, ref ushort cellType) =>
            {
                //ushort cellType = *(ushort*)ptr;
                if (converterMap.ContainsKey(cellType))
                {
                    return(converterMap[cellType](ptr, id, count, ref cellType));
                }
                else
                {
                    byte[] ret = new byte[count];
                    Memory.Copy(ptr, 0, ret, 0, count);
                    return(ret);
                }
            });
        }
Exemplo n.º 23
0
        public unsafe ICell LoadGenericCell(Trinity.Storage.LocalMemoryStorage storage, long cellId)
        {
            ushort type;
            int    size;
            byte * cellPtr;
            int    entryIndex;
            var    err = storage.GetLockedCellInfo(cellId, out size, out type, out cellPtr, out entryIndex);

            if (err != TrinityErrorCode.E_SUCCESS)
            {
                throw new CellNotFoundException("Cannot access the cell.");
            }
            switch ((CellType)type)
            {
            case CellType.Movie:
                var Movie_accessor = new Movie_Accessor(cellPtr);
                var Movie_cell     = (Movie)Movie_accessor;
                storage.ReleaseCellLock(cellId, entryIndex);
                Movie_cell.CellID = cellId;
                return(Movie_cell);

                break;

            case CellType.Celebrity:
                var Celebrity_accessor = new Celebrity_Accessor(cellPtr);
                var Celebrity_cell     = (Celebrity)Celebrity_accessor;
                storage.ReleaseCellLock(cellId, entryIndex);
                Celebrity_cell.CellID = cellId;
                return(Celebrity_cell);

                break;

            default:
                throw new CellTypeNotMatchException("Cannot determine cell type.");
            }
        }
Exemplo n.º 24
0
 public unsafe static SSSPCell_Accessor UseSSSPCell(this Trinity.Storage.LocalMemoryStorage storage, long CellID, ActionOnCellNotFound action)
 {
     return(SSSPCell_Accessor.New(CellID, action));
 }
Exemplo n.º 25
0
 public unsafe static bool SaveSSSPCell(this Trinity.Storage.LocalMemoryStorage storage, SSSPCell CellContent)
 {
     return(SaveSSSPCell(storage, CellContent.CellID, CellContent.distance, CellContent.parent, CellContent.neighbors));
 }
Exemplo n.º 26
0
 public IEnumerable <ICell> EnumerateGenericCells(LocalMemoryStorage storage)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 27
0
 public void SaveGenericCell(LocalMemoryStorage storage, CellAccessOptions writeAheadLogOptions, long cellId, ICell cell)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 28
0
 public static Type[] GetCellStructureTypeArray(this Trinity.Storage.LocalMemoryStorage storage)
 {
     return(new Type[] {
         typeof(StorageExtension_SSSPCell),
     });
 }
Exemplo n.º 29
0
 public ICellAccessor UseGenericCell(LocalMemoryStorage storage, long cellId)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 30
0
 public ICellAccessor UseGenericCell(LocalMemoryStorage storage, long cellId, CellAccessOptions options, string cellType)
 {
     throw new NotImplementedException();
 }