#MongoDB Concurrency

1 messages · Page 1 of 1 (latest)

halcyon mesa
#

I have a MongoDB database, and there I plan to store Player Info in JSON. I do not want to allow 2 servers to concurrently update the same info (could lead to data loss).
I want to lock reads and writes, somehow, for the time a server is updating the Info until another server could take over and update it as well! (Server has to read the data, update it and write it back)
I am a newbie to MongoDB, what are my solutions, if any?

dawn totemBOT
#

<@&987246584574140416> please have a look, thanks.

halcyon mesa
#

I could not find a "findAndModify()" method

halcyon mesa
#

bump

true willow
#

don't use mongodb

#

mongodb does not allow for the

#
LOCK
  * GET ROW
  * UPDATE ROW IN MEMORY
  * SAVE NEW ROW
UNLOCK
#

sort of thing you want to do

#

it is one of a few databases (dynamodb, cassandra, mongo) which are extremely good when

#
  • You know your access patterns up front
  • You have reason to suspect your DB will grow beyond what you could put on a single machine
  • You are willing to lose some amount of atomicity and consistency
#

if you are a beginner, i reccomend sqlite

#

it will be a slightly higher onramp than "put json in collection" but it won't have these issues

#

or i guess postgresql if you are already wanting multiple server machines

halcyon mesa
#

Like file based..

#

I just picked Mongo because it was simple to access the stuff since I'm already working with json..

true willow
halcyon mesa
#

OK understood.

#

Uhm so how would you transform a json to sql...

true willow
#

give me an example json

#

for what you are doing

halcyon mesa
#

My design has to be compatible with earlier versions so I have.. A map of strings to objects (can be int, string, anything serializable) and I'm using Jackson to directly transform that map to a json... And also included in the map is a list of remapped fields (in case I fuck a field or sum and earlier versions can still continue to work..

true willow
#

thats a bit too generic

#

data always has a schema

#

even if its implicit

#

with a sql database its best if we make the schema explicit

#

not required but is the path of least resistence

#

And also included in the map is a list of remapped fields
Schema versioning would be less of a PITA than that

halcyon mesa
# true willow data always has a schema

I currently don't because I am still in my "prototyping phase" and database isn't the most important aspect (YET), but one I want to start NOW so I know how to implement it later on..

halcyon mesa
#

I guess?

true willow
#

can you give me an example player info?

halcyon mesa
#

uhm yes

#

so

#

playerName (String),
playerId (UUID),
firstLogin (long (epoch)),
password (String (hashed))

#

more fields added later for the minigames

#

for example...
skywarsWins (int),
skywarsKills (int),
skywarsXp (int)

#

etc

#

do you need values for the fields.. or I got it completely wrong..?

true willow
#

thats a good start for the info i need

#

so for anything that is "flat", you can just make a table

#
CREATE TABLE player (
    id uuid,
    name text,
    first_login timestamptz,
    password text,
    skywars_wins integer,
    skywars_kills integer,
    skywars_xp integer
)
#

anything thats "nested" you need to deal with joins

#

so like, if you wanted to store every login for a player

#
CREATE TABLE login(
    id uuid,
    player_id uuid,
    time timestamptz
)
#

so like for player A you would have many login rows

halcyon mesa
#

hmh

true willow
#

and if you want all the logins for a player you write a query

halcyon mesa
#

yes

#

so...

#

I am just gonna ask how you would uhm

#

add a field to the table..

#

if for example I wanted to add now.. that the player also has

#

network_experience

true willow
#
ALTER TABLE player ADD COLUMN network_experience integer;
halcyon mesa
#

ok I think I got it.

#

uhm.. one last question I guess..

#

do you think I should use my map approach or something.. I am really unsure how I could transfer my objects from the current format of map -> json to the format map -> sql

#

or if I should just scrap it and start over... or some other way.

#

I mean how would you handle getting the data from SQL and parsing some meaningful way in programming..

#

I think that's a better question

true willow
#

so its gonna be a little bit of work

#

SQL wasn't invented because people loved working with tables and writing extra code

#

its because the relational model (i.e. things joining by ids) is the only one thats flexible to future changes

#

the basics are

#
  • You send queries and inserts using Connections
  • You get Connections from DataSources
  • You get results with a ResultSet and executeQuery
halcyon mesa
#

got it..

#

Let me just give you an example..

#

I get the basic idea that I query, I get the lines and stuff, now is there a more manageable way with objects or sum? like this:

public class PlayerInfo {

  public UUID playerId;
  public String playerName;
  //etc..
}

and I could manage this PlayerInfo and it updates on SQL.. (with like a Consumer<PlayerInfo> on an update function)? or am I just being PLAIN stupid..

#

I mean I guess I could deal with using SELECT playerName FROM players WHERE playerId=? and then completing the statement and querying it..

#

but I feel like my queries are gonna be all over the place.... and I don't really like that, but it might be unavoidable?