Skip to content
This repository
Browse code

StorageView that stores entire Thing in column.

  • Loading branch information...
commit c1d7e73acb340e70c1b6c5f6fe59651114f46c14 1 parent 4111c73
Brian Simpson bsimpson63 authored committed

Showing 1 changed file with 55 additions and 0 deletions. Show diff stats Hide diff stats

  1. +55 0 r2/r2/lib/db/tdb_cassandra.py
55 r2/r2/lib/db/tdb_cassandra.py
@@ -1247,6 +1247,61 @@ def _set_values(cls, row_key, col_values,
1247 1247 # can we be smarter here?
1248 1248 thing_cache.delete(cls._cache_key_id(row_key))
1249 1249
  1250 +# if cjson is installed, use it. it's faster.
  1251 +try:
  1252 + import cjson as json
  1253 +except ImportError:
  1254 + import json
  1255 +else:
  1256 + json.dumps, json.loads = json.encode, json.decode
  1257 +
  1258 +class StorageView(View):
  1259 + """Store the entire underlying object inside the View column."""
  1260 +
  1261 + # Do we need to check for _dirty?
  1262 +
  1263 + @classmethod
  1264 + def _thing_dumper(cls, thing):
  1265 + serialize_fn = cls._view_of._serialize_column
  1266 + serialized_columns = dict((attr, serialize_fn(attr, val)) for
  1267 + (attr, val) in thing._orig.iteritems())
  1268 + dump = json.dumps(serialized_columns)
  1269 + return dump
  1270 +
  1271 + @classmethod
  1272 + def _thing_loader(cls, _id, dump):
  1273 + serialized_columns = json.loads(dump)
  1274 + obj = cls._view_of._from_serialized_columns(_id, serialized_columns)
  1275 + return obj
  1276 +
  1277 + @classmethod
  1278 + def _obj_to_column(cls, objs):
  1279 + objs = tup(objs)
  1280 + columns = []
  1281 + for o in objs:
  1282 + _id = o._id
  1283 + dump = cls._thing_dumper(o)
  1284 + columns.append({_id: dump})
  1285 +
  1286 + if len(columns) == 1:
  1287 + return columns[0]
  1288 + else:
  1289 + return columns
  1290 +
  1291 + @classmethod
  1292 + def _column_to_obj(cls, columns):
  1293 + columns = tup(columns)
  1294 + objs = []
  1295 + for column in columns:
  1296 + _id, dump = column.items()[0]
  1297 + obj = cls._thing_loader(_id, dump)
  1298 + objs.append(obj)
  1299 +
  1300 + if len(objs) == 1:
  1301 + return objs[0]
  1302 + else:
  1303 + return objs
  1304 +
1250 1305 def schema_report():
1251 1306 manager = get_manager()
1252 1307 print manager.describe_keyspace(keyspace)

0 comments on commit c1d7e73

Please sign in to comment.
Something went wrong with that request. Please try again.