Ind.ie is now Small Technology Foundation.
Commit d56debfc authored by Aral Balkan's avatar Aral Balkan

Added database singleton. Added migrations spike.

parent 85e70b8c
......@@ -120,6 +120,12 @@ class Main
@createConfiguration()
#
# Run database migrations.
#
@runDatabaseMigrations()
#
# Start the services.
#
......@@ -142,6 +148,14 @@ class Main
return instance
#
# Run database migrations
#
runDatabaseMigrations: =>
currentDatabaseVersion = 1
#
# Parses the command-line arguments and creates the main configuration object.
......
......@@ -28,6 +28,8 @@ WriteStream = require 'write-stream'
liveStream = require 'level-live-stream'
database = require './database'
class StreamWeaver
instance = null
......@@ -43,8 +45,6 @@ class StreamWeaver
friendsCached: null
db: null
constructor: ->
# Singleton access
......@@ -57,13 +57,10 @@ class StreamWeaver
console.log 'Initialising StreamWeaver singleton.'
# Create database
@db = Sublevel (LevelUp 'db')
@createTimelines()
# Create a level to hold friends
@friends = @db.sublevel "friends"
@friends = database.sublevel "friends"
# Save reference to the singleton instance.
instance = @
......@@ -82,9 +79,9 @@ class StreamWeaver
@timeline.public = "public"
@timeline.allFriends = "allfriends"
@privateTimeline = @db.sublevel @timeline.private
@publicTimeline = @db.sublevel @timeline.public
@allFriendsTimeline = @db.sublevel @timeline.allFriends
@privateTimeline = database.sublevel @timeline.private
@publicTimeline = database.sublevel @timeline.public
@allFriendsTimeline = database.sublevel @timeline.allFriends
@timelines[@timeline.private] = @privateTimeline
@timelines[@timeline.public] = @publicTimeline
......@@ -125,7 +122,7 @@ class StreamWeaver
if @timelines[timeline] == undefined
console.log "No sublevel for #{timeline} in database. Creating one…"
@timelines[timeline] = @db.sublevel timeline
@timelines[timeline] = database.sublevel timeline
console.log "Created new DB sublevel for #{timeline}."
@timelines[timeline].put messageID, message
......@@ -221,7 +218,7 @@ class StreamWeaver
# If not, create it.
if @timelines[timeline] == undefined
console.log "Asked to get #{timeline} timeline but its level has not been created in the DB yet. Creating…"
@timelines[timeline] = @db.sublevel timeline
@timelines[timeline] = database.sublevel timeline
console.log "Level for #{timeline} timeline created in the database."
timelineStream = @timelines[timeline].createReadStream options
......
################################################################################
#
# Ind.ie Heartbeat Node
#
# Database — singleton access to the LevelDB database.
#
# This is a singleton object that proxies a reference to the database instance.
#
# This is independent technology. See ind.ie/manifesto
#
# Copyright © 2014-2015, Aral Balkan. © 2014-2015, Ind.ie.
# Released with ♥ by Ind.ie under GNU AGPLv3 or later.
# Free as in freedom. Please see the LICENSE file.
#
################################################################################
path = require 'path-extra'
LevelUp = require 'levelup'
Sublevel = require 'level-sublevel'
class Database
instance = null
db: null
constructor: ->
# Singleton access
if instance
return instance
#
# Initialise the singleton instance
#
console.log 'Initialising Database singleton.'
# Create database
databasePath = 'db'
console.log "Waystone LevelDB database path: #{databasePath}"
@db = Sublevel (LevelUp databasePath)
# Save reference to the singleton instance.
instance = @
return instance
#
# Actually proxy the database instance to cut down on boilerploit code when used.
#
module.exports = (new Database).db
LevelUp = require 'levelup'
Sublevel = require 'level-sublevel'
WriteStream = require 'write-stream'
liveStream = require 'level-live-stream'
path = require 'path-extra'
# TEST
db = LevelUp '/Users/aral/Library/Containers/ind.ie.Heartbeat/Data/db'
#db = LevelUp 'db'
# console.log db
version0toVersion1 = ->
return new Promise ((fulfill, reject) =>
console.log "Getting all entries"
# Create options for a new stream
options = { gt: '\x00', lt: '\uffff' }
allEntriesStream = db.createReadStream options
allEntriesStream.on 'error', (err) ->
console.log "All entries stream error: #{err}"
toArray = WriteStream.toArray (allEntries) =>
for entry in allEntries
console.log "Checking #{entry.key}"
depracatedMessageClockMatcher = /.*?\!(\d{9}-)/
matches = entry.key.match depracatedMessageClockMatcher
if matches != null
console.log "Will migrate: #{entry.key}"
# Remove the depracated message clock from message keys.
newKey = entry.key.replace matches[1], ''
# Put the new key in
console.log "Will put: #{newKey}: #{entry.value}"
#db.put newKey, entry.value
# Delete the old entry
console.log "Will delete: #{entry.key}"
#db.delete entry.key
console.log "Migrated all #{allEntries.length} entries."
process.nextTick(=>
fulfill true
)
allEntriesStream.pipe toArray
)
version0toVersion1()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment