Example #1
0
        /// <summary>
        /// This method will be used to make all the new vertex objects
        /// that we need for drawing the graph
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void Initialize(Point startLocation, int gridWidth, int gridHeight, int blockSize, List <Wall> walls)
        {
            // Loop throgh and make the number of rows
            for (int x = startLocation.X; x < gridWidth + startLocation.X; x += blockSize)
            {
                // For each row, make a column
                for (int y = startLocation.Y; y < gridHeight + startLocation.Y; y += blockSize)
                {
                    // Make a new Vertex and add it to OPEN
                    // Use blocksize - 1 in order to draw a little bit of space in between the blocks
                    Vertex newVert = new Vertex(new Point(x, y), new Point(blockSize - 1));
                    _ALL_VERTECIES.Add(newVert);
                }
            }


            // Set the walls
            SetWalls(walls);


            // Pick a current vertex
            if (_ALL_VERTECIES.Capacity > 0)
            {
                _currentVertex = _ALL_VERTECIES[0];
            }

            // Change th color of the starting vertex
            _currentVertex.VertColor = Color.Green;
            // The distance is 0
            _currentVertex.Distance = 0;

            // Add the current vertex to the OPEN priority queue
            OPEN.Enqueue(_currentVertex);
        }
Example #2
0
        /// <summary>
        /// Returns a list that is the path that the unit should take.
        /// The list is 'backwards' so to speak, as in the closest move to make
        /// is at the end of the list.
        /// </summary>
        /// <param name="START"></param>
        /// <param name="GOAL"></param>
        /// <returns></returns>
        public List <Vertex> ShortestPathSlow(Vertex START, Vertex GOAL)
        {
            OPEN.Clear();
            CLOSED.Clear();
            _currentVertex = START;
            OPEN.Enqueue(_currentVertex);

            // Set this so taht we can traverse later
            Vertex startingPoint = _currentVertex;

            GOAL.VertColor = Color.Red;
            // While the peek of open is not the goal
            while (_OPEN.Peek() != GOAL)
            {
                // Take the current node out of open
                // Current should be the thing on top of the priority queue
                _currentVertex = _OPEN.DequeueTwo();
                _CLOSED.Add(_currentVertex);

                // Find all of the neighbors of the current vertex
                List <Vertex> currentNeighbors = GetNieghbors(_currentVertex);

                foreach (Vertex neighbor in currentNeighbors)
                {
                    // Get the cost, which is G(current)
                    double cost = _currentVertex.Distance + GetCost(_currentVertex, neighbor);

                    // If neighbor is in OPEN and cost less then neighbor.distance
                    // Neighbor.Distance is G(neighbor)
                    if (_OPEN.heap.Contains(neighbor) && cost < neighbor.Distance)
                    {
                        // Remove neighbor from OPEN, because the new path is better
                        _OPEN.heap.Remove(neighbor);
                        _currentVertex = OPEN.DequeueTwo();
                    }
                    // If neighbor is in CLOSED and cost is less then g(neighbor)
                    if (_CLOSED.Contains(neighbor) && cost < neighbor.Distance)
                    {
                        // Remove neighbor from CLOSED
                        _CLOSED.Remove(neighbor);
                    }
                    // If neighbor is not in open and neighbor is not in CLOSED:
                    if (!_OPEN.heap.Contains(neighbor) && !_CLOSED.Contains(neighbor))
                    {
                        neighbor.Distance = cost;
                        _OPEN.Enqueue(neighbor);

                        neighbor.NeighboringVertex = _currentVertex;
                    }
                }
            }

            // Reconstruct the reverse path from goal to start

            _ONES_TO_DRAW = ColorPathSlow(startingPoint, GOAL);
            return(_ONES_TO_DRAW);
        }
Example #3
0
        /// <summary>
        /// Calls the Reset() method on every vertex in the grid
        /// </summary>
        /// <param name="currentVertex"></param>
        public void ResetAllVertecies(Vertex currentVertex)
        {
            foreach (Vertex v in _ALL_VERTECIES)
            {
                v.Reset();
            }
            OPEN.Clear();
            CLOSED.Clear();

            _currentVertex = currentVertex;
            OPEN.Enqueue(_currentVertex);
        }
Example #4
0
 public void CommandCheck()
 {
     Input = Input.ToLower();
     if (Input.StartsWith("ls"))
     {
         LS.Options(Input);
     }
     else if (Input.StartsWith("cat"))
     {
         CAT.Options(Input);
     }
     else if (Input.StartsWith("os"))
     {
         OS.Options(Input);
     }
     else if (Input.StartsWith("help"))
     {
         new HELP();
     }
     else if (Input.StartsWith("system"))
     {
         SYSTEM.Options(Input);
     }
     else if (Input.StartsWith("process"))
     {
         PROCESSES.Options(Input);
     }
     else if (Input.StartsWith("open"))
     {
         OPEN.Options(Input);
     }
     else if (Input == "exit")
     {
         Environment.Exit(0);
     }
     else
     {
         Console.WriteLine("Non-existent command");
     }
 }
Example #5
0
        public RC OpenAndAlloc(string path, out VFile file, OPEN flags, out OPEN outFlags)
        {
            file     = null;
            outFlags = 0;
            VFile file2 = CreateOsFile();

            if (file2 == null)
            {
                return(RC.NOMEM);
            }
            RC rc = Open(path, file2, flags, out outFlags);

            if (rc != RC.OK)
            {
                C._free(ref file2);
            }
            else
            {
                file = file2;
            }
            return(rc);
        }
