private List <object[]> NewerRows(JsonArray data, string table, int modIdx) { long[] ids = new long[data.Count]; for (uint i = 0; i < data.Count; i++) { ids[i] = (long)data.GetArrayAt(i).GetNumberAt(0); } Dictionary <long, long> lmods = new Dictionary <long, long>(); var list = collection.Database.QueryColumn <UnknownTable>( "SELECT id, mod FROM " + table + " WHERE id IN " + Utils.Ids2str(ids) + " AND " + UsnLim()); foreach (var c in list) { lmods.Add(c.Id, c.Mod); } List <object[]> update = new List <object[]>(); for (uint i = 0; i < data.Count; i++) { JsonArray r = data.GetArrayAt(i); long num = (long)r.GetNumberAt(0); if (!lmods.ContainsKey(num) || lmods[num] < r.GetNumberAt((uint)modIdx)) { update.Add(Utils.JsonArray2Objects(r)); } } collection.Log(args: new object[] { table, data }); return(update); }
private void MergeDecks(JsonArray rchg) { JsonArray decks = rchg.GetArrayAt(0); for (uint i = 0; i < decks.Count; i++) { JsonObject r = decks.GetObjectAt(i); JsonObject l = collection.Deck.Get((int)JsonHelper.GetNameNumber(r, "id"), false); // if missing locally or server is newer, update if (l == null || JsonHelper.GetNameNumber(r, "mod") > JsonHelper.GetNameNumber(l, "mod")) { collection.Deck.Update(r); } } JsonArray confs = rchg.GetArrayAt(1); for (uint i = 0; i < confs.Count; i++) { JsonObject rightConf = confs.GetObjectAt(i); var confId = (int)JsonHelper.GetNameNumber(rightConf, "id"); JsonObject leftConf = null; var isSuccess = collection.Deck.DeckConf.TryGetValue((int)JsonHelper.GetNameNumber(rightConf, "id"), out leftConf); // if missing locally or server is newer, update if (!isSuccess || leftConf == null || JsonHelper.GetNameNumber(rightConf, "mod") > JsonHelper.GetNameNumber(leftConf, "mod")) { collection.Deck.UpdateConf(rightConf); } } }
private void MergeDecks(JsonArray rchg) { JsonArray decks = rchg.GetArrayAt(0); for (uint i = 0; i < decks.Count; i++) { JsonObject r = decks.GetObjectAt(i); JsonObject l = collection.Deck.Get((int)r.GetNamedNumber("id"), false); // if missing locally or server is newer, update if (l == null || r.GetNamedNumber("mod") > l.GetNamedNumber("mod")) { collection.Deck.Update(r); } } JsonArray confs = rchg.GetArrayAt(1); for (uint i = 0; i < confs.Count; i++) { JsonObject r = confs.GetObjectAt(i); JsonObject l = collection.Deck.DeckConf[(int)r.GetNamedNumber("id")]; // if missing locally or server is newer, update if (l == null || r.GetNamedNumber("mod") > l.GetNamedNumber("mod")) { collection.Deck.UpdateConf(r); } } }
public Neighbourhood(JsonObject vals) { JsonObject props = vals.GetNamedObject("properties"); name = props.GetNamedString("Name"); nameFr = props.GetNamedValue("Name_FR") == null ? name : props.GetNamedValue("Name_FR").ToString(); id = (int)props.GetNamedNumber("ONS_ID"); JsonObject geo = vals.GetNamedObject("geometry"); JsonArray neighbourhoodZones = new JsonArray(); if (geo.GetNamedString("type") == "Polygon") { neighbourhoodZones.Add(geo.GetNamedArray("coordinates")); } else { neighbourhoodZones = geo.GetNamedArray("coordinates"); } for (uint i = 0; i < neighbourhoodZones.Count; i++) { JsonArray neighbourhoodPoints = neighbourhoodZones.GetArrayAt(i).GetArrayAt(0); List <LatLng> list = new List <LatLng>(); for (uint it = 0; it < neighbourhoodPoints.Count; it++) { list.Add(new LatLng(neighbourhoodPoints.GetArrayAt(it).GetNumberAt(1), neighbourhoodPoints.GetArrayAt(it).GetNumberAt(0))); } boundaries.Add(list); } }
public static async Task <string> Translate(string text, string sourceLang) { string uri = "https://translate.googleapis.com/translate_a/single?" + "client=gtx" + "&dt=t" + // source language "&sl=" + sourceLang + // target language "&tl=" + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName + // query "&q=" + HttpUtility.UrlEncode(text); HttpWebRequest req = HttpWebRequest.CreateHttp(uri); req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"; WebResponse resp = await req.GetResponseAsync(); using (Stream strm = resp.GetResponseStream()) using (StreamReader rdr = new StreamReader(strm)) { string data = rdr.ReadToEnd(); JsonArray values = JsonArray.Parse(data); return(values.GetArrayAt(0).GetArrayAt(0).GetStringAt(0)); } }
private void MergeRevlog(JsonArray logs) { for (uint i = 0; i < logs.Count; i++) { collection.Database.Execute("INSERT OR IGNORE INTO revlog VALUES (?,?,?,?,?,?,?,?,?)", Utils.JsonArray2Objects(logs.GetArrayAt(i))); } }
public static Table FromJson(JsonObject jsonObj) { // gather columns JsonArray columnNamesJson = jsonObj.GetNamedArray(JSON_KEY_COL_NAMES); String[] cols = new String[columnNamesJson.Count]; for (int i = 0; i < columnNamesJson.Count; i++) { cols[i] = columnNamesJson[i].GetString(); // should get the string name from the entity. } // gather the column types JsonArray columnTypesJson = jsonObj.GetNamedArray(JSON_KEY_COL_TYPES); if (columnTypesJson == null) { throw new Exception("Cannot create Table from json: no column types specified"); } String[] colTypes = new String[columnTypesJson.Count]; for (int i = 0; i < columnTypesJson.Count; i++) { if (columnTypesJson[i] == null) { throw new Exception("Cannot create Table with null column type"); } colTypes[i] = columnTypesJson[i].GetString(); } // gather rows String s; List <List <String> > rows = new List <List <String> >(); JsonArray rowsJson = jsonObj.GetNamedArray(JSON_KEY_ROWS); for (int i = 0; i < rowsJson.Count; i++) { List <String> row = new List <String>(); // the current row we will be making. JsonArray rowJson = rowsJson.GetArrayAt((uint)i); for (int j = 0; j < rowJson.Count; j++) { s = rowJson.GetStringAt((uint)j); // get the string if (s != null) { row.Add(s); } else { row.Add("null"); } } rows.Add(row); } return(new Table(cols, colTypes, rows)); }
internal static PointF VertexAtIndex(int idx, JsonArray points) { if (idx >= points.Count) { throw new System.ArgumentException("Invalid index " + idx + ". There are only " + points.Count + " points."); } var pointArray = points.GetArrayAt((uint)idx); var x = pointArray[0]; var y = pointArray[1]; return(new PointF(x != null ? (float)x.GetNumber() : 0, y != null ? (float)y.GetNumber() : 0)); }
public byte[][] GetRGBColours(string scheme, int nClasses) { byte[][] rgbArray = new byte[nClasses][]; JsonObject colourSchemes = colourData.GetNamedObject(scheme); JsonArray colourScheme = colourSchemes.GetNamedArray(nClasses.ToString()); for (uint i = 0; i < colourScheme.Count; i++) { JsonArray rgbValues = colourScheme.GetArrayAt(i); byte[] parsedRgbValues = new byte[3]; for (uint j = 0; j < rgbValues.Count; j++) { parsedRgbValues[j] = (byte)rgbValues.GetNumberAt(j); } rgbArray[i] = parsedRgbValues; } return(rgbArray); }
/// <summary> /// Given a joined field string, return available template ordinals /// </summary> /// <param name="m"></param> /// <param name="flds"></param> /// <returns></returns> public List <int> AvailableOrds(JsonObject m, string flds) { bool ok; if (m.GetNamedNumber("type") == (double)ModelType.CLOZE) { return(AvailableClozeOrds(m, flds)); } string[] fields = Utils.SplitFields(flds); for (int i = 0; i < fields.Length; i++) { fields[i] = fields[i].Trim(); } List <int> available = new List <int>(); JsonArray reqArray = m.GetNamedArray("req"); for (uint i = 0; i < reqArray.Count; i++) { JsonArray sr = reqArray.GetArrayAt(i); int ord = (int)sr.GetNumberAt(0); string type = sr.GetStringAt(1); JsonArray req = sr.GetArrayAt(2); if (type.Equals("none")) { // unsatisfiable template continue; } else if (type.Equals("all")) { // AND requirement? ok = true; for (uint j = 0; j < req.Count; j++) { int idx = (int)req.GetNumberAt(j); if (fields[idx] == null || fields[idx].Length == 0) { // missing and was required ok = false; break; } } if (!ok) { continue; } } else if (type.Equals("any")) { // OR requirement? ok = false; for (uint j = 0; j < req.Count; j++) { int idx = (int)req.GetNumberAt(j); if (fields[idx] != null && fields[idx].Length != 0) { // missing and was required ok = true; break; } } if (!ok) { continue; } } available.Add(ord); } return(available); }
public async Task <string> Sync() { // check if there have been any changes // If we haven't built the media db yet, do so on this sync. See note at the top // of this class about this difference to the original. if (collection.Media.NeedScan()) { collection.Log(args: "findChanges"); await collection.Media.ScanForChangesAsync(); } // begin session and check if in sync long lastUsn = collection.Media.GetLastUnixTimeSync(); JsonObject ret = await server.Begin(); int srvUsn = (int)ret.GetNamedNumber("usn"); if ((lastUsn == srvUsn) && !(collection.Media.HaveDirty())) { return("noChanges"); } // loop through and process changes from server collection.Log(args: "last local usn is " + lastUsn); downloadCount = 0; while (true) { JsonArray data = await server.MediaChanges(lastUsn); collection.Log(args: new object[] { "mediaChanges resp count: ", data.Count }); if (data.Count == 0) { break; } List <string> need = new List <string>(); lastUsn = (int)data.GetArrayAt((uint)data.Count - 1).GetNumberAt(1); for (uint i = 0; i < data.Count; i++) { JsonArray array = data.GetArrayAt(i); string fname = array.GetStringAt(0); int rusn = (int)array.GetNumberAt(1); string rsum = null; if (array.Count >= 3) { rsum = array.GetStringAt(2); } KeyValuePair <string, int> info = collection.Media.GetSyncInfo(fname); string lsum = info.Key; int ldirty = info.Value; collection.Log(args: String.Format(Media.locale, "check: lsum={0} rsum={1} ldirty={2} rusn={3} fname={4}", String.IsNullOrEmpty(lsum) ? "" : lsum.Substring(0, 5), String.IsNullOrEmpty(rsum) ? "" : lsum.Substring(0, 5), ldirty, rusn, fname)); if (!String.IsNullOrEmpty(rsum)) { // added/changed remotely if (String.IsNullOrEmpty(lsum) || !lsum.Equals(rsum)) { collection.Log(args: "will fetch"); need.Add(fname); } else { collection.Log(args: "have same already"); } List <string> newList = new List <string>(); newList.Add(fname); collection.Media.MarkClean(newList); } else if (!String.IsNullOrEmpty(lsum)) { // deleted remotely if (ldirty != 0) { collection.Log(args: "delete local"); await collection.Media.SyncDelete(fname); } else { // conflict: local add overrides remote delete collection.Log(args: "conflict; will send"); } } else { // deleted both sides collection.Log(args: "both sides deleted"); List <string> newList = new List <string>(); newList.Add(fname); collection.Media.MarkClean(newList); } } await DownloadFiles(need); collection.Log(args: "update last usn to " + lastUsn); collection.Media.SetLastUnixTimeSync(lastUsn); // commits } // at this point, we're all up to date with the server's changes, // and we need to send our own bool updateConflict = false; int toSend = collection.Media.DirtyCount(); while (true) { KeyValuePair <StorageFile, List <string> > changesZip = await collection.Media.MediaChangesZip(); StorageFile zip = changesZip.Key; try { List <string> fnames = changesZip.Value; if (fnames.Count == 0) { break; } JsonArray changes = await server.UploadChanges(zip); int processedCnt = (int)changes.GetNumberAt(0); int serverLastUsn = (int)changes.GetNumberAt(1); collection.Media.MarkClean(fnames.GetRange(0, processedCnt)); collection.Log(args: String.Format(Media.locale, "processed {0}, serverUsn {1}, clientUsn {2}", processedCnt, serverLastUsn, lastUsn)); if (serverLastUsn - processedCnt == lastUsn) { collection.Log(args: "lastUsn in sync, updating local"); lastUsn = serverLastUsn; collection.Media.SetLastUnixTimeSync(serverLastUsn); // commits } else { collection.Log(args: "concurrent update, skipping usn update"); updateConflict = true; } toSend -= processedCnt; } finally { System.IO.File.Delete(zip.Path); } } if (updateConflict) { collection.Log(args: "restart sync due to concurrent update"); return(await Sync()); } int lcnt = collection.Media.MediaCount(); string sRet = await server.MediaSanity(lcnt); if (sRet.Equals("OK")) { return("OK"); } else { collection.Media.ForceReSync(); return(sRet); } }
public ChartData(JsonObject jsonObject) { JsonArray columns = jsonObject.GetNamedArray("columns"); int n = columns.Count; for (uint i = 0; i < columns.Count; i++) { JsonArray a = columns.GetArrayAt(i); if (a.GetStringAt(0).Equals("x")) { int len = a.Count - 1; x = new long[len]; for (uint j = 0; j < len; j++) { x[j] = (long)a.GetNumberAt(j + 1); } } else { Line l = new Line(); lines.Add(l); int len = a.Count - 1; l.id = a.GetStringAt(0); l.y = new int[len]; for (uint j = 0; j < len; j++) { l.y[j] = (int)a.GetNumberAt(j + 1); if (l.y[j] > l.maxValue) { l.maxValue = l.y[j]; } if (l.y[j] < l.minValue) { l.minValue = l.y[j]; } } } if (x.Length > 1) { timeStep = x[1] - x[0]; } else { timeStep = 86400000L; } Measure(); } JsonObject colors = jsonObject.GetNamedObject("colors"); JsonObject names = jsonObject.GetNamedObject("names"); Regex colorPattern = new Regex("(.*)(#.*)", RegexOptions.Compiled); for (int i = 0; i < lines.Count; i++) { Line line = lines[i]; if (colors != null) { var matcher = colorPattern.Match(colors.GetNamedString(line.id)); if (matcher.Success) { string key = matcher.Groups[1].Value; if (key != null) { line.colorKey = "StatisticChartLine_" + matcher.Groups[1].Value; } line.color = matcher.Groups[2].Value.ToColor(); //line.colorDark = ColorUtils.blendARGB(Color.WHITE, line.color, 0.85f); } } if (names != null) { line.name = names.GetNamedString(line.id); } } }