Skip to content

Commit 042be7f

Browse files
committed
Support "use/drop database"
1 parent 822c8a2 commit 042be7f

24 files changed

+149
-59
lines changed

MiniSQL.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSQL.RecordManager", "sr
1919
EndProject
2020
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSQL.Library", "src\MiniSQL.Library\MiniSQL.Library.csproj", "{3EA08587-04EB-40CA-B025-5D02A9030178}"
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSQL.Startup", "src\MiniSQL.Startup\MiniSQL.Startup.csproj", "{B6695733-2C87-4D7B-B8A8-0BC444A23712}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -116,6 +118,18 @@ Global
116118
{3EA08587-04EB-40CA-B025-5D02A9030178}.Release|x64.Build.0 = Release|Any CPU
117119
{3EA08587-04EB-40CA-B025-5D02A9030178}.Release|x86.ActiveCfg = Release|Any CPU
118120
{3EA08587-04EB-40CA-B025-5D02A9030178}.Release|x86.Build.0 = Release|Any CPU
121+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
122+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Debug|Any CPU.Build.0 = Debug|Any CPU
123+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Debug|x64.ActiveCfg = Debug|Any CPU
124+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Debug|x64.Build.0 = Debug|Any CPU
125+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Debug|x86.ActiveCfg = Debug|Any CPU
126+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Debug|x86.Build.0 = Debug|Any CPU
127+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Release|Any CPU.ActiveCfg = Release|Any CPU
128+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Release|Any CPU.Build.0 = Release|Any CPU
129+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Release|x64.ActiveCfg = Release|Any CPU
130+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Release|x64.Build.0 = Release|Any CPU
131+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Release|x86.ActiveCfg = Release|Any CPU
132+
{B6695733-2C87-4D7B-B8A8-0BC444A23712}.Release|x86.Build.0 = Release|Any CPU
119133
EndGlobalSection
120134
GlobalSection(NestedProjects) = preSolution
121135
{C40E4A7D-AFD1-45A0-8AA5-EE52EE583FF9} = {73A93E49-D1AE-4052-909C-AD8EED74D8D7}
@@ -125,5 +139,6 @@ Global
125139
{05C292B3-4C6B-4470-9EDB-725DEF786183} = {73A93E49-D1AE-4052-909C-AD8EED74D8D7}
126140
{511A214E-372C-4D27-8EE7-50CFAD5F8505} = {73A93E49-D1AE-4052-909C-AD8EED74D8D7}
127141
{3EA08587-04EB-40CA-B025-5D02A9030178} = {73A93E49-D1AE-4052-909C-AD8EED74D8D7}
142+
{B6695733-2C87-4D7B-B8A8-0BC444A23712} = {73A93E49-D1AE-4052-909C-AD8EED74D8D7}
128143
EndGlobalSection
129144
EndGlobal

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ execfile "PATH_TO_SQL_FILE" ;
5252

5353
-- ensure writing back all dirty pages
5454
exit
55+
56+
-- write all pages back to disk
57+
flush
58+
59+
use database DATABASE_NAME
60+
drop database DATABASE_NAME
5561
```
5662

5763
## Spec

src/MiniSQL.Api/Api.cs renamed to src/MiniSQL.Api/Controllers/ApiController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
using System;
55
using System.Linq;
66

7-
namespace MiniSQL.Api
7+
namespace MiniSQL.Api.Controllers
88
{
9-
public class Api : IApi
9+
public class ApiController : IApi
1010
{
1111

1212
private readonly IInterpreter _interpreter;
1313
private readonly ICatalogManager _catalogManager;
1414
private readonly IRecordManager _recordManager;
1515

16-
public Api(IInterpreter interpreter, ICatalogManager catalogManager, IRecordManager recordManager)
16+
public ApiController(IInterpreter interpreter, ICatalogManager catalogManager, IRecordManager recordManager)
1717
{
1818
_catalogManager = catalogManager;
1919
_interpreter = interpreter;

src/MiniSQL.Api/Program.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,12 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.IO;
5-
using System.Text;
6-
using MiniSQL.Api.Controllers;
7-
using MiniSQL.BufferManager.Controllers;
8-
using MiniSQL.BufferManager.Models;
9-
using MiniSQL.CatalogManager;
10-
using MiniSQL.IndexManager.Controllers;
11-
using MiniSQL.IndexManager.Interfaces;
12-
using MiniSQL.Interpreter;
13-
using MiniSQL.Library.Interfaces;
14-
using MiniSQL.Library.Models;
15-
using MiniSQL.RecordManager;
162

173
namespace MiniSQL.Api
184
{
195
class Program
206
{
217
static void Main(string[] args)
228
{
23-
UseDatabase("test");
24-
}
25-
26-
public static void UseDatabase(string databaseName)
27-
{
28-
// init
29-
string dbPath = $"./{databaseName}.minidb";
30-
Pager pager = new Pager(dbPath, 1024 * 8, 400);
31-
FreeList freeList = new FreeList(pager);
32-
IIndexManager bTreeController = new BTreeController(pager, freeList, 40);
33-
IInterpreter interpreter = new Parsing();
34-
ICatalogManager catalogManager = new Catalog(databaseName);
35-
IRecordManager recordManager = new RecordContext(pager, bTreeController);
36-
IApi api = new Api(interpreter, catalogManager, recordManager);
37-
38-
View view = new View(
39-
bTreeController,
40-
interpreter,
41-
catalogManager,
42-
recordManager,
43-
api,
44-
pager
45-
);
46-
view.Interactive();
47-
9+
Console.WriteLine("Hello world");
4810
}
4911
}
5012
}

src/MiniSQL.BufferManager/Controllers/Pager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public void ReadPage(MemoryPage page)
150150
// remove LRU if out of limit
151151
if (this.Pages.Count >= this.InMemoryPageCountLimit)
152152
RemoveLRUPage();
153+
if (this.Pages.Count >= this.InMemoryPageCountLimit)
154+
throw new Exception("Race condition!");
153155

154156
page.Core = new MemoryPage.MemoryPageCore(this, page.PageNumber);
155157
page.Core.data = new byte[this.PageSize];
@@ -187,6 +189,8 @@ public void Close()
187189
}
188190
}
189191

192+
// write back dirty pages without closing connection to the file system
193+
// the changes will not immediately affect the file in secondary memory (the disk)
190194
public void CleanAllPagesFromMainMemory()
191195
{
192196
lock (this)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
using MiniSQL.Api.Controllers;
3+
using MiniSQL.BufferManager.Controllers;
4+
using MiniSQL.CatalogManager;
5+
using MiniSQL.IndexManager.Controllers;
6+
using MiniSQL.IndexManager.Interfaces;
7+
using MiniSQL.Interpreter;
8+
using MiniSQL.Library.Interfaces;
9+
using MiniSQL.RecordManager;
10+
11+
namespace MiniSQL.Startup.Controllers
12+
{
13+
public class DatabaseController
14+
{
15+
public (IApi, Pager) UseDatabase(string databaseName)
16+
{
17+
// init
18+
string dbPath = $"./{databaseName}.minidb";
19+
Pager pager = new Pager(dbPath, 1024 * 8, 400);
20+
FreeList freeList = new FreeList(pager);
21+
IIndexManager bTreeController = new BTreeController(pager, freeList, 40);
22+
IInterpreter interpreter = new Parsing();
23+
ICatalogManager catalogManager = new Catalog(databaseName);
24+
IRecordManager recordManager = new RecordContext(pager, bTreeController);
25+
IApi api = new ApiController(interpreter, catalogManager, recordManager);
26+
27+
return (api, pager);
28+
}
29+
}
30+
}

src/MiniSQL.Api/Controllers/View.cs renamed to src/MiniSQL.Startup/Controllers/View.cs

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Text;
6+
using System.Text.RegularExpressions;
57
using MiniSQL.BufferManager.Controllers;
68
using MiniSQL.IndexManager.Interfaces;
79
using MiniSQL.Library.Interfaces;
810
using MiniSQL.Library.Models;
911

10-
namespace MiniSQL.Api.Controllers
12+
namespace MiniSQL.Startup.Controllers
1113
{
1214
public class View
1315
{
14-
private readonly IIndexManager _bTreeController;
15-
private readonly IInterpreter _interpreter;
16-
private readonly ICatalogManager _catalogManager;
17-
private readonly IRecordManager _recordManager;
18-
private readonly IApi _api;
19-
private readonly Pager _pager;
16+
private bool isUsingDatabase = false;
17+
private string nameOfDatabaseInUse;
18+
private IApi _api;
19+
private Pager _pager;
20+
private readonly DatabaseController _databaseController;
2021
private bool isCtrlC = false;
2122

22-
public View(IIndexManager bTreeController, IInterpreter interpreter, ICatalogManager catalogManager, IRecordManager recordManager, IApi api, Pager pager)
23+
public View(DatabaseController databaseController)
2324
{
2425
// ensure writing back when ctrl-c
2526
Console.CancelKeyPress += OnExit;
@@ -28,14 +29,34 @@ public View(IIndexManager bTreeController, IInterpreter interpreter, ICatalogMan
2829
Console.WriteLine();
2930
Console.WriteLine("Hello MiniSQL!");
3031
Console.WriteLine();
31-
32+
33+
_databaseController = databaseController;
34+
}
35+
36+
// TODO: wrap this to a class
37+
private void ChangeContext(string newDatabaseName)
38+
{
39+
if (isUsingDatabase)
40+
{
41+
_pager.Close();
42+
}
3243
// init
33-
_pager = pager;
34-
_bTreeController = bTreeController;
35-
_interpreter = interpreter;
36-
_catalogManager = catalogManager;
37-
_recordManager = recordManager;
38-
_api = api;
44+
this.isUsingDatabase = true;
45+
this.nameOfDatabaseInUse = newDatabaseName;
46+
(_api, _pager) = _databaseController.UseDatabase(newDatabaseName);
47+
}
48+
49+
// TODO: wrap this to a class
50+
private void DropDatabase(string databaseName)
51+
{
52+
if (nameOfDatabaseInUse == databaseName)
53+
{
54+
_pager.Close();
55+
isUsingDatabase = false;
56+
}
57+
File.Delete($"{databaseName}.minidb");
58+
File.Delete($"{databaseName}.indices.txt");
59+
File.Delete($"{databaseName}.tables.txt");
3960
}
4061

4162
// interactive(blocking) view of the whole solution
@@ -68,6 +89,26 @@ public void Interactive()
6889
isExit = true;
6990
continue;
7091
}
92+
// use database
93+
if (Regex.IsMatch(line, @"(?i)\s*use\s*database\s*.*(?-i)"))
94+
{
95+
string databaseName = line.Split(new string[] { " ", "\n" }, StringSplitOptions.RemoveEmptyEntries)[2].TrimEnd(';');
96+
ChangeContext(databaseName);
97+
continue;
98+
}
99+
// drop database
100+
if (Regex.IsMatch(line, @"(?i)\s*drop\s*database\s*.*(?-i)"))
101+
{
102+
string databaseName = line.Split(new string[] { " ", "\n" }, StringSplitOptions.RemoveEmptyEntries)[2].TrimEnd(';');
103+
DropDatabase(databaseName);
104+
continue;
105+
}
106+
if (!this.isUsingDatabase)
107+
{
108+
// WORKAROUND
109+
Console.WriteLine("[Error] No database in use");
110+
continue;
111+
}
71112
// flush all the dirty pages back to secondary memory and clean the main memory out of any page
72113
if (line == "flush")
73114
{
@@ -137,7 +178,8 @@ public void Interactive()
137178
input.Clear();
138179
}
139180
// save database file
140-
_pager.Close();
181+
if (this.isUsingDatabase)
182+
_pager.Close();
141183
}
142184

143185
// BUG: NOT WORKING
@@ -247,4 +289,4 @@ private static void Print(string toPrint, ConsoleColor color)
247289
Console.ForegroundColor = defaultColor;
248290
}
249291
}
250-
}
292+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\MiniSQL.Library\MiniSQL.Library.csproj" />
5+
<ProjectReference Include="..\MiniSQL.Api\MiniSQL.Api.csproj" />
6+
<ProjectReference Include="..\MiniSQL.Interpreter\MiniSQL.Interpreter.csproj" />
7+
<ProjectReference Include="..\MiniSQL.CatalogManager\MiniSQL.CatalogManager.csproj" />
8+
<ProjectReference Include="..\MiniSQL.IndexManager\MiniSQL.IndexManager.csproj" />
9+
<ProjectReference Include="..\MiniSQL.RecordManager\MiniSQL.RecordManager.csproj" />
10+
</ItemGroup>
11+
12+
<PropertyGroup>
13+
<OutputType>Exe</OutputType>
14+
<TargetFramework>netcoreapp3.1</TargetFramework>
15+
</PropertyGroup>
16+
17+
</Project>

src/MiniSQL.Startup/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using MiniSQL.Startup.Controllers;
2+
3+
namespace MiniSQL.Startup
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
DatabaseController controller = new DatabaseController();
10+
View view = new View(controller);
11+
view.Interactive();
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)