Example #6
0
        public static RC Open(VSystem vfs, string filename, BContext ctx, ref Btree btree, OPEN flags, VSystem.OPEN vfsFlags)
        {
            // True if opening an ephemeral, temporary database
            bool tempDB = string.IsNullOrEmpty(filename);

            // Set the variable isMemdb to true for an in-memory database, or false for a file-based database.
            bool memoryDB = (filename == ":memory:") ||
                (tempDB && ctx.TempInMemory()) ||
                (vfsFlags & VSystem.OPEN.MEMORY) != 0;

            Debug.Assert(ctx != null);
            Debug.Assert(vfs != null);
            Debug.Assert(MutexEx.Held(ctx.Mutex));
            Debug.Assert(((uint)flags & 0xff) == (uint)flags); // flags fit in 8 bits

            // Only a BTREE_SINGLE database can be BTREE_UNORDERED
            Debug.Assert((flags & OPEN.UNORDERED) == 0 || (flags & OPEN.SINGLE) != 0);

            // A BTREE_SINGLE database is always a temporary and/or ephemeral
            Debug.Assert((flags & OPEN.SINGLE) == 0 || tempDB);

            if (memoryDB)
                flags |= OPEN.MEMORY;
            if ((vfsFlags & VSystem.OPEN.MAIN_DB) != 0 && (memoryDB || tempDB))
                vfsFlags = (vfsFlags & ~VSystem.OPEN.MAIN_DB) | VSystem.OPEN.TEMP_DB;
            var p = new Btree(); // Handle to return
            if (p == null)
                return RC.NOMEM;
            p.InTrans = TRANS.NONE;
            p.Ctx = ctx;
#if !OMIT_SHARED_CACHE
            p.Lock.Btree = p;
            p.Lock.Table = 1;
#endif

            RC rc = RC.OK; // Result code from this function
            BtShared bt = null; // Shared part of btree structure
            MutexEx mutexOpen = null;
#if !OMIT_SHARED_CACHE && !OMIT_DISKIO
            // If this Btree is a candidate for shared cache, try to find an existing BtShared object that we can share with
            if (!tempDB && (!memoryDB || (vfsFlags & VSystem.OPEN.URI) != 0))
                if ((vfsFlags & VSystem.OPEN.SHAREDCACHE) != 0)
                {
                    string fullPathname;
                    p.Sharable_ = true;
                    if (memoryDB)
                        fullPathname = filename;
                    else
                        vfs.FullPathname(filename, out fullPathname);
                    MutexEx mutexShared;
#if THREADSAFE
                    mutexOpen = MutexEx.Alloc(MutexEx.MUTEX.STATIC_OPEN); // Prevents a race condition. Ticket #3537
                    MutexEx.Enter(mutexOpen);
                    mutexShared = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER);
                    MutexEx.Enter(mutexShared);
#endif
                    for (bt = _sharedCacheList; bt != null; bt = bt.Next)
                    {
                        Debug.Assert(bt.Refs > 0);
                        if (fullPathname == bt.Pager.get_Filename(false) && bt.Pager.get_Vfs() == vfs)
                        {
                            for (var i = ctx.DBs.length - 1; i >= 0; i--)
                            {
                                var existing = ctx.DBs[i].Bt;
                                if (existing != null && existing.Bt == bt)
                                {
                                    MutexEx.Leave(mutexShared);
                                    MutexEx.Leave(mutexOpen);
                                    fullPathname = null;
                                    p = null;
                                    return RC.CONSTRAINT;
                                }
                            }
                            p.Bt = bt;
                            bt.Refs++;
                            break;
                        }
                    }
                    MutexEx.Leave(mutexShared);
                    fullPathname = null;
                }
#if DEBUG
                else
                    // In debug mode, we mark all persistent databases as sharable even when they are not.  This exercises the locking code and
                    // gives more opportunity for asserts(sqlite3_mutex_held()) statements to find locking problems.
                    p.Sharable_ = true;
#endif
#endif

            byte reserves; // Byte of unused space on each page
            var dbHeader = new byte[100]; // Database header content
            if (bt == null)
            {
                // The following asserts make sure that structures used by the btree are the right size.  This is to guard against size changes that result
                // when compiling on a different architecture.
                Debug.Assert(sizeof(long) == 8 || sizeof(long) == 4);
                Debug.Assert(sizeof(ulong) == 8 || sizeof(ulong) == 4);
                Debug.Assert(sizeof(uint) == 4);
                Debug.Assert(sizeof(ushort) == 2);
                Debug.Assert(sizeof(Pid) == 4);

                bt = new BtShared();
                if (bt == null)
                {
                    rc = RC.NOMEM;
                    goto btree_open_out;
                }
                rc = Pager.Open(vfs, out bt.Pager, filename, EXTRA_SIZE, (IPager.PAGEROPEN)flags, vfsFlags, pageReinit, null);
                if (rc == RC.OK)
                    rc = bt.Pager.ReadFileHeader(dbHeader.Length, dbHeader);
                if (rc != RC.OK)
                    goto btree_open_out;
                bt.OpenFlags = flags;
                bt.Ctx = ctx;
                bt.Pager.SetBusyHandler(btreeInvokeBusyHandler, bt);
                p.Bt = bt;

                bt.Cursor = null;
                bt.Page1 = null;
                if (bt.Pager.get_Readonly()) bt.BtsFlags |= BTS.READ_ONLY;
#if SECURE_DELETE
                bt.BtsFlags |= BTS.SECURE_DELETE;
#endif
                bt.PageSize = (Pid)((dbHeader[16] << 8) | (dbHeader[17] << 16));
                if (bt.PageSize < 512 || bt.PageSize > Pager.MAX_PAGE_SIZE || ((bt.PageSize - 1) & bt.PageSize) != 0)
                {
                    bt.PageSize = 0;
#if !OMIT_AUTOVACUUM
                    // If the magic name ":memory:" will create an in-memory database, then leave the autoVacuum mode at 0 (do not auto-vacuum), even if
                    // SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
                    // regular file-name. In this case the auto-vacuum applies as per normal.
                    if (filename != null && !memoryDB)
                    {
                        bt.AutoVacuum = (DEFAULT_AUTOVACUUM != 0);
                        bt.IncrVacuum = (DEFAULT_AUTOVACUUM == AUTOVACUUM.INCR);
                    }
#endif
                    reserves = 0;
                }
                else
                {
                    reserves = dbHeader[20];
                    bt.BtsFlags |= BTS.PAGESIZE_FIXED;
#if !OMIT_AUTOVACUUM
                    bt.AutoVacuum = (ConvertEx.Get4(dbHeader, 36 + 4 * 4) != 0);
                    bt.IncrVacuum = (ConvertEx.Get4(dbHeader, 36 + 7 * 4) != 0);
#endif
                }
                rc = bt.Pager.SetPageSize(ref bt.PageSize, reserves);
                if (rc != RC.OK) goto btree_open_out;
                bt.UsableSize = (ushort)(bt.PageSize - reserves);
                Debug.Assert((bt.PageSize & 7) == 0); // 8-byte alignment of pageSize

#if !SHARED_CACHE && !OMIT_DISKIO
                // Add the new BtShared object to the linked list sharable BtShareds.
                if (p.Sharable_)
                {
                    bt.Refs = 1;
                    MutexEx mutexShared;
#if THREADSAFE
                    mutexShared = MutexEx.Alloc(MutexEx.MUTEX.STATIC_MASTER);
                    bt.Mutex = MutexEx.Alloc(MutexEx.MUTEX.FAST);
#endif
                    MutexEx.Enter(mutexShared);
                    bt.Next = _sharedCacheList;
                    _sharedCacheList = bt;
                    MutexEx.Leave(mutexShared);
                }
#endif
            }

