Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / examples / csharp / excs_btrec / excs_btrec.cs
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2009, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 using System;
8 using System.IO;
9 using System.Collections.Generic;
10 using System.Collections;
11 using System.Diagnostics;
12 using System.Text;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using BerkeleyDB;
15
16 namespace excs_btrec {
17     class Program {
18         static void Main(string[] args) {
19             string dbFileName;
20             string dbName;
21             string wordFile;
22
23             /*
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.
29              */
30             try {
31                 String pwd = Environment.CurrentDirectory;
32                 pwd = Path.Combine(pwd, "..");
33                 pwd = Path.Combine(pwd, "..");
34                 if (IntPtr.Size == 4)
35                     pwd = Path.Combine(pwd, "Win32");
36                 else
37                     pwd = Path.Combine(pwd, "x64");
38 #if DEBUG
39                 pwd = Path.Combine(pwd, "Debug");
40 #else
41                 pwd = Path.Combine(pwd, "Release");
42 #endif
43                 pwd += ";" + Environment.GetEnvironmentVariable("PATH");
44                 Environment.SetEnvironmentVariable("PATH", pwd);
45             } catch (Exception e) {
46                 Console.WriteLine(
47                     "Unable to set the PATH environment variable.");
48                 Console.WriteLine(e.Message);
49                 return;
50             }
51             
52             try {
53                 dbFileName = args[0];
54                 dbName = args[1];
55                 wordFile = args[2];
56             } catch {
57                 usage();
58                 return;
59             }
60
61             excs_btrec(dbFileName, dbName, wordFile);
62         }
63
64         public static int excs_btrec(string dbFileName,
65             string dbName, string wordFile) {
66             BTreeCursor dbc;
67             BTreeDatabase btreeDB;
68             BTreeDatabaseConfig btreeDBconfig;
69             DatabaseEntry key, data;
70             KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
71             StreamReader wordList;
72             string buff;
73             char[] rbuff;
74             int i, j;
75             uint cnt;
76
77             string progName = "excs_btrec";
78
79             /* Check if word list exists. */
80             if (!File.Exists(wordFile)) {
81                 Console.WriteLine("Cannot find {0}/{1}.",
82                     Environment.CurrentDirectory, wordFile);
83                 return (1);
84             }
85
86             /* Optionally remove the previous database. */
87             if (File.Exists(dbFileName)) {
88                 while (true) {
89                     Console.Write
90                         ("Database already exists, delete or not (y/n)?");
91                     buff = Console.ReadLine();
92                     if (buff == "y" || buff == "n")
93                         break;
94                 }
95
96                 if (buff == "y") {
97                     File.Delete(dbFileName);
98                     Console.WriteLine("The existing {0} is deleted",
99                         dbName);
100                 }
101             }
102
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;
108             try {
109                 btreeDB = BTreeDatabase.Open(dbFileName, dbName,
110                     btreeDBconfig);
111             } catch {
112                 Console.WriteLine("Database can not be opened");
113                 return (1);
114             }
115
116             /*
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.
120              */
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. */
126                 try {
127                     buff = wordList.ReadLine();
128                 } catch {
129                     Console.WriteLine("Error when reading word list");
130                     btreeDB.Close();
131                     wordList.Close();
132                     return (1);
133                 }
134
135                 /* 
136                  * Prefix the word with record number and store
137                  * them in key.
138                  */
139                 try {
140                     buff = cnt.ToString("0000") + buff;
141                 } catch {
142                     Console.WriteLine(
143                         "Wrong format for record number.");
144                     btreeDB.Close();
145                     wordList.Close();
146                     return (1);
147                 }
148                 dbtFromString(key, buff);
149
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--)
153                     rbuff[i] = buff[j];
154                 dbtFromString(data, new string(rbuff));
155
156                 /* Put the key/data pair into database. */
157                 try {
158                     btreeDB.PutNoOverwrite(key, data);
159                 } catch {
160                     Console.WriteLine("Put error with key::{0} and"
161                         + "data::{1}", buff, new string(rbuff));
162                     btreeDB.Close();
163                     wordList.Close();
164                     return (1);
165                 }
166             }
167
168             /* Close the word list. */
169             wordList.Close();
170
171             /* Acquire a cursor for the database. */
172             dbc = btreeDB.Cursor();
173
174             /* 
175              * Input an available record number and get its 
176              * corresponding key/data pair in the database. 
177              */
178             while (true) {
179                 Console.WriteLine("Please input record number"
180                     + ", input 0 to stop");
181
182                 while (true) {
183                     Console.WriteLine("recno #:\t");
184                     buff = Console.ReadLine();
185
186                     try {
187                         cnt = uint.Parse(buff);
188                         break;
189                     } catch {
190                         Console.WriteLine("Invalid Record Number. "
191                             + "Try Again.");
192                     }
193                 }
194
195                 if (cnt == 0)
196                     break;
197
198                 /*
199                  * Position the cursor at a key/data pair in the database 
200                  * where the record number points to.
201                  */
202                 if (!dbc.Move(cnt)) {
203                     Console.WriteLine("No record is found");
204                     break;
205                 }
206
207                 /* Get the current key/data pair and display them.*/
208                 pair = dbc.Current;
209                 Console.WriteLine("key::{0, 18:G}    data::{1, 18:G}",
210                     strFromDBT(pair.Key), strFromDBT(pair.Value));
211
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.");
216                     btreeDB.Close();
217                     dbc.Close();
218                     return (1);
219                 }
220
221                 /* Get the next record. */
222                 if (!dbc.MoveNext()) {
223                     Console.WriteLine(
224                         "Next record is not found");
225                     break;
226                 }
227
228                 /* Get the current key/data pair and display them.*/
229                 pair = dbc.Current;
230                 Console.WriteLine("key::{0, 18:G}    data::{1, 18:G}\n",
231                     strFromDBT(pair.Key), strFromDBT(pair.Value));
232
233             }
234
235             /* Close cursor and database. */
236             dbc.Close();
237             btreeDB.Close();
238
239             return (0);
240         }
241
242         #region Utilities
243         /* Show the usage. */
244         public static void usage() {
245             Console.WriteLine(
246                 "Usage: [db file name]  [db name] [word list file]");
247         }
248
249         /* Get dbt from string. */
250         static void dbtFromString(DatabaseEntry dbt, string s) {
251             dbt.Data = System.Text.Encoding.ASCII.GetBytes(s);
252         }
253
254         /* Get string from dbt. */
255         public static string strFromDBT(DatabaseEntry dbt) {
256
257             System.Text.ASCIIEncoding decode =
258                 new ASCIIEncoding();
259             return decode.GetString(dbt.Data);
260         }
261         #endregion Utilities
262     }
263 }