2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2009, 2012 Oracle and/or its affiliates. All rights reserved.
9 using System.Collections.Generic;
10 using System.Collections;
11 using System.Diagnostics;
13 using System.Runtime.Serialization.Formatters.Binary;
16 namespace excs_btrec {
18 static void Main(string[] args) {
24 * excs_btrec is meant to be run from build_windows\AnyCPU, in either
25 * the Debug or Release directory. The required core libraries,
26 * however, are in either build_windows\Win32 or build_windows\x64,
27 * depending upon the platform. That location needs to be added to
28 * the PATH environment variable for the P/Invoke calls to work.
31 String pwd = Environment.CurrentDirectory;
32 pwd = Path.Combine(pwd, "..");
33 pwd = Path.Combine(pwd, "..");
35 pwd = Path.Combine(pwd, "Win32");
37 pwd = Path.Combine(pwd, "x64");
39 pwd = Path.Combine(pwd, "Debug");
41 pwd = Path.Combine(pwd, "Release");
43 pwd += ";" + Environment.GetEnvironmentVariable("PATH");
44 Environment.SetEnvironmentVariable("PATH", pwd);
45 } catch (Exception e) {
47 "Unable to set the PATH environment variable.");
48 Console.WriteLine(e.Message);
61 excs_btrec(dbFileName, dbName, wordFile);
64 public static int excs_btrec(string dbFileName,
65 string dbName, string wordFile) {
67 BTreeDatabase btreeDB;
68 BTreeDatabaseConfig btreeDBconfig;
69 DatabaseEntry key, data;
70 KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
71 StreamReader wordList;
77 string progName = "excs_btrec";
79 /* Check if word list exists. */
80 if (!File.Exists(wordFile)) {
81 Console.WriteLine("Cannot find {0}/{1}.",
82 Environment.CurrentDirectory, wordFile);
86 /* Optionally remove the previous database. */
87 if (File.Exists(dbFileName)) {
90 ("Database already exists, delete or not (y/n)?");
91 buff = Console.ReadLine();
92 if (buff == "y" || buff == "n")
97 File.Delete(dbFileName);
98 Console.WriteLine("The existing {0} is deleted",
103 /* Create and initialize database object, open the database. */
104 btreeDBconfig = new BTreeDatabaseConfig();
105 btreeDBconfig.Creation = CreatePolicy.IF_NEEDED;
106 btreeDBconfig.ErrorPrefix = progName;
107 btreeDBconfig.UseRecordNumbers = true;
109 btreeDB = BTreeDatabase.Open(dbFileName, dbName,
112 Console.WriteLine("Database can not be opened");
117 * Insert records into the database, where the key is the word
118 * preceded by its record number, and the data is the same,
119 * but in reverse order.
121 key = new DatabaseEntry();
122 data = new DatabaseEntry();
123 wordList = new StreamReader(wordFile);
124 for (cnt = 1; cnt <= 1000; cnt++) {
125 /* Read one word from word list. */
127 buff = wordList.ReadLine();
129 Console.WriteLine("Error when reading word list");
136 * Prefix the word with record number and store
140 buff = cnt.ToString("0000") + buff;
143 "Wrong format for record number.");
148 dbtFromString(key, buff);
150 /* Reverse buff to rbuff and store them in data. */
151 rbuff = new char[buff.Length];
152 for (i = 0, j = buff.Length - 1; i < buff.Length; i++, j--)
154 dbtFromString(data, new string(rbuff));
156 /* Put the key/data pair into database. */
158 btreeDB.PutNoOverwrite(key, data);
160 Console.WriteLine("Put error with key::{0} and"
161 + "data::{1}", buff, new string(rbuff));
168 /* Close the word list. */
171 /* Acquire a cursor for the database. */
172 dbc = btreeDB.Cursor();
175 * Input an available record number and get its
176 * corresponding key/data pair in the database.
179 Console.WriteLine("Please input record number"
180 + ", input 0 to stop");
183 Console.WriteLine("recno #:\t");
184 buff = Console.ReadLine();
187 cnt = uint.Parse(buff);
190 Console.WriteLine("Invalid Record Number. "
199 * Position the cursor at a key/data pair in the database
200 * where the record number points to.
202 if (!dbc.Move(cnt)) {
203 Console.WriteLine("No record is found");
207 /* Get the current key/data pair and display them.*/
209 Console.WriteLine("key::{0, 18:G} data::{1, 18:G}",
210 strFromDBT(pair.Key), strFromDBT(pair.Value));
212 /* Check if it's the right record number. */
213 if (dbc.Recno() != cnt) {
214 Console.WriteLine("The current record is not"
215 + "the right one of the given record number.");
221 /* Get the next record. */
222 if (!dbc.MoveNext()) {
224 "Next record is not found");
228 /* Get the current key/data pair and display them.*/
230 Console.WriteLine("key::{0, 18:G} data::{1, 18:G}\n",
231 strFromDBT(pair.Key), strFromDBT(pair.Value));
235 /* Close cursor and database. */
243 /* Show the usage. */
244 public static void usage() {
246 "Usage: [db file name] [db name] [word list file]");
249 /* Get dbt from string. */
250 static void dbtFromString(DatabaseEntry dbt, string s) {
251 dbt.Data = System.Text.Encoding.ASCII.GetBytes(s);
254 /* Get string from dbt. */
255 public static string strFromDBT(DatabaseEntry dbt) {
257 System.Text.ASCIIEncoding decode =
259 return decode.GetString(dbt.Data);