#if !OMIT_SHARED_CACHE && !OMIT_DISKIO
            // If the new Btree uses a sharable pBtShared, then link the new Btree into the list of all sharable Btrees for the same connection.
            // The list is kept in ascending order by pBt address.
            if (p.Sharable_)
            {
                Btree sib;
                for (var i = 0; i < ctx.DBs.length; i++)
                    if ((sib = ctx.DBs[i].Bt) != null && sib.Sharable_)
                    {
                        while (sib.Prev != null) { sib = sib.Prev; }
                        if (p.Bt.AutoID < sib.Bt.AutoID)
                        {
                            p.Next = sib;
                            p.Prev = null;
                            sib.Prev = p;
                        }
                        else
                        {
                            while (sib.Next != null && sib.Next.Bt.AutoID < p.Bt.AutoID)
                                sib = sib.Next;
                            p.Next = sib.Next;
                            p.Prev = sib;
                            if (p.Next != null)
                                p.Next.Prev = p;
                            sib.Next = p;
                        }
                        break;
                    }
            }
#endif
            btree = p;

        btree_open_out:
            if (rc != RC.OK)
            {
                if (bt != null && bt.Pager != null)
                    bt.Pager.Close();
                bt = null;
                p = null;
                btree = null;
            }
            else
                // If the B-Tree was successfully opened, set the pager-cache size to the default value. Except, when opening on an existing shared pager-cache,
                // do not change the pager-cache size.
                if (p.Schema(0, null) == null)
                    p.Bt.Pager.SetCacheSize(DEFAULT_CACHE_SIZE);
#if THREADSAFE
            Debug.Assert(MutexEx.Held(mutexOpen));
            MutexEx.Leave(mutexOpen);
#endif
            return rc;
        }
 public RC xOpen(string zName, VirtualFile pFile, OPEN flags, out OPEN pOutFlags)
 {
     pOutFlags = 0;
     // If argument zPath is a NULL pointer, this function is required to open a temporary file. Use this buffer to store the file name in.
     //var zTmpname = new StringBuilder(MAX_PATH + 1);        // Buffer used to create temp filename
     var rc = RC.OK;
     var eType = (OPEN)((int)flags & 0xFFFFFF00);  // Type of file to open
     var isExclusive = (flags & OPEN.EXCLUSIVE) != 0;
     var isDelete = (flags & OPEN.DELETEONCLOSE) != 0;
     var isCreate = (flags & OPEN.CREATE) != 0;
     var isReadonly = (flags & OPEN.READONLY) != 0;
     var isReadWrite = (flags & OPEN.READWRITE) != 0;
     var isOpenJournal = (isCreate && (eType == OPEN.MASTER_JOURNAL || eType == OPEN.MAIN_JOURNAL || eType == OPEN.WAL));
     // Check the following statements are true:
     //   (a) Exactly one of the READWRITE and READONLY flags must be set, and
     //   (b) if CREATE is set, then READWRITE must also be set, and
     //   (c) if EXCLUSIVE is set, then CREATE must also be set.
     //   (d) if DELETEONCLOSE is set, then CREATE must also be set.
     Debug.Assert((!isReadonly || !isReadWrite) && (isReadWrite || isReadonly));
     Debug.Assert(!isCreate || isReadWrite);
     Debug.Assert(!isExclusive || isCreate);
     Debug.Assert(!isDelete || isCreate);
     // The main DB, main journal, WAL file and master journal are never automatically deleted. Nor are they ever temporary files.
     //Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.MAIN_DB);
     Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.MAIN_JOURNAL);
     Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.MASTER_JOURNAL);
     Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.WAL);
     // Assert that the upper layer has set one of the "file-type" flags.
     Debug.Assert(eType == OPEN.MAIN_DB || eType == OPEN.TEMP_DB || eType == OPEN.MAIN_JOURNAL || eType == OPEN.TEMP_JOURNAL ||
         eType == OPEN.SUBJOURNAL || eType == OPEN.MASTER_JOURNAL || eType == OPEN.TRANSIENT_DB || eType == OPEN.WAL);
     pFile.S = null;
     // If the second argument to this function is NULL, generate a temporary file name to use
     if (string.IsNullOrEmpty(zName))
     {
         Debug.Assert(isDelete && !isOpenJournal);
         zName = Path.GetRandomFileName();
     }
     // Convert the filename to the system encoding.
     if (zName.StartsWith("/") && !zName.StartsWith("//"))
         zName = zName.Substring(1);
     var dwDesiredAccess = (isReadWrite ? FileAccess.Read | FileAccess.Write : FileAccess.Read);
     // SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is created. SQLite doesn't use it to indicate "exclusive access" as it is usually understood.
     FileMode dwCreationDisposition;
     if (isExclusive)
         // Creates a new file, only if it does not already exist. */ If the file exists, it fails.
         dwCreationDisposition = FileMode.CreateNew;
     else if (isCreate)
         // Open existing file, or create if it doesn't exist
         dwCreationDisposition = FileMode.OpenOrCreate;
     else
         // Opens a file, only if it exists.
         dwCreationDisposition = FileMode.Open;
     var dwShareMode = FileShare.Read | FileShare.Write;
     #if !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
     FileOptions dwFlagsAndAttributes;
     #endif
     if (isDelete)
     #if !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
         dwFlagsAndAttributes = FileOptions.DeleteOnClose;
     #endif
     else
     #if !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
         dwFlagsAndAttributes = FileOptions.None;
     #endif
     // Reports from the internet are that performance is always better if FILE_FLAG_RANDOM_ACCESS is used.
     FileStream fs = null;
     if (Environment.OSVersion.Platform >= PlatformID.Win32NT)
     {
         // retry opening the file a few times; this is because of a racing condition between a delete and open call to the FS
         var retries = 3;
         while (fs == null && retries > 0)
             try
             {
                 retries--;
     #if WINDOWS_PHONE || SQLITE_SILVERLIGHT
                 fs = new IsolatedStorageFileStream(zConverted, dwCreationDisposition, dwDesiredAccess, dwShareMode, IsolatedStorageFile.GetUserStoreForApplication());
     #elif !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
                 fs = new FileStream(zName, dwCreationDisposition, dwDesiredAccess, dwShareMode, 4096, dwFlagsAndAttributes);
     #else
                 fs = new FileStream(zName, dwCreationDisposition, dwDesiredAccess, dwShareMode, 4096);
     #endif
     #if DEBUG
                 SysEx.OSTRACE("OPEN {0} ({1})", fs.GetHashCode(), fs.Name);
     #endif
             }
             catch (Exception) { Thread.Sleep(100); }
     }
     SysEx.OSTRACE("OPEN {0} {1} 0x{2:x} {3}", pFile.GetHashCode(), zName, dwDesiredAccess, fs == null ? "failed" : "ok");
     if (fs == null ||
     #if !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
      fs.SafeFileHandle.IsInvalid
     #else
      !fs.CanRead
     #endif
     )
     {
     #if SQLITE_SILVERLIGHT
     pFile.lastErrno = 1;
     #else
         pFile.LastErrno = (uint)Marshal.GetLastWin32Error();
     #endif
         VirtualFile.winLogError(RC.CANTOPEN, "winOpen", zName);
         return (isReadWrite ? xOpen(zName, pFile, ((flags | OPEN.READONLY) & ~(OPEN.CREATE | OPEN.READWRITE)), out pOutFlags) : SysEx.SQLITE_CANTOPEN_BKPT());
     }
     pOutFlags = (isReadWrite ? OPEN.READWRITE : OPEN.READONLY);
     pFile.Clear();
     pFile.IsOpen = true;
     pFile.S = fs;
     pFile.LastErrno = 0;
     pFile.Vfs = this;
     pFile.Shm = null;
     pFile.Path = zName;
     pFile.SectorSize = (uint)getSectorSize(zName);
     return rc;
 }
