-
Notifications
You must be signed in to change notification settings - Fork 0
/
FigaroDataManager.cs
178 lines (164 loc) · 6.98 KB
/
FigaroDataManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Diagnostics;
using Figaro;
using Figaro.Configuration.Factory;
namespace Nancy.Demos.Figaro
{
/// <summary>
/// The purpose of this class is to help developers get a quick-start introduction to using
/// the Figaro library in their application(s). For more information on how to use the library, it is
/// highly recommended that developers review the included help file (Figaro.chm) or online at
/// http://help.bdbxml.net .
/// </summary>
/// <seealso cref="http://bdbxml.net/blog"/>
public class FigaroDataManager : IDisposable
{
/// <summary>
/// Gets or sets the database environment.
/// </summary>
/// <seealso cref="http://help.bdbxml.net/api/Figaro.FigaroEnv.html"/>
public FigaroEnv Environment { get; }
/// <summary>
/// Gets or sets the database manager.
/// </summary>
/// <seealso cref="http://help.bdbxml.net/api/Figaro.XmlManager.html"/>
public XmlManager Manager { get; }
/// <summary>
/// Gets the Figaro database.
/// </summary>
/// <seealso cref="http://help.bdbxml.net/api/Figaro.Container.html"/>
public Container Database { get; }
/// <summary>
/// Initialize the Figaro data objects via Figaro.Configuration
/// </summary>
public FigaroDataManager()
{
//The Figaro.Configuration will create the FigaroEnv object for the XmlManager it is
// assigned to, so we can simply retrieve the reference to it from the manager and
// avoid creating multiple instances and adding additional, unnecessary reference
// instances. Otherwise, we'd simply create it first and assign to the manager.
Manager = ManagerFactory.Create("demoMgr");
Environment = Manager.Environment;
//configure logging, progress and panic event output
Environment.OnErr += Environment_OnErr;
Environment.OnMessage += Environment_OnMessage;
Environment.OnProcess += Environment_OnProcess;
Environment.OnProgress += Environment_OnProgress;
Environment.ErrEventEnabled = true;
Environment.MessageEventEnabled = true;
Environment.ProcessEventEnabled = true;
Environment.ProgressEventEnabled = true;
//http://help.bdbxml.net/api/Figaro.XmlManager.html#Figaro_XmlManager_CreateTransaction
var trans = Manager.CreateTransaction();
Database = Manager.OpenContainer(trans,ContainerConfigFactory.Create("demoMgr", "demo"));
trans.Commit();
}
public ulong GetRecordCount()
{
return Database.GetNumDocuments();
}
/// <summary>
/// Inserts an XML record pulled from a URL.
/// </summary>
/// <param name="url">The URL to extract XML from.</param>
/// <returns>The name of the record.</returns>
public string InsertRecordFromUrl(string url)
{
var name = "record" + DateTime.Now.ToFileTimeUtc();
using (var trans = Manager.CreateTransaction(TransactionType.SyncTransaction))
{
var stm = Manager.CreateUrlInputStream(string.Empty, url);
var doc = Manager.CreateDocument();
doc.SetContentAsInputStream(stm);
doc.Name = name;
//add metadata to the record.
doc.SetMetadata("http://schemas.bdbxml.net/metadata", "CreatedDate",
new XmlValue(DateTime.Now.ToString()));
Database.PutDocument(trans, doc, Manager.CreateUpdateContext());
trans.Commit();
}
return name;
}
/// <summary>
/// Insert a System.Xml.XmlDocument into the database.
/// </summary>
/// <param name="doc">The document to insert.</param>
/// <returns>The document name, for lookup purposes.</returns>
public string InsertRecord(System.Xml.XmlDocument doc)
{
var name = "record" + DateTime.Now.ToFileTimeUtc();
using (var trans = Manager.CreateTransaction(TransactionType.NoWaitTransaction))
{
var xml = Manager.CreateDocument(doc);
xml.Name = name;
//add metadata to the record.
xml.SetMetadata("http://schemas.bdbxml.net/metadata", "CreatedDate",
new XmlValue(DateTime.Now.ToString()));
Database.PutDocument(trans,xml, Manager.CreateUpdateContext());
trans.Commit();
}
return name;
}
/// <summary>
/// Environment progress event handler.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Environment_OnProgress(object sender, ProgressEventArgs e)
{
switch (e.EventType)
{
case FeedbackEvent.Other:
Trace.WriteLine($"feedback event {e.PercentComplete}%");
break;
case FeedbackEvent.Recover:
Trace.WriteLine($"feedback event {e.PercentComplete}%");
break;
case FeedbackEvent.Upgrade:
Trace.WriteLine($"feedback event {e.PercentComplete}%");
break;
}
}
/// <summary>
/// Handle panic and write failure events here.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Environment_OnProcess(object sender, ProcessEventArgs e)
{
if (e.EventType == EnvironmentEvent.Panic)
Trace.WriteLine("A panic event occurred!", "Panic");
if (e.EventType == EnvironmentEvent.WriteEventFailure)
Trace.WriteLine("A write event failed to occur!", "Fatal");
}
/// <summary>
/// For diagnostic message events.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Environment_OnMessage(object sender, MsgEventArgs e)
{
Trace.WriteLine($"{e.Message}", "Message");
}
/// <summary>
/// For writing error related events.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Environment_OnErr(object sender, ErrEventArgs e)
{
Trace.WriteLine($"{e.Prefix}: {e.Message}", "Error");
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// gracefully exit from our resources. Note that these need to be closed in
// the sequence shown.
Database.Dispose();
Manager?.Dispose();
Environment?.Dispose();
}
}
}