// zip loader, called from Main() before the web server starts public static void LoadFromZip(string path, Storage store) { // load options var optionsFile = path + "/data/options.txt"; if (File.Exists(optionsFile)) { Log.Info("Opening " + optionsFile); using (StreamReader r = new StreamReader(File.OpenRead(optionsFile))) { store.Now = int.Parse(r.ReadLine()); store.IsRatingRun = int.Parse(r.ReadLine()); } } else { Log.Error(optionsFile + " is not found"); } // load zipped data var zipFile = path + "/data/data.zip"; if (File.Exists(zipFile)) { Log.Info("Opening " + zipFile); var totalAccounts = 0; var errorAccounts = 0; var fileBuffer = new byte[20000000]; using (var file = File.OpenRead(zipFile)) using (var zip = new ZipArchive(file, ZipArchiveMode.Read)) { foreach (var entry in zip.Entries) { if (entry.Name == "options.txt") { Log.Info("Loading options.txt"); using (StreamReader r = new StreamReader(entry.Open())) { store.Now = int.Parse(r.ReadLine()); store.IsRatingRun = int.Parse(r.ReadLine()); } } else { //Log.Info("Loading " + entry.Name); using (var stream = entry.Open()) { Console.Write('.'); var ms = new MemoryStream(fileBuffer); stream.CopyTo(ms); JsonReader reader = new JsonReader(fileBuffer); if (reader.ReadIsNull() || !reader.ReadIsBeginObject()) { throw new Exception("Could not read the init json"); } if (reader.ReadPropertyName() != "accounts") { throw new Exception("Unexpected object in init json"); } // read array members if (!reader.ReadIsBeginArray()) { throw new Exception("Array of accounts not found"); } var fileParseStart = Stats.Watch.Elapsed; var dto = DtoAccount.Obtain(); while (true) { if (DtoAccount.Parse(ref reader, dto, store)) { dto.flags = DtoFlags.Init; // now, add to the storage the account with internal ids/likes store.InitNewAccout(dto); totalAccounts++; dto.Reset(); } else { break; } if (!reader.ReadIsValueSeparator()) { break; } } DtoAccount.Release(dto); var fileParseEnd = Stats.Watch.Elapsed; if ((fileParseEnd - fileParseStart).TotalSeconds > 1) { Garbage.CollectAll(); } /* * else * Garbage.Collect0();*/ } } } } Log.Info("Total accounts loaded: " + totalAccounts + ", " + errorAccounts + " errors found"); fileBuffer = null; } else { Log.Error(zipFile + " not found"); } }
// synchronously process the request from requestBuffer, and return statusCode public int Process(HttpCtx ctx, int extId) { var startTime = Stats.Watch.Elapsed; // query sanity check foreach (var query in ctx.Params) { var value = query.Value; if (value.IsEmpty) { return(400); } if (query.Key != "query_id") { // all other parameters are invalid return(400); } } // translate to internal id and check for account existance if (!Mapper.ExtIdToIntId(extId, out int id) || !store.All[id]) { return(404); } // parse Json var dto = DtoAccount.Obtain(); dto.id = id; int statusCode = 400; try { JsonReader reader = new JsonReader(ctx.Buffer, ctx.RequestBodyStart); if (DtoAccount.Parse(ref reader, dto, store) && dto.flags != 0) { // update the account, register post-process action statusCode = store.ValidateUpdateAccount(dto); if (statusCode == 202) { ctx.PostAction = () => { store.PostUpdateAccount(dto); DtoAccount.Release(dto); } } ; } } catch (Exception) { // fall through } // return the borrowed object if (ctx.PostAction == null) { DtoAccount.Release(dto); } var stopTime = Stats.Watch.Elapsed; ctx.ContextType = "PostUpdate"; return(statusCode); }
// synchronously process the request from requestBuffer, and return statusCode public int Process(HttpCtx ctx, int dummy) { var startTime = Stats.Watch.Elapsed; // path sanity check foreach (var query in ctx.Params) { var value = query.Value; if (value.IsEmpty) { return(400); } if (query.Key != "query_id") { // all other parameters are invalid return(400); } } // load the body if (ctx.RequestBodyStart == ctx.ResponseBodyStart) { return(400); } // parse Json and process dto var dto = DtoAccount.Obtain(); int statusCode = 400; try { JsonReader reader = new JsonReader(ctx.Buffer, ctx.RequestBodyStart); if (DtoAccount.Parse(ref reader, dto, store) && dto.flags != 0) { // add the new account statusCode = store.ValidateNewAccount(dto); if (statusCode == 201) { ctx.PostAction = () => { store.PostNewAccount(dto); DtoAccount.Release(dto); } } ; } } catch (Exception) { // fall through } // return the borrowed object if (ctx.PostAction == null) { DtoAccount.Release(dto); } var stopTime = Stats.Watch.Elapsed; ctx.ContextType = "PostNew"; return(statusCode); }