all repos — litestore @ e25b0330e0df0142e2269ee332b687e7bcb7ef57

A minimalist nosql document store.

litestore.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
import 
  sqlite3, 
  db_sqlite as db, 
  strutils, 
  os,
  oids,
  times,
  json,
  pegs, 
  strtabs,
  base64
import
  types,
  contenttypes,
  queries,
  utils

{.compile: "vendor/sqlite/libsqlite3.c".}
{.passC: "-DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS".}


# Manage Datastores

proc createDatastore*(file:string) = 
  if file.fileExists:
    raise newException(EDatastoreExists, "Datastore '$1' already exists." % file)
  let store = db.open(file, "", "", "")
  store.exec(SQL_CREATE_DOCUMENTS_TABLE)
  store.exec(SQL_CREATE_SEARCHCONTENTS_TABLE)
  store.exec(SQL_CREATE_TAGS_TABLE)

proc deleteDatastore*(file:string) =
  try:
    file.removeFile
  except:
    raise newException(EDatastoreUnavailable, "Datastore '$1' cannot deleted." % file)

proc openDatastore(file:string): Datastore =
  if not file.fileExists:
    raise newException(EDatastoreDoesNotExist, "Datastore '$1' does not exists." % file)
  try:
    result.db = db.open(file, "", "", "")
    result.path = file
  except:
    raise newException(EDatastoreUnavailable, "Datastore '$1' cannot be opened." % file)

proc closeDatastore(store:Datastore) = 
  try:
    db.close(store.db)
  except:
    raise newException(EDatastoreUnavailable, "Datastore '$1' cannot be closed." % store.path)

# Manage Documents

proc createDocument*(store: Datastore,  id="", data = "", contenttype = "text/plain", binary = -1, searchable = 1): string =
  var binary = checkIfBinary(binary, contenttype)
  var data = data
  if binary == 1:
    data = data.encode(data.len*2)
  if id == "":
    result = $genOid()
  else:
    result = id
  # Store document
  store.db.exec(SQL_INSERT_DOCUMENT, result, data, contenttype, binary, searchable, getTime().getGMTime().format("yyyy-MM-dd'T'hh:mm:ss'Z'"))
  if binary == 0 and searchable == 1:
    # Add to search index
    store.db.exec(SQL_INSERT_SEARCHCONTENT, result, data)
  store.addDocumentSystemTags(result, contenttype)
  return result

proc updateDocument*(store: Datastore, id: string, data: string, contenttype = "text/plain", binary = -1, searchable = 1): int64 =
  var binary = checkIfBinary(binary, contenttype)
  var data = data
  if binary == 1:
    data = data.encode(data.len*2)
  result = store.db.execAffectedRows(SQL_UPDATE_DOCUMENT, data, contenttype, binary, searchable, getTime().getGMTime().format("yyyy-MM-dd'T'hh:mm:ss'Z'"), id)
  store.deleteDocumentSystemTags(id)
  store.addDocumentSystemTags(id, contenttype)
  store.db.exec(SQL_UPDATE_SEARCHCONTENT, data, id)

proc deleteDocument*(store: Datastore, id: string): int64 =
  result = store.db.execAffectedRows(SQL_DELETE_DOCUMENT, id)
  store.db.exec(SQL_DELETE_SEARCHCONTENT, id)
  store.db.exec(SQL_DELETE_DOCUMENT_TAGS, id)

proc retrieveDocument*(store: Datastore, id: string, options: QueryOptions = newQueryOptions()): string =
  var options = options
  options.single = true
  var select = prepareSelectDocumentsQuery(options)
  var raw_document = store.db.getRow(select.sql, id)
  return $store.prepareJsonDocument(raw_document)

proc retrieveDocuments*(store: Datastore, options: QueryOptions = newQueryOptions()): string =
  var select = prepareSelectDocumentsQuery(options)
  var raw_documents = store.db.getAllRows(select.sql)
  var documents = newSeq[JsonNode](0)
  for doc in raw_documents:
    documents.add store.prepareJsonDocument(doc)
  return $(%documents)

# Manage Tags

proc createTag*(store: Datastore, tagid, documentid: string) =
  if not tagid.match(PEG_USER_TAG):
    raise newException(EInvalidTag, "Invalid Tag: $1" % tagid)
  store.db.exec(SQL_INSERT_TAG, tagid, documentid)

proc deleteTag*(store: Datastore, tagid, documentid: string): int64 =
  if not tagid.match(PEG_USER_TAG):
    raise newException(EInvalidTag, "Invalid Tag: $1" % tagid)
  return store.db.execAffectedRows(SQL_DELETE_TAG, documentid, tagid)

proc retrieveTag*(store: Datastore, id: string, options: QueryOptions = newQueryOptions()): string =
  var options = options
  options.single = true
  var query = prepareSelectTagsQuery(options)
  var raw_tag = store.db.getRow(query.sql, id)
  return $(%[("id", %raw_tag[0]), ("documents", %(raw_tag[1].parseInt))])

proc retrieveTags*(store: Datastore, options: QueryOptions = newQueryOptions()): string =
  var query = prepareSelectTagsQuery(options)
  var raw_tags = store.db.getAllRows(query.sql)
  var tags = newSeq[JsonNode](0)
  for tag in raw_tags:
    tags.add(%[("id", %tag[0]), ("documents", %(tag[1].parseInt))])
  return $(%tags)

proc packDir*(store: Datastore, dir: string) =
  if not dir.dirExists:
    raise newException(EDirectoryNotFound, "Directory '$1' not found." % dir)
  for f in dir.walkDirRec():
    let ext = f.splitFile.ext
    var d_id = f
    var d_contents = f.readFile
    var d_ct = "text/plain"
    if CONTENT_TYPES.hasKey(ext):
      d_ct = CONTENT_TYPES[ext]
    var d_binary = 0
    var d_searchable = 1
    if d_ct.isBinary:
      d_binary = 1
      d_searchable = 0
    discard store.createDocument(d_id, d_contents, d_ct, d_binary, d_searchable)
    store.db.exec(SQL_INSERT_TAG, "$dir:"&dir, d_id)

proc unpackDir*(store: Datastore, dir: string) =
  let docs = store.db.getAllRows(SQL_SELECT_DOCUMENTS_BY_TAG, "$dir:"&dir)
  for doc in docs:
    let file = doc[0]
    var data: string
    if doc[3].parseInt == 1:
      data = doc[1].decode
    else:
      data = doc[1]
    file.parentDir.createDir
    file.writeFile(data)

proc deleteDocumentsByTag(store: Datastore, tag: string): int64 =
  result = 0
  var ids = store.db.getAllRows(SQL_SELECT_DOCUMENT_IDS_BY_TAG, tag)
  for id in ids:
    result.inc(store.deleteDocument(id[0]).int)

# Test

var file = "test.ls"
if file.fileExists:
  file.removeFile
createDatastore(file)
var store = file.openDatastore
var id1 = store.createDocument "This is a test document"
var id2 = store.createDocument "This is another test document"
var id3 = store.createDocument "This is yet another test document"
store.createTag "test1", id1
store.createTag "test2", id2
store.createTag "test3", id2
store.createTag "test", id1
store.createTag "test", id2
store.createTag "test", id3
var opts = newQueryOptions()
#opts.tags = "test,test2"
#opts.search = "another yet"
store.packDir("nimcache")
"test".createDir
"test".setCurrentDir
store.unpackDir("nimcache")
echo store.deleteDocumentsByTag("$dir:nimcache")