Example #8
0
        // was:sqlite3BtreeOpen
        public static RC Open(VirtualFileSystem pVfs, string zFilename, sqlite3 db, ref Btree rTree, OPEN flags, VFSOPEN vfsFlags)
        {
            Btree p;                      // Handle to return   
            var rc = RC.OK;
            byte nReserve;                   // Byte of unused space on each page
            var zDbHeader = new byte[100]; // Database header content
            // True if opening an ephemeral, temporary database */
            bool isTempDb = string.IsNullOrEmpty(zFilename);
            // Set the variable isMemdb to true for an in-memory database, or  false for a file-based database.
#if SQLITE_OMIT_MEMORYDB
            var isMemdb = false;
#else
            var isMemdb = (zFilename == ":memory:" || isTempDb && db.sqlite3TempInMemory());
#endif
            Debug.Assert(db != null);
            Debug.Assert(pVfs != null);
            Debug.Assert(MutexEx.Held(db.Mutex));
            Debug.Assert(((uint)flags & 0xff) == (uint)flags);   // flags fit in 8 bits
            // Only a BTREE_SINGLE database can be BTREE_UNORDERED
            Debug.Assert((flags & OPEN.UNORDERED) == 0 || (flags & OPEN.SINGLE) != 0);
            // A BTREE_SINGLE database is always a temporary and/or ephemeral
            Debug.Assert((flags & OPEN.SINGLE) == 0 || isTempDb);
            if ((db.flags & sqlite3b.SQLITE.NoReadlock) != 0)
                flags |= OPEN.NO_READLOCK;
            if (isMemdb)
                flags |= OPEN.MEMORY;
            if ((vfsFlags & VFSOPEN.MAIN_DB) != 0 && (isMemdb || isTempDb))
                vfsFlags = (vfsFlags & ~VFSOPEN.MAIN_DB) | VFSOPEN.TEMP_DB;
            p = new Btree();
            p.InTransaction = TRANS.NONE;
            p.DB = db;
#if !SQLITE_OMIT_SHARED_CACHE
            p.Locks.Tree = p;
            p.Locks.TableID = 1;
#endif
            BtShared shared = null;          // Shared part of btree structure
            MutexEx mutexOpen = null;  // Prevents a race condition.
#if !SQLITE_OMIT_SHARED_CACHE && !SQLITE_OMIT_DISKIO
            // If this Btree is a candidate for shared cache, try to find an existing BtShared object that we can share with
            if (!isMemdb && !isTempDb)
            {
                if ((vfsFlags & VFSOPEN.SHAREDCACHE) != 0)
                {
                    p.Sharable = true;
                    string zPathname;
                    rc = pVfs.xFullPathname(zFilename, out zPathname);
                    mutexOpen = MutexEx.Alloc(MUTEX.STATIC_OPEN);
                    MutexEx.Enter(mutexOpen);
                    var mutexShared = MutexEx.Alloc(MUTEX.STATIC_MASTER);
                    MutexEx.Enter(mutexShared);
                    for (shared = SysEx.getGLOBAL<BtShared>(s_sqlite3SharedCacheList); shared != null; shared = shared.Next)
                    {
                        Debug.Assert(shared.nRef > 0);
                        if (string.Equals(zPathname, shared.Pager.sqlite3PagerFilename) && shared.Pager.sqlite3PagerVfs == pVfs)
                        {
                            for (var iDb = db.DBs - 1; iDb >= 0; iDb--)
                            {
                                var existingTree = db.AllocDBs[iDb].Tree;
                                if (existingTree != null && existingTree.Shared == shared)
                                {
                                    MutexEx.Leave(mutexShared);
                                    MutexEx.Leave(mutexOpen);
                                    p = null;
                                    return RC.CONSTRAINT;
                                }
                            }
                            p.Shared = shared;
                            shared.nRef++;
                            break;
                        }
                    }
                    MutexEx.Leave(mutexShared);
                }
#if DEBUG
                else
                    // In debug mode, we mark all persistent databases as sharable even when they are not.  This exercises the locking code and
                    // gives more opportunity for asserts(sqlite3_mutex_held()) statements to find locking problems.
                    p.Sharable = true;
#endif
            }
#endif
            if (shared == null)
            {
                // The following asserts make sure that structures used by the btree are the right size.  This is to guard against size changes that result
                // when compiling on a different architecture.
                Debug.Assert(sizeof(long) == 8 || sizeof(long) == 4);
                Debug.Assert(sizeof(ulong) == 8 || sizeof(ulong) == 4);
                Debug.Assert(sizeof(uint) == 4);
                Debug.Assert(sizeof(ushort) == 2);
                Debug.Assert(sizeof(Pgno) == 4);
                shared = new BtShared();
                rc = Pager.Open(pVfs, out shared.Pager, zFilename, EXTRA_SIZE, (Pager.PAGEROPEN)flags, vfsFlags, pageReinit, () => new MemPage());
                if (rc == RC.OK)
                    rc = shared.Pager.ReadFileHeader(zDbHeader.Length, zDbHeader);
                if (rc != RC.OK)
                    goto btree_open_out;
                shared.OpenFlags = flags;
                shared.DB = db;
                shared.Pager.SetBusyHandler(btreeInvokeBusyHandler, shared);
                p.Shared = shared;
                shared.Cursors = null;
                shared.Page1 = null;
                shared.ReadOnly = shared.Pager.IsReadonly;
#if SQLITE_SECURE_DELETE
pBt.secureDelete = true;
#endif
                shared.PageSize = (uint)((zDbHeader[16] << 8) | (zDbHeader[17] << 16));
                if (shared.PageSize < 512 || shared.PageSize > Pager.SQLITE_MAX_PAGE_SIZE || ((shared.PageSize - 1) & shared.PageSize) != 0)
                {
                    shared.PageSize = 0;
#if !SQLITE_OMIT_AUTOVACUUM
                    // If the magic name ":memory:" will create an in-memory database, then leave the autoVacuum mode at 0 (do not auto-vacuum), even if
                    // SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
                    // regular file-name. In this case the auto-vacuum applies as per normal.
                    if (zFilename != string.Empty && !isMemdb)
                    {
                        shared.AutoVacuum = (AUTOVACUUM.DEFAULT != AUTOVACUUM.NONE);
                        shared.IncrVacuum = (AUTOVACUUM.DEFAULT == AUTOVACUUM.INCR);
                    }
#endif
                    nReserve = 0;
                }
                else
                {
                    nReserve = zDbHeader[20];
                    shared.PageSizeFixed = true;
#if !SQLITE_OMIT_AUTOVACUUM
                    shared.AutoVacuum = ConvertEx.Get4(zDbHeader, 36 + 4 * 4) != 0;
                    shared.IncrVacuum = ConvertEx.Get4(zDbHeader, 36 + 7 * 4) != 0;
#endif
                }
                rc = shared.Pager.SetPageSize(ref shared.PageSize, nReserve);
                if (rc != RC.OK)
                    goto btree_open_out;
                shared.UsableSize = (ushort)(shared.PageSize - nReserve);
                Debug.Assert((shared.PageSize & 7) == 0);  // 8-byte alignment of pageSize
#if !SQLITE_OMIT_SHARED_CACHE && !SQLITE_OMIT_DISKIO
                // Add the new BtShared object to the linked list sharable BtShareds.
                if (p.Sharable)
                {
                    MutexEx mutexShared;
                    shared.nRef = 1;
                    mutexShared = MutexEx.Alloc(MUTEX.STATIC_MASTER);
                    if (MutexEx.SQLITE_THREADSAFE && MutexEx.WantsCoreMutex)
                        shared.Mutex = MutexEx.Alloc(MUTEX.FAST);
                    MutexEx.Enter(mutexShared);
                    shared.Next = SysEx.getGLOBAL<BtShared>(s_sqlite3SharedCacheList);
                    SysEx.setGLOBAL<BtShared>(s_sqlite3SharedCacheList, shared);
                    MutexEx.Leave(mutexShared);
                }
#endif
            }
#if !SQLITE_OMIT_SHARED_CACHE && !SQLITE_OMIT_DISKIO
            // If the new Btree uses a sharable pBtShared, then link the new Btree into the list of all sharable Btrees for the same connection.
            // The list is kept in ascending order by pBt address.
            Btree existingTree2;
            if (p.Sharable)
                for (var i = 0; i < db.DBs; i++)
                    if ((existingTree2 = db.AllocDBs[i].Tree) != null && existingTree2.Sharable)
                    {
                        while (existingTree2.Prev != null) { existingTree2 = existingTree2.Prev; }
                        if (p.Shared.Version < existingTree2.Shared.Version)
                        {
                            p.Next = existingTree2;
                            p.Prev = null;
                            existingTree2.Prev = p;
                        }
                        else
                        {
                            while (existingTree2.Next != null && existingTree2.Next.Shared.Version < p.Shared.Version)
                                existingTree2 = existingTree2.Next;
                            p.Next = existingTree2.Next;
                            p.Prev = existingTree2;
                            if (p.Next != null)
                                p.Next.Prev = p;
                            existingTree2.Next = p;
                        }
                        break;
                    }
#endif
            rTree = p;
        //
        btree_open_out:
            if (rc != RC.OK)
            {
                if (shared != null && shared.Pager != null)
                    shared.Pager.Close();
                shared = null;
                p = null;
                rTree = null;
            }
            else
            {
                // If the B-Tree was successfully opened, set the pager-cache size to the default value. Except, when opening on an existing shared pager-cache,
                // do not change the pager-cache size.
                if (p.GetSchema(0, null, null) == null)
                    p.Shared.Pager.SetCacheSize(SQLITE_DEFAULT_CACHE_SIZE);
            }
            if (mutexOpen != null)
            {
                Debug.Assert(MutexEx.Held(mutexOpen));
                MutexEx.Leave(mutexOpen);
            }
            return rc;
        }
