MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / encode_context

Function encode_context

atomic-core/examples/graph_codec_lab/codec.rs:478–514  ·  view source on GitHub ↗
(
    value: &T,
    content: Option<&[u8]>,
    sequential: bool,
)

Source from the content-addressed store, hash-verified

476 encode_context(value, Some(content), true)
477}
478fn encode_context<T: Serialize>(
479 value: &T,
480 content: Option<&[u8]>,
481 sequential: bool,
482) -> Result<Vec<u8>> {
483 let mut writer = Writer {
484 sequential,
485 ..Writer::default()
486 };
487 if let Some(content) = content {
488 ensure!(content.len() <= LIMIT, "authored lookup limit");
489 writer.c.content_hash = Some(*blake3::hash(content).as_bytes());
490 writer.authored = content.to_vec();
491 let mut base = 0;
492 for line in content.split_inclusive(|b| *b == b'\n') {
493 writer.content_lookup.entry(line.to_vec()).or_insert(base);
494 for token in atomic_core::diff::token::Tokenizer::new(line) {
495 let at = base + token.offset();
496 let key = token.content();
497 if content.get(at..at + key.len()) == Some(key) {
498 writer.content_lookup.entry(key.to_vec()).or_insert(at);
499 }
500 }
501 base += line.len();
502 }
503 }
504 value.serialize(&mut writer)?;
505 ensure!(
506 writer.c.dictionary.len() <= 1_000_000,
507 "graph dictionary limit"
508 );
509 let payload = postcard::to_allocvec(&writer.c)?;
510 ensure!(payload.len() <= LIMIT * 2, "graph frame limit");
511 let mut out = MAGIC.to_vec();
512 out.extend(zstd::encode_all(payload.as_slice(), 3)?);
513 Ok(out)
514}
515pub fn decode(encoded: &[u8]) -> Result<Vec<u8>> {
516 decode_context(encoded, None)
517}

Callers 3

encodeFunction · 0.85
encode_with_contentFunction · 0.85

Calls 9

extendMethod · 0.80
getMethod · 0.65
hashFunction · 0.50
as_bytesMethod · 0.45
offsetMethod · 0.45
contentMethod · 0.45
lenMethod · 0.45
serializeMethod · 0.45
as_sliceMethod · 0.45

Tested by

no test coverage detected