Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / versaplexd / vxdbusschema.cs
blob31d2ac1c43ed9eaaaf013972f780fb9d1dbc248f
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Linq;
8 using System.Collections;
9 using System.Collections.Generic;
10 using Wv;
11 using Wv.Extensions;
13 // An ISchemaBackend that uses a DBus connection to Versaplex as a backing
14 // store.
15 [WvMoniker]
16 internal class VxDbusSchema : ISchemaBackend
18 WvDbus bus;
20 public static void wvmoniker_register()
22 WvMoniker<ISchemaBackend>.register("vx",
23 (string m, object o) => new VxDbusSchema(m));
24 WvMoniker<ISchemaBackend>.register("vx",
25 (string m, object o) => new VxDbusSchema(m));
28 public VxDbusSchema()
29 : this((string)null)
33 public VxDbusSchema(string busurl)
35 WvUrl url = new WvUrl(busurl);
37 if (url.host.e() || url.host == "session")
38 bus = WvDbus.session_bus;
39 else
40 bus = new WvDbus(wv.fmt("tcp:host={0},port={1}",
41 url.host, url.port));
44 // If you've already got a Bus you'd like to use.
45 public VxDbusSchema(WvDbus _bus)
47 bus = _bus;
50 public void Dispose()
52 using (bus)
54 bus = null;
58 static WvDbusMsg methodcall(string method, string signature)
60 return new WvDbusCall("vx.versaplexd", "/db",
61 "vx.db", method, signature);
64 //
65 // The ISchema interface
68 // Note: this implementation ignores the sums.
69 public VxSchemaErrors Put(VxSchema schema, VxSchemaChecksums sums,
70 VxPutOpts opts)
72 WvDbusMsg call = methodcall("PutSchema",
73 String.Format("{0}i", VxSchema.GetDbusSignature()));
75 WvDbusWriter writer = new WvDbusWriter();
77 schema.WriteSchema(writer);
78 writer.Write((int)opts);
79 call.Body = writer.ToArray();
81 WvDbusMsg reply = bus.send_and_wait(call);
82 if (reply.signature == VxSchemaErrors.GetDbusSignature())
83 return new VxSchemaErrors(reply.iter().pop());
84 else
85 reply.check(VxSchemaErrors.GetDbusSignature());
86 return null;
89 // Utility API so you can say Get("foo").
90 public VxSchema Get(params string[] keys)
92 WvDbusMsg call = methodcall("GetSchema", "as");
94 WvDbusWriter writer = new WvDbusWriter();
96 if (keys == null)
97 keys = new string[0];
99 writer.WriteArray(4, keys, (w2, k) => {
100 w2.Write(k);
102 call.Body = writer.ToArray();
104 WvDbusMsg reply = bus.send_and_wait(call);
105 reply.check("a(sssy)");
106 VxSchema schema = new VxSchema(reply.iter().pop());
107 return schema;
110 public VxSchema Get(IEnumerable<string> keys)
112 if (keys == null)
113 keys = new string[0];
114 return Get(keys.ToArray());
117 public VxSchemaChecksums GetChecksums()
119 WvDbusMsg call = methodcall("GetSchemaChecksums", "");
121 WvDbusMsg reply = bus.send_and_wait(call);
122 reply.check("a(sat)");
123 VxSchemaChecksums sums = new VxSchemaChecksums(reply);
124 return sums;
127 public VxSchemaErrors DropSchema(IEnumerable<string> keys)
129 if (keys == null)
130 keys = new string[0];
131 return DropSchema(keys.ToArray());
134 // A method exported over DBus but not exposed in ISchemaBackend
135 public VxSchemaErrors DropSchema(params string[] keys)
137 WvDbusMsg call = methodcall("DropSchema", "as");
139 WvDbusWriter writer = new WvDbusWriter();
141 writer.WriteArray(4, keys, (w2, k) => {
142 w2.Write(k);
144 call.Body = writer.ToArray();
146 WvDbusMsg reply = bus.send_and_wait(call);
147 if (reply.signature == VxSchemaErrors.GetDbusSignature())
148 return new VxSchemaErrors(reply.iter().pop());
149 else
150 reply.check(VxSchemaErrors.GetDbusSignature());
151 return null;
154 public string GetSchemaData(string tablename, int seqnum, string where,
155 Dictionary<string,string> replaces, List<string> skipfields)
157 WvDbusMsg call = methodcall("GetSchemaData", "ss");
159 WvDbusWriter writer = new WvDbusWriter();
161 if (where == null)
162 where = "";
164 writer.Write(tablename);
165 writer.Write(where);
166 call.Body = writer.ToArray();
168 WvDbusMsg reply = bus.send_and_wait(call);
169 reply.check("s");
170 return reply.iter().pop();
173 public void PutSchemaData(string tablename, string text, int seqnum)
175 WvDbusMsg call = methodcall("PutSchemaData", "ss");
177 WvDbusWriter writer = new WvDbusWriter();
179 writer.Write(tablename);
180 writer.Write(text);
181 call.Body = writer.ToArray();
183 WvDbusMsg reply = bus.send_and_wait(call);
184 reply.check("");