Example #9
0
        private List <Member> Djikstra(List <Member> initMembers, int starterNum, int dinnerNum, int dessertNum, int numnerOfMembersPerDateAss1)
        {
            List <Node> OPEN  = new List <Node>();
            List <Node> CLOSE = new List <Node>();

            int groupsizeStarter = starterNum % numnerOfMembersPerDateAss1 == 0 ? 2 : (int)Math.Floor((double)((dinnerNum + dessertNum) / starterNum));
            int groupsizeDinner  = dinnerNum % numnerOfMembersPerDateAss1 == 0 ? 2 : (int)Math.Floor((double)((starterNum + dessertNum) / dinnerNum));
            int groupsizeDessert = dessertNum % numnerOfMembersPerDateAss1 == 0 ? 2 : (int)Math.Floor((double)((dinnerNum + starterNum) / dessertNum));

            int highestZ = 0;

            int  z           = 0;
            int  id          = 0;
            int  goal        = initMembers.Count * 2;
            int  backupcost  = 500;
            Node backupNode  = new Node(0, 0, z, initMembers, new List <Tuple <int, int> >(), 500);
            Node currentNode = new Node(0, 0, z, initMembers, new List <Tuple <int, int> >(), 500);

            OPEN.Add(currentNode);

            while (true)
            {
                if (OPEN.Count > 0 && currentNode.datesFound.Count != goal)
                {
                    currentNode = getNodeWithLowestCost(OPEN);
                    OPEN.Remove(currentNode);
                }
                else if (currentNode.datesFound.Count != goal)
                {
                    currentNode = getNodeWithLowestCost(CLOSE);
                    Debug.WriteLine("Ops");
                    currentNode.cost = 10000;
                }

                if (OPEN.Count > 8000)
                {
                    OPEN = new List <Node>();
                }
                List <Member> members = currentNode.members;

                if (currentNode.datesFound.Count == goal)
                {
                    Debug.WriteLine("Z: " + currentNode.z);
                    Debug.WriteLine("Allready met: " + currentNode.allreadyMet);
                    Debug.WriteLine("Association error: " + currentNode.associationCost);
                    Debug.WriteLine("Cost: " + currentNode.cost);
                    currentNode.members.ForEach(p => p.allReadyMet.ForEach(l => Debug.WriteLine(p.ID + ": " + l)));
                    return(currentNode.members);
                }

                OPEN.Remove(currentNode);

                int numerOfmembers = members.Count;
                for (int i = 0; i < numerOfmembers; i++)
                {
                    List <Member> membersClone = members.Clone();
                    if (membersClone[i].allDatesFound())
                    {
                        continue;
                    }

                    List <Tuple <int, int> > dates = new List <Tuple <int, int> >(currentNode.datesFound);
                    int cost            = 0;
                    int allreadyMet     = 0;
                    int associationcost = 0;
                    for (int j = 0; j < numerOfmembers; j++)
                    {
                        bool allreadyRegistered = currentNode.datesFound.FirstOrDefault(n => n.Item1 == i && n.Item2 == j) != null;
                        if (allreadyRegistered || membersClone[i].allDatesFound())
                        {
                            continue;
                        }

                        int groupsize = 2;
                        if (membersClone[j].duty == "starter")
                        {
                            groupsize = groupsizeStarter;
                        }
                        else if (membersClone[j].duty == "dinner")
                        {
                            groupsize = groupsizeDinner;
                        }
                        else if (membersClone[j].duty == "dessert")
                        {
                            groupsize = groupsizeDessert;
                        }

                        int release = 0;
                        int whenToIncreaseGroupsize = 2 * numerOfmembers - groupsizeDinner * dinnerNum - groupsizeDessert * dessertNum - groupsizeStarter * starterNum;
                        if (whenToIncreaseGroupsize == 0)
                        {
                            groupsize = (goal - dates.Count) <= (numerOfmembers % (groupsize + 1)) * 2 ? groupsize + 1 : groupsize;
                        }
                        else
                        {
                            groupsize = (goal - dates.Count) <= whenToIncreaseGroupsize + release ? groupsize + 1 : groupsize;
                        }

                        bool possibleMatch = findMatch(membersClone[i], membersClone[j], groupsize);
                        if (!possibleMatch)
                        {
                            continue;
                        }

                        Tuple <int, int> date = new Tuple <int, int>(i, j);
                        dates.Add(date);

                        int[] errorAndCost = calculateCost(membersClone[i], membersClone[j], goal, (goal - dates.Count), associationcost);
                        cost            = cost + errorAndCost[0];
                        allreadyMet     = allreadyMet + errorAndCost[3];
                        associationcost = associationcost + errorAndCost[2];
                        registerDate(membersClone[i], membersClone[j], membersClone);
                    }

                    int  datecount        = dates.Count;
                    Node currentNeighbour = new Node(i, datecount, id, membersClone, dates, cost + associationcost);
                    currentNeighbour.allreadyMet     = currentNode.allreadyMet + allreadyMet;
                    currentNeighbour.associationCost = currentNode.associationCost + associationcost;

                    if (cost < currentNode.cost)
                    {
                        OPEN.Insert(0, currentNeighbour);
                        //Debug.WriteLine("Cost: " + cost + ", Z: " + datecount);
                        id++;
                    }
                    else if (datecount >= highestZ && cost < backupcost)
                    {
                        if (datecount > highestZ)
                        {
                            //CLOSE = new List<Node>();
                            highestZ = datecount;
                        }
                        CLOSE.Add(currentNeighbour);
                        backupcost = cost;
                    }
                }
            }
        }
