예제 #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.");
            }
        }
예제 #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;
            }
        }
예제 #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.");
            }
        }
예제 #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.");
            }
        }
예제 #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);
     }
 }
예제 #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;
     }
 }
예제 #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);
     }
 }
예제 #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;
     }
 }
예제 #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*/
     }
 }
예제 #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*/
     }
 }
예제 #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);
            }
        }
예제 #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);
 }
예제 #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));
 }
예제 #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.");
            }
            ;
        }
예제 #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;
            }
        }
예제 #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
            }
        }
예제 #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;
            }
        }
예제 #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;
            }
        }
예제 #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();
                    }
                }
            }
        }
예제 #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));
        }
예제 #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);
        }
예제 #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);
                }
            });
        }
예제 #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.");
            }
        }
예제 #24
0
 public unsafe static SSSPCell_Accessor UseSSSPCell(this Trinity.Storage.LocalMemoryStorage storage, long CellID, ActionOnCellNotFound action)
 {
     return(SSSPCell_Accessor.New(CellID, action));
 }
예제 #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));
 }
예제 #26
0
 public IEnumerable <ICell> EnumerateGenericCells(LocalMemoryStorage storage)
 {
     throw new NotImplementedException();
 }
예제 #27
0
 public void SaveGenericCell(LocalMemoryStorage storage, CellAccessOptions writeAheadLogOptions, long cellId, ICell cell)
 {
     throw new NotImplementedException();
 }
예제 #28
0
 public static Type[] GetCellStructureTypeArray(this Trinity.Storage.LocalMemoryStorage storage)
 {
     return(new Type[] {
         typeof(StorageExtension_SSSPCell),
     });
 }
예제 #29
0
 public ICellAccessor UseGenericCell(LocalMemoryStorage storage, long cellId)
 {
     throw new NotImplementedException();
 }
예제 #30
0
 public ICellAccessor UseGenericCell(LocalMemoryStorage storage, long cellId, CellAccessOptions options, string cellType)
 {
     throw new NotImplementedException();
 }