Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:18:08+00:00 2026-06-09T15:18:08+00:00

I’m trying to design a basic ORM for a data app that I’m working

  • 0

I’m trying to design a basic ORM for a data app that I’m working on. My question about the model I came up with is 2-fold:

  • Is this the ‘best’ most ‘efficient’ model for database tracking?
  • Is this idiomatic Go?

The idea is to load a complete model of the db into memory at app start. Use this model to generate a map with a crc32 hash of each object (corresponding to a row in the database). This is what we use to compare with model to find specifically where changes have been made when .save() is called.

The first part of the following generates the crc32 map, the second part introduces a random change, and the last part would be part of .save() to write db changes to disk

The code:

func main() {
    // Read data off of disk into memory
    memDB := ddb

    // Create hash map of memDB
    peopleMap := make(map[int]uint32)
    for _, v := range memDB.people {
        // make row into byte array, looks kludgy
        hash := []byte(fmt.Sprintf("%#v", v))
        peopleMap[v.pID] = crc32.ChecksumIEEE(hash)
        fmt.Printf("%v: %v %v \t(%v %v) - crc sum: %v\n",
            v.pID, v.fName, v.lName, v.job, v.location,
            peopleMap[v.pID])
    }
    fmt.Println("\n# of people in memory:", len(memDB.people))

    // Sometime later, we need to delete Danielle, so
    // Delete in memory:
    var tmpSlice []ddPerson
    for _, v := range memDB.people {
        if v.fName == "Danielle" {
            continue
        }
        tmpSlice = append(tmpSlice, v)
    }
    memDB.people = tmpSlice
    fmt.Println("# of people in memory:", len(memDB.people))

    // Okay, we save in-memory representation mem.DB back
    // to disk with some kind of .save() assertion
    // a len(peopleMap) comparison to len(memDB.people) will
    // tell us there has been a change for INSERTS and
    // DELETES, but it won't tell us about updates or which
    // record was inserted or deleted

    // First, check for additions
    if len(peopleMap) < len(memDB.people) {
        // Code to find and add person to disk db ddb here
        fmt.Println("Adding someone to disk database...")
    } else if len(peopleMap) > len(memDB.people) {
        // Check for deletions
        fmt.Println("Purging someone from disk database...")
    }

    // in any case, recheck hashes
    tMap := make(map[int]uint32)
    for _, v := range memDB.people {
        hash := []byte(fmt.Sprintf("%#v", v))
        t := crc32.ChecksumIEEE(hash)
        // Add to temporary map
        tMap[v.pID] = t
        // And check for changes
        if t != peopleMap[v.pID] {
            fmt.Println("Change detected in in-memory model...")
            fmt.Println("Writing changes to disk db now")
            // Writing any changes to DB here
            ddb.people = memDB.people
        }
    }

    // 'Fix' our hashmap checker deal
    peopleMap = tMap

    // Carry on
}

There is a working version at: http://play.golang.org/p/XMTmynNy7t

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:18:09+00:00Added an answer on June 9, 2026 at 3:18 pm

    You’ve implemented an in memory cache of your db which isn’t really the same thing as an ORM.

    There are several problems with what you have:

    • If some other process/application ever modifies the database your in memory model will be out of date. This could cause your writes to clobber writes to the db because you didn’t know your cache was stale.
    • If the db gets large you’re application will get correspondingly large.

    Most ORM’s provide a way to map a type (eg. structs) to a db read and write. This allows you to read a single object from the database modify it and then write that object back to the database. That is probably a better approach for this.

    As for how idiomatic your go is I didn’t see obviously non idiomatic go but there also isn’t much going on in it. At the top memDb := ddb is a function call but you don’t have parens after it. This is a syntax error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.