Example #10
0
        // was:sqlite3BtreeOpen
        public static RC Open(VirtualFileSystem pVfs, string zFilename, sqlite3 db, ref Btree rTree, OPEN flags, VFSOPEN vfsFlags)
        {
            Btree p;                      // Handle to return   
            var rc = RC.OK;
            byte nReserve;                   // Byte of unused space on each page
            var zDbHeader = new byte[100]; // Database header content
            // True if opening an ephemeral, temporary database */
            bool isTempDb = string.IsNullOrEmpty(zFilename);
            // Set the variable isMemdb to true for an in-memory database, or  false for a file-based database.
#if SQLITE_OMIT_MEMORYDB
            var isMemdb = false;
#else
            var isMemdb = (zFilename == ":memory:" || isTempDb && db.sqlite3TempInMemory());
#endif
            Debug.Assert(db != null);
            Debug.Assert(pVfs != null);
            Debug.Assert(MutexEx.Held(db.Mutex));
            Debug.Assert(((uint)flags & 0xff) == (uint)flags);   // flags fit in 8 bits
            // Only a BTREE_SINGLE database can be BTREE_UNORDERED
            Debug.Assert((flags & OPEN.UNORDERED) == 0 || (flags & OPEN.SINGLE) != 0);
            // A BTREE_SINGLE database is always a temporary and/or ephemeral
            Debug.Assert((flags & OPEN.SINGLE) == 0 || isTempDb);
            if ((db.flags & sqlite3b.SQLITE.NoReadlock) != 0)
                flags |= OPEN.NO_READLOCK;
            if (isMemdb)
                flags |= OPEN.MEMORY;
            if ((vfsFlags & VFSOPEN.MAIN_DB) != 0 && (isMemdb || isTempDb))
                vfsFlags = (vfsFlags & ~VFSOPEN.MAIN_DB) | VFSOPEN.TEMP_DB;
            p = new Btree();
            p.InTransaction = TRANS.NONE;
            p.DB = db;
#if !SQLITE_OMIT_SHARED_CACHE
            p.Locks.Tree = p;
            p.Locks.TableID = 1;
#endif
            BtShared shared = null;          // Shared part of btree structure
            sqlite3_mutex mutexOpen = null;  // Prevents a race condition.
#if !SQLITE_OMIT_SHARED_CACHE && !SQLITE_OMIT_DISKIO
            // If this Btree is a candidate for shared cache, try to find an existing BtShared object that we can share with
            if (!isMemdb && !isTempDb)
            {
                if ((vfsFlags & VFSOPEN.SHAREDCACHE) != 0)
                {
                    p.Sharable = true;
                    string zPathname;
                    rc = pVfs.xFullPathname(zFilename, out zPathname);
                    mutexOpen = MutexEx.sqlite3MutexAlloc(MUTEX.STATIC_OPEN);
                    MutexEx.sqlite3_mutex_enter(mutexOpen);
                    var mutexShared = MutexEx.sqlite3MutexAlloc(MUTEX.STATIC_MASTER);
                    MutexEx.sqlite3_mutex_enter(mutexShared);
                    for (shared = SysEx.getGLOBAL<BtShared>(s_sqlite3SharedCacheList); shared != null; shared = shared.Next)
                    {
                        Debug.Assert(shared.nRef > 0);
                        if (string.Equals(zPathname, shared.Pager.sqlite3PagerFilename) && shared.Pager.sqlite3PagerVfs == pVfs)
                        {
                            for (var iDb = db.DBs - 1; iDb >= 0; iDb--)
                            {
                                var existingTree = db.AllocDBs[iDb].Tree;
                                if (existingTree != null && existingTree.Shared == shared)
                                {
                                    MutexEx.sqlite3_mutex_leave(mutexShared);
                                    MutexEx.sqlite3_mutex_leave(mutexOpen);
                                    p = null;
                                    return RC.CONSTRAINT;
                                }
                            }
                            p.Shared = shared;
                            shared.nRef++;
                            break;
                        }
                    }
                    MutexEx.sqlite3_mutex_leave(mutexShared);
                }
#if DEBUG
                else
                    // In debug mode, we mark all persistent databases as sharable even when they are not.  This exercises the locking code and
                    // gives more opportunity for asserts(sqlite3_mutex_held()) statements to find locking problems.
                    p.Sharable = true;
#endif
            }
#endif
            if (shared == null)
            {
                // The following asserts make sure that structures used by the btree are the right size.  This is to guard against size changes that result
                // when compiling on a different architecture.
                Debug.Assert(sizeof(long) == 8 || sizeof(long) == 4);
                Debug.Assert(sizeof(ulong) == 8 || sizeof(ulong) == 4);
                Debug.Assert(sizeof(uint) == 4);
                Debug.Assert(sizeof(ushort) == 2);
                Debug.Assert(sizeof(Pgno) == 4);
                shared = new BtShared();
                rc = Pager.Open(pVfs, out shared.Pager, zFilename, EXTRA_SIZE, (Pager.PAGEROPEN)flags, vfsFlags, pageReinit, () => new MemPage());
                if (rc == RC.OK)
                    rc = shared.Pager.ReadFileHeader(zDbHeader.Length, zDbHeader);
                if (rc != RC.OK)
                    goto btree_open_out;
                shared.OpenFlags = flags;
                shared.DB = db;
                shared.Pager.SetBusyHandler(btreeInvokeBusyHandler, shared);
                p.Shared = shared;
                shared.Cursors = null;
                shared.Page1 = null;
                shared.ReadOnly = shared.Pager.IsReadonly;
#if SQLITE_SECURE_DELETE
pBt.secureDelete = true;
#endif
                shared.PageSize = (uint)((zDbHeader[16] << 8) | (zDbHeader[17] << 16));
                if (shared.PageSize < 512 || shared.PageSize > Pager.SQLITE_MAX_PAGE_SIZE || ((shared.PageSize - 1) & shared.PageSize) != 0)
                {
                    shared.PageSize = 0;
#if !SQLITE_OMIT_AUTOVACUUM
                    // If the magic name ":memory:" will create an in-memory database, then leave the autoVacuum mode at 0 (do not auto-vacuum), even if
                    // SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
                    // regular file-name. In this case the auto-vacuum applies as per normal.
                    if (zFilename != string.Empty && !isMemdb)
                    {
                        shared.AutoVacuum = (AUTOVACUUM.DEFAULT != AUTOVACUUM.NONE);
                        shared.IncrVacuum = (AUTOVACUUM.DEFAULT == AUTOVACUUM.INCR);
                    }
#endif
                    nReserve = 0;
                }
                else
                {
                    nReserve = zDbHeader[20];
                    shared.PageSizeFixed = true;
#if !SQLITE_OMIT_AUTOVACUUM
                    shared.AutoVacuum = ConvertEx.Get4(zDbHeader, 36 + 4 * 4) != 0;
                    shared.IncrVacuum = ConvertEx.Get4(zDbHeader, 36 + 7 * 4) != 0;
#endif
                }
                rc = shared.Pager.SetPageSize(ref shared.PageSize, nReserve);
                if (rc != RC.OK)
                    goto btree_open_out;
                shared.UsableSize = (ushort)(shared.PageSize - nReserve);
                Debug.Assert((shared.PageSize & 7) == 0);  // 8-byte alignment of pageSize
#if !SQLITE_OMIT_SHARED_CACHE && !SQLITE_OMIT_DISKIO
                // Add the new BtShared object to the linked list sharable BtShareds.
                if (p.Sharable)
                {
                    sqlite3_mutex mutexShared;
                    shared.nRef = 1;
                    mutexShared = MutexEx.sqlite3MutexAlloc(MUTEX.STATIC_MASTER);
                    if (MutexEx.SQLITE_THREADSAFE && MutexEx.WantsCoreMutex)
                        shared.Mutex = MutexEx.sqlite3MutexAlloc(MUTEX.FAST);
                    MutexEx.sqlite3_mutex_enter(mutexShared);
                    shared.Next = SysEx.getGLOBAL<BtShared>(s_sqlite3SharedCacheList);
                    SysEx.setGLOBAL<BtShared>(s_sqlite3SharedCacheList, shared);
                    MutexEx.sqlite3_mutex_leave(mutexShared);
                }
#endif
            }
#if !SQLITE_OMIT_SHARED_CACHE && !SQLITE_OMIT_DISKIO
            // If the new Btree uses a sharable pBtShared, then link the new Btree into the list of all sharable Btrees for the same connection.
            // The list is kept in ascending order by pBt address.
            Btree existingTree2;
            if (p.Sharable)
                for (var i = 0; i < db.DBs; i++)
                    if ((existingTree2 = db.AllocDBs[i].Tree) != null && existingTree2.Sharable)
                    {
                        while (existingTree2.Prev != null) { existingTree2 = existingTree2.Prev; }
                        if (p.Shared.Version < existingTree2.Shared.Version)
                        {
                            p.Next = existingTree2;
                            p.Prev = null;
                            existingTree2.Prev = p;
                        }
                        else
                        {
                            while (existingTree2.Next != null && existingTree2.Next.Shared.Version < p.Shared.Version)
                                existingTree2 = existingTree2.Next;
                            p.Next = existingTree2.Next;
                            p.Prev = existingTree2;
                            if (p.Next != null)
                                p.Next.Prev = p;
                            existingTree2.Next = p;
                        }
                        break;
                    }
#endif
            rTree = p;
        //
        btree_open_out:
            if (rc != RC.OK)
            {
                if (shared != null && shared.Pager != null)
                    shared.Pager.Close();
                shared = null;
                p = null;
                rTree = null;
            }
            else
            {
                // If the B-Tree was successfully opened, set the pager-cache size to the default value. Except, when opening on an existing shared pager-cache,
                // do not change the pager-cache size.
                if (p.GetSchema(0, null, null) == null)
                    p.Shared.Pager.SetCacheSize(SQLITE_DEFAULT_CACHE_SIZE);
            }
            if (mutexOpen != null)
            {
                Debug.Assert(MutexEx.Held(mutexOpen));
                MutexEx.sqlite3_mutex_leave(mutexOpen);
            }
            return rc;
        }
