/dotnet/samples/SampleEnv3/Program.cs

https://github.com/GerHobbelt/hamsterdb · C# · 275 lines · 141 code · 35 blank · 99 comment · 12 complexity · 5855a5c5eb8789490efab4a0e36a5929 MD5 · raw file

  1. /**
  2. * Copyright (C) 2005-2010 Christoph Rupp (chris@crupp.de).
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * See file COPYING.GPL2 and COPYING.GPL3 for License information.
  10. *
  11. */
  12. /*
  13. * This sample is the implementation of /samples/env3.cpp in C#.
  14. *
  15. * It creates an Environment with three Databases - one for the Customers,
  16. * one for Orders, and a third for managing the 1:n relationship between
  17. * the other two.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Text;
  22. using Hamster;
  23. namespace SampleEnv3
  24. {
  25. /*
  26. * a Customer class
  27. */
  28. public struct Customer
  29. {
  30. public Customer(int id, String name) {
  31. this.id = id;
  32. this.name = name;
  33. }
  34. public Customer(byte[] id, byte[] name) {
  35. this.id = BitConverter.ToInt32(id, 0);
  36. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  37. this.name = enc.GetString(name);
  38. }
  39. public int id;
  40. public string name;
  41. public byte[] GetKey() {
  42. return BitConverter.GetBytes(id);
  43. }
  44. public byte[] GetRecord() {
  45. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  46. return enc.GetBytes(name);
  47. }
  48. }
  49. /*
  50. * An Order class; it stores the ID of the Customer,
  51. * and the name of the employee who is assigned to this order
  52. */
  53. public class Order
  54. {
  55. public Order(int id, int customerId, string assignee) {
  56. this.id = id;
  57. this.customerId = customerId;
  58. this.assignee = assignee;
  59. }
  60. public int id;
  61. public int customerId;
  62. public String assignee;
  63. public byte[] GetKey() {
  64. return BitConverter.GetBytes(id);
  65. }
  66. public byte[] GetCustomerKey() {
  67. return BitConverter.GetBytes(customerId);
  68. }
  69. public byte[] GetRecord() {
  70. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  71. return enc.GetBytes(assignee);
  72. }
  73. }
  74. class Program
  75. {
  76. const int DBIDX_CUSTOMER = 0;
  77. const int DBIDX_ORDER = 1;
  78. const int DBIDX_C2O = 2;
  79. const short DBNAME_CUSTOMER = 1;
  80. const short DBNAME_ORDER = 2;
  81. const short DBNAME_C2O = 3;
  82. static void Main(string[] args) {
  83. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  84. Hamster.Environment env = new Hamster.Environment();
  85. Database[] db=new Database[3];
  86. Cursor[] cursor=new Cursor[3];
  87. /*
  88. * set up the customer and order data - these arrays will later
  89. * be inserted into the Databases
  90. */
  91. Customer[] customers=new Customer[4];
  92. customers[0] = new Customer(1, "Alan Antonov Corp.");
  93. customers[1] = new Customer(2, "Barry Broke Inc.");
  94. customers[2] = new Customer(3, "Carl Caesar Lat.");
  95. customers[3] = new Customer(4, "Doris Dove Brd.");
  96. Order[] orders = new Order[8];
  97. orders[0] = new Order(1, 1, "Joe");
  98. orders[1] = new Order(2, 1, "Tom");
  99. orders[2] = new Order(3, 3, "Joe");
  100. orders[3] = new Order(4, 4, "Tom");
  101. orders[4] = new Order(5, 3, "Ben");
  102. orders[5] = new Order(6, 3, "Ben");
  103. orders[6] = new Order(7, 4, "Chris");
  104. orders[7] = new Order(8, 1, "Ben");
  105. /*
  106. * Create a new Environment
  107. */
  108. env.Create("test.db");
  109. /*
  110. * then create the three Databases in this Environment; each Database
  111. * has a name - the first is our "customer" Database, the second
  112. * is for the "orders"; the third manages our 1:n relation and
  113. * therefore needs to enable duplicate keys
  114. */
  115. db[DBIDX_CUSTOMER] = env.CreateDatabase(DBNAME_CUSTOMER);
  116. db[DBIDX_ORDER] = env.CreateDatabase(DBNAME_ORDER);
  117. db[DBIDX_C2O] = env.CreateDatabase(DBNAME_C2O,
  118. HamConst.HAM_ENABLE_DUPLICATES);
  119. /*
  120. * create a Cursor for each Database
  121. */
  122. cursor[DBIDX_CUSTOMER] = new Cursor(db[DBIDX_CUSTOMER]);
  123. cursor[DBIDX_ORDER] = new Cursor(db[DBIDX_ORDER]);
  124. cursor[DBIDX_C2O] = new Cursor(db[DBIDX_C2O]);
  125. /*
  126. * Insert the customers in the customer Database
  127. *
  128. * INSERT INTO customers VALUES (1, "Alan Antonov Corp.");
  129. * INSERT INTO customers VALUES (2, "Barry Broke Inc.");
  130. * etc.
  131. */
  132. for (int i = 0; i < customers.GetLength(0); i++) {
  133. byte[] key = customers[i].GetKey();
  134. byte[] rec = customers[i].GetRecord();
  135. db[DBIDX_CUSTOMER].Insert(key, rec);
  136. }
  137. /*
  138. * Insert the orders in the order Database
  139. *
  140. * INSERT INTO orders VALUES (1, "Joe");
  141. * INSERT INTO orders VALUES (2, "Tom");
  142. * etc.
  143. */
  144. for (int i = 0; i < orders.GetLength(0); i++) {
  145. byte[] key = orders[i].GetKey();
  146. byte[] rec = orders[i].GetRecord();
  147. db[DBIDX_ORDER].Insert(key, rec);
  148. }
  149. /*
  150. * and now the 1:n relationships; the flag HAM_DUPLICATE creates
  151. * a duplicate key, if the key already exists
  152. *
  153. * INSERT INTO c2o VALUES (1, 1);
  154. * INSERT INTO c2o VALUES (2, 1);
  155. * etc
  156. */
  157. for (int i = 0; i < orders.GetLength(0); i++) {
  158. byte[] key = orders[i].GetCustomerKey();
  159. byte[] rec = orders[i].GetKey();
  160. db[DBIDX_C2O].Insert(key, rec, HamConst.HAM_DUPLICATE);
  161. }
  162. /*
  163. * now start the queries - we want to dump each customer and
  164. * his orders
  165. *
  166. * loop over the customer; for each customer, loop over the
  167. * 1:n table and pick those orders with the customer id.
  168. * then load the order and print it
  169. *
  170. * the outer loop is similar to
  171. * SELECT * FROM customers WHERE 1;
  172. */
  173. while (1 == 1) {
  174. Customer c;
  175. try {
  176. cursor[DBIDX_CUSTOMER].MoveNext();
  177. }
  178. catch (DatabaseException e) {
  179. // reached end of Database?
  180. if (e.ErrorCode == HamConst.HAM_KEY_NOT_FOUND)
  181. break;
  182. Console.Out.WriteLine("cursor.MoveNext failed: " + e);
  183. return;
  184. }
  185. // load the customer
  186. c = new Customer(cursor[DBIDX_CUSTOMER].GetKey(),
  187. cursor[DBIDX_CUSTOMER].GetRecord());
  188. // print information about this customer
  189. Console.Out.WriteLine("customer " + c.id + " ('" + c.name + "')");
  190. /*
  191. * loop over the 1:n table
  192. *
  193. * SELECT * FROM customers, orders, c2o
  194. * WHERE c2o.customer_id=customers.id AND
  195. * c2o.order_id=orders.id;
  196. */
  197. try {
  198. cursor[DBIDX_C2O].Find(c.GetKey());
  199. }
  200. catch (DatabaseException e) {
  201. // no order for this customer?
  202. if (e.ErrorCode == HamConst.HAM_KEY_NOT_FOUND)
  203. continue;
  204. Console.Out.WriteLine("cursor.Find failed: " + e);
  205. return;
  206. }
  207. do {
  208. /*
  209. * load the order; orderId is a byteArray with the ID of the
  210. * Order; the record of the item is a byteArray with the
  211. * name of the assigned employee
  212. *
  213. * SELECT * FROM orders WHERE id = order_id;
  214. */
  215. byte[] orderId = cursor[DBIDX_C2O].GetRecord();
  216. cursor[DBIDX_ORDER].Find(orderId);
  217. String assignee = enc.GetString(cursor[DBIDX_ORDER].GetRecord());
  218. Console.Out.WriteLine(" order: " + BitConverter.ToInt32(orderId, 0) +
  219. " (assigned to " + assignee + ")");
  220. /*
  221. * move to the next order of this customer
  222. *
  223. * the flag HAM_ONLY_DUPLICATES restricts the cursor
  224. * movement to the duplicates of the current key.
  225. */
  226. try {
  227. cursor[DBIDX_C2O].MoveNext(HamConst.HAM_ONLY_DUPLICATES);
  228. }
  229. catch (DatabaseException e) {
  230. // no more orders for this customer?
  231. if (e.ErrorCode == HamConst.HAM_KEY_NOT_FOUND)
  232. break;
  233. Console.Out.WriteLine("cursor.MoveNext failed: " + e);
  234. return;
  235. }
  236. } while (1==1);
  237. }
  238. }
  239. }
  240. }