Skip to content

Commit

Permalink
Fix bugs in new append only database
Browse files Browse the repository at this point in the history
  • Loading branch information
Alextopher committed Jul 18, 2023
1 parent b408d38 commit 3d4c257
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
19 changes: 14 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"sort"
"sync"
)

Expand Down Expand Up @@ -124,19 +125,28 @@ func (t *Talks) create(c *CreateTalkEvent) {
t.weeks[c.Week] = make([]*Talk, 0)
}
t.weeks[c.Week] = append(t.weeks[c.Week], t.talks[c.ID])

sort.Slice(t.weeks[c.Week], func(i, j int) bool {
if t.weeks[c.Week][i].Type == t.weeks[c.Week][j].Type {
return t.weeks[c.Week][i].ID < t.weeks[c.Week][j].ID
}
return t.weeks[c.Week][i].Type < t.weeks[c.Week][j].Type
})
}

// Create creates a new talk
func (t *Talks) Create(name string, talkType TalkType, description string, week string) {
func (t *Talks) Create(name string, talkType TalkType, description string, week string) uint32 {
t.Lock()

// Increment the ID counter
t.id++

id := t.id
event := TalkEvent{
Time: Now(),
Type: Create,
Create: &CreateTalkEvent{
ID: t.id,
ID: id,
Name: name,
Type: talkType,
Description: description,
Expand All @@ -146,6 +156,7 @@ func (t *Talks) Create(name string, talkType TalkType, description string, week
t.writeAndApply(event)

t.Unlock()
return id
}

// Applies a hide event to the in-memory state
Expand Down Expand Up @@ -183,9 +194,7 @@ func (t *Talks) delete(d *DeleteTalkEvent) {
week := t.talks[d.ID].Week
for i, talk := range t.weeks[week] {
if talk.ID == d.ID {
// Swap remove
t.weeks[week][i] = t.weeks[week][len(t.weeks[week])-1]
t.weeks[week] = t.weeks[week][:len(t.weeks[week])-1]
t.weeks[week] = append(t.weeks[week][:i], t.weeks[week][i+1:]...)
}
}
delete(t.talks, d.ID)
Expand Down
2 changes: 1 addition & 1 deletion hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func processMessage(message *Message) bool {
return false
}

talks.Create(message.New.Name, message.New.Talktype, message.New.Description, message.New.Week)
message.New.ID = talks.Create(message.New.Name, message.New.Talktype, message.New.Description, message.New.Week)

// Update the message's description to be parsed as markdown
message.New.Description = string(markDownerSafe(message.New.Description))
Expand Down
6 changes: 3 additions & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (t TalkType) MarshalJSON() ([]byte, error) {
case Announcement:
s = "announcement"
case AfterMeetingSlot:
s = "after meeting slot"
s = "after-meeting slot"
default:
s = "unknown"
}
Expand All @@ -105,7 +105,7 @@ func (t *TalkType) UnmarshalJSON(b []byte) error {
*t = ProjectUpdate
case "announcement":
*t = Announcement
case "after meeting slot":
case "after-meeting slot":
*t = AfterMeetingSlot
}
return nil
Expand All @@ -123,7 +123,7 @@ func (t TalkType) String() string {
case Announcement:
return "announcement"
case AfterMeetingSlot:
return "after meeting slot"
return "after-meeting slot"
default:
return "unknown"
}
Expand Down
13 changes: 9 additions & 4 deletions static/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ function auth() {
var seen = new Set();
// Sync requests are used to update the table with the latest data
function sync() {
console.log("Syncing week " + week);

const data = {
type: 4,
sync: {
Expand Down Expand Up @@ -131,13 +133,14 @@ function connect() {
seen.add(data.new.id);
} else if (data.type == 1 || data.type == 2) {
// Hide the talk from the table
hideTalk(data.hide);
hideTalk(data.hide.id);
} else if (data.type == 3) {
// Receiving an auth message means we have successfully authenticated
handleAuth(data.auth);
} else if (data.type == 4) {
// Remove talks that we didn't see in this sync
hideTalksNotIn(seen);
console.log("Sync complete");
}
};
socket.onclose = function (e) {
Expand Down Expand Up @@ -248,16 +251,18 @@ function hideTalks(predicate) {
const table = document.getElementById("tb");
const rows = document.getElementById('tb').children;

for(let i = rows.length - 1; i >= 0; i--) {
for (let i = rows.length - 1; i >= 0; i--) {
if (rows[i].getAttribute("class") == "event" && predicate(rows[i])) {
table.removeChild(rows[i]);
}
}
}

function hideTalk(id) {
console.log("Hiding talk", id);

hideTalks((row) => {
return row.children[0].innerText == id;
return parseInt(row.children[0].innerText) == id;
});
}

Expand Down Expand Up @@ -299,7 +304,7 @@ function addTalk(talk) {
// Order by talk type then by id
let childtype = stringToType[rows[i].children[2].innerText]

if (talk.talktype < childtype) {
if (stringToType[talk.talktype] < childtype) {
break;
}
}
Expand Down

0 comments on commit 3d4c257

Please sign in to comment.