Example #11
0
 public abstract RC Open(string path, VFile file, OPEN flags, out OPEN outFlags);
Example #12
0
 public object moBai(params object[] thamso)
 {
     return(OPEN?.Invoke(thamso));
 }
Example #13
0
 public abstract RC Open(string path, VFile file, OPEN flags, out OPEN outFlags);
Example #14
0
        public RC xOpen(string zName, VirtualFile pFile, OPEN flags, out OPEN pOutFlags)
        {
            pOutFlags = 0;
            // If argument zPath is a NULL pointer, this function is required to open a temporary file. Use this buffer to store the file name in.
            //var zTmpname = new StringBuilder(MAX_PATH + 1);        // Buffer used to create temp filename
            var rc            = RC.OK;
            var eType         = (OPEN)((int)flags & 0xFFFFFF00); // Type of file to open
            var isExclusive   = (flags & OPEN.EXCLUSIVE) != 0;
            var isDelete      = (flags & OPEN.DELETEONCLOSE) != 0;
            var isCreate      = (flags & OPEN.CREATE) != 0;
            var isReadonly    = (flags & OPEN.READONLY) != 0;
            var isReadWrite   = (flags & OPEN.READWRITE) != 0;
            var isOpenJournal = (isCreate && (eType == OPEN.MASTER_JOURNAL || eType == OPEN.MAIN_JOURNAL || eType == OPEN.WAL));

            // Check the following statements are true:
            //   (a) Exactly one of the READWRITE and READONLY flags must be set, and
            //   (b) if CREATE is set, then READWRITE must also be set, and
            //   (c) if EXCLUSIVE is set, then CREATE must also be set.
            //   (d) if DELETEONCLOSE is set, then CREATE must also be set.
            Debug.Assert((!isReadonly || !isReadWrite) && (isReadWrite || isReadonly));
            Debug.Assert(!isCreate || isReadWrite);
            Debug.Assert(!isExclusive || isCreate);
            Debug.Assert(!isDelete || isCreate);
            // The main DB, main journal, WAL file and master journal are never automatically deleted. Nor are they ever temporary files.
            //Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.MAIN_DB);
            Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.MAIN_JOURNAL);
            Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.MASTER_JOURNAL);
            Debug.Assert((!isDelete && !string.IsNullOrEmpty(zName)) || eType != OPEN.WAL);
            // Assert that the upper layer has set one of the "file-type" flags.
            Debug.Assert(eType == OPEN.MAIN_DB || eType == OPEN.TEMP_DB || eType == OPEN.MAIN_JOURNAL || eType == OPEN.TEMP_JOURNAL ||
                         eType == OPEN.SUBJOURNAL || eType == OPEN.MASTER_JOURNAL || eType == OPEN.TRANSIENT_DB || eType == OPEN.WAL);
            pFile.S = null;
            // If the second argument to this function is NULL, generate a temporary file name to use
            if (string.IsNullOrEmpty(zName))
            {
                Debug.Assert(isDelete && !isOpenJournal);
                zName = Path.GetRandomFileName();
            }
            // Convert the filename to the system encoding.
            if (zName.StartsWith("/") && !zName.StartsWith("//"))
            {
                zName = zName.Substring(1);
            }
            var dwDesiredAccess = (isReadWrite ? FileAccess.Read | FileAccess.Write : FileAccess.Read);
            // SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is created. SQLite doesn't use it to indicate "exclusive access" as it is usually understood.
            FileMode dwCreationDisposition;

            if (isExclusive)
            {
                // Creates a new file, only if it does not already exist. */ If the file exists, it fails.
                dwCreationDisposition = FileMode.CreateNew;
            }
            else if (isCreate)
            {
                // Open existing file, or create if it doesn't exist
                dwCreationDisposition = FileMode.OpenOrCreate;
            }
            else
            {
                // Opens a file, only if it exists.
                dwCreationDisposition = FileMode.Open;
            }
            var dwShareMode = FileShare.Read | FileShare.Write;

#if !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
            FileOptions dwFlagsAndAttributes;
#endif
            if (isDelete)
#if !(SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
            { dwFlagsAndAttributes = FileOptions.DeleteOnClose; }
#endif
            { else