diff --git a/src/LINQ/LINQ/test/resourceTest.vb b/src/LINQ/LINQ/test/resourceTest.vb index 0aa4370..3b40396 100644 --- a/src/LINQ/LINQ/test/resourceTest.vb +++ b/src/LINQ/LINQ/test/resourceTest.vb @@ -1,6 +1,35 @@ -Module resourceTest +Imports Microsoft.VisualBasic.ComponentModel.Ranges +Imports Microsoft.VisualBasic.DataStorage.HDSPack.FileSystem +Imports RQL + +Module resourceTest + + Const filepath As String = "./resource.db" Sub Main() + Call write() + Call read() + Call Pause() + End Sub + + Sub write() + Dim res As New Resource(StreamPack.CreateNewStream(filepath)) + + Call res.Add("Although the molecules of water", "the physical and chemical properties of the compound are extraordinarily complicated, and they are not typical of most substances found on Earth.") + Call res.Add("Water occurs as a liquid", "Water occurs as a liquid on the surface of Earth under normal conditions, which makes it invaluable for transportation, for recreation, and as a habitat for a myriad of plants and animals. The fact that water is readily changed to a vapour (gas) allows it to be transported through the atmosphere from the oceans to inland areas where it condenses and, as rain, nourishes plant and animal life. (See hydrosphere: The hydrologic cycle for a description of the cycle by which water is transferred over Earth.)") + + Call res.Dispose() + End Sub + + Sub read() + Dim res As New Resource(New StreamPack(filepath.Open(IO.FileMode.Open, doClear:=False, [readOnly]:=True))) + Dim maps As New List(Of NumericTagged(Of String)) + + maps.AddRange(res.Get("the molecules")) + maps.AddRange(res.Get("occurs as a liquid")) + For Each key As NumericTagged(Of String) In maps + Call Console.WriteLine(res.ReadString(key.value)) + Next End Sub End Module diff --git a/src/LINQ/RQL/Resource.vb b/src/LINQ/RQL/Resource.vb index 13fa710..db39b2b 100644 --- a/src/LINQ/RQL/Resource.vb +++ b/src/LINQ/RQL/Resource.vb @@ -1,4 +1,5 @@ Imports System.IO +Imports System.Text Imports Microsoft.VisualBasic.ComponentModel.Ranges Imports Microsoft.VisualBasic.Data.GraphTheory Imports Microsoft.VisualBasic.DataStorage.HDSPack.FileSystem @@ -18,10 +19,26 @@ Public Class Resource : Implements IDisposable index = New IndexReader(res.OpenFile("/index.dat", FileMode.OpenOrCreate, FileAccess.Read)).Read End Sub + Public Function Add(key As String, str As String) + Return Add(key, Encoding.UTF8.GetBytes(str)) + End Function + + Public Function ReadString(map As String) As String + Dim path As String = URL(map) + Dim file As Stream = buf.OpenFile(path, FileMode.Open, FileAccess.Read) + Dim bytes As Byte() = New Byte(file.Length - 1) {} + Call file.Read(bytes, Scan0, bytes.Length) + Return Encoding.UTF8.GetString(bytes) + End Function + + Private Shared Function URL(map As String) As String + Return $"/pool/{map.Substring(4, 2)}/{map.Substring(16, 6)}/{map}" + End Function + Public Function Add(key As String, data As Byte()) As Boolean Dim tokens As String() = Strings.LCase(key).Split Dim map As String = key.MD5 - Dim path As String = $"/pool/{map.Substring(4, 2)}/{map.Substring(16, 6)}/{map}" + Dim path As String = URL(map) For Each si As String In tokens Dim v = index